2021-06-27 21:59:06 +08:00
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
|
|
|
|
package metrics
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2021-06-30 19:59:59 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
2021-06-27 21:59:06 +08:00
|
|
|
"github.com/iwind/TeaGo/actions"
|
|
|
|
|
"github.com/iwind/TeaGo/maps"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CreatePopupAction struct {
|
|
|
|
|
actionutils.ParentAction
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreatePopupAction) Init() {
|
|
|
|
|
this.Nav("", "", "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreatePopupAction) RunGet(params struct {
|
|
|
|
|
Category string
|
|
|
|
|
}) {
|
|
|
|
|
this.Data["category"] = params.Category
|
2021-06-30 19:59:59 +08:00
|
|
|
this.Data["valueDefinitions"] = serverconfigs.FindAllMetricValueDefinitions(params.Category)
|
2021-06-27 21:59:06 +08:00
|
|
|
|
|
|
|
|
this.Show()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *CreatePopupAction) RunPost(params struct {
|
|
|
|
|
Name string
|
|
|
|
|
Category string
|
|
|
|
|
KeysJSON []byte
|
|
|
|
|
PeriodJSON []byte
|
|
|
|
|
Value string
|
2021-07-19 15:23:20 +08:00
|
|
|
IsPublic bool
|
2021-06-27 21:59:06 +08:00
|
|
|
|
|
|
|
|
Must *actions.Must
|
|
|
|
|
CSRF *actionutils.CSRF
|
|
|
|
|
}) {
|
|
|
|
|
params.Must.
|
|
|
|
|
Field("name", params.Name).
|
|
|
|
|
Require("请输入指标名称")
|
|
|
|
|
|
|
|
|
|
if len(params.Category) == 0 {
|
|
|
|
|
this.Fail("请选择指标类型")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 统计对象
|
|
|
|
|
if len(params.KeysJSON) == 0 {
|
|
|
|
|
this.FailField("keys", "请选择指标统计的对象")
|
|
|
|
|
}
|
|
|
|
|
var keys = []string{}
|
|
|
|
|
err := json.Unmarshal(params.KeysJSON, &keys)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.FailField("keys", "解析指标对象失败")
|
|
|
|
|
}
|
|
|
|
|
if len(keys) == 0 {
|
|
|
|
|
this.FailField("keys", "请选择指标统计的对象")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var periodMap = maps.Map{}
|
|
|
|
|
err = json.Unmarshal(params.PeriodJSON, &periodMap)
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.FailField("period", "解析统计周期失败")
|
|
|
|
|
}
|
|
|
|
|
var period = periodMap.GetInt32("period")
|
|
|
|
|
var periodUnit = periodMap.GetString("unit")
|
|
|
|
|
|
|
|
|
|
createResp, err := this.RPC().MetricItemRPC().CreateMetricItem(this.AdminContext(), &pb.CreateMetricItemRequest{
|
|
|
|
|
Code: "", // TODO 未来实现
|
|
|
|
|
Category: params.Category,
|
|
|
|
|
Name: params.Name,
|
|
|
|
|
Keys: keys,
|
|
|
|
|
Period: period,
|
|
|
|
|
PeriodUnit: periodUnit,
|
|
|
|
|
Value: params.Value,
|
2021-07-19 15:23:20 +08:00
|
|
|
IsPublic: params.IsPublic,
|
2021-06-27 21:59:06 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
this.ErrorPage(err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer this.CreateLogInfo("创建统计指标 %d", createResp.MetricItemId)
|
|
|
|
|
this.Success()
|
|
|
|
|
}
|