diff --git a/main.go b/main.go index 9f47c0e7..e1c7b700 100644 --- a/main.go +++ b/main.go @@ -133,6 +133,7 @@ func main() { orm.RegisterSchemaWithIndexName(alerting.Channel{}, "channel") orm.RegisterSchemaWithIndexName(insight.Visualization{}, "visualization") orm.RegisterSchemaWithIndexName(insight.Dashboard{}, "dashboard") + orm.RegisterSchemaWithIndexName(insight.Widget{}, "widget") orm.RegisterSchemaWithIndexName(task1.Task{}, "task") orm.RegisterSchemaWithIndexName(model.Layout{}, "layout") orm.RegisterSchemaWithIndexName(model.Notification{}, "notification") diff --git a/model/insight/widget.go b/model/insight/widget.go new file mode 100644 index 00000000..0cb77858 --- /dev/null +++ b/model/insight/widget.go @@ -0,0 +1,41 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * Web: https://infinilabs.com + * Email: hello#infini.ltd */ + +package insight + +import "infini.sh/framework/core/orm" + +type Widget struct { + orm.ORMObjectBase + Formatter string `json:"formatter" elastic_mapping:"formatter: { type: keyword }"` + Series [] WidgetSeriesItem `json:"series" elastic_mapping:"series: { type: object,enabled:false }"` + Title string `json:"title" elastic_mapping:"title: { type: text }"` +} + +type WidgetSeriesItem struct { + Metric WidgetMetric `json:"metric"` + Queries WidgetQuery `json:"queries"` + Type string `json:"type"` +} + +type WidgetQuery struct { + ClusterId string `json:"cluster_id"` + Indices []string `json:"indices"` + Query string `json:"query"` + TimeField string `json:"time_field"` +} +type WidgetMetric struct { + BucketSize string `json:"bucket_size"` + FormatType string `json:"format_type"` + Formula string `json:"formula"` + Groups []struct { + Field string `json:"field"` + Limit int `json:"limit"` + } `json:"groups"` + Items []struct { + Field string `json:"field"` + Name string `json:"name"` + Statistic string `json:"statistic"` + } `json:"items"` +} diff --git a/plugin/api/insight/api.go b/plugin/api/insight/api.go index 8ae2a96e..c20b6b1f 100644 --- a/plugin/api/insight/api.go +++ b/plugin/api/insight/api.go @@ -28,4 +28,5 @@ func InitAPI() { api.HandleAPIMethod(api.DELETE, "/insight/dashboard/:dashboard_id", insight.deleteDashboard) api.HandleAPIMethod(api.GET, "/insight/dashboard/_search", insight.searchDashboard) api.HandleAPIMethod(api.POST, "/elasticsearch/:id/map_label/_render", insight.renderMapLabelTemplate) + api.HandleAPIMethod(api.GET, "/insight/widget/:widget_id", insight.getWidget) } diff --git a/plugin/api/insight/widget.go b/plugin/api/insight/widget.go new file mode 100644 index 00000000..8ef34858 --- /dev/null +++ b/plugin/api/insight/widget.go @@ -0,0 +1,35 @@ +/* Copyright © INFINI Ltd. All rights reserved. + * Web: https://infinilabs.com + * Email: hello#infini.ltd */ + +package insight + +import ( + "infini.sh/console/model/insight" + httprouter "infini.sh/framework/core/api/router" + "infini.sh/framework/core/orm" + "infini.sh/framework/core/util" + "net/http" +) + +func (h *InsightAPI) getWidget(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + id := ps.MustGetParameter("widget_id") + + obj := insight.Widget{} + obj.ID = id + + exists, err := orm.Get(&obj) + if !exists || err != nil { + h.WriteJSON(w, util.MapStr{ + "_id": id, + "found": false, + }, http.StatusNotFound) + return + } + + h.WriteJSON(w, util.MapStr{ + "found": true, + "_id": id, + "_source": obj, + }, 200) +}