Skip to content

Commit

Permalink
Merge pull request #36 from Cairry/master
Browse files Browse the repository at this point in the history
feat search
  • Loading branch information
Cairry authored Jun 20, 2024
2 parents 807ff88 + 080412a commit de6ebed
Show file tree
Hide file tree
Showing 16 changed files with 242 additions and 60 deletions.
9 changes: 9 additions & 0 deletions internal/models/alert_current_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ type AlertCurEventQuery struct {
DatasourceType string `json:"datasourceType" form:"datasourceType"`
DatasourceId string `json:"datasourceId" form:"datasourceId"`
Fingerprint string `json:"fingerprint" form:"fingerprint"`
Query string `json:"query" form:"query"`
Scope int64 `json:"scope" form:"scope"`
Severity string `json:"severity" form:"severity"`
Page
}

type CurEventResponse struct {
List []AlertCurEvent `json:"list"`
Page
}

func (ace *AlertCurEvent) GetFiringAlertCacheKey() string {
Expand Down
10 changes: 4 additions & 6 deletions internal/models/alert_history_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ type AlertHisEventQuery struct {
RuleName string `json:"ruleName" form:"ruleName"`
StartAt int64 `json:"startAt" form:"startAt"`
EndAt int64 `json:"endAt" form:"endAt"`
PageIndex int64 `json:"pageIndex" form:"pageIndex"`
PageSize int64 `json:"pageSize" form:"pageSize"`
Query string `json:"query" form:"query"`
Page
}

type HistoryEventResponse struct {
List []AlertHisEvent
PageIndex int64
PageSize int64
TotalCount int64
List []AlertHisEvent `json:"list"`
Page
}
12 changes: 5 additions & 7 deletions internal/models/auditLog.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ type AuditLogQuery struct {
}

type Page struct {
Total string `json:"total" form:"total" gorm:"-"`
PageIndex string `json:"pageIndex" form:"pageIndex" gorm:"-"`
PageSize string `json:"pageSize" form:"pageSize" gorm:"-"`
Total int64 `json:"total" form:"total" gorm:"-"`
Index int64 `json:"index" form:"index" gorm:"-"`
Size int64 `json:"size" form:"size" gorm:"-"`
}

type AuditLogResponse struct {
List []AuditLog
PageIndex int64
PageSize int64
TotalCount int64
List []AuditLog `json:"list"`
Page
}

func (a AuditLog) String() string {
Expand Down
8 changes: 8 additions & 0 deletions internal/models/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ type AlertRuleQuery struct {
DatasourceIdList []string `json:"datasourceId" form:"datasourceId"`
RuleName string `json:"ruleName" form:"ruleName"`
Enabled string `json:"enabled" form:"enabled"`
Query string `json:"query" form:"query"`
Status string `json:"status" form:"status"` // 查询规则状态
Page
}

type RuleResponse struct {
List []AlertRule `json:"list"`
Page
}

var (
Expand Down
7 changes: 7 additions & 0 deletions internal/models/rule_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ type RuleGroupQuery struct {
ID string `json:"id" form:"id"`
Name string `json:"name" form:"name"`
Description string `json:"description" form:"description"`
Query string `json:"query" form:"query"`
Page
}

type RuleGroupResponse struct {
List []RuleGroups `json:"list"`
Page
}
7 changes: 7 additions & 0 deletions internal/models/silences.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ type AlertSilenceQuery struct {
Datasource string `json:"datasource" form:"datasource"`
DatasourceType string `json:"datasourceType" form:"datasourceType"`
Comment string `json:"comment" form:"comment"`
Query string `json:"query" form:"query"`
Page
}

type SilenceResponse struct {
List []AlertSilences `json:"list"`
Page
}
35 changes: 18 additions & 17 deletions internal/repo/auditLog.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package repo

import (
"fmt"
"gorm.io/gorm"
"strconv"
"time"
Expand Down Expand Up @@ -43,24 +42,26 @@ func (a AuditLogRepo) List(r models.AuditLogQuery) (models.AuditLogResponse, err
var data []models.AuditLog
var count int64

pageIndexInt, _ := strconv.Atoi(r.PageIndex)
pageSizeInt, _ := strconv.Atoi(r.PageSize)
pageIndexInt := r.Page.Index
pageSizeInt := r.Page.Size

db.Where("tenant_id = ?", r.TenantId)

db.Count(&count)

db.Limit(pageSizeInt).Offset((pageIndexInt - 1) * pageSizeInt).Order("created_at desc")
db.Limit(int(pageSizeInt)).Offset(int((pageIndexInt - 1) * pageSizeInt)).Order("created_at desc")
err := db.Find(&data).Error
if err != nil {
return models.AuditLogResponse{}, err
}

d := models.AuditLogResponse{
List: data,
PageIndex: int64(pageIndexInt),
PageSize: int64(pageSizeInt),
TotalCount: count,
List: data,
Page: models.Page{
Index: pageIndexInt,
Size: pageSizeInt,
Total: count,
},
}
return d, nil
}
Expand All @@ -70,10 +71,8 @@ func (a AuditLogRepo) Search(r models.AuditLogQuery) (models.AuditLogResponse, e
var data []models.AuditLog
var count int64

pageIndexInt, _ := strconv.Atoi(r.PageIndex)
pageSizeInt, _ := strconv.Atoi(r.PageSize)

fmt.Println("-->", pageSizeInt)
pageIndexInt := r.Page.Index
pageSizeInt := r.Page.Size

db.Where("tenant_id = ?", r.TenantId)

Expand All @@ -91,17 +90,19 @@ func (a AuditLogRepo) Search(r models.AuditLogQuery) (models.AuditLogResponse, e

db.Count(&count)

db.Limit(pageSizeInt).Offset((pageIndexInt - 1) * pageSizeInt).Order("created_at desc")
db.Limit(int(pageSizeInt)).Offset(int((pageIndexInt - 1) * pageSizeInt)).Order("created_at desc")

err := db.Find(&data).Error
if err != nil {
return models.AuditLogResponse{}, err
}
d := models.AuditLogResponse{
List: data,
PageIndex: int64(pageIndexInt),
PageSize: int64(pageSizeInt),
TotalCount: count,
List: data,
Page: models.Page{
Index: pageIndexInt,
Size: pageSizeInt,
Total: count,
},
}

return d, nil
Expand Down
16 changes: 11 additions & 5 deletions internal/repo/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func (e EventRepo) GetHistoryEvent(r models.AlertHisEventQuery) (models.HistoryE
db := e.DB().Model(&models.AlertHisEvent{})
db.Where("tenant_id = ?", r.TenantId)

if r.Query != "" {
db.Where("rule_name LIKE ? OR severity LIKE ? OR annotations LIKE ?", "%"+r.Query+"%", "%"+r.Query+"%", "%"+r.Query+"%")
}

if r.DatasourceType != "" {
db = db.Where("datasource_type = ?", r.DatasourceType)
}
Expand All @@ -48,15 +52,17 @@ func (e EventRepo) GetHistoryEvent(r models.AlertHisEventQuery) (models.HistoryE
return models.HistoryEventResponse{}, err
}

if err := db.Limit(int(r.PageSize)).Offset(int((r.PageIndex - 1) * r.PageSize)).Order("recover_time desc").Find(&data).Error; err != nil {
if err := db.Limit(int(r.Page.Size)).Offset(int((r.Page.Index - 1) * r.Page.Size)).Order("recover_time desc").Find(&data).Error; err != nil {
return models.HistoryEventResponse{}, err
}

return models.HistoryEventResponse{
List: data,
PageIndex: r.PageIndex,
PageSize: r.PageSize,
TotalCount: count,
List: data,
Page: models.Page{
Index: r.Page.Index,
Size: r.Page.Size,
Total: count,
},
}, nil
}

Expand Down
42 changes: 37 additions & 5 deletions internal/repo/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type (
InterRuleRepo interface {
GetQuota(id string) bool
Search(r models.AlertRuleQuery) (models.AlertRule, error)
List(r models.AlertRuleQuery) ([]models.AlertRule, error)
List(r models.AlertRuleQuery) (models.RuleResponse, error)
Create(r models.AlertRule) error
Update(r models.AlertRule) error
Delete(r models.AlertRuleQuery) error
Expand Down Expand Up @@ -63,18 +63,50 @@ func (rr RuleRepo) Search(r models.AlertRuleQuery) (models.AlertRule, error) {
return data, nil
}

func (rr RuleRepo) List(r models.AlertRuleQuery) ([]models.AlertRule, error) {
var data []models.AlertRule
func (rr RuleRepo) List(r models.AlertRuleQuery) (models.RuleResponse, error) {
var (
data []models.AlertRule
count int64
)

db := rr.db.Model(&models.AlertRule{})
db.Where("tenant_id = ?", r.TenantId)
db.Where("rule_group_id = ?", r.RuleGroupId)

if r.Query != "" {
db.Where("rule_id LIKE ? OR rule_name LIKE ? OR description LIKE ?",
"%"+r.Query+"%", "%"+r.Query+"%", "%"+r.Query+"%")
}

if r.Status != "all" {
var s bool
switch r.Status {
case "enabled":
s = true
case "disabled":
s = false
}
db.Where("enabled = ?", s)
}

db.Count(&count)

db.Limit(int(r.Page.Size)).Offset(int((r.Page.Index - 1) * r.Page.Size))

err := db.Find(&data).Error

if err != nil {
return data, err
return models.RuleResponse{}, err
}

return data, nil
return models.RuleResponse{
List: data,
Page: models.Page{
Total: count,
Index: r.Page.Index,
Size: r.Page.Size,
},
}, nil
}

func (rr RuleRepo) Create(r models.AlertRule) error {
Expand Down
35 changes: 28 additions & 7 deletions internal/repo/rule_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type (
}

InterRuleGroupRepo interface {
List(req models.RuleGroupQuery) ([]models.RuleGroups, error)
List(req models.RuleGroupQuery) (models.RuleGroupResponse, error)
Create(req models.RuleGroups) error
Update(req models.RuleGroups) error
Delete(req models.RuleGroupQuery) error
Expand All @@ -29,24 +29,45 @@ func newRuleGroupInterface(db *gorm.DB, g InterGormDBCli) InterRuleGroupRepo {
}
}

func (r RuleGroupRepo) List(req models.RuleGroupQuery) ([]models.RuleGroups, error) {
func (r RuleGroupRepo) List(req models.RuleGroupQuery) (models.RuleGroupResponse, error) {
var (
data []models.RuleGroups
db = r.db.Model(&models.RuleGroups{})
data []models.RuleGroups
db = r.db.Model(&models.RuleGroups{})
count int64
)

db.Model(&models.RuleGroups{}).Where("tenant_id = ?", req.TenantId)
pageIndexInt := req.Page.Index
pageSizeInt := req.Page.Size

db.Where("tenant_id = ?", req.TenantId)

if req.Query != "" {
db.Where("id LIKE ? OR name LIKE ? OR description LIKE ?",
"%"+req.Query+"%", "%"+req.Query+"%", "%"+req.Query+"%")
}

db.Count(&count)

db.Limit(int(pageSizeInt)).Offset(int((pageIndexInt - 1) * pageSizeInt))

err := db.Find(&data).Error
if err != nil {
return nil, err
return models.RuleGroupResponse{}, err
}

for k, v := range data {
var resRules []models.AlertRule
r.db.Model(&models.AlertRule{}).Where("tenant_id = ? AND rule_group_id = ?", req.TenantId, v.ID).Find(&resRules)
data[k].Number = len(resRules)
}
return data, nil
return models.RuleGroupResponse{
List: data,
Page: models.Page{
Index: pageIndexInt,
Size: pageSizeInt,
Total: count,
},
}, nil
}

func (r RuleGroupRepo) Create(req models.RuleGroups) error {
Expand Down
27 changes: 22 additions & 5 deletions internal/repo/silence.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type (
}

InterSilenceRepo interface {
List(r models.AlertSilenceQuery) ([]models.AlertSilences, error)
List(r models.AlertSilenceQuery) (models.SilenceResponse, error)
Create(r models.AlertSilences) error
Update(r models.AlertSilences) error
Delete(r models.AlertSilenceQuery) error
Expand All @@ -27,16 +27,33 @@ func newSilenceInterface(db *gorm.DB, g InterGormDBCli) InterSilenceRepo {
}
}

func (sr SilenceRepo) List(r models.AlertSilenceQuery) ([]models.AlertSilences, error) {
var silenceList []models.AlertSilences
func (sr SilenceRepo) List(r models.AlertSilenceQuery) (models.SilenceResponse, error) {
var (
silenceList []models.AlertSilences
count int64
)
db := sr.db.Model(models.AlertSilences{})
db.Where("tenant_id = ?", r.TenantId)

if r.Query != "" {
db.Where("id LIKE ? OR comment LIKE ?", "%"+r.Query+"%", "%"+r.Query+"%")
}

db.Count(&count)
db.Limit(int(r.Page.Size)).Offset(int((r.Page.Index - 1) * r.Page.Size))
err := db.Find(&silenceList).Error
if err != nil {
return silenceList, err
return models.SilenceResponse{}, err
}

return silenceList, nil
return models.SilenceResponse{
List: silenceList,
Page: models.Page{
Total: count,
Index: r.Page.Index,
Size: r.Page.Size,
},
}, nil
}

func (sr SilenceRepo) Create(r models.AlertSilences) error {
Expand Down
4 changes: 2 additions & 2 deletions internal/services/auditLog.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (as auditLogService) List(req interface{}) (interface{}, interface{}) {
r := req.(*models.AuditLogQuery)
data, err := as.ctx.DB.AuditLog().List(*r)
if err != nil {
return nil, nil
return nil, err
}

return data, nil
Expand All @@ -34,7 +34,7 @@ func (as auditLogService) Search(req interface{}) (interface{}, interface{}) {
r := req.(*models.AuditLogQuery)
data, err := as.ctx.DB.AuditLog().Search(*r)
if err != nil {
return nil, nil
return nil, err
}

return data, nil
Expand Down
Loading

0 comments on commit de6ebed

Please sign in to comment.