esMqttDomain.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package domain
  2. // Elasticsearch(iotcore-stat) MQTT 报文统计相关的响应结构。
  3. // 服务端遵循 Elasticsearch API 规范。索引名形如 iotcore-stat-<yyyy.MM.dd>,
  4. // 每条文档字段:@timestamp(date)、timeStamp(long,秒)、equipNo(keyword,站点)、cmdType(int)、event(object)。
  5. //
  6. // 说明:需求中的“站点”即 equipNo(设备号)。
  7. /*************** 通用 ES 结构 ***************/
  8. // EsShardInfo 分片信息
  9. type EsShardInfo struct {
  10. Total int `json:"total"`
  11. Successful int `json:"successful"`
  12. Skipped int `json:"skipped"`
  13. Failed int `json:"failed"`
  14. }
  15. // EsCountResponse 对应 GET <index>/_count
  16. type EsCountResponse struct {
  17. Count int64 `json:"count"`
  18. Shards *EsShardInfo `json:"_shards,omitempty"`
  19. }
  20. // EsBucket terms 聚合的单个桶(key 为字符串)
  21. type EsBucket struct {
  22. Key string `json:"key"`
  23. DocCount int64 `json:"doc_count"`
  24. }
  25. // EsKeyedBucket terms 聚合的单个桶(key 为数值,如 cmdType)
  26. type EsKeyedBucket struct {
  27. Key int64 `json:"key"`
  28. DocCount int64 `json:"doc_count"`
  29. }
  30. // EsTermsAggregation 字符串 key 的 terms 聚合结果
  31. type EsTermsAggregation struct {
  32. DocCountErrorUpperBound int64 `json:"doc_count_error_upper_bound"`
  33. SumOtherDocCount int64 `json:"sum_other_doc_count"`
  34. Buckets []EsBucket `json:"buckets"`
  35. }
  36. // EsNumericTermsAggregation 数值 key 的 terms 聚合结果
  37. type EsNumericTermsAggregation struct {
  38. DocCountErrorUpperBound int64 `json:"doc_count_error_upper_bound"`
  39. SumOtherDocCount int64 `json:"sum_other_doc_count"`
  40. Buckets []EsKeyedBucket `json:"buckets"`
  41. }
  42. // EsDateBucket date_histogram 的单个桶
  43. type EsDateBucket struct {
  44. KeyAsString string `json:"key_as_string"`
  45. Key int64 `json:"key"` // epoch millis
  46. DocCount int64 `json:"doc_count"`
  47. }
  48. // EsDateHistogramAggregation date_histogram 聚合结果
  49. type EsDateHistogramAggregation struct {
  50. Buckets []EsDateBucket `json:"buckets"`
  51. }
  52. // EsCardinality cardinality 聚合结果(去重计数)
  53. type EsCardinality struct {
  54. Value int64 `json:"value"`
  55. }
  56. /*************** 看板:报文总量 ***************/
  57. // MessageTotal 报文总量(月/日通用)
  58. type MessageTotal struct {
  59. Period string `json:"period"` // 统计周期,如 "2026.09" 或 "2026.09.08"
  60. Total int64 `json:"total"` // 报文总量
  61. }
  62. /*************** 看板:站点总量(分页,按量降序) ***************/
  63. // SiteTotalItem 单个站点(equipNo)的报文总量
  64. type SiteTotalItem struct {
  65. EquipNo string `json:"equipNo"`
  66. Total int64 `json:"total"`
  67. }
  68. // SiteTotalPage 站点总量分页结果
  69. type SiteTotalPage struct {
  70. Page int `json:"page"` // 当前页,从 1 开始
  71. PageSize int `json:"pageSize"` // 每页条数
  72. SiteCount int64 `json:"siteCount"` // 站点总数(去重 equipNo 数量),用于计算总页数
  73. Items []SiteTotalItem `json:"items"`
  74. }
  75. /*************** 下钻:站点每日总量 ***************/
  76. // DailyTotal 某天的报文总量
  77. type DailyTotal struct {
  78. Date string `json:"date"` // "2026-09-08"
  79. Total int64 `json:"total"`
  80. }
  81. /*************** 下钻:站点每日命令类型总量(分页,按量降序) ***************/
  82. // CmdTypeTotalItem 单个命令类型的报文总量
  83. type CmdTypeTotalItem struct {
  84. CmdType int64 `json:"cmdType"`
  85. Total int64 `json:"total"`
  86. }
  87. // CmdTypeTotalPage 命令类型总量分页结果
  88. type CmdTypeTotalPage struct {
  89. Page int `json:"page"`
  90. PageSize int `json:"pageSize"`
  91. CmdTypeCount int64 `json:"cmdTypeCount"` // 命令类型总数(去重),用于计算总页数
  92. Items []CmdTypeTotalItem `json:"items"`
  93. }
  94. /*************** 查询:逐条原始报文 ***************/
  95. // MqttMessageItem 单条 MQTT 报文记录(对应 _source)
  96. type MqttMessageItem struct {
  97. Timestamp string `json:"@timestamp"` // ISO8601,如 2026-09-08T21:49:53.000Z
  98. TimeStamp int64 `json:"timeStamp"` // Unix 秒
  99. EquipNo string `json:"equipNo"`
  100. CmdType int64 `json:"cmdType"`
  101. Event map[string]any `json:"event,omitempty"`
  102. }
  103. // MessagePage 逐条报文查询的分页结果
  104. type MessagePage struct {
  105. Page int `json:"page"`
  106. PageSize int `json:"pageSize"`
  107. Total int64 `json:"total"` // 命中总数(ES track_total_hits,可能为下界)
  108. Items []MqttMessageItem `json:"items"`
  109. }
  110. /*************** 内部:解析 ES _search 响应用 ***************/
  111. // EsHitsTotal hits.total
  112. type EsHitsTotal struct {
  113. Value int64 `json:"value"`
  114. Relation string `json:"relation"` // "eq" | "gte"
  115. }
  116. // EsMessageHit 单条命中
  117. type EsMessageHit struct {
  118. Source MqttMessageItem `json:"_source"`
  119. }
  120. // EsMessageHits hits 部分
  121. type EsMessageHits struct {
  122. Total EsHitsTotal `json:"total"`
  123. Hits []EsMessageHit `json:"hits"`
  124. }
  125. // EsMessageSearchResponse 逐条查询 _search 的完整返回
  126. type EsMessageSearchResponse struct {
  127. Hits *EsMessageHits `json:"hits,omitempty"`
  128. }