diff --git a/main.go b/main.go index 03a5e007..89ca7d52 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "infini.sh/console/config" "infini.sh/console/model" "infini.sh/console/model/alerting" + "infini.sh/console/model/insight" _ "infini.sh/console/plugin" setup1 "infini.sh/console/plugin/setup" alerting2 "infini.sh/console/service/alerting" @@ -17,7 +18,6 @@ import ( "infini.sh/framework/core/elastic" "infini.sh/framework/core/env" "infini.sh/framework/core/global" - "infini.sh/framework/core/insight" _ "infini.sh/framework/core/log" "infini.sh/framework/core/module" "infini.sh/framework/core/orm" diff --git a/model/alerting/metric.go b/model/alerting/metric.go index 5dcc419b..4eefdb18 100644 --- a/model/alerting/metric.go +++ b/model/alerting/metric.go @@ -6,7 +6,7 @@ package alerting import ( "fmt" - "infini.sh/framework/core/insight" + "infini.sh/console/model/insight" "regexp" ) diff --git a/model/insight/dashboard.go b/model/insight/dashboard.go new file mode 100644 index 00000000..6276cddb --- /dev/null +++ b/model/insight/dashboard.go @@ -0,0 +1,25 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * web: https://infinilabs.com + * mail: hello#infini.ltd */ + +package insight + +import "time" + +type Dashboard struct { + ID string `json:"id,omitempty" elastic_meta:"_id" elastic_mapping:"id: { type: keyword }"` + Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"` + Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"` + ClusterId string `json:"cluster_id" elastic_mapping:"cluster_id: { type: keyword }"` + IndexPattern string `json:"index_pattern" elastic_mapping:"index_pattern: { type: keyword }"` + TimeField string `json:"time_field,omitempty" elastic_mapping:"time_field: { type: keyword }"` + Filter interface{} `json:"filter,omitempty" elastic_mapping:"filter: { type: object }"` + BucketSize string `json:"bucket_size" elastic_mapping:"bucket_size: { type: keyword }"` + Title string `json:"title" elastic_mapping:"title: { type: keyword }"` + Description string `json:"description" elastic_mapping:"description: { type: keyword }"` + Visualizations interface{} `json:"visualizations" elastic_mapping:"visualizations: { type: object }"` + Tags []string `json:"tags,omitempty" elastic_mapping:"tags: { type: keyword }"` + User string `json:"user" elastic_mapping:"user: { type: keyword }"` + Query interface{} `json:"query,omitempty"` + TimeFilter interface{} `json:"time_filter,omitempty"` +} diff --git a/model/insight/field_meta.go b/model/insight/field_meta.go new file mode 100644 index 00000000..181b64fe --- /dev/null +++ b/model/insight/field_meta.go @@ -0,0 +1,12 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * web: https://infinilabs.com + * mail: hello#infini.ltd */ + +package insight + + +type SeriesItem struct { + Type string `json:"type"` + Options map[string]interface{} `json:"options"` + Metric Metric `json:"metric"` +} \ No newline at end of file diff --git a/model/insight/metric_data.go b/model/insight/metric_data.go new file mode 100644 index 00000000..98a5924b --- /dev/null +++ b/model/insight/metric_data.go @@ -0,0 +1,72 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * web: https://infinilabs.com + * mail: hello#infini.ltd */ + +package insight + +import ( + "fmt" + "regexp" +) + +type Metric struct { + AggTypes []string `json:"agg_types,omitempty"` + IndexPattern string `json:"index_pattern,omitempty"` + TimeField string `json:"time_field,omitempty"` + BucketSize string `json:"bucket_size,omitempty"` + Filter interface{} `json:"filter,omitempty"` + Groups []MetricGroupItem `json:"groups,omitempty"` //bucket group + ClusterId string `json:"cluster_id,omitempty"` + Formula string `json:"formula,omitempty"` + Items []MetricItem `json:"items"` + FormatType string `json:"format_type,omitempty"` + TimeFilter interface{} `json:"time_filter,omitempty"` + TimeBeforeGroup bool `json:"time_before_group,omitempty"` +} + +type MetricGroupItem struct { + Field string `json:"field"` + Limit int `json:"limit"` +} + +func (m *Metric) GenerateExpression() (string, error){ + if len(m.Items) == 1 { + return fmt.Sprintf("%s(%s)", m.Items[0].Statistic, m.Items[0].Field), nil + } + if m.Formula == "" { + return "", fmt.Errorf("formula should not be empty since there are %d metrics", len(m.Items)) + } + var ( + expressionBytes = []byte(m.Formula) + metricExpression string + ) + for _, item := range m.Items { + metricExpression = fmt.Sprintf("%s(%s)", item.Statistic, item.Field) + reg, err := regexp.Compile(item.Name+`([^\w]|$)`) + if err != nil { + return "", err + } + expressionBytes = reg.ReplaceAll(expressionBytes, []byte(metricExpression+"$1")) + } + + return string(expressionBytes), nil +} + +type MetricItem struct { + Name string `json:"name,omitempty"` + Field string `json:"field"` + FieldType string `json:"field_type,omitempty"` + Statistic string `json:"statistic,omitempty"` +} + +type MetricDataItem struct { + Timestamp interface{} `json:"timestamp,omitempty"` + Value interface{} `json:"value"` + Group string `json:"group,omitempty"` +} + +type MetricData struct { + Group string `json:"group,omitempty"` + Data map[string][]MetricDataItem +} + diff --git a/model/insight/visualization.go b/model/insight/visualization.go new file mode 100644 index 00000000..aba3045a --- /dev/null +++ b/model/insight/visualization.go @@ -0,0 +1,26 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * web: https://infinilabs.com + * mail: hello#infini.ltd */ + +package insight + +import "time" + +type Visualization struct { + ID string `json:"id,omitempty" elastic_meta:"_id" elastic_mapping:"id: { type: keyword }"` + Created *time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"` + Updated *time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"` + Title string `json:"title,omitempty" elastic_mapping:"title: { type: keyword }"` + IndexPattern string `json:"index_pattern,omitempty" elastic_mapping:"index_pattern: { type: keyword }"` + ClusterId string `json:"cluster_id,omitempty" elastic_mapping:"cluster_id: { type: keyword }"` + Series []SeriesItem `json:"series" elastic_mapping:"series: { type: object }"` + Position *Position `json:"position,omitempty" elastic_mapping:"position: { type: object }"` + Description string `json:"description,omitempty" elastic_mapping:"description: { type: keyword }"` +} + +type Position struct { + X int `json:"x"` + Y int `json:"y"` + H int `json:"h"` + W int `json:"w"` +} diff --git a/plugin/api/alerting/rule.go b/plugin/api/alerting/rule.go index 12ac28ca..ac0b3498 100644 --- a/plugin/api/alerting/rule.go +++ b/plugin/api/alerting/rule.go @@ -17,7 +17,7 @@ import ( "infini.sh/framework/core/elastic" "infini.sh/framework/core/event" "infini.sh/framework/core/global" - "infini.sh/framework/core/insight" + "infini.sh/console/model/insight" "infini.sh/framework/core/kv" "infini.sh/framework/core/orm" "infini.sh/framework/core/queue" diff --git a/plugin/api/insight/dashboard.go b/plugin/api/insight/dashboard.go index 1409357c..a5ead708 100644 --- a/plugin/api/insight/dashboard.go +++ b/plugin/api/insight/dashboard.go @@ -7,16 +7,15 @@ package insight import ( "net/http" "strconv" - log "github.com/cihub/seelog" + insight2 "infini.sh/console/model/insight" httprouter "infini.sh/framework/core/api/router" - "infini.sh/framework/core/insight" "infini.sh/framework/core/orm" "infini.sh/framework/core/util" ) func (h *InsightAPI) createDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { - var obj = &insight.Dashboard{} + var obj = &insight2.Dashboard{} err := h.DecodeJSON(req, obj) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) @@ -40,7 +39,7 @@ func (h *InsightAPI) createDashboard(w http.ResponseWriter, req *http.Request, p func (h *InsightAPI) getDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { id := ps.MustGetParameter("dashboard_id") - obj := insight.Dashboard{} + obj := insight2.Dashboard{} obj.ID = id exists, err := orm.Get(&obj) @@ -61,7 +60,7 @@ func (h *InsightAPI) getDashboard(w http.ResponseWriter, req *http.Request, ps h q := &orm.Query{ RawQuery: util.MustToJSONBytes(query), } - err, result := orm.Search(insight.Visualization{}, q) + err, result := orm.Search(insight2.Visualization{}, q) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) log.Error(err) @@ -78,7 +77,7 @@ func (h *InsightAPI) getDashboard(w http.ResponseWriter, req *http.Request, ps h func (h *InsightAPI) updateDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { id := ps.MustGetParameter("dashboard_id") - obj := insight.Dashboard{} + obj := insight2.Dashboard{} obj.ID = id exists, err := orm.Get(&obj) @@ -92,7 +91,7 @@ func (h *InsightAPI) updateDashboard(w http.ResponseWriter, req *http.Request, p id = obj.ID create := obj.Created - obj = insight.Dashboard{} + obj = insight2.Dashboard{} err = h.DecodeJSON(req, &obj) if err != nil { h.WriteError(w, err.Error(), http.StatusInternalServerError) @@ -119,7 +118,7 @@ func (h *InsightAPI) updateDashboard(w http.ResponseWriter, req *http.Request, p func (h *InsightAPI) deleteDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { id := ps.MustGetParameter("dashboard_id") - obj := insight.Dashboard{} + obj := insight2.Dashboard{} obj.ID = id exists, err := orm.Get(&obj) @@ -193,7 +192,7 @@ func (h *InsightAPI) searchDashboard(w http.ResponseWriter, req *http.Request, p RawQuery: util.MustToJSONBytes(queryDSL), } - err, res := orm.Search(&insight.Dashboard{}, &q) + err, res := orm.Search(&insight2.Dashboard{}, &q) if err != nil { log.Error(err) h.WriteError(w, err.Error(), http.StatusInternalServerError) diff --git a/plugin/api/insight/metadata.go b/plugin/api/insight/metadata.go index 1fc4173a..1c2b1cab 100644 --- a/plugin/api/insight/metadata.go +++ b/plugin/api/insight/metadata.go @@ -9,7 +9,7 @@ import ( log "github.com/cihub/seelog" httprouter "infini.sh/framework/core/api/router" "infini.sh/framework/core/elastic" - "infini.sh/framework/core/insight" + "infini.sh/console/model/insight" "infini.sh/framework/core/orm" "infini.sh/framework/core/util" "math" diff --git a/plugin/api/insight/metric_util.go b/plugin/api/insight/metric_util.go index 73a0ac69..5290b4c4 100644 --- a/plugin/api/insight/metric_util.go +++ b/plugin/api/insight/metric_util.go @@ -10,7 +10,7 @@ import ( "strings" "infini.sh/framework/core/elastic" - "infini.sh/framework/core/insight" + "infini.sh/console/model/insight" "infini.sh/framework/core/util" ) diff --git a/plugin/api/insight/visualization.go b/plugin/api/insight/visualization.go index a32512c3..9538db10 100644 --- a/plugin/api/insight/visualization.go +++ b/plugin/api/insight/visualization.go @@ -12,7 +12,7 @@ import ( log "github.com/cihub/seelog" httprouter "infini.sh/framework/core/api/router" - "infini.sh/framework/core/insight" + "infini.sh/console/model/insight" "infini.sh/framework/core/orm" "infini.sh/framework/core/util" ) diff --git a/service/alerting/elasticsearch/engine.go b/service/alerting/elasticsearch/engine.go index f2f3bcf0..075778ca 100644 --- a/service/alerting/elasticsearch/engine.go +++ b/service/alerting/elasticsearch/engine.go @@ -16,7 +16,7 @@ import ( "infini.sh/console/service/alerting/action" "infini.sh/console/service/alerting/funcs" "infini.sh/framework/core/elastic" - "infini.sh/framework/core/insight" + "infini.sh/console/model/insight" "infini.sh/framework/core/kv" "infini.sh/framework/core/orm" "infini.sh/framework/core/util"