Skip to content

Commit

Permalink
support markdown api and downtime select (ccfos#1645)
Browse files Browse the repository at this point in the history
  • Loading branch information
710leo authored Jul 25, 2023
1 parent d0c842f commit dd49afe
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
1 change: 1 addition & 0 deletions center/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions center/router/router_builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 9 additions & 8 deletions center/router/router_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions models/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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++ {
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions prom/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ func (pc *PromClientMap) Del(datasourceId int64) {
pc.Lock()
defer pc.Unlock()
delete(pc.ReaderClients, datasourceId)
delete(pc.WriterClients, datasourceId)
}

0 comments on commit dd49afe

Please sign in to comment.