Skip to content

Commit

Permalink
Merge pull request #337 from uliian/feat-router-search
Browse files Browse the repository at this point in the history
feat:新增路由搜索功能
  • Loading branch information
feiyu563 committed Nov 15, 2023
2 parents d4afcdd + 05aba25 commit 08d0d7d
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 146 deletions.
15 changes: 10 additions & 5 deletions controllers/WebAlertRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
)

//router
// router
func (c *MainController) AlertRouter() {
if !CheckAccount(c.Ctx) {
c.Redirect("/login", 302)
Expand All @@ -16,14 +16,19 @@ func (c *MainController) AlertRouter() {
c.Data["IsAlertRouter"] = true
c.Data["IsAlertManageMenu"] = true
c.TplName = "alertrouter.html"

GlobalAlertRouter, _ = models.GetAllAlertRouter()
query := models.AlertRouterQuery{}
query.Name = c.GetString("name", "")
query.Webhook = c.GetString("webhook", "")
//刷新告警路由AlertRouter
GlobalAlertRouter, _ = models.GetAllAlertRouter(query)
c.Data["AlertRouter"] = GlobalAlertRouter

c.Data["IsLogin"] = CheckAccount(c.Ctx)
c.Data["SearchName"] = query.Name
c.Data["SearchWebhook"] = query.Webhook
}

//router add
// router add
func (c *MainController) RouterAdd() {
if !CheckAccount(c.Ctx) {
c.Redirect("/login", 302)
Expand Down Expand Up @@ -83,7 +88,7 @@ func (c *MainController) AddRouter() {
c.ServeJSON()
}

//router edit
// router edit
func (c *MainController) RouterEdit() {
if !CheckAccount(c.Ctx) {
c.Redirect("/login", 302)
Expand Down
10 changes: 5 additions & 5 deletions controllers/WebTemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package controllers
import (
"PrometheusAlert/models"
"encoding/json"
"github.com/astaxie/beego"
"github.com/astaxie/beego/logs"
"github.com/astaxie/beego"
"strconv"
"strings"
)

//template page
// template page
func (c *MainController) Template() {
if !CheckAccount(c.Ctx) {
c.Redirect("/login", 302)
Expand All @@ -26,7 +26,7 @@ func (c *MainController) Template() {
c.Data["IsLogin"] = CheckAccount(c.Ctx)
}

//template add
// template add
func (c *MainController) TemplateAdd() {
if !CheckAccount(c.Ctx) {
c.Redirect("/login", 302)
Expand Down Expand Up @@ -65,7 +65,7 @@ func (c *MainController) AddTpl() {
resp = err
GlobalPrometheusAlertTpl, _ = models.GetAllTpl()
}
GlobalAlertRouter, _ = models.GetAllAlertRouter()
GlobalAlertRouter, _ = models.GetAllAlertRouter(models.AlertRouterQuery{})
c.Data["json"] = resp
c.ServeJSON()
}
Expand All @@ -88,7 +88,7 @@ func (c *MainController) ImportTpl() {
}

GlobalPrometheusAlertTpl, _ = models.GetAllTpl()
GlobalAlertRouter, _ = models.GetAllAlertRouter()
GlobalAlertRouter, _ = models.GetAllAlertRouter(models.AlertRouterQuery{})
c.Data["json"] = resp
c.ServeJSON()
} else {
Expand Down
5 changes: 4 additions & 1 deletion controllers/prometheusalert.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,11 @@ func (c *PrometheusAlertController) PrometheusAlert() {
if pMsg.Split != "false" && PrometheusAlertTpl.Tpluse == "Prometheus" {
//判断告警路由AlertRouter列表是否为空
if GlobalAlertRouter == nil {
query := models.AlertRouterQuery{}
query.Name = c.GetString("name", "")
query.Webhook = c.GetString("webhook", "")
//刷新告警路由AlertRouter
GlobalAlertRouter, _ = models.GetAllAlertRouter()
GlobalAlertRouter, _ = models.GetAllAlertRouter(query)
}
Alerts_Value, _ := p_alertmanager_json["alerts"].([]interface{})
//拆分告警消息
Expand Down
13 changes: 12 additions & 1 deletion models/AlertRouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type AlertRouter struct {
Created time.Time
}

type AlertRouterQuery struct {
Name string
Webhook string
}

func AddAlertRouter(id int, tplid int, name, rules, url_or_phone, at_some_one string, sendResolved bool) error {
tpl, _ := GetTpl(tplid)
o := orm.NewOrm()
Expand Down Expand Up @@ -61,10 +66,16 @@ func DelAlertRouter(id int) error {
return err
}

func GetAllAlertRouter() ([]*AlertRouter, error) {
func GetAllAlertRouter(query AlertRouterQuery) ([]*AlertRouter, error) {
o := orm.NewOrm()
Tpl_all := make([]*AlertRouter, 0)
qs := o.QueryTable("AlertRouter")
if len(query.Name) > 0 {
qs = qs.Filter("name__icontains", query.Name)
}
if len(query.Webhook) > 0 {
qs = qs.Filter("url_or_phone__istartswith", query.Webhook)
}
_, err := qs.RelatedSel().All(&Tpl_all)
return Tpl_all, err
}
Expand Down
Loading

0 comments on commit 08d0d7d

Please sign in to comment.