From dd49afef011feb2a559fc522e591e69935139e22 Mon Sep 17 00:00:00 2001 From: Yening Qin <710leo@gmail.com> Date: Tue, 25 Jul 2023 17:06:25 +0800 Subject: [PATCH] support markdown api and downtime select (#1645) --- center/router/router.go | 1 + center/router/router_builtin.go | 23 +++++++++++++++++++++++ center/router/router_target.go | 17 +++++++++-------- models/target.go | 14 +++++++++----- prom/client.go | 1 + 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/center/router/router.go b/center/router/router.go index a6dc90e2e..c0d220a33 100644 --- a/center/router/router.go +++ b/center/router/router.go @@ -243,6 +243,7 @@ func (rt *Router) Config(r *gin.Engine) { pages.GET("/builtin-boards-cates", rt.auth(), rt.user(), rt.builtinBoardCateGets) pages.POST("/builtin-boards-detail", rt.auth(), rt.user(), rt.builtinBoardDetailGets) pages.GET("/integrations/icon/:cate/:name", rt.builtinIcon) + pages.GET("/integrations/makedown/:cate", rt.builtinMarkdown) pages.GET("/busi-group/:id/boards", rt.auth(), rt.user(), rt.perm("/dashboards"), rt.bgro(), rt.boardGets) pages.POST("/busi-group/:id/boards", rt.auth(), rt.user(), rt.perm("/dashboards/add"), rt.bgrw(), rt.boardAdd) diff --git a/center/router/router_builtin.go b/center/router/router_builtin.go index 8ab40c483..05a9a7937 100644 --- a/center/router/router_builtin.go +++ b/center/router/router_builtin.go @@ -315,3 +315,26 @@ func (rt *Router) builtinIcon(c *gin.Context) { iconPath := fp + "/" + cate + "/icon/" + ginx.UrlParamStr(c, "name") c.File(path.Join(iconPath)) } + +func (rt *Router) builtinMarkdown(c *gin.Context) { + fp := rt.Center.BuiltinIntegrationsDir + if fp == "" { + fp = path.Join(runner.Cwd, "integrations") + } + cate := ginx.UrlParamStr(c, "cate") + + var markdown []byte + markdownDir := fp + "/" + cate + "/markdown" + markdownFiles, err := file.FilesUnder(markdownDir) + if err != nil { + logger.Warningf("get markdown fail: %v", err) + } else if len(markdownFiles) > 0 { + f := markdownFiles[0] + fn := markdownDir + "/" + f + markdown, err = file.ReadBytes(fn) + if err != nil { + logger.Warningf("get collect fail: %v", err) + } + } + ginx.NewRender(c).Data(string(markdown), nil) +} diff --git a/center/router/router_target.go b/center/router/router_target.go index 64343509a..2cdd9cd9a 100644 --- a/center/router/router_target.go +++ b/center/router/router_target.go @@ -45,6 +45,7 @@ func (rt *Router) targetGets(c *gin.Context) { bgid := ginx.QueryInt64(c, "bgid", -1) query := ginx.QueryStr(c, "query", "") limit := ginx.QueryInt(c, "limit", 30) + downtime := ginx.QueryInt64(c, "downtime", 0) dsIds := queryDatasourceIds(c) var bgids []int64 @@ -64,10 +65,10 @@ func (rt *Router) targetGets(c *gin.Context) { bgids = append(bgids, bgid) } - total, err := models.TargetTotal(rt.Ctx, bgids, dsIds, query) + total, err := models.TargetTotal(rt.Ctx, bgids, dsIds, query, downtime) ginx.Dangerous(err) - list, err := models.TargetGets(rt.Ctx, bgids, dsIds, query, limit, ginx.Offset(c, limit)) + list, err := models.TargetGets(rt.Ctx, bgids, dsIds, query, downtime, limit, ginx.Offset(c, limit)) ginx.Dangerous(err) if err == nil { @@ -78,6 +79,12 @@ func (rt *Router) targetGets(c *gin.Context) { for i := 0; i < len(list); i++ { ginx.Dangerous(list[i].FillGroup(rt.Ctx, cache)) keys = append(keys, models.WrapIdent(list[i].Ident)) + + if now.Unix()-list[i].UpdateAt < 60 { + list[i].TargetUp = 2 + } else if now.Unix()-list[i].UpdateAt < 180 { + list[i].TargetUp = 1 + } } if len(keys) > 0 { @@ -103,12 +110,6 @@ func (rt *Router) targetGets(c *gin.Context) { // 未上报过元数据的主机,cpuNum默认为-1, 用于前端展示 unknown list[i].CpuNum = -1 } - - if now.Unix()-list[i].UnixTime/1000 < 60 { - list[i].TargetUp = 2 - } else if now.Unix()-list[i].UnixTime/1000 < 180 { - list[i].TargetUp = 1 - } } } diff --git a/models/target.go b/models/target.go index eddf00f47..8cb1f942b 100644 --- a/models/target.go +++ b/models/target.go @@ -85,7 +85,7 @@ func TargetDel(ctx *ctx.Context, idents []string) error { return DB(ctx).Where("ident in ?", idents).Delete(new(Target)).Error } -func buildTargetWhere(ctx *ctx.Context, bgids []int64, dsIds []int64, query string) *gorm.DB { +func buildTargetWhere(ctx *ctx.Context, bgids []int64, dsIds []int64, query string, downtime int64) *gorm.DB { session := DB(ctx).Model(&Target{}) if len(bgids) > 0 { @@ -96,6 +96,10 @@ func buildTargetWhere(ctx *ctx.Context, bgids []int64, dsIds []int64, query stri session = session.Where("datasource_id in (?)", dsIds) } + if downtime > 0 { + session = session.Where("update_at < ?", time.Now().Unix()-downtime) + } + if query != "" { arr := strings.Fields(query) for i := 0; i < len(arr); i++ { @@ -111,13 +115,13 @@ func TargetTotalCount(ctx *ctx.Context) (int64, error) { return Count(DB(ctx).Model(new(Target))) } -func TargetTotal(ctx *ctx.Context, bgids []int64, dsIds []int64, query string) (int64, error) { - return Count(buildTargetWhere(ctx, bgids, dsIds, query)) +func TargetTotal(ctx *ctx.Context, bgids []int64, dsIds []int64, query string, downtime int64) (int64, error) { + return Count(buildTargetWhere(ctx, bgids, dsIds, query, downtime)) } -func TargetGets(ctx *ctx.Context, bgids []int64, dsIds []int64, query string, limit, offset int) ([]*Target, error) { +func TargetGets(ctx *ctx.Context, bgids []int64, dsIds []int64, query string, downtime int64, limit, offset int) ([]*Target, error) { var lst []*Target - err := buildTargetWhere(ctx, bgids, dsIds, query).Order("ident").Limit(limit).Offset(offset).Find(&lst).Error + err := buildTargetWhere(ctx, bgids, dsIds, query, downtime).Order("ident").Limit(limit).Offset(offset).Find(&lst).Error if err == nil { for i := 0; i < len(lst); i++ { lst[i].TagsJSON = strings.Fields(lst[i].Tags) diff --git a/prom/client.go b/prom/client.go index 53a200e4d..c8e2ab0d2 100644 --- a/prom/client.go +++ b/prom/client.go @@ -99,4 +99,5 @@ func (pc *PromClientMap) Del(datasourceId int64) { pc.Lock() defer pc.Unlock() delete(pc.ReaderClients, datasourceId) + delete(pc.WriterClients, datasourceId) }