report.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package model
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. )
  7. //hash fields
  8. const (
  9. Dispatch = "ds"
  10. Delay = "dy"
  11. Submit = "st_%d_%d" // 参数1:提交状态(任务提交,资源提交,任务关闭) 参数2:提交前属于谁
  12. Release = "rl"
  13. RscState = "rs_%d"
  14. UseTime = "ut"
  15. SetKey = "report_set"
  16. //type
  17. TypeMeta = int8(0)
  18. TypeTotal = int8(1)
  19. )
  20. //RIR resource item report
  21. type RIR struct {
  22. BizID int64
  23. FlowID int64
  24. UID int64
  25. RID int64
  26. }
  27. //Report .
  28. type Report struct {
  29. ID int64 `gorm:"AUTO_INCREMENT;primary_key;"`
  30. BusinessID int64 `gorm:"column:business_id"`
  31. FlowID int64 `gorm:"column:flow_id"`
  32. UID int64 `gorm:"column:uid"`
  33. TYPE int8 `gorm:"column:type"`
  34. Content []byte `gorm:"column:content"`
  35. }
  36. //TableName .
  37. func (r Report) TableName() string {
  38. return "task_report"
  39. }
  40. //PersonalHashKey .
  41. func PersonalHashKey(bizid, flowid, uid int64) string {
  42. return fmt.Sprintf("report_hash_%d_%d_%d", bizid, flowid, uid)
  43. }
  44. //TotalHashKey .
  45. func TotalHashKey(bizid, flowid int64) string {
  46. return fmt.Sprintf("total_inout_%d_%d_%d", bizid, flowid, 0)
  47. }
  48. //ParseKey .
  49. func ParseKey(key string) (tp int8, bizid, flowid, uid int, err error) {
  50. arr := strings.Split(key, "_")
  51. if len(arr) != 5 {
  52. err = fmt.Errorf(key)
  53. return
  54. }
  55. prefix := arr[0] + "_" + arr[1]
  56. switch prefix {
  57. case "report_hash":
  58. tp = TypeMeta
  59. case "total_inout":
  60. tp = TypeTotal
  61. default:
  62. err = fmt.Errorf(key)
  63. return
  64. }
  65. if bizid, err = strconv.Atoi(arr[2]); err != nil || bizid == 0 {
  66. err = fmt.Errorf(key)
  67. return
  68. }
  69. if flowid, err = strconv.Atoi(arr[3]); err != nil || flowid == 0 {
  70. err = fmt.Errorf(key)
  71. return
  72. }
  73. if uid, err = strconv.Atoi(arr[4]); err != nil {
  74. err = fmt.Errorf(key)
  75. return
  76. }
  77. return
  78. }