Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
add BatchGetStatisticDataByKey
Browse files Browse the repository at this point in the history
  • Loading branch information
November-12 committed Sep 16, 2023
1 parent 9756810 commit 0b1adb0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
69 changes: 69 additions & 0 deletions controllers/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

}
3 changes: 3 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
// 系统设置接口
Expand Down
13 changes: 13 additions & 0 deletions validate/kv_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)"`
}

0 comments on commit 0b1adb0

Please sign in to comment.