report.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package http
  2. import (
  3. "net/http"
  4. "time"
  5. "go-common/app/admin/main/aegis/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. func commonPre(c *bm.Context) (res map[string][]*model.ReportFlowItem, form map[string][24]*model.ReportFlowItem, err error) {
  11. opt := new(model.OptReport)
  12. if err = c.Bind(opt); err != nil {
  13. return
  14. }
  15. if opt.UName == "debug" {
  16. opt.Type = model.TypeTotal
  17. opt.UName = "total"
  18. }
  19. return srv.ReportTaskflow(c, opt)
  20. }
  21. func taskflow(c *bm.Context) {
  22. res, form, err := commonPre(c)
  23. c.JSONMap(map[string]interface{}{
  24. "data": res,
  25. "form": form,
  26. }, err)
  27. }
  28. func taskflowCSV(c *bm.Context) {
  29. opt := new(model.OptReport)
  30. if err := c.Bind(opt); err != nil {
  31. return
  32. }
  33. res, _, err := commonPre(c)
  34. if err != nil || len(res) == 0 {
  35. c.JSON(nil, err)
  36. }
  37. csv, err := formatReport(res)
  38. if err != nil {
  39. log.Error("formatReport error(%v)", err)
  40. c.JSON(nil, err)
  41. return
  42. }
  43. c.Render(http.StatusOK, CSV{
  44. Content: FormatCSV(csv),
  45. Title: "任务吞吐量报表",
  46. })
  47. }
  48. func tasksubmitpre(c *bm.Context, pager bool) (res *model.ReportSubmitRes, p *model.DayPager, err error) {
  49. pm := new(model.OptReportSubmit)
  50. if err = c.Bind(pm); err != nil {
  51. return
  52. }
  53. if pm.FlowID <= 0 || pm.Bt > pm.Et || (pager && (pm.Pn > pm.Et || pm.Pn < pm.Bt)) {
  54. err = ecode.RequestErr
  55. return
  56. }
  57. //默认展示近期7天内的数据
  58. yesterday := time.Now().Add(-24 * time.Hour).Format("2006-01-02")
  59. if pm.Bt == "" && pm.Et == "" {
  60. pm.Bt = time.Now().Add(-8 * 24 * time.Hour).Format("2006-01-02")
  61. pm.Et = yesterday
  62. } else if pm.Et == "" { //最近日期
  63. pm.Et = yesterday
  64. } else if pm.Bt == "" { //最远日期
  65. pm.Bt = "2019-01-01"
  66. }
  67. //分页的话,默认获取第一页内容(按照日期降序排列,则为最后一日的内容)
  68. bt := pm.Bt
  69. et := pm.Et
  70. if pager {
  71. if pm.Pn == "" {
  72. pm.Pn = et
  73. }
  74. pm.Et = pm.Pn
  75. pm.Bt = pm.Pn
  76. }
  77. if res, err = srv.ReportTaskSubmit(c, pm); err != nil || res == nil {
  78. return
  79. }
  80. //按照日期降序排列
  81. if pager {
  82. p = &model.DayPager{
  83. Pn: pm.Pn,
  84. IsLast: bt == pm.Pn,
  85. IsFirst: et == pm.Pn,
  86. }
  87. }
  88. return
  89. }
  90. func taskSubmit(c *bm.Context) {
  91. res, pager, err := tasksubmitpre(c, true)
  92. c.JSONMap(map[string]interface{}{
  93. "data": res,
  94. "pager": pager,
  95. }, err)
  96. }
  97. func taskSubmitCSV(c *bm.Context) {
  98. res, _, err := tasksubmitpre(c, false)
  99. if err != nil {
  100. c.JSON(nil, err)
  101. return
  102. }
  103. csv, err := formatReportTaskSubmit(res)
  104. if err != nil {
  105. c.JSON(nil, err)
  106. return
  107. }
  108. c.Render(http.StatusOK, CSV{
  109. Content: FormatCSV(csv),
  110. Title: "任务操作报表",
  111. })
  112. }