From d0ab79f47de51806f6b4ddc5f1f6071fa888c75d Mon Sep 17 00:00:00 2001 From: liugq Date: Thu, 21 Sep 2023 16:08:57 +0800 Subject: [PATCH] insight metric support formula template (#158) only support template variable 'bucket_size_in_second' currently, eg frmula: `a/{{.bucket_size_in_second}}` Reviewed-on: https://git.infini.ltd:64443/infini/console/pulls/158 Co-authored-by: liugq Co-committed-by: liugq --- plugin/api/insight/metadata.go | 20 ++++++++++++++++++++ plugin/task_manager/common_api.go | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/plugin/api/insight/metadata.go b/plugin/api/insight/metadata.go index 8b4c27c1..013eaa57 100644 --- a/plugin/api/insight/metadata.go +++ b/plugin/api/insight/metadata.go @@ -5,8 +5,10 @@ package insight import ( + "bytes" "github.com/Knetic/govaluate" log "github.com/cihub/seelog" + "text/template" "infini.sh/console/model/insight" httprouter "infini.sh/framework/core/api/router" "infini.sh/framework/core/elastic" @@ -224,6 +226,24 @@ func getMetricData(metric *insight.Metric) (interface{}, error) { if len(metric.Items) == 1 && formula == "" { targetMetricData = metricData }else { + tpl, err := template.New("insight_formula").Parse(formula) + if err != nil { + return nil, err + } + msgBuffer := &bytes.Buffer{} + params := map[string]interface{}{} + if metric.BucketSize != "" { + du, err := util.ParseDuration(metric.BucketSize) + if err != nil { + return nil, err + } + params["bucket_size_in_second"] = du.Seconds() + } + err = tpl.Execute(msgBuffer, params) + if err != nil { + return nil, err + } + formula = msgBuffer.String() for _, md := range metricData { targetData := insight.MetricData{ Groups: md.Groups, diff --git a/plugin/task_manager/common_api.go b/plugin/task_manager/common_api.go index 6d2c1bd5..6d6bf075 100644 --- a/plugin/task_manager/common_api.go +++ b/plugin/task_manager/common_api.go @@ -439,6 +439,7 @@ type RepeatStatus struct { LastRunTime int64 `json:"last_run_time"` NextRunTime int64 `json:"next_run_time"` LastRunChildTaskID string `json:"last_run_child_task_id"` + LastCompleteTime int64 `json:"last_complete_time"` } func (h *APIHandler) calcRepeatingStatus(taskItem *task.Task) (*task.Task, *RepeatStatus, error) { @@ -470,6 +471,9 @@ func (h *APIHandler) calcRepeatingStatus(taskItem *task.Task) (*task.Task, *Repe ret.LastRunTime = lastRepeatingChild.StartTimeInMillis if ret.LastRunTime == 0 && lastRunChild != nil { ret.LastRunTime = lastRunChild.StartTimeInMillis + if !lastRunChild.CompletedTime.IsZero(){ + ret.LastCompleteTime = lastRunChild.CompletedTime.UnixMilli() + } } if lastRunChild != nil { ret.LastRunChildTaskID = lastRunChild.ID