123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package access_log
- import (
- "context"
- "gd_statistics/apis"
- "gd_statistics/errors"
- "strconv"
- )
- func sortApiPayDetailInfo(array []apis.ProviderServicePayDetail) []apis.ProviderServicePayDetail {
- length := len(array)
- for i := 0; i < length; i++ {
- for j := i + 1; j < length; j++ {
- if array[i].Charge < array[j].Charge {
- tmp := array[i]
- array[i] = array[j]
- array[j] = tmp
- }
- }
- }
- return array
- }
- func sortApiPayInfo(array []apis.ProviderServicePayInfo) []apis.ProviderServicePayInfo {
- length := len(array)
- for i := 0; i < length; i++ {
- for j := i + 1; j < length; j++ {
- if array[i].Charge < array[j].Charge {
- tmp := array[i]
- array[i] = array[j]
- array[j] = tmp
- }
- }
- }
- for i, v := range array {
- array[i].Detail = sortApiPayDetailInfo(v.Detail)
- }
- return array
- }
- func apiServiceGetPayInfo(ctx context.Context, req *apis.BiGetProviderServicePayInfoReq, start, end int64, isToday bool) ([]apis.ProviderServicePayInfo, error) {
- mreq := apis.BiGetProviderReportReq{}
- mreply := apis.BiGetProviderReportReply{}
- mreq.IsAll = true
- mreq.TodayEndTimestamp = req.TodayEndTimestamp
- mreq.Date = getDatesFromTimestamp(start, end)
- var err error
- if isToday {
- mreply.Infos, err = getTodayProviderReportFromHour(&mreq, start, end)
- if err != nil {
- return nil, err
- }
- } else {
- err = BiGetProviderReport(ctx, &mreq, &mreply)
- if err != nil {
- return nil, err
- }
- }
- m := map[string]map[string]*apis.BiProviderReportInfo{}
- for i, v := range mreply.Infos {
- if _, ok := m[v.ProviderName]; ok == false {
- item := map[string]*apis.BiProviderReportInfo{}
- item[v.ProviderApiName] = &mreply.Infos[i]
- m[v.ProviderName] = item
- continue
- }
- if _, ok := m[v.ProviderName][v.ProviderApiName]; ok == false {
- m[v.ProviderName][v.ProviderApiName] = &mreply.Infos[i]
- continue
- }
- m[v.ProviderName][v.ProviderApiName].Charge += v.Charge
- m[v.ProviderName][v.ProviderApiName].Total += v.Total
- }
- ret := []apis.ProviderServicePayInfo{}
- for providerName, apiMap := range m {
- item := apis.ProviderServicePayInfo{}
- item.ProviderName = providerName
- for _, api := range apiMap {
- apiItem := apis.ProviderServicePayDetail{}
- item.Charge += api.Charge
- apiItem.Charge = api.Charge
- apiItem.Valid = api.Total
- apiItem.ChargeRate = strconv.FormatFloat(float64(100*api.Charge/api.Total), 'f', 2, 64) + "%"
- apiItem.ProviderApiName = api.ProviderApiName
- item.Detail = append(item.Detail, apiItem)
- }
- ret = append(ret, item)
- }
- for i, v := range ret {
- for k, d := range ret[i].Detail {
- if v.Charge == 0 {
- ret[i].Detail[k].Rate = "0%"
- } else {
- ret[i].Detail[k].Rate = strconv.FormatFloat(float64(100*d.Charge/v.Charge), 'f', 2, 64) + "%"
- }
- }
- }
- return ret, nil
- }
- func BiGetProviderServicePayInfo(ctx context.Context, req *apis.BiGetProviderServicePayInfoReq, reply *apis.BiGetProviderServicePayInfoReply) (err error) {
- // 参数预处理
- if req.PageNumber <= 0 {
- req.PageNumber = 1
- }
- if req.PageSize <= 0 {
- req.PageSize = pageSize
- }
- reply.PageNumber = req.PageNumber
- reply.PageSize = req.PageSize
- offset := (req.PageNumber - 1) * req.PageSize
- limit := req.PageSize
- start := int64(0)
- end := int64(0)
- isToday := false
- // 今日,昨日,本周,本月
- switch req.TimeType {
- case 0:
- isToday = true
- start, end = getTodayTimestamp()
- case 1:
- start, end = getYesterdayTimestamp()
- case 2:
- start, end = getWeekTimestamp()
- case 3:
- start, end = getMonthTimestamp()
- default:
- return errors.ArgsError
- }
- // 获取所有信息
- infos, err := apiServiceGetPayInfo(ctx, req, start, end, isToday)
- if err != nil {
- return err
- }
- reply.Total = int64(len(infos))
- infos = sortApiPayInfo(infos)
- reply.Infos = infos
- // 分页
- switch {
- case reply.Total-int64(offset) <= 0:
- reply.Infos = []apis.ProviderServicePayInfo{}
- case reply.Total-int64(offset) <= int64(limit):
- reply.Infos = reply.Infos[offset:]
- case reply.Total-int64(offset) > int64(limit):
- reply.Infos = reply.Infos[offset : offset+limit]
- }
- return nil
- }
|