123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package crontab
- import (
- "context"
- "gd_crontab/apis"
- "gd_crontab/errors"
- "fmt"
- "strconv"
- "strings"
- "time"
- gerrors "errors"
- "gd_crontab/common.in/config"
- "gd_crontab/common.in/utils"
- "github.com/astaxie/beego/orm"
- "github.com/tealeg/xlsx"
- "go.uber.org/zap"
- )
- func makeProviderSingleHeader(sheet *xlsx.Sheet, timeList string) {
- row := sheet.AddRow()
- cell := row.AddCell()
- cell.Value = "日期"
- sheet.SetColWidth(0, 0, 24)
- apiCell := row.AddCell()
- apiCell.Value = "API"
- totalCell := row.AddCell()
- totalCell.Value = "总量"
- successCell := row.AddCell()
- successCell.Value = "成功"
- successRateCell := row.AddCell()
- successRateCell.Value = "成功率"
- chargeCell := row.AddCell()
- chargeCell.Value = "计费量"
- queryCell := row.AddCell()
- queryCell.Value = "查得"
- queryRateCell := row.AddCell()
- queryRateCell.Value = "查得率"
- failedCell := row.AddCell()
- failedCell.Value = "异常量"
- failedRateCell := row.AddCell()
- failedRateCell.Value = "异常率"
- avgElapsedCell := row.AddCell()
- avgElapsedCell.Value = "平均时长"
- tArray := strings.Split(timeList, ",")
- for _, v := range tArray {
- if v == "" {
- return
- }
- cell := row.AddCell()
- cell.Value = fmt.Sprintf("时长大于%s秒", v)
- }
- }
- func makeProviderXlsxHeader(sheet *xlsx.Sheet, timeList string, needDate bool) {
- row := sheet.AddRow()
- sheet.SetColWidth(0, 0, 10)
- if needDate == true {
- cell := row.AddCell()
- cell.Value = "日期"
- sheet.SetColWidth(0, 0, 24)
- sheet.SetColWidth(0, 1, 10)
- }
- nameCell := row.AddCell()
- nameCell.Value = "供应商"
- apiCell := row.AddCell()
- apiCell.Value = "API"
- totalCell := row.AddCell()
- totalCell.Value = "总量"
- successCell := row.AddCell()
- successCell.Value = "成功"
- successRateCell := row.AddCell()
- successRateCell.Value = "成功率"
- chargeCell := row.AddCell()
- chargeCell.Value = "计费量"
- queryCell := row.AddCell()
- queryCell.Value = "查得"
- queryRateCell := row.AddCell()
- queryRateCell.Value = "查得率"
- failedCell := row.AddCell()
- failedCell.Value = "异常量"
- failedRateCell := row.AddCell()
- failedRateCell.Value = "异常率"
- avgElapsedCell := row.AddCell()
- avgElapsedCell.Value = "平均时长"
- tArray := strings.Split(timeList, ",")
- for _, v := range tArray {
- if v == "" {
- return
- }
- cell := row.AddCell()
- cell.Value = fmt.Sprintf("时长大于%s秒", v)
- }
- }
- func makeProviderXlsxHeader2(sheet *xlsx.Sheet, timeList string, needDate bool) {
- row := sheet.AddRow()
- sheet.SetColWidth(0, 0, 10)
- if needDate == true {
- cell := row.AddCell()
- cell.Value = "日期"
- sheet.SetColWidth(0, 0, 24)
- sheet.SetColWidth(0, 1, 10)
- }
- nameCell := row.AddCell()
- nameCell.Value = "商户"
- apiCell := row.AddCell()
- apiCell.Value = "商户api"
- totalCell := row.AddCell()
- totalCell.Value = "供应商"
- successCell := row.AddCell()
- successCell.Value = "API"
- successRateCell := row.AddCell()
- successRateCell.Value = "用量"
- chargeCell := row.AddCell()
- chargeCell.Value = "计费量"
- }
- type TGdProviderReport struct {
- Id int64
- Date string `json:"date"`
- ProviderName string `json:"provider_name"`
- ProviderApiName string `json:"provider_api_name"`
- Total int64 `json:"total"`
- Success int64 `json:"success"`
- SuccessRate string
- Failed int64 `json:"failed"`
- FailedRate string `json:"failed_rate"`
- Query int64 `json:"query"`
- QueryRate string `json:"query_no_record_rate"`
- AvgElapsed string `json:"avg_elapsed"`
- BigElapsedList string `json:"big_elapsed_list"`
- Charge int64 `json:"charge"`
- ChargeRate string `json:"charge_rate"`
- ProviderApiId int64
- }
- func addProviderReport(data *apis.LogQueryProviderCountExport, providerReports []TGdProviderReport, timeList string) []TGdProviderReport {
- report := TGdProviderReport{}
- report.Date = data.Date
- report.ProviderApiName = data.ProviderApiName
- report.Total = data.Total
- report.Success = data.Success
- report.SuccessRate = strconv.FormatFloat(float64(100*data.Success)/float64(data.Total), 'f', 2, 64) + "%"
- report.Failed = data.Failed
- report.FailedRate = data.FailedRate
- report.Query = data.Query
- report.ProviderApiId = data.ProviderApiId
- report.QueryRate = strconv.FormatFloat(float64(100*data.Query)/float64(data.Total), 'f', 2, 64) + "%"
- report.AvgElapsed = strconv.FormatFloat(data.AvgElapsed, 'f', 2, 64)
- report.Charge = data.Charge
- report.ChargeRate = data.ChargeRate
- report.ProviderName = data.ProviderName
- array := strings.Split(timeList, ",")
- if len(array) == len(data.BigElapsedList) {
- for i, _ := range array {
- if report.BigElapsedList == "" {
- report.BigElapsedList = fmt.Sprintf("%s:%d", array[i], data.BigElapsedList[i])
- } else {
- report.BigElapsedList = fmt.Sprintf("%s;%s:%d", report.BigElapsedList, array[i], data.BigElapsedList[i])
- }
- }
- }
- providerReports = append(providerReports, report)
- return providerReports
- }
- func parseProviderAccessCountMonthDetail(exportInfos []apis.LogQueryProviderCountExport, file *xlsx.File, timeList string) error {
- table := make(map[int64][]apis.LogQueryProviderCountExport, 0)
- for _, info := range exportInfos {
- table[info.ProviderApiId] = append(table[info.ProviderApiId], info)
- }
- if len(table) == 0 {
- file.AddSheet("空")
- return nil
- }
- for _, v := range table {
- name := ""
- if len(v) > 0 {
- name = v[0].ProviderApiName
- }
- sheet, _ := file.AddSheet(name)
- makeProviderSingleHeader(sheet, timeList)
- for index, _ := range v {
- row := sheet.AddRow()
- dateCell := row.AddCell()
- dateCell.Value = v[index].Date
- apiCell := row.AddCell()
- apiCell.Value = v[index].ProviderApiName
- parseProviderSumDetail(&v[index], row, true)
- }
- }
- return nil
- }
- func parseProviderAccessCountViewAndDetail(exportInfos []apis.LogQueryProviderCountExport, sheet *xlsx.Sheet, detailSheet *xlsx.Sheet, timeList string) error {
- dateTable := make(map[string][]apis.LogQueryProviderCountExport, 0)
- providerTable := make(map[string][]apis.LogQueryProviderCountExport, 0)
- if len(exportInfos) == 0 {
- return nil
- }
- for _, info := range exportInfos {
- dateTable[info.Date] = append(dateTable[info.Date], info)
- providerTable[info.ProviderName] = append(providerTable[info.ProviderName], info)
- }
- for _, infos := range providerTable {
- table := make(map[int64][]apis.LogQueryProviderCountExport, 0)
- for _, info := range infos {
- table[info.ProviderApiId] = append(table[info.ProviderApiId], info)
- }
- for _, v := range table {
- sum := apis.LogQueryProviderCountExport{}
- row := sheet.AddRow()
- providerCell := row.AddCell()
- apiCell := row.AddCell()
- for index, _ := range v {
- if providerCell.Value == "" {
- providerCell.Value = v[index].ProviderName
- }
- if apiCell.Value == "" {
- apiCell.Value = v[index].ProviderApiName
- }
- sum.Total += v[index].Total
- sum.Charge += v[index].Charge
- sum.Failed += sum.Failed
- sum.Success += v[index].Success
- sum.Query += v[index].Query
- sum.SumElapsed += v[index].SumElapsed
- sum.BigElapsedList = sumBigElapseds(sum.BigElapsedList, v[index].BigElapsedList)
- }
- parseProviderSumDetail(&sum, row, true)
- }
- }
- for date, infos := range dateTable {
- providerTable := make(map[string][]apis.LogQueryProviderCountExport, 0)
- for _, v := range infos {
- providerTable[v.ProviderName] = append(providerTable[v.ProviderName], v)
- }
- for _, array := range providerTable {
- for _, v := range array {
- row := detailSheet.AddRow()
- dateCell := row.AddCell()
- dateCell.Value = date
- providerCell := row.AddCell()
- providerCell.Value = v.ProviderName
- apiCell := row.AddCell()
- apiCell.Value = v.ProviderApiName
- parseProviderSumDetail(&v, row, true)
- }
- }
- }
- return nil
- }
- func parseProviderAccessCountView(mreq *apis.LogQueryProviderCountExportReq, exportInfos []apis.LogQueryProviderCountExport, providerReports []TGdProviderReport, sheet *xlsx.Sheet, timeList string, name string, isRecord bool, date string) ([]TGdProviderReport, error) {
- table := make(map[string][]apis.LogQueryProviderCountExport, 0)
- if len(exportInfos) == 0 {
- return providerReports, nil
- }
- for index, _ := range exportInfos {
- table[exportInfos[index].ProviderApiName] = append(table[exportInfos[index].ProviderApiName], exportInfos[index])
- }
- for k, v := range table {
- sum := apis.LogQueryProviderCountExport{}
- row := sheet.AddRow()
- dateCell := row.AddCell()
- dateCell.Value = date
- providerCell := row.AddCell()
- providerCell.Value = name
- apiCell := row.AddCell()
- apiCell.Value = k
- for index, _ := range v {
- sum.ProviderApiId = v[index].ProviderApiId
- sum.Total += v[index].Total
- sum.Charge += v[index].Charge
- sum.Failed += v[index].Failed
- sum.Success += v[index].Success
- sum.Query += v[index].Query
- sum.SumElapsed += v[index].SumElapsed
- sum.BigElapsedList = sumBigElapseds(sum.BigElapsedList, v[index].BigElapsedList)
- }
- providerId := strconv.Itoa(int(sum.ProviderApiId))
- if strings.Contains(config.Conf.ZqyCountIds, providerId) {
- sum.Date = date
- zqyProviderCount(mreq, &sum)
- sum.ChargeRate = strconv.FormatFloat(float64(100*sum.Charge)/float64(sum.Total), 'f', 2, 64) + "%"
- }
- parseProviderSumDetail(&sum, row, true)
- if isRecord {
- data := sum
- data.ProviderApiName = k
- data.ProviderName = name
- data.Date = date
- providerReports = addProviderReport(&data, providerReports, timeList)
- }
- }
- table = nil
- return providerReports, nil
- }
- func parseProviderAccessCountView2(mreq *apis.LogQueryProviderCountExportReq, exportInfos []apis.LogQueryProviderCountExport, providerReports []TGdProviderReport, sheet *xlsx.Sheet, timeList string, name string, isRecord bool, date string) ([]TGdProviderReport, error) {
- if len(exportInfos) == 0 {
- return providerReports, nil
- }
- for _, info := range exportInfos {
- sum := apis.LogQueryProviderCountExport{}
- row := sheet.AddRow()
- dateCell := row.AddCell()
- dateCell.Value = date
- merchantCell := row.AddCell()
- merchantCell.Value = name
- sum.ApiName = info.ApiName
- sum.ProviderApiId = info.ProviderApiId
- sum.ProviderApiName = info.ProviderApiName
- sum.ProviderName = info.ProviderName
- sum.Total += info.Total
- sum.Charge = info.Charge
- providerId := strconv.Itoa(int(sum.ProviderApiId))
- if strings.Contains(config.Conf.ZqyCountIds, providerId) {
- sum.Date = date
- mreq.MerchantId = info.MerchantId
- mreq.ApiId = info.ApiId
- zqyProviderCount(mreq, &sum)
- sum.ChargeRate = strconv.FormatFloat(float64(100*sum.Charge)/float64(sum.Total), 'f', 2, 64) + "%"
- }
- parseProviderSumDetail2(&sum, row, true)
- if isRecord {
- data := sum
- data.ProviderApiName = info.ProviderApiName
- data.ProviderName = name
- data.Date = date
- providerReports = addProviderReport(&data, providerReports, timeList)
- }
- }
- return providerReports, nil
- }
- func parseProviderSumDetail(sum *apis.LogQueryProviderCountExport, row *xlsx.Row, charge bool) {
- if sum.Total == 0 {
- sum.ChargeRate = "0%"
- } else {
- sum.ChargeRate = strconv.FormatFloat(float64(100*sum.Charge)/float64(sum.Total), 'f', 2, 64) + "%"
- }
- queryRate := strconv.FormatFloat(float64(100*sum.Query)/float64(sum.Total), 'f', 2, 64) + "%"
- sum.FailedRate = strconv.FormatFloat(float64(100*sum.Failed)/float64(sum.Total), 'f', 2, 64) + "%"
- successRate := strconv.FormatFloat(float64(100*sum.Success)/float64(sum.Total), 'f', 2, 64) + "%"
- totalCell := row.AddCell()
- totalCell.SetInt(int(sum.Total))
- successCell := row.AddCell()
- successCell.SetInt(int(sum.Success))
- successRateCell := row.AddCell()
- SetPercentForXlsx(successRate, successRateCell)
- if charge {
- chargeCell := row.AddCell()
- chargeCell.SetInt(int(sum.Charge))
- style := xlsx.NewStyle()
- font := xlsx.NewFont(10, "Microsoft YaHei")
- style.Font = *font
- style.Border = *xlsx.NewBorder("thin", "none", "thin", "thin")
- fill := xlsx.NewFill("solid", "00FFFF00", "00000000")
- style.Fill = *fill
- chargeCell.SetStyle(style)
- }
- queryCell := row.AddCell()
- queryCell.SetInt(int(sum.Query))
- queryRateCell := row.AddCell()
- SetPercentForXlsx(queryRate, queryRateCell)
- failedCell := row.AddCell()
- failedCell.SetInt(int(sum.Failed))
- failedRateCell := row.AddCell()
- SetPercentForXlsx(sum.FailedRate, failedRateCell)
- avgElapsedCell := row.AddCell()
- sum.AvgElapsed = sum.SumElapsed / float64(sum.Total)
- elStr := strconv.FormatFloat(sum.AvgElapsed, 'f', 2, 64)
- el, _ := strconv.ParseFloat(elStr, 64)
- avgElapsedCell.SetFloatWithFormat(el, "0.00")
- for _, v := range sum.BigElapsedList {
- cell := row.AddCell()
- cell.SetInt(v)
- }
- }
- func parseProviderSumDetail2(sum *apis.LogQueryProviderCountExport, row *xlsx.Row, charge bool) {
- apiCell := row.AddCell()
- apiCell.Value = sum.ApiName
- providerName := row.AddCell()
- providerName.Value = sum.ProviderName
- providerApiName := row.AddCell()
- providerApiName.Value = sum.ProviderApiName
- totalCell := row.AddCell()
- totalCell.SetInt(int(sum.Total))
- if charge {
- chargeCell := row.AddCell()
- chargeCell.SetInt(int(sum.Charge))
- style := xlsx.NewStyle()
- font := xlsx.NewFont(10, "Microsoft YaHei")
- style.Font = *font
- style.Border = *xlsx.NewBorder("thin", "thin", "thin", "thin")
- fill := xlsx.NewFill("solid", "00FFFF00", "00000000")
- style.Fill = *fill
- chargeCell.SetStyle(style)
- }
- }
- func sendProviderMail(to, host, user, password string, files ...string) error {
- if TestExceptionMail {
- return gerrors.New("异常邮件测试:统计邮件发送失败")
- }
- err := sendMailExec(user,
- password,
- host,
- to,
- "gd数据源报表", files)
- if err != nil {
- l.Error("func",
- zap.String("call", "sendMailExec"),
- zap.String("args",
- fmt.Sprintf("password:%s,host:%s,to:%s,subject:%s,files:%v\n",
- password, host, to, "gd数据源报表", files)),
- zap.String("error", err.Error()))
- }
- return err
- }
- func insertProviderReports(providerReports []TGdProviderReport, date string, providerApiId int64) {
- if len(providerReports) == 0 {
- return
- }
- if providerApiId > 0 {
- for i, v := range providerReports {
- db.Raw("delete from t_gd_provider_report where date=? and provider_api_id=?", date, v.ProviderApiId).Exec()
- db.Insert(&providerReports[i])
- }
- return
- }
- db.Raw("delete from t_gd_provider_report where date=?", date).Exec()
- db.InsertMulti(len(providerReports), &providerReports)
- }
- func setAllProviderCells(file *xlsx.File) {
- style := xlsx.NewStyle()
- font := xlsx.NewFont(10, "Microsoft YaHei")
- style.Font = *font
- style.Border = *xlsx.NewBorder("thin", "none", "thin", "thin")
- headerStyle := xlsx.NewStyle()
- fill := xlsx.NewFill("solid", "00B0C4DE", "00000000")
- headerStyle.Font = *font
- headerStyle.Fill = *fill
- headerStyle.Border = *xlsx.NewBorder("thin", "none", "thin", "thin")
- sheets := file.Sheets
- for i := 0; i < len(sheets); i++ {
- rows := sheets[i].Rows
- for j := 0; j < len(rows); j++ {
- cells := rows[j].Cells
- for k := 0; k < len(cells); k++ {
- if j < 1 {
- cells[k].SetStyle(headerStyle)
- } else {
- if cells[k].GetStyle().Fill.FgColor == "00FFFF00" {
- continue
- }
- cells[k].SetStyle(style)
- }
- }
- }
- }
- }
- func exportProviderDay(start, end, providerApiId int64, isInsert bool) (string, error) {
- if TestExceptionCompute {
- return "", gerrors.New("异常邮件测试:数据源日报统计失败")
- }
- if TestExceptionSql {
- return "", gerrors.New("异常邮件测试:数据源sql执行失败")
- }
- date := time.Unix(start, 0).Format("2006-01-02")
- file := xlsx.NewFile()
- sheet, err := file.AddSheet("总览")
- if err != nil {
- l.Error("func",
- zap.String("call", "file.AddSheet"),
- zap.String("args", "总览"),
- zap.String("error", err.Error()))
- return "", err
- }
- makeProviderXlsxHeader(sheet, "", true)
- providerReports := []TGdProviderReport{}
- providerMap := map[string][]apis.LogQueryProviderCountExport{}
- mreq := apis.LogQueryProviderCountExportReq{}
- reply := apis.LogQueryProviderCountExportReply{}
- mreq.StartTimestamp = start
- mreq.EndTimestamp = end
- if providerApiId != 0 {
- mreq.ProviderApiIdList = append(mreq.ProviderApiIdList, providerApiId)
- }
- if time.Now().Unix()-start < 3600*26 {
- mreq.TabName = "t_gd_thirdpart_log_day"
- }
- err = LogQueryProviderCountExport(context.Background(), &mreq, &reply)
- if err != nil {
- l.Error("func",
- zap.String("call", "LogQueryProviderCountExport"),
- zap.String("args", utils.MarshalJsonString(mreq)),
- zap.String("error", err.Error()))
- return "", err
- }
- for _, v := range reply.LogQueryProviderCount {
- providerMap[v.ProviderName] = append(providerMap[v.ProviderName], v)
- }
- for name, array := range providerMap {
- if providerReports, err = parseProviderAccessCountView(&mreq, array, providerReports, sheet, mreq.TimeList, name, true, date); err != nil {
- return "", err
- }
- }
- // ==============================================================================================
- sheet2, err := file.AddSheet("详情")
- if err != nil {
- l.Error("func",
- zap.String("call", "file.AddSheet"),
- zap.String("args", "详情"),
- zap.String("error", err.Error()))
- return "", err
- }
- makeProviderXlsxHeader2(sheet2, "", true)
- providerReports2 := []TGdProviderReport{}
- merchantMap := map[string][]apis.LogQueryProviderCountExport{}
- mreq2 := apis.LogQueryProviderCountExportReq{}
- reply2 := apis.LogQueryProviderCountExportReply{}
- mreq2.StartTimestamp = start
- mreq2.EndTimestamp = end
- mreq2.HasMerchantName = true
- mreq2.GroupByMerchantApi = true
- if providerApiId != 0 {
- mreq2.ProviderApiIdList = append(mreq2.ProviderApiIdList, providerApiId)
- }
- if time.Now().Unix()-start < 3600*26 {
- mreq2.TabName = "t_gd_thirdpart_log_day"
- }
- err = LogQueryProviderCountExport(context.Background(), &mreq2, &reply2)
- if err != nil {
- l.Error("func",
- zap.String("call", "LogQueryProviderCountExport"),
- zap.String("args", utils.MarshalJsonString(mreq2)),
- zap.String("error", err.Error()))
- return "", err
- }
- for _, v := range reply2.LogQueryProviderCount {
- merchantMap[v.MerchantName] = append(merchantMap[v.MerchantName], v)
- }
- for name, array := range merchantMap {
- if providerReports2, err = parseProviderAccessCountView2(&mreq2, array, providerReports2, sheet2, mreq2.TimeList, name, true, date); err != nil {
- return "", err
- }
- }
- // ==============================================================================================
- /*for index,_ := range providerReports{
- providerId := strconv.Itoa(int(providerReports[index].ProviderApiId))
- if strings.Contains(config.Conf.ZqyCountIds,providerId) {
- zqyProviderCount(&mreq,&providerReports[index])
- providerReports[index].ChargeRate = strconv.FormatFloat(float64(100*providerReports[index].Charge)/float64(providerReports[index].Total), 'f', 2, 64) + "%"
- }
- }*/
- if isInsert {
- insertProviderReports(providerReports, date, providerApiId)
- }
- filename := fmt.Sprintf("gd数据源日报-%s", time.Unix(start, 0).Format("2006-01-02"))
- path := config.Conf.ReportLocalDir + filename + ".xlsx"
- setAllProviderCells(file)
- err = file.Save(path)
- if err != nil {
- l.Error("func",
- zap.String("call", "save excel"),
- zap.String("file_name", path),
- zap.String("error", err.Error()))
- }
- return path, err
- }
- func getProviderCountExportFromReport(startDate, endDate string) (*apis.LogQueryProviderCountExportReply, error) {
- tabs := []TGdProviderReport{}
- ret := &apis.LogQueryProviderCountExportReply{}
- o := orm.NewOrm()
- sql := fmt.Sprintf("select * from t_gd_provider_report where date >= '%s' and date <='%s'", startDate, endDate)
- _, err := o.Raw(sql).QueryRows(&tabs)
- if err != nil {
- l.Error("sql",
- zap.String("call", "getProviderCountExportFromReport"),
- zap.String("args", sql),
- zap.String("error", err.Error()))
- return nil, errors.DataBaseError
- }
- for _, v := range tabs {
- item := apis.LogQueryProviderCountExport{}
- item.Date = v.Date
- item.Total = v.Total
- item.Failed = v.Failed
- item.FailedRate = v.FailedRate
- item.ProviderApiId = v.ProviderApiId
- item.ProviderApiName = v.ProviderApiName
- item.ProviderName = v.ProviderName
- item.Success = v.Success
- item.Charge = v.Charge
- avgElapsed, _ := strconv.ParseFloat(v.AvgElapsed, 64)
- item.SumElapsed = avgElapsed * float64(v.Total)
- item.AvgElapsed, _ = strconv.ParseFloat(v.AvgElapsed, 64)
- item.Query = v.Query
- item.ChargeRate = v.ChargeRate
- ret.LogQueryProviderCount = append(ret.LogQueryProviderCount, item)
- }
- return ret, nil
- }
- func exportProviderWeek(weekStart string) (string, error) {
- start, end := getWeekExportTimestamp(weekStart)
- file := xlsx.NewFile()
- sheet, err := file.AddSheet("总览")
- if err != nil {
- l.Error("func",
- zap.String("call", "file.AddSheet"),
- zap.String("args", "总览"),
- zap.String("error", err.Error()))
- return "", err
- }
- detailSheet, err := file.AddSheet("明细")
- if err != nil {
- l.Error("func",
- zap.String("call", "file.AddSheet"),
- zap.String("args", "明细"),
- zap.String("error", err.Error()))
- return "", err
- }
- makeProviderXlsxHeader(sheet, "", false)
- makeProviderXlsxHeader(detailSheet, "", true)
- startDay := time.Unix(start, 0).Format("2006-01-02")
- endDay := time.Unix(end, 0).AddDate(0, 0, -1).Format("2006-01-02")
- reply, err := getProviderCountExportFromReport(startDay, endDay)
- if err != nil {
- return "", err
- }
- if err = parseProviderAccessCountViewAndDetail(reply.LogQueryProviderCount, sheet, detailSheet, ""); err != nil {
- return "", err
- }
- filename := fmt.Sprintf("gd数据源周报-%s", time.Unix(start, 0).Format("2006-01-02"))
- path := config.Conf.ReportLocalDir + filename + ".xlsx"
- setAllProviderCells(file)
- err = file.Save(path)
- if err != nil {
- l.Error("func",
- zap.String("call", "save excel"),
- zap.String("file_name", path),
- zap.String("error", err.Error()))
- }
- return path, err
- }
- func exportProviderMonth(monthStart string) (string, string, error) {
- start, end := getMonthExportTimestamp(monthStart)
- file := xlsx.NewFile()
- sheet, err := file.AddSheet("总览")
- if err != nil {
- l.Error("func",
- zap.String("call", "file.AddSheet"),
- zap.String("args", "总览"),
- zap.String("error", err.Error()))
- return "", "", err
- }
- detailSheet, err := file.AddSheet("明细")
- if err != nil {
- l.Error("func",
- zap.String("call", "file.AddSheet"),
- zap.String("args", "明细"),
- zap.String("error", err.Error()))
- return "", "", err
- }
- makeProviderXlsxHeader(sheet, "", false)
- makeProviderXlsxHeader(detailSheet, "", true)
- providerFiles := []string{}
- providerMap := map[string][]apis.LogQueryProviderCountExport{}
- startDay := time.Unix(start, 0).Format("2006-01-02")
- endDay := time.Unix(end, 0).AddDate(0, 0, -1).Format("2006-01-02")
- reply, err := getProviderCountExportFromReport(startDay, endDay)
- if err != nil {
- return "", "", err
- }
- for _, v := range reply.LogQueryProviderCount {
- if (strings.Contains(v.ProviderName, "觅实") || strings.Contains(v.ProviderName, "A02-MS")) && strings.Contains(v.ProviderApiName, "MSN0") == false && strings.Contains(v.ProviderApiName, "MSM00001") == false && strings.Contains(v.ProviderApiName, "觅实N0") == false {
- continue
- }
- providerMap[v.ProviderName] = append(providerMap[v.ProviderName], v)
- }
- if err = parseProviderAccessCountViewAndDetail(reply.LogQueryProviderCount, sheet, detailSheet, ""); err != nil {
- return "", "", err
- }
- for name, array := range providerMap {
- providerFile := xlsx.NewFile()
- if err := parseProviderAccessCountMonthDetail(array, providerFile, ""); err != nil {
- return "", "", err
- }
- providerFileName := fmt.Sprintf("gd数据源-%s-%s-月报", name, time.Unix(start, 0).Format("2006-01"))
- providerFilePath := config.Conf.ReportLocalDir + providerFileName + ".xlsx"
- setAllProviderCells(providerFile)
- providerFile.Save(providerFilePath)
- providerFiles = append(providerFiles, providerFilePath)
- }
- filename := fmt.Sprintf("gd数据源月报-%s", time.Unix(start, 0).Format("2006-01-02"))
- path := config.Conf.ReportLocalDir + filename + ".xlsx"
- setAllProviderCells(file)
- err = file.Save(path)
- if err != nil {
- l.Error("func",
- zap.String("call", "save excel"),
- zap.String("file_name", path),
- zap.String("error", err.Error()))
- }
- zipfileName := fmt.Sprintf(config.Conf.ReportLocalDir+"gd数据源月报-%s.zip", time.Unix(start, 0).Format("2006-01"))
- zipfile := makeZip(zipfileName, providerFiles)
- remove(providerFiles...)
- return path, zipfile, err
- }
|