update widget struct

This commit is contained in:
liugq 2023-08-17 11:35:35 +08:00
parent b349c66b14
commit 14f391b58b
3 changed files with 23 additions and 34 deletions

View File

@ -8,34 +8,6 @@ import "infini.sh/framework/core/orm"
type Widget struct { type Widget struct {
orm.ORMObjectBase 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 }"` Title string `json:"title" elastic_mapping:"title: { type: text }"`
} Config interface{}`json:"config" elastic_mapping:"config: { type: object,enabled:false }"`
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"`
} }

View File

@ -29,4 +29,5 @@ func InitAPI() {
api.HandleAPIMethod(api.GET, "/insight/dashboard/_search", insight.searchDashboard) api.HandleAPIMethod(api.GET, "/insight/dashboard/_search", insight.searchDashboard)
api.HandleAPIMethod(api.POST, "/elasticsearch/:id/map_label/_render", insight.renderMapLabelTemplate) api.HandleAPIMethod(api.POST, "/elasticsearch/:id/map_label/_render", insight.renderMapLabelTemplate)
api.HandleAPIMethod(api.GET, "/insight/widget/:widget_id", insight.getWidget) api.HandleAPIMethod(api.GET, "/insight/widget/:widget_id", insight.getWidget)
api.HandleAPIMethod(api.POST, "/insight/widget", insight.RequireLogin(insight.createWidget))
} }

View File

@ -5,6 +5,7 @@
package insight package insight
import ( import (
log "github.com/cihub/seelog"
"infini.sh/console/model/insight" "infini.sh/console/model/insight"
httprouter "infini.sh/framework/core/api/router" httprouter "infini.sh/framework/core/api/router"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
@ -12,6 +13,25 @@ import (
"net/http" "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) { func (h *InsightAPI) getWidget(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("widget_id") id := ps.MustGetParameter("widget_id")
@ -27,9 +47,5 @@ func (h *InsightAPI) getWidget(w http.ResponseWriter, req *http.Request, ps http
return return
} }
h.WriteJSON(w, util.MapStr{ h.WriteGetOKJSON(w, id, obj)
"found": true,
"_id": id,
"_source": obj,
}, 200)
} }