123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- // 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 sortApiIncomeDetailInfo(array []apis.ApiServiceIncomeDetail) []apis.ApiServiceIncomeDetail {
- 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 sortApiIncomeInfo(array []apis.ApiServiceIncomeInfo) []apis.ApiServiceIncomeInfo {
- 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 = sortApiIncomeDetailInfo(v.Detail)
- }
- return array
- }
- func apiServiceGetIncomeInfo(ctx context.Context, req *apis.BiGetApiServiceIncomeInfoReq, start, end int64, isToday bool) ([]apis.ApiServiceIncomeInfo, error) {
- mreq := apis.BiGetReportReq{}
- mreply := apis.BiGetReportReply{}
- mreq.IsAll = true
- mreq.TodayEndTimestamp = req.TodayEndTimestamp
- mreq.Date = getDatesFromTimestamp(start, end)
- var err error
- if isToday {
- mreply.Infos, err = getTodayReportFromHour(&mreq, start, end)
- if err != nil {
- return nil, err
- }
- } else {
- err = BiGetReport(ctx, &mreq, &mreply)
- if err != nil {
- return nil, err
- }
- }
- m := map[string]map[string]*apis.BiReportInfo{}
- for i, v := range mreply.Infos {
- if _, ok := m[v.ApiName]; ok == false {
- item := map[string]*apis.BiReportInfo{}
- item[v.MerchantName] = &mreply.Infos[i]
- m[v.ApiName] = item
- continue
- }
- if _, ok := m[v.ApiName][v.MerchantName]; ok == false {
- m[v.ApiName][v.MerchantName] = &mreply.Infos[i]
- continue
- }
- m[v.ApiName][v.MerchantName].Charge += v.Charge
- m[v.ApiName][v.MerchantName].Valid += v.Valid
- m[v.ApiName][v.MerchantName].Total += v.Total
- }
- ret := []apis.ApiServiceIncomeInfo{}
- for apiName, apiMap := range m {
- item := apis.ApiServiceIncomeInfo{}
- item.ApiName = apiName
- for _, api := range apiMap {
- apiItem := apis.ApiServiceIncomeDetail{}
- item.Charge += api.Charge
- apiItem.Charge = api.Charge
- apiItem.Valid = api.Valid
- apiItem.MerchantName = api.MerchantName
- 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 BiGetApiServiceIncomeInfo(ctx context.Context, req *apis.BiGetApiServiceIncomeInfoReq, reply *apis.BiGetApiServiceIncomeInfoReply) (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 := apiServiceGetIncomeInfo(ctx, req, start, end, isToday)
- if err != nil {
- return err
- }
- reply.Total = int64(len(infos))
- infos = sortApiIncomeInfo(infos)
- // 分页
- switch {
- case reply.Total-int64(offset) <= 0:
- reply.Infos = []apis.ApiServiceIncomeInfo{}
- case reply.Total-int64(offset) <= int64(limit):
- reply.Infos = infos[offset:]
- case reply.Total-int64(offset) > int64(limit):
- reply.Infos = infos[offset : offset+limit]
- }
- return nil
- }
|