auto_export_provider.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. // Copyright 2019 getensh.com. All rights reserved.
  2. // Use of this source code is governed by getensh.com.
  3. package crontab
  4. import (
  5. "context"
  6. "gd_crontab/apis"
  7. "gd_crontab/errors"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. gerrors "errors"
  13. "gd_crontab/common.in/config"
  14. "gd_crontab/common.in/utils"
  15. "github.com/astaxie/beego/orm"
  16. "github.com/tealeg/xlsx"
  17. "go.uber.org/zap"
  18. )
  19. func makeProviderSingleHeader(sheet *xlsx.Sheet, timeList string) {
  20. row := sheet.AddRow()
  21. cell := row.AddCell()
  22. cell.Value = "日期"
  23. sheet.SetColWidth(0, 0, 24)
  24. apiCell := row.AddCell()
  25. apiCell.Value = "API"
  26. totalCell := row.AddCell()
  27. totalCell.Value = "总量"
  28. successCell := row.AddCell()
  29. successCell.Value = "成功"
  30. successRateCell := row.AddCell()
  31. successRateCell.Value = "成功率"
  32. chargeCell := row.AddCell()
  33. chargeCell.Value = "计费量"
  34. queryCell := row.AddCell()
  35. queryCell.Value = "查得"
  36. queryRateCell := row.AddCell()
  37. queryRateCell.Value = "查得率"
  38. failedCell := row.AddCell()
  39. failedCell.Value = "异常量"
  40. failedRateCell := row.AddCell()
  41. failedRateCell.Value = "异常率"
  42. avgElapsedCell := row.AddCell()
  43. avgElapsedCell.Value = "平均时长"
  44. tArray := strings.Split(timeList, ",")
  45. for _, v := range tArray {
  46. if v == "" {
  47. return
  48. }
  49. cell := row.AddCell()
  50. cell.Value = fmt.Sprintf("时长大于%s秒", v)
  51. }
  52. }
  53. func makeProviderXlsxHeader(sheet *xlsx.Sheet, timeList string, needDate bool) {
  54. row := sheet.AddRow()
  55. sheet.SetColWidth(0, 0, 10)
  56. if needDate == true {
  57. cell := row.AddCell()
  58. cell.Value = "日期"
  59. sheet.SetColWidth(0, 0, 24)
  60. sheet.SetColWidth(0, 1, 10)
  61. }
  62. nameCell := row.AddCell()
  63. nameCell.Value = "供应商"
  64. apiCell := row.AddCell()
  65. apiCell.Value = "API"
  66. totalCell := row.AddCell()
  67. totalCell.Value = "总量"
  68. successCell := row.AddCell()
  69. successCell.Value = "成功"
  70. successRateCell := row.AddCell()
  71. successRateCell.Value = "成功率"
  72. chargeCell := row.AddCell()
  73. chargeCell.Value = "计费量"
  74. queryCell := row.AddCell()
  75. queryCell.Value = "查得"
  76. queryRateCell := row.AddCell()
  77. queryRateCell.Value = "查得率"
  78. failedCell := row.AddCell()
  79. failedCell.Value = "异常量"
  80. failedRateCell := row.AddCell()
  81. failedRateCell.Value = "异常率"
  82. avgElapsedCell := row.AddCell()
  83. avgElapsedCell.Value = "平均时长"
  84. tArray := strings.Split(timeList, ",")
  85. for _, v := range tArray {
  86. if v == "" {
  87. return
  88. }
  89. cell := row.AddCell()
  90. cell.Value = fmt.Sprintf("时长大于%s秒", v)
  91. }
  92. }
  93. func makeProviderXlsxHeader2(sheet *xlsx.Sheet, timeList string, needDate bool) {
  94. row := sheet.AddRow()
  95. sheet.SetColWidth(0, 0, 10)
  96. if needDate == true {
  97. cell := row.AddCell()
  98. cell.Value = "日期"
  99. sheet.SetColWidth(0, 0, 24)
  100. sheet.SetColWidth(0, 1, 10)
  101. }
  102. nameCell := row.AddCell()
  103. nameCell.Value = "商户"
  104. apiCell := row.AddCell()
  105. apiCell.Value = "商户api"
  106. totalCell := row.AddCell()
  107. totalCell.Value = "供应商"
  108. successCell := row.AddCell()
  109. successCell.Value = "API"
  110. successRateCell := row.AddCell()
  111. successRateCell.Value = "用量"
  112. chargeCell := row.AddCell()
  113. chargeCell.Value = "计费量"
  114. }
  115. type TGdProviderReport struct {
  116. Id int64
  117. Date string `json:"date"`
  118. ProviderName string `json:"provider_name"`
  119. ProviderApiName string `json:"provider_api_name"`
  120. Total int64 `json:"total"`
  121. Success int64 `json:"success"`
  122. SuccessRate string
  123. Failed int64 `json:"failed"`
  124. FailedRate string `json:"failed_rate"`
  125. Query int64 `json:"query"`
  126. QueryRate string `json:"query_no_record_rate"`
  127. AvgElapsed string `json:"avg_elapsed"`
  128. BigElapsedList string `json:"big_elapsed_list"`
  129. Charge int64 `json:"charge"`
  130. ChargeRate string `json:"charge_rate"`
  131. ProviderApiId int64
  132. }
  133. func addProviderReport(data *apis.LogQueryProviderCountExport, providerReports []TGdProviderReport, timeList string) []TGdProviderReport {
  134. report := TGdProviderReport{}
  135. report.Date = data.Date
  136. report.ProviderApiName = data.ProviderApiName
  137. report.Total = data.Total
  138. report.Success = data.Success
  139. report.SuccessRate = strconv.FormatFloat(float64(100*data.Success)/float64(data.Total), 'f', 2, 64) + "%"
  140. report.Failed = data.Failed
  141. report.FailedRate = data.FailedRate
  142. report.Query = data.Query
  143. report.ProviderApiId = data.ProviderApiId
  144. report.QueryRate = strconv.FormatFloat(float64(100*data.Query)/float64(data.Total), 'f', 2, 64) + "%"
  145. report.AvgElapsed = strconv.FormatFloat(data.AvgElapsed, 'f', 2, 64)
  146. report.Charge = data.Charge
  147. report.ChargeRate = data.ChargeRate
  148. report.ProviderName = data.ProviderName
  149. array := strings.Split(timeList, ",")
  150. if len(array) == len(data.BigElapsedList) {
  151. for i, _ := range array {
  152. if report.BigElapsedList == "" {
  153. report.BigElapsedList = fmt.Sprintf("%s:%d", array[i], data.BigElapsedList[i])
  154. } else {
  155. report.BigElapsedList = fmt.Sprintf("%s;%s:%d", report.BigElapsedList, array[i], data.BigElapsedList[i])
  156. }
  157. }
  158. }
  159. providerReports = append(providerReports, report)
  160. return providerReports
  161. }
  162. func parseProviderAccessCountMonthDetail(exportInfos []apis.LogQueryProviderCountExport, file *xlsx.File, timeList string) error {
  163. table := make(map[int64][]apis.LogQueryProviderCountExport, 0)
  164. for _, info := range exportInfos {
  165. table[info.ProviderApiId] = append(table[info.ProviderApiId], info)
  166. }
  167. if len(table) == 0 {
  168. file.AddSheet("空")
  169. return nil
  170. }
  171. for _, v := range table {
  172. name := ""
  173. if len(v) > 0 {
  174. name = v[0].ProviderApiName
  175. }
  176. sheet, _ := file.AddSheet(name)
  177. makeProviderSingleHeader(sheet, timeList)
  178. for index, _ := range v {
  179. row := sheet.AddRow()
  180. dateCell := row.AddCell()
  181. dateCell.Value = v[index].Date
  182. apiCell := row.AddCell()
  183. apiCell.Value = v[index].ProviderApiName
  184. parseProviderSumDetail(&v[index], row, true)
  185. }
  186. }
  187. return nil
  188. }
  189. func parseProviderAccessCountViewAndDetail(exportInfos []apis.LogQueryProviderCountExport, sheet *xlsx.Sheet, detailSheet *xlsx.Sheet, timeList string) error {
  190. dateTable := make(map[string][]apis.LogQueryProviderCountExport, 0)
  191. providerTable := make(map[string][]apis.LogQueryProviderCountExport, 0)
  192. if len(exportInfos) == 0 {
  193. return nil
  194. }
  195. for _, info := range exportInfos {
  196. dateTable[info.Date] = append(dateTable[info.Date], info)
  197. providerTable[info.ProviderName] = append(providerTable[info.ProviderName], info)
  198. }
  199. for _, infos := range providerTable {
  200. table := make(map[int64][]apis.LogQueryProviderCountExport, 0)
  201. for _, info := range infos {
  202. table[info.ProviderApiId] = append(table[info.ProviderApiId], info)
  203. }
  204. for _, v := range table {
  205. sum := apis.LogQueryProviderCountExport{}
  206. row := sheet.AddRow()
  207. providerCell := row.AddCell()
  208. apiCell := row.AddCell()
  209. for index, _ := range v {
  210. if providerCell.Value == "" {
  211. providerCell.Value = v[index].ProviderName
  212. }
  213. if apiCell.Value == "" {
  214. apiCell.Value = v[index].ProviderApiName
  215. }
  216. sum.Total += v[index].Total
  217. sum.Charge += v[index].Charge
  218. sum.Failed += sum.Failed
  219. sum.Success += v[index].Success
  220. sum.Query += v[index].Query
  221. sum.SumElapsed += v[index].SumElapsed
  222. sum.BigElapsedList = sumBigElapseds(sum.BigElapsedList, v[index].BigElapsedList)
  223. }
  224. parseProviderSumDetail(&sum, row, true)
  225. }
  226. }
  227. for date, infos := range dateTable {
  228. providerTable := make(map[string][]apis.LogQueryProviderCountExport, 0)
  229. for _, v := range infos {
  230. providerTable[v.ProviderName] = append(providerTable[v.ProviderName], v)
  231. }
  232. for _, array := range providerTable {
  233. for _, v := range array {
  234. row := detailSheet.AddRow()
  235. dateCell := row.AddCell()
  236. dateCell.Value = date
  237. providerCell := row.AddCell()
  238. providerCell.Value = v.ProviderName
  239. apiCell := row.AddCell()
  240. apiCell.Value = v.ProviderApiName
  241. parseProviderSumDetail(&v, row, true)
  242. }
  243. }
  244. }
  245. return nil
  246. }
  247. func parseProviderAccessCountView(mreq *apis.LogQueryProviderCountExportReq, exportInfos []apis.LogQueryProviderCountExport, providerReports []TGdProviderReport, sheet *xlsx.Sheet, timeList string, name string, isRecord bool, date string) ([]TGdProviderReport, error) {
  248. table := make(map[string][]apis.LogQueryProviderCountExport, 0)
  249. if len(exportInfos) == 0 {
  250. return providerReports, nil
  251. }
  252. for index, _ := range exportInfos {
  253. table[exportInfos[index].ProviderApiName] = append(table[exportInfos[index].ProviderApiName], exportInfos[index])
  254. }
  255. for k, v := range table {
  256. sum := apis.LogQueryProviderCountExport{}
  257. row := sheet.AddRow()
  258. dateCell := row.AddCell()
  259. dateCell.Value = date
  260. providerCell := row.AddCell()
  261. providerCell.Value = name
  262. apiCell := row.AddCell()
  263. apiCell.Value = k
  264. for index, _ := range v {
  265. sum.ProviderApiId = v[index].ProviderApiId
  266. sum.Total += v[index].Total
  267. sum.Charge += v[index].Charge
  268. sum.Failed += v[index].Failed
  269. sum.Success += v[index].Success
  270. sum.Query += v[index].Query
  271. sum.SumElapsed += v[index].SumElapsed
  272. sum.BigElapsedList = sumBigElapseds(sum.BigElapsedList, v[index].BigElapsedList)
  273. }
  274. providerId := strconv.Itoa(int(sum.ProviderApiId))
  275. if strings.Contains(config.Conf.ZqyCountIds, providerId) {
  276. sum.Date = date
  277. zqyProviderCount(mreq, &sum)
  278. sum.ChargeRate = strconv.FormatFloat(float64(100*sum.Charge)/float64(sum.Total), 'f', 2, 64) + "%"
  279. }
  280. parseProviderSumDetail(&sum, row, true)
  281. if isRecord {
  282. data := sum
  283. data.ProviderApiName = k
  284. data.ProviderName = name
  285. data.Date = date
  286. providerReports = addProviderReport(&data, providerReports, timeList)
  287. }
  288. }
  289. table = nil
  290. return providerReports, nil
  291. }
  292. func parseProviderAccessCountView2(mreq *apis.LogQueryProviderCountExportReq, exportInfos []apis.LogQueryProviderCountExport, providerReports []TGdProviderReport, sheet *xlsx.Sheet, timeList string, name string, isRecord bool, date string) ([]TGdProviderReport, error) {
  293. if len(exportInfos) == 0 {
  294. return providerReports, nil
  295. }
  296. for _, info := range exportInfos {
  297. sum := apis.LogQueryProviderCountExport{}
  298. row := sheet.AddRow()
  299. dateCell := row.AddCell()
  300. dateCell.Value = date
  301. merchantCell := row.AddCell()
  302. merchantCell.Value = name
  303. sum.ApiName = info.ApiName
  304. sum.ProviderApiId = info.ProviderApiId
  305. sum.ProviderApiName = info.ProviderApiName
  306. sum.ProviderName = info.ProviderName
  307. sum.Total += info.Total
  308. sum.Charge = info.Charge
  309. providerId := strconv.Itoa(int(sum.ProviderApiId))
  310. if strings.Contains(config.Conf.ZqyCountIds, providerId) {
  311. sum.Date = date
  312. mreq.MerchantId = info.MerchantId
  313. mreq.ApiId = info.ApiId
  314. zqyProviderCount(mreq, &sum)
  315. sum.ChargeRate = strconv.FormatFloat(float64(100*sum.Charge)/float64(sum.Total), 'f', 2, 64) + "%"
  316. }
  317. parseProviderSumDetail2(&sum, row, true)
  318. if isRecord {
  319. data := sum
  320. data.ProviderApiName = info.ProviderApiName
  321. data.ProviderName = name
  322. data.Date = date
  323. providerReports = addProviderReport(&data, providerReports, timeList)
  324. }
  325. }
  326. return providerReports, nil
  327. }
  328. func parseProviderSumDetail(sum *apis.LogQueryProviderCountExport, row *xlsx.Row, charge bool) {
  329. if sum.Total == 0 {
  330. sum.ChargeRate = "0%"
  331. } else {
  332. sum.ChargeRate = strconv.FormatFloat(float64(100*sum.Charge)/float64(sum.Total), 'f', 2, 64) + "%"
  333. }
  334. queryRate := strconv.FormatFloat(float64(100*sum.Query)/float64(sum.Total), 'f', 2, 64) + "%"
  335. sum.FailedRate = strconv.FormatFloat(float64(100*sum.Failed)/float64(sum.Total), 'f', 2, 64) + "%"
  336. successRate := strconv.FormatFloat(float64(100*sum.Success)/float64(sum.Total), 'f', 2, 64) + "%"
  337. totalCell := row.AddCell()
  338. totalCell.SetInt(int(sum.Total))
  339. successCell := row.AddCell()
  340. successCell.SetInt(int(sum.Success))
  341. successRateCell := row.AddCell()
  342. SetPercentForXlsx(successRate, successRateCell)
  343. if charge {
  344. chargeCell := row.AddCell()
  345. chargeCell.SetInt(int(sum.Charge))
  346. style := xlsx.NewStyle()
  347. font := xlsx.NewFont(10, "Microsoft YaHei")
  348. style.Font = *font
  349. style.Border = *xlsx.NewBorder("thin", "none", "thin", "thin")
  350. fill := xlsx.NewFill("solid", "00FFFF00", "00000000")
  351. style.Fill = *fill
  352. chargeCell.SetStyle(style)
  353. }
  354. queryCell := row.AddCell()
  355. queryCell.SetInt(int(sum.Query))
  356. queryRateCell := row.AddCell()
  357. SetPercentForXlsx(queryRate, queryRateCell)
  358. failedCell := row.AddCell()
  359. failedCell.SetInt(int(sum.Failed))
  360. failedRateCell := row.AddCell()
  361. SetPercentForXlsx(sum.FailedRate, failedRateCell)
  362. avgElapsedCell := row.AddCell()
  363. sum.AvgElapsed = sum.SumElapsed / float64(sum.Total)
  364. elStr := strconv.FormatFloat(sum.AvgElapsed, 'f', 2, 64)
  365. el, _ := strconv.ParseFloat(elStr, 64)
  366. avgElapsedCell.SetFloatWithFormat(el, "0.00")
  367. for _, v := range sum.BigElapsedList {
  368. cell := row.AddCell()
  369. cell.SetInt(v)
  370. }
  371. }
  372. func parseProviderSumDetail2(sum *apis.LogQueryProviderCountExport, row *xlsx.Row, charge bool) {
  373. apiCell := row.AddCell()
  374. apiCell.Value = sum.ApiName
  375. providerName := row.AddCell()
  376. providerName.Value = sum.ProviderName
  377. providerApiName := row.AddCell()
  378. providerApiName.Value = sum.ProviderApiName
  379. totalCell := row.AddCell()
  380. totalCell.SetInt(int(sum.Total))
  381. if charge {
  382. chargeCell := row.AddCell()
  383. chargeCell.SetInt(int(sum.Charge))
  384. style := xlsx.NewStyle()
  385. font := xlsx.NewFont(10, "Microsoft YaHei")
  386. style.Font = *font
  387. style.Border = *xlsx.NewBorder("thin", "thin", "thin", "thin")
  388. fill := xlsx.NewFill("solid", "00FFFF00", "00000000")
  389. style.Fill = *fill
  390. chargeCell.SetStyle(style)
  391. }
  392. }
  393. func sendProviderMail(to, host, user, password string, files ...string) error {
  394. if TestExceptionMail {
  395. return gerrors.New("异常邮件测试:统计邮件发送失败")
  396. }
  397. err := sendMailExec(user,
  398. password,
  399. host,
  400. to,
  401. "gd数据源报表", files)
  402. if err != nil {
  403. l.Error("func",
  404. zap.String("call", "sendMailExec"),
  405. zap.String("args",
  406. fmt.Sprintf("password:%s,host:%s,to:%s,subject:%s,files:%v\n",
  407. password, host, to, "gd数据源报表", files)),
  408. zap.String("error", err.Error()))
  409. }
  410. return err
  411. }
  412. func insertProviderReports(providerReports []TGdProviderReport, date string, providerApiId int64) {
  413. if len(providerReports) == 0 {
  414. return
  415. }
  416. if providerApiId > 0 {
  417. for i, v := range providerReports {
  418. db.Raw("delete from t_gd_provider_report where date=? and provider_api_id=?", date, v.ProviderApiId).Exec()
  419. db.Insert(&providerReports[i])
  420. }
  421. return
  422. }
  423. db.Raw("delete from t_gd_provider_report where date=?", date).Exec()
  424. db.InsertMulti(len(providerReports), &providerReports)
  425. }
  426. func setAllProviderCells(file *xlsx.File) {
  427. style := xlsx.NewStyle()
  428. font := xlsx.NewFont(10, "Microsoft YaHei")
  429. style.Font = *font
  430. style.Border = *xlsx.NewBorder("thin", "none", "thin", "thin")
  431. headerStyle := xlsx.NewStyle()
  432. fill := xlsx.NewFill("solid", "00B0C4DE", "00000000")
  433. headerStyle.Font = *font
  434. headerStyle.Fill = *fill
  435. headerStyle.Border = *xlsx.NewBorder("thin", "none", "thin", "thin")
  436. sheets := file.Sheets
  437. for i := 0; i < len(sheets); i++ {
  438. rows := sheets[i].Rows
  439. for j := 0; j < len(rows); j++ {
  440. cells := rows[j].Cells
  441. for k := 0; k < len(cells); k++ {
  442. if j < 1 {
  443. cells[k].SetStyle(headerStyle)
  444. } else {
  445. if cells[k].GetStyle().Fill.FgColor == "00FFFF00" {
  446. continue
  447. }
  448. cells[k].SetStyle(style)
  449. }
  450. }
  451. }
  452. }
  453. }
  454. func exportProviderDay(start, end, providerApiId int64, isInsert bool) (string, error) {
  455. if TestExceptionCompute {
  456. return "", gerrors.New("异常邮件测试:数据源日报统计失败")
  457. }
  458. if TestExceptionSql {
  459. return "", gerrors.New("异常邮件测试:数据源sql执行失败")
  460. }
  461. date := time.Unix(start, 0).Format("2006-01-02")
  462. file := xlsx.NewFile()
  463. sheet, err := file.AddSheet("总览")
  464. if err != nil {
  465. l.Error("func",
  466. zap.String("call", "file.AddSheet"),
  467. zap.String("args", "总览"),
  468. zap.String("error", err.Error()))
  469. return "", err
  470. }
  471. makeProviderXlsxHeader(sheet, "", true)
  472. providerReports := []TGdProviderReport{}
  473. providerMap := map[string][]apis.LogQueryProviderCountExport{}
  474. mreq := apis.LogQueryProviderCountExportReq{}
  475. reply := apis.LogQueryProviderCountExportReply{}
  476. mreq.StartTimestamp = start
  477. mreq.EndTimestamp = end
  478. if providerApiId != 0 {
  479. mreq.ProviderApiIdList = append(mreq.ProviderApiIdList, providerApiId)
  480. }
  481. if time.Now().Unix()-start < 3600*26 {
  482. mreq.TabName = "t_gd_thirdpart_log_day"
  483. }
  484. err = LogQueryProviderCountExport(context.Background(), &mreq, &reply)
  485. if err != nil {
  486. l.Error("func",
  487. zap.String("call", "LogQueryProviderCountExport"),
  488. zap.String("args", utils.MarshalJsonString(mreq)),
  489. zap.String("error", err.Error()))
  490. return "", err
  491. }
  492. for _, v := range reply.LogQueryProviderCount {
  493. providerMap[v.ProviderName] = append(providerMap[v.ProviderName], v)
  494. }
  495. for name, array := range providerMap {
  496. if providerReports, err = parseProviderAccessCountView(&mreq, array, providerReports, sheet, mreq.TimeList, name, true, date); err != nil {
  497. return "", err
  498. }
  499. }
  500. // ==============================================================================================
  501. sheet2, err := file.AddSheet("详情")
  502. if err != nil {
  503. l.Error("func",
  504. zap.String("call", "file.AddSheet"),
  505. zap.String("args", "详情"),
  506. zap.String("error", err.Error()))
  507. return "", err
  508. }
  509. makeProviderXlsxHeader2(sheet2, "", true)
  510. providerReports2 := []TGdProviderReport{}
  511. merchantMap := map[string][]apis.LogQueryProviderCountExport{}
  512. mreq2 := apis.LogQueryProviderCountExportReq{}
  513. reply2 := apis.LogQueryProviderCountExportReply{}
  514. mreq2.StartTimestamp = start
  515. mreq2.EndTimestamp = end
  516. mreq2.HasMerchantName = true
  517. mreq2.GroupByMerchantApi = true
  518. if providerApiId != 0 {
  519. mreq2.ProviderApiIdList = append(mreq2.ProviderApiIdList, providerApiId)
  520. }
  521. if time.Now().Unix()-start < 3600*26 {
  522. mreq2.TabName = "t_gd_thirdpart_log_day"
  523. }
  524. err = LogQueryProviderCountExport(context.Background(), &mreq2, &reply2)
  525. if err != nil {
  526. l.Error("func",
  527. zap.String("call", "LogQueryProviderCountExport"),
  528. zap.String("args", utils.MarshalJsonString(mreq2)),
  529. zap.String("error", err.Error()))
  530. return "", err
  531. }
  532. for _, v := range reply2.LogQueryProviderCount {
  533. merchantMap[v.MerchantName] = append(merchantMap[v.MerchantName], v)
  534. }
  535. for name, array := range merchantMap {
  536. if providerReports2, err = parseProviderAccessCountView2(&mreq2, array, providerReports2, sheet2, mreq2.TimeList, name, true, date); err != nil {
  537. return "", err
  538. }
  539. }
  540. // ==============================================================================================
  541. /*for index,_ := range providerReports{
  542. providerId := strconv.Itoa(int(providerReports[index].ProviderApiId))
  543. if strings.Contains(config.Conf.ZqyCountIds,providerId) {
  544. zqyProviderCount(&mreq,&providerReports[index])
  545. providerReports[index].ChargeRate = strconv.FormatFloat(float64(100*providerReports[index].Charge)/float64(providerReports[index].Total), 'f', 2, 64) + "%"
  546. }
  547. }*/
  548. if isInsert {
  549. insertProviderReports(providerReports, date, providerApiId)
  550. }
  551. filename := fmt.Sprintf("gd数据源日报-%s", time.Unix(start, 0).Format("2006-01-02"))
  552. path := config.Conf.ReportLocalDir + filename + ".xlsx"
  553. setAllProviderCells(file)
  554. err = file.Save(path)
  555. if err != nil {
  556. l.Error("func",
  557. zap.String("call", "save excel"),
  558. zap.String("file_name", path),
  559. zap.String("error", err.Error()))
  560. }
  561. return path, err
  562. }
  563. func getProviderCountExportFromReport(startDate, endDate string) (*apis.LogQueryProviderCountExportReply, error) {
  564. tabs := []TGdProviderReport{}
  565. ret := &apis.LogQueryProviderCountExportReply{}
  566. o := orm.NewOrm()
  567. sql := fmt.Sprintf("select * from t_gd_provider_report where date >= '%s' and date <='%s'", startDate, endDate)
  568. _, err := o.Raw(sql).QueryRows(&tabs)
  569. if err != nil {
  570. l.Error("sql",
  571. zap.String("call", "getProviderCountExportFromReport"),
  572. zap.String("args", sql),
  573. zap.String("error", err.Error()))
  574. return nil, errors.DataBaseError
  575. }
  576. for _, v := range tabs {
  577. item := apis.LogQueryProviderCountExport{}
  578. item.Date = v.Date
  579. item.Total = v.Total
  580. item.Failed = v.Failed
  581. item.FailedRate = v.FailedRate
  582. item.ProviderApiId = v.ProviderApiId
  583. item.ProviderApiName = v.ProviderApiName
  584. item.ProviderName = v.ProviderName
  585. item.Success = v.Success
  586. item.Charge = v.Charge
  587. avgElapsed, _ := strconv.ParseFloat(v.AvgElapsed, 64)
  588. item.SumElapsed = avgElapsed * float64(v.Total)
  589. item.AvgElapsed, _ = strconv.ParseFloat(v.AvgElapsed, 64)
  590. item.Query = v.Query
  591. item.ChargeRate = v.ChargeRate
  592. ret.LogQueryProviderCount = append(ret.LogQueryProviderCount, item)
  593. }
  594. return ret, nil
  595. }
  596. func exportProviderWeek(weekStart string) (string, error) {
  597. start, end := getWeekExportTimestamp(weekStart)
  598. file := xlsx.NewFile()
  599. sheet, err := file.AddSheet("总览")
  600. if err != nil {
  601. l.Error("func",
  602. zap.String("call", "file.AddSheet"),
  603. zap.String("args", "总览"),
  604. zap.String("error", err.Error()))
  605. return "", err
  606. }
  607. detailSheet, err := file.AddSheet("明细")
  608. if err != nil {
  609. l.Error("func",
  610. zap.String("call", "file.AddSheet"),
  611. zap.String("args", "明细"),
  612. zap.String("error", err.Error()))
  613. return "", err
  614. }
  615. makeProviderXlsxHeader(sheet, "", false)
  616. makeProviderXlsxHeader(detailSheet, "", true)
  617. startDay := time.Unix(start, 0).Format("2006-01-02")
  618. endDay := time.Unix(end, 0).AddDate(0, 0, -1).Format("2006-01-02")
  619. reply, err := getProviderCountExportFromReport(startDay, endDay)
  620. if err != nil {
  621. return "", err
  622. }
  623. if err = parseProviderAccessCountViewAndDetail(reply.LogQueryProviderCount, sheet, detailSheet, ""); err != nil {
  624. return "", err
  625. }
  626. filename := fmt.Sprintf("gd数据源周报-%s", time.Unix(start, 0).Format("2006-01-02"))
  627. path := config.Conf.ReportLocalDir + filename + ".xlsx"
  628. setAllProviderCells(file)
  629. err = file.Save(path)
  630. if err != nil {
  631. l.Error("func",
  632. zap.String("call", "save excel"),
  633. zap.String("file_name", path),
  634. zap.String("error", err.Error()))
  635. }
  636. return path, err
  637. }
  638. func exportProviderMonth(monthStart string) (string, string, error) {
  639. start, end := getMonthExportTimestamp(monthStart)
  640. file := xlsx.NewFile()
  641. sheet, err := file.AddSheet("总览")
  642. if err != nil {
  643. l.Error("func",
  644. zap.String("call", "file.AddSheet"),
  645. zap.String("args", "总览"),
  646. zap.String("error", err.Error()))
  647. return "", "", err
  648. }
  649. detailSheet, err := file.AddSheet("明细")
  650. if err != nil {
  651. l.Error("func",
  652. zap.String("call", "file.AddSheet"),
  653. zap.String("args", "明细"),
  654. zap.String("error", err.Error()))
  655. return "", "", err
  656. }
  657. makeProviderXlsxHeader(sheet, "", false)
  658. makeProviderXlsxHeader(detailSheet, "", true)
  659. providerFiles := []string{}
  660. providerMap := map[string][]apis.LogQueryProviderCountExport{}
  661. startDay := time.Unix(start, 0).Format("2006-01-02")
  662. endDay := time.Unix(end, 0).AddDate(0, 0, -1).Format("2006-01-02")
  663. reply, err := getProviderCountExportFromReport(startDay, endDay)
  664. if err != nil {
  665. return "", "", err
  666. }
  667. for _, v := range reply.LogQueryProviderCount {
  668. 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 {
  669. continue
  670. }
  671. providerMap[v.ProviderName] = append(providerMap[v.ProviderName], v)
  672. }
  673. if err = parseProviderAccessCountViewAndDetail(reply.LogQueryProviderCount, sheet, detailSheet, ""); err != nil {
  674. return "", "", err
  675. }
  676. for name, array := range providerMap {
  677. providerFile := xlsx.NewFile()
  678. if err := parseProviderAccessCountMonthDetail(array, providerFile, ""); err != nil {
  679. return "", "", err
  680. }
  681. providerFileName := fmt.Sprintf("gd数据源-%s-%s-月报", name, time.Unix(start, 0).Format("2006-01"))
  682. providerFilePath := config.Conf.ReportLocalDir + providerFileName + ".xlsx"
  683. setAllProviderCells(providerFile)
  684. providerFile.Save(providerFilePath)
  685. providerFiles = append(providerFiles, providerFilePath)
  686. }
  687. filename := fmt.Sprintf("gd数据源月报-%s", time.Unix(start, 0).Format("2006-01-02"))
  688. path := config.Conf.ReportLocalDir + filename + ".xlsx"
  689. setAllProviderCells(file)
  690. err = file.Save(path)
  691. if err != nil {
  692. l.Error("func",
  693. zap.String("call", "save excel"),
  694. zap.String("file_name", path),
  695. zap.String("error", err.Error()))
  696. }
  697. zipfileName := fmt.Sprintf(config.Conf.ReportLocalDir+"gd数据源月报-%s.zip", time.Unix(start, 0).Format("2006-01"))
  698. zipfile := makeZip(zipfileName, providerFiles)
  699. remove(providerFiles...)
  700. return path, zipfile, err
  701. }