add view layout api

This commit is contained in:
liugq 2023-02-24 19:32:10 +08:00
parent a6b44b63f3
commit 5637c65977
5 changed files with 220 additions and 0 deletions

View File

@ -133,6 +133,7 @@ func main() {
orm.RegisterSchemaWithIndexName(insight.Dashboard{}, "dashboard")
orm.RegisterSchemaWithIndexName(task1.Task{}, "task")
orm.RegisterSchemaWithIndexName(task1.Log{}, "task-log")
orm.RegisterSchemaWithIndexName(model.Layout{}, "layout")
api.RegisterSchema()
if global.Env().SetupRequired() {

19
model/layout.go Normal file
View File

@ -0,0 +1,19 @@
/* Copyright © INFINI Ltd. All rights reserved.
* Web: https://infinilabs.com
* Email: hello#infini.ltd */
package model
import "infini.sh/framework/core/orm"
type Layout struct {
orm.ORMObjectBase
Name string `json:"name" elastic_mapping:"name: { type: text }"`
Description string `json:"description" elastic_mapping:"description: { type: text }"`
Creator struct {
Name string `json:"name"`
Id string `json:"id"`
} `json:"creator"`
ViewID string `json:"view_id" elastic_mapping:"view_id: { type: keyword }"`
Config interface{} `json:"config" elastic_mapping:"config: { type: object, enabled:false }"`
}

View File

@ -6,6 +6,7 @@ import (
"infini.sh/console/plugin/api/gateway"
"infini.sh/console/plugin/api/index_management"
"infini.sh/console/plugin/api/insight"
"infini.sh/console/plugin/api/layout"
"infini.sh/framework/core/api"
"infini.sh/framework/core/api/rbac/enum"
"path"
@ -66,4 +67,5 @@ func Init(cfg *config.AppConfig) {
gateway.InitAPI()
insight.InitAPI()
layout.InitAPI()
}

22
plugin/api/layout/api.go Normal file
View File

@ -0,0 +1,22 @@
/* Copyright © INFINI Ltd. All rights reserved.
* Web: https://infinilabs.com
* Email: hello#infini.ltd */
package layout
import (
"infini.sh/framework/core/api"
)
type LayoutAPI struct {
api.Handler
}
func InitAPI() {
layoutAPI := LayoutAPI{}
api.HandleAPIMethod(api.GET, "/layout/:layout_id", layoutAPI.getLayout)
api.HandleAPIMethod(api.POST, "/layout", layoutAPI.RequireLogin(layoutAPI.createLayout))
api.HandleAPIMethod(api.PUT, "/layout/:layout_id", layoutAPI.updateLayout)
api.HandleAPIMethod(api.DELETE, "/layout/:layout_id", layoutAPI.deleteLayout)
api.HandleAPIMethod(api.GET, "/layout/_search", layoutAPI.searchLayout)
}

176
plugin/api/layout/layout.go Normal file
View File

@ -0,0 +1,176 @@
/* Copyright © INFINI Ltd. All rights reserved.
* Web: https://infinilabs.com
* Email: hello#infini.ltd */
package layout
import (
"fmt"
log "github.com/cihub/seelog"
"infini.sh/console/model"
"infini.sh/framework/core/api/rbac"
httprouter "infini.sh/framework/core/api/router"
"infini.sh/framework/core/orm"
"infini.sh/framework/core/util"
"net/http"
"strconv"
"strings"
)
func (h *LayoutAPI) createLayout(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
var obj = &model.Layout{}
err := h.DecodeJSON(req, obj)
if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError)
log.Error(err)
return
}
obj.ID = util.GetUUID()
user, err := rbac.FromUserContext(req.Context())
if err != nil {
log.Error(err)
h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
if user != nil {
obj.Creator.Name = user.Username
obj.Creator.Id = user.UserId
}
ctx := &orm.Context{
Refresh: "wait_for",
}
err = orm.Create(ctx, obj)
if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError)
log.Error(err)
return
}
h.WriteCreatedOKJSON(w, obj.ID)
}
func (h *LayoutAPI) getLayout(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("layout_id")
obj := model.Layout{}
obj.ID = id
exists, err := orm.Get(&obj)
if !exists || err != nil {
h.WriteJSON(w, util.MapStr{
"_id": id,
"found": false,
}, http.StatusNotFound)
return
}
if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError)
log.Error(err)
return
}
h.WriteGetOKJSON(w, id, obj)
}
func (h *LayoutAPI) updateLayout(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("layout_id")
oldLayout := model.Layout{}
oldLayout.ID = id
exists, err := orm.Get(&oldLayout)
if !exists || err != nil {
h.WriteJSON(w, util.MapStr{
"_id": id,
"result": "not_found",
}, http.StatusNotFound)
return
}
create := oldLayout.Created
obj := model.Layout{}
err = h.DecodeJSON(req, &obj)
if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError)
log.Error(err)
return
}
//protect
obj.ID = id
obj.Created = create
obj.Creator = oldLayout.Creator
ctx := &orm.Context{
Refresh: "wait_for",
}
err = orm.Update(ctx, &obj)
if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError)
log.Error(err)
return
}
h.WriteUpdatedOKJSON(w, obj.ID)
}
func (h *LayoutAPI) deleteLayout(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("layout_id")
obj := model.Layout{}
obj.ID = id
exists, err := orm.Get(&obj)
if !exists || err != nil {
h.WriteJSON(w, util.MapStr{
"_id": id,
"result": "not_found",
}, http.StatusNotFound)
return
}
ctx := &orm.Context{
Refresh: "wait_for",
}
err = orm.Delete(ctx, &obj)
if err != nil {
h.WriteError(w, err.Error(), http.StatusInternalServerError)
log.Error(err)
return
}
h.WriteDeletedOKJSON(w, id)
}
func (h *LayoutAPI) searchLayout(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
var (
keyword = h.GetParameterOrDefault(req, "keyword", "")
queryDSL = `{"query":{"bool":{"must":[%s]}}, "size": %d, "from": %d}`
strSize = h.GetParameterOrDefault(req, "size", "20")
strFrom = h.GetParameterOrDefault(req, "from", "0")
mustBuilder = &strings.Builder{}
)
if keyword != "" {
mustBuilder.WriteString(fmt.Sprintf(`{"query_string":{"default_field":"*","query": "%s"}}`, keyword))
}
size, _ := strconv.Atoi(strSize)
if size <= 0 {
size = 20
}
from, _ := strconv.Atoi(strFrom)
if from < 0 {
from = 0
}
q := orm.Query{}
queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String(), size, from)
q.RawQuery = []byte(queryDSL)
err, res := orm.Search(&model.Layout{}, &q)
if err != nil {
log.Error(err)
h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
h.Write(w, res.Raw)
}