From 14f391b58b64604d6f72704cab0245e59818763c Mon Sep 17 00:00:00 2001 From: liugq Date: Thu, 17 Aug 2023 11:35:35 +0800 Subject: [PATCH] update widget struct --- model/insight/widget.go | 30 +----------------------------- plugin/api/insight/api.go | 1 + plugin/api/insight/widget.go | 26 +++++++++++++++++++++----- 3 files changed, 23 insertions(+), 34 deletions(-) diff --git a/model/insight/widget.go b/model/insight/widget.go index 0cb77858..e004cc3f 100644 --- a/model/insight/widget.go +++ b/model/insight/widget.go @@ -8,34 +8,6 @@ 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"` + Config interface{}`json:"config" elastic_mapping:"config: { type: object,enabled:false }"` } diff --git a/plugin/api/insight/api.go b/plugin/api/insight/api.go index c20b6b1f..27b232be 100644 --- a/plugin/api/insight/api.go +++ b/plugin/api/insight/api.go @@ -29,4 +29,5 @@ func InitAPI() { 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) + api.HandleAPIMethod(api.POST, "/insight/widget", insight.RequireLogin(insight.createWidget)) } diff --git a/plugin/api/insight/widget.go b/plugin/api/insight/widget.go index 8ef34858..1bc60e70 100644 --- a/plugin/api/insight/widget.go +++ b/plugin/api/insight/widget.go @@ -5,6 +5,7 @@ package insight import ( + log "github.com/cihub/seelog" "infini.sh/console/model/insight" httprouter "infini.sh/framework/core/api/router" "infini.sh/framework/core/orm" @@ -12,6 +13,25 @@ import ( "net/http" ) +func (h *InsightAPI) createWidget(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + var obj = &insight.Widget{} + err := h.DecodeJSON(req, obj) + if err != nil { + h.WriteError(w, err.Error(), http.StatusInternalServerError) + log.Error(err) + return + } + err = orm.Create(nil, obj) + if err != nil { + h.WriteError(w, err.Error(), http.StatusInternalServerError) + log.Error(err) + return + } + + h.WriteCreatedOKJSON(w, obj.ID) + +} + func (h *InsightAPI) getWidget(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { id := ps.MustGetParameter("widget_id") @@ -27,9 +47,5 @@ func (h *InsightAPI) getWidget(w http.ResponseWriter, req *http.Request, ps http return } - h.WriteJSON(w, util.MapStr{ - "found": true, - "_id": id, - "_source": obj, - }, 200) + h.WriteGetOKJSON(w, id, obj) }