diff --git a/src/webapi/router/router.go b/src/webapi/router/router.go index 25948d8d..d9d18da5 100644 --- a/src/webapi/router/router.go +++ b/src/webapi/router/router.go @@ -160,6 +160,8 @@ func configRoute(r *gin.Engine, version string) { pages.PUT("/targets/note", jwtAuth(), user(), perm("/targets/put"), targetUpdateNote) pages.PUT("/targets/bgid", jwtAuth(), user(), perm("/targets/put"), targetUpdateBgid) + pages.GET("/dashboards/builtin/list", dashboardBuiltinList) + pages.POST("/busi-group/:id/dashboards/builtin", jwtAuth(), user(), perm("/dashboards/add"), bgrw(), dashboardBuiltinImport) pages.GET("/busi-group/:id/dashboards", jwtAuth(), user(), perm("/dashboards"), bgro(), dashboardGets) pages.POST("/busi-group/:id/dashboards", jwtAuth(), user(), perm("/dashboards/add"), bgrw(), dashboardAdd) pages.POST("/busi-group/:id/dashboards/export", jwtAuth(), user(), perm("/dashboards"), bgro(), dashboardExport) @@ -183,7 +185,7 @@ func configRoute(r *gin.Engine, version string) { pages.POST("/share-charts", jwtAuth(), chartShareAdd) pages.GET("/alert-rules/builtin/list", alertRuleBuiltinList) - pages.POST("/busi-group/:id/alert-rules/builtin", alertRuleBuiltinImport) + pages.POST("/busi-group/:id/alert-rules/builtin", jwtAuth(), user(), perm("/alert-rules/add"), bgrw(), alertRuleBuiltinImport) pages.GET("/busi-group/:id/alert-rules", jwtAuth(), user(), perm("/alert-rules"), alertRuleGets) pages.POST("/busi-group/:id/alert-rules", jwtAuth(), user(), perm("/alert-rules/add"), bgrw(), alertRuleAdd) pages.DELETE("/busi-group/:id/alert-rules", jwtAuth(), user(), perm("/alert-rules/del"), bgrw(), alertRuleDel) diff --git a/src/webapi/router/router_builtin.go b/src/webapi/router/router_builtin.go index c03d6ddc..68b184e7 100644 --- a/src/webapi/router/router_builtin.go +++ b/src/webapi/router/router_builtin.go @@ -84,3 +84,102 @@ func alertRuleBuiltinImport(c *gin.Context) { ginx.NewRender(c).Data(reterr, nil) } + +func dashboardBuiltinList(c *gin.Context) { + fp := config.C.BuiltinAlertsDir + if fp == "" { + fp = path.Join(runner.Cwd, "etc", "dashboards") + } + + files, err := file.DirsUnder(fp) + ginx.Dangerous(err) + + names := make([]string, 0, len(files)) + + for _, f := range files { + if !strings.HasSuffix(f, ".json") { + continue + } + + name := strings.TrimRight(f, ".json") + names = append(names, name) + } + + ginx.NewRender(c).Data(names, nil) +} + +type dashboardBuiltinImportForm struct { + Name string `json:"name" binding:"required"` +} + +func dashboardBuiltinImport(c *gin.Context) { + var f dashboardBuiltinImportForm + ginx.BindJSON(c, &f) + + dirpath := config.C.BuiltinAlertsDir + if dirpath == "" { + dirpath = path.Join(runner.Cwd, "etc", "dashboards") + } + + jsonfile := path.Join(dirpath, f.Name+".json") + if !file.IsExist(jsonfile) { + ginx.Bomb(http.StatusBadRequest, "%s not found", jsonfile) + } + + var dashPures []DashboardPure + ginx.Dangerous(file.ReadJson(jsonfile, &dashPures)) + + me := c.MustGet("user").(*models.User) + bg := c.MustGet("busi_group").(*models.BusiGroup) + + ret := make(map[string]string) + + for _, dashPure := range dashPures { + dash := &models.Dashboard{ + Name: dashPure.Name, + Tags: dashPure.Tags, + Configs: dashPure.Configs, + GroupId: bg.Id, + CreateBy: me.Username, + UpdateBy: me.Username, + } + + ret[dash.Name] = "" + + err := dash.Add() + if err != nil { + ret[dash.Name] = i18n.Sprintf(c.GetHeader("X-Language"), err.Error()) + continue + } + + for _, cgPure := range dashPure.ChartGroups { + cg := &models.ChartGroup{ + Name: cgPure.Name, + Weight: cgPure.Weight, + DashboardId: dash.Id, + } + + err := cg.Add() + if err != nil { + ret[dash.Name] = err.Error() + continue + } + + for _, chartPure := range cgPure.Charts { + chart := &models.Chart{ + Configs: chartPure.Configs, + Weight: chartPure.Weight, + GroupId: cg.Id, + } + + err := chart.Add() + if err != nil { + ret[dash.Name] = err.Error() + continue + } + } + } + } + + ginx.NewRender(c).Data(ret, nil) +}