Merge pull request 'move insight model from framework to console' (#80) from refactor_dashboard into master

This commit is contained in:
silenceqi 2023-05-08 16:26:03 +08:00
commit 1ee1fa9337
12 changed files with 150 additions and 16 deletions

View File

@ -10,6 +10,7 @@ import (
"infini.sh/console/config" "infini.sh/console/config"
"infini.sh/console/model" "infini.sh/console/model"
"infini.sh/console/model/alerting" "infini.sh/console/model/alerting"
"infini.sh/console/model/insight"
_ "infini.sh/console/plugin" _ "infini.sh/console/plugin"
setup1 "infini.sh/console/plugin/setup" setup1 "infini.sh/console/plugin/setup"
alerting2 "infini.sh/console/service/alerting" alerting2 "infini.sh/console/service/alerting"
@ -17,7 +18,6 @@ import (
"infini.sh/framework/core/elastic" "infini.sh/framework/core/elastic"
"infini.sh/framework/core/env" "infini.sh/framework/core/env"
"infini.sh/framework/core/global" "infini.sh/framework/core/global"
"infini.sh/framework/core/insight"
_ "infini.sh/framework/core/log" _ "infini.sh/framework/core/log"
"infini.sh/framework/core/module" "infini.sh/framework/core/module"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"

View File

@ -6,7 +6,7 @@ package alerting
import ( import (
"fmt" "fmt"
"infini.sh/framework/core/insight" "infini.sh/console/model/insight"
"regexp" "regexp"
) )

View File

@ -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"`
}

View File

@ -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"`
}

View File

@ -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
}

View File

@ -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"`
}

View File

@ -17,7 +17,7 @@ import (
"infini.sh/framework/core/elastic" "infini.sh/framework/core/elastic"
"infini.sh/framework/core/event" "infini.sh/framework/core/event"
"infini.sh/framework/core/global" "infini.sh/framework/core/global"
"infini.sh/framework/core/insight" "infini.sh/console/model/insight"
"infini.sh/framework/core/kv" "infini.sh/framework/core/kv"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"infini.sh/framework/core/queue" "infini.sh/framework/core/queue"

View File

@ -7,16 +7,15 @@ package insight
import ( import (
"net/http" "net/http"
"strconv" "strconv"
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
insight2 "infini.sh/console/model/insight"
httprouter "infini.sh/framework/core/api/router" httprouter "infini.sh/framework/core/api/router"
"infini.sh/framework/core/insight"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
) )
func (h *InsightAPI) createDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { 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) err := h.DecodeJSON(req, obj)
if err != nil { if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError) 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) { func (h *InsightAPI) getDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("dashboard_id") id := ps.MustGetParameter("dashboard_id")
obj := insight.Dashboard{} obj := insight2.Dashboard{}
obj.ID = id obj.ID = id
exists, err := orm.Get(&obj) exists, err := orm.Get(&obj)
@ -61,7 +60,7 @@ func (h *InsightAPI) getDashboard(w http.ResponseWriter, req *http.Request, ps h
q := &orm.Query{ q := &orm.Query{
RawQuery: util.MustToJSONBytes(query), RawQuery: util.MustToJSONBytes(query),
} }
err, result := orm.Search(insight.Visualization{}, q) err, result := orm.Search(insight2.Visualization{}, q)
if err != nil { if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError) h.WriteError(w, err.Error(), http.StatusInternalServerError)
log.Error(err) 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) { func (h *InsightAPI) updateDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("dashboard_id") id := ps.MustGetParameter("dashboard_id")
obj := insight.Dashboard{} obj := insight2.Dashboard{}
obj.ID = id obj.ID = id
exists, err := orm.Get(&obj) exists, err := orm.Get(&obj)
@ -92,7 +91,7 @@ func (h *InsightAPI) updateDashboard(w http.ResponseWriter, req *http.Request, p
id = obj.ID id = obj.ID
create := obj.Created create := obj.Created
obj = insight.Dashboard{} obj = insight2.Dashboard{}
err = h.DecodeJSON(req, &obj) err = h.DecodeJSON(req, &obj)
if err != nil { if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError) 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) { func (h *InsightAPI) deleteDashboard(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("dashboard_id") id := ps.MustGetParameter("dashboard_id")
obj := insight.Dashboard{} obj := insight2.Dashboard{}
obj.ID = id obj.ID = id
exists, err := orm.Get(&obj) exists, err := orm.Get(&obj)
@ -193,7 +192,7 @@ func (h *InsightAPI) searchDashboard(w http.ResponseWriter, req *http.Request, p
RawQuery: util.MustToJSONBytes(queryDSL), RawQuery: util.MustToJSONBytes(queryDSL),
} }
err, res := orm.Search(&insight.Dashboard{}, &q) err, res := orm.Search(&insight2.Dashboard{}, &q)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
h.WriteError(w, err.Error(), http.StatusInternalServerError) h.WriteError(w, err.Error(), http.StatusInternalServerError)

View File

@ -9,7 +9,7 @@ import (
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
httprouter "infini.sh/framework/core/api/router" httprouter "infini.sh/framework/core/api/router"
"infini.sh/framework/core/elastic" "infini.sh/framework/core/elastic"
"infini.sh/framework/core/insight" "infini.sh/console/model/insight"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
"math" "math"

View File

@ -10,7 +10,7 @@ import (
"strings" "strings"
"infini.sh/framework/core/elastic" "infini.sh/framework/core/elastic"
"infini.sh/framework/core/insight" "infini.sh/console/model/insight"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
) )

View File

@ -12,7 +12,7 @@ import (
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
httprouter "infini.sh/framework/core/api/router" 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/orm"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
) )

View File

@ -16,7 +16,7 @@ import (
"infini.sh/console/service/alerting/action" "infini.sh/console/service/alerting/action"
"infini.sh/console/service/alerting/funcs" "infini.sh/console/service/alerting/funcs"
"infini.sh/framework/core/elastic" "infini.sh/framework/core/elastic"
"infini.sh/framework/core/insight" "infini.sh/console/model/insight"
"infini.sh/framework/core/kv" "infini.sh/framework/core/kv"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"