From 0b1adb024166333de1a1df10d0ba9668d0120764 Mon Sep 17 00:00:00 2001 From: "November.12" <845255519@qq.com> Date: Sat, 16 Sep 2023 09:55:00 +0800 Subject: [PATCH] add BatchGetStatisticDataByKey --- controllers/kv.go | 69 +++++++++++++++++++++++++++++++++++++++++ routers/router.go | 3 ++ validate/kv_validate.go | 13 ++++++++ 3 files changed, 85 insertions(+) diff --git a/controllers/kv.go b/controllers/kv.go index 81430f987..a5c73620e 100644 --- a/controllers/kv.go +++ b/controllers/kv.go @@ -611,3 +611,72 @@ func (c *KvController) GetKVHistoryData() { } response.SuccessWithDetailed(200, "success", easyData, map[string]string{}, (*context2.Context)(c.Ctx)) } + +func (c *KvController) BatchGetStatisticDataByKey() { + inputData := valid.BatchStatisticDataValidate{} + err := json.Unmarshal(c.Ctx.Input.RequestBody, &inputData) + if err != nil { + fmt.Println("参数解析失败", err.Error()) + } + v := validation.Validation{} + status, _ := v.Valid(inputData) + if !status { + for _, err := range v.Errors { + // 获取字段别称 + alias := gvalid.GetAlias(inputData, err.Field) + message := strings.Replace(err.Message, err.Field, alias, 1) + response.SuccessWithMessage(1000, message, (*context2.Context)(c.Ctx)) + break + } + return + } + + if (inputData.EndTime - inputData.StartTime) > int64(time.Duration(3)*time.Hour/time.Microsecond) { + response.SuccessWithMessage(400, "时间段大于3小时", (*context2.Context)(c.Ctx)) + return + } + + var TSKVService services.TSKVService + + var result []map[string]interface{} + for _, v := range inputData.Data { + var data []map[string]interface{} + // 继续进行参数校验,如果是不聚合,仅校验时间范围是否是3小时内 + if inputData.AggregateWindow == "no_aggregate" { + // 不聚合查询 + data, err = TSKVService.GetKVDataWithNoAggregate( + v.DeviceId, + v.Key, + inputData.StartTime, + inputData.EndTime) + if err != nil { + response.SuccessWithMessage(400, err.Error(), (*context2.Context)(c.Ctx)) + return + } + } else { + // 聚合查询 + data, err = TSKVService.GetKVDataWithAggregate( + v.DeviceId, + v.Key, + inputData.StartTime, + inputData.EndTime, + valid.StatisticAggregateWindow[inputData.AggregateWindow], // 30s -> 30000000 + valid.StatisticAggregateFunction[inputData.AggregateFunction], // avg -> AVG + ) + if err != nil { + response.SuccessWithMessage(400, err.Error(), (*context2.Context)(c.Ctx)) + return + } + } + + bigData := make(map[string]interface{}) + + bigData["device_id"] = v.DeviceId + bigData["key"] = v.Key + bigData["time_series"] = data + + result = append(result, bigData) + } + response.SuccessWithDetailed(200, "success", result, map[string]string{}, (*context2.Context)(c.Ctx)) + +} diff --git a/routers/router.go b/routers/router.go index fc29a7f7c..06e73918d 100644 --- a/routers/router.go +++ b/routers/router.go @@ -279,6 +279,9 @@ func init() { // 通过设备ID、key查询某个时间范围内的设备统计类数据 web.NSRouter("/kv/statistic", &controllers.KvController{}, "*:GetStatisticDataByKey"), + + // 批量通过设备ID、key查询某个时间范围内的设备统计类数据 + web.NSRouter("/kv/statistic/batch", &controllers.KvController{}, "*:BatchGetStatisticDataByKey"), // 通过设备id、key删除设备历史数据 web.NSRouter("/kv/history/delete", &controllers.KvController{}, "*:DeleteHistoryData"), // 系统设置接口 diff --git a/validate/kv_validate.go b/validate/kv_validate.go index 13264af21..a4c589fa6 100644 --- a/validate/kv_validate.go +++ b/validate/kv_validate.go @@ -136,3 +136,16 @@ type KVHistoryDataValidate struct { PageRecords int `json:"page_records" alias:"每页数量" valid:"Required"` ExportExcel bool `json:"export_excel" alias:"导出开关"` } + +type BatchStatisticDataValidate struct { + Data []BatchStatisticDataDeviceInfoValidate `json:"data" alias:"设备信息" valid:"Required"` + StartTime int64 `json:"start_time" alias:"开始时间" valid:"Required"` + EndTime int64 `json:"end_time" alias:"结束时间" valid:"Required"` + AggregateWindow string `json:"aggregate_window" alias:"聚合间隔"` + AggregateFunction string `json:"aggregate_function" alias:"聚合方法"` +} + +type BatchStatisticDataDeviceInfoValidate struct { + DeviceId string `json:"device_id" alias:"设备ID" valid:"Required;MaxSize(36)"` + Key string `json:"key" alias:"属性" valid:"Required;MaxSize(36)"` +}