Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: code format #72

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/unit_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,18 @@ jobs:
- name: Checkout current repository
uses: actions/checkout@v4
with:
fetch-depth: 0
path: console

- name: Checkout framework repository
uses: actions/checkout@v4
with:
fetch-depth: 0
repository: infinilabs/framework
path: framework

- name: Checkout framework-vendor
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
repository: infinilabs/framework-vendor
path: vendor

Expand Down
8 changes: 4 additions & 4 deletions common/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func GetMapStringValue(m util.MapStr, key string) string {

func MapLabel(labelName, indexName, keyField, valueField string, client elastic.API, cacheLabels map[string]string) string {
if len(cacheLabels) > 0 {
if v, ok := cacheLabels[labelName]; ok{
if v, ok := cacheLabels[labelName]; ok {
return v
}
}
Expand All @@ -58,7 +58,7 @@ func MapLabel(labelName, indexName, keyField, valueField string, client elastic.
return labelMaps[labelName]
}

func GetLabelMaps( indexName, keyField, valueField string, client elastic.API, keyFieldValues []string, cacheSize int) (map[string]string, error){
func GetLabelMaps(indexName, keyField, valueField string, client elastic.API, keyFieldValues []string, cacheSize int) (map[string]string, error) {
if client == nil {
return nil, fmt.Errorf("cluster client must not be empty")
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func GetLabelMaps( indexName, keyField, valueField string, client elastic.API, k
var key string
if keyField == "_id" {
key = hit.ID
}else{
} else {
key = GetMapStringValue(sourceM, keyField)
}
if key != "" {
Expand All @@ -99,7 +99,7 @@ func GetLabelMaps( indexName, keyField, valueField string, client elastic.API, k
return labelMaps, nil
}

func ExecuteTemplate( tpl *template.Template, ctx map[string]interface{}) ([]byte, error){
func ExecuteTemplate(tpl *template.Template, ctx map[string]interface{}) ([]byte, error) {
msgBuffer := &bytes.Buffer{}
err := tpl.Execute(msgBuffer, ctx)
return msgBuffer.Bytes(), err
Expand Down
30 changes: 15 additions & 15 deletions core/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,47 +29,47 @@ import (
)

const (
ErrTypeRequestParams = "request_params_error"
ErrTypeApplication = "application_error"
ErrTypeAlreadyExists = "already_exists_error"
ErrTypeNotExists = "not_exists_error"
ErrTypeIncorrectPassword = "incorrect_password_error"
ErrTypeRequestParams = "request_params_error"
ErrTypeApplication = "application_error"
ErrTypeAlreadyExists = "already_exists_error"
ErrTypeNotExists = "not_exists_error"
ErrTypeIncorrectPassword = "incorrect_password_error"
ErrTypeDomainPrefixMismatch = "domain_prefix_mismatch_error"
ErrTypeDisabled = "disabled_error"
ErrTypeRequestTimeout = "request_timeout_error"
ErrTypeDisabled = "disabled_error"
ErrTypeRequestTimeout = "request_timeout_error"
)

var (
ErrPasswordIncorrect = errors.New("incorrect password")
ErrNotExistsErr = errors.New("not exists")
ErrNotExistsErr = errors.New("not exists")
)

type Error struct {
typ string
msg interface{}
typ string
msg interface{}
field string
}

func (err Error) Error() string {
return fmt.Sprintf("%s:%v: %v", err.typ, err.field, err.msg)
}

//NewAppError returns an application error
// NewAppError returns an application error
func NewAppError(msg any) *Error {
return New(ErrTypeApplication, "", msg)
}

//NewParamsError returns a request params error
// NewParamsError returns a request params error
func NewParamsError(field string, msg any) *Error {
return New(ErrTypeRequestParams, field, msg)
}

//NewAlreadyExistsError returns an already exists error
// NewAlreadyExistsError returns an already exists error
func NewAlreadyExistsError(field string, msg any) *Error {
return New(ErrTypeAlreadyExists, field, msg)
}

//NewNotExistsError returns a not exists error
// NewNotExistsError returns a not exists error
func NewNotExistsError(field string, msg any) *Error {
return New(ErrTypeNotExists, field, msg)
}
Expand All @@ -80,4 +80,4 @@ func New(typ string, field string, msg any) *Error {
msg,
field,
}
}
}
12 changes: 6 additions & 6 deletions core/security/enum/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ const (
PermissionMigrationTaskWrite = "task:write"
PermissionComparisonTaskRead = "comparison_task:read"
PermissionComparisonTaskWrite = "comparison_task:write"
PermissionSmtpServerRead = "smtp_server:read"
PermissionSmtpServerWrite = "smtp_server:write"
PermissionSmtpServerRead = "smtp_server:read"
PermissionSmtpServerWrite = "smtp_server:write"
)

var (
Expand Down Expand Up @@ -221,8 +221,8 @@ var (
DashboardAllPermission = []string{PermissionLayoutRead, PermissionLayoutWrite}
WorkbenchReadPermission = []string{PermissionElasticsearchClusterRead, PermissionActivityRead, PermissionAlertMessageRead, PermissionElasticsearchMetricRead}
WorkbenchAllPermission = WorkbenchReadPermission
SmtpServerReadPermission = []string{PermissionSmtpServerRead}
SmtpServerAllPermission = []string{PermissionSmtpServerRead, PermissionSmtpServerWrite}
SmtpServerReadPermission = []string{PermissionSmtpServerRead}
SmtpServerAllPermission = []string{PermissionSmtpServerRead, PermissionSmtpServerWrite}
)

var AdminPrivilege = []string{
Expand Down Expand Up @@ -304,8 +304,8 @@ func init() {

SubscriptionRead: SubscriptionReadPermission,
SubscriptionAll: SubscriptionAllPermission,
SmtpServerRead: SmtpServerReadPermission,
SmtpServerAll: SmtpServerAllPermission,
SmtpServerRead: SmtpServerReadPermission,
SmtpServerAll: SmtpServerAllPermission,
}

}
98 changes: 49 additions & 49 deletions model/alerting/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,72 +32,72 @@ import (
)

type Alert struct {
ID string `json:"id,omitempty" elastic_meta:"_id" elastic_mapping:"id: { type: keyword }"`
Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"`
Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"`
RuleID string `json:"rule_id" elastic_mapping:"rule_id: { type: keyword }"`
RuleName string `json:"rule_name" elastic_mapping:"rule_name: { type: keyword }"`
ResourceID string `json:"resource_id" elastic_mapping:"resource_id: { type: keyword }"`
ResourceName string `json:"resource_name" elastic_mapping:"resource_name: { type: keyword }"`
Expression string `json:"expression" elastic_mapping:"expression: { type: keyword, copy_to:search_text }"`
Objects []string `json:"objects" elastic_mapping:"objects: { type:keyword,copy_to:search_text }"`
Priority string `json:"priority" elastic_mapping:"priority: { type: keyword }"`
Title string `json:"title" elastic_mapping:"title: { type: keyword }"`
Message string `json:"message" elastic_mapping:"context: { type: keyword, copy_to:search_text }"`
AcknowledgedTime interface{} `json:"acknowledged_time,omitempty"`
ActionExecutionResults []ActionExecutionResult `json:"action_execution_results,omitempty"`
RecoverActionResults []ActionExecutionResult `json:"recover_action_results,omitempty"`
ID string `json:"id,omitempty" elastic_meta:"_id" elastic_mapping:"id: { type: keyword }"`
Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"`
Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"`
RuleID string `json:"rule_id" elastic_mapping:"rule_id: { type: keyword }"`
RuleName string `json:"rule_name" elastic_mapping:"rule_name: { type: keyword }"`
ResourceID string `json:"resource_id" elastic_mapping:"resource_id: { type: keyword }"`
ResourceName string `json:"resource_name" elastic_mapping:"resource_name: { type: keyword }"`
Expression string `json:"expression" elastic_mapping:"expression: { type: keyword, copy_to:search_text }"`
Objects []string `json:"objects" elastic_mapping:"objects: { type:keyword,copy_to:search_text }"`
Priority string `json:"priority" elastic_mapping:"priority: { type: keyword }"`
Title string `json:"title" elastic_mapping:"title: { type: keyword }"`
Message string `json:"message" elastic_mapping:"context: { type: keyword, copy_to:search_text }"`
AcknowledgedTime interface{} `json:"acknowledged_time,omitempty"`
ActionExecutionResults []ActionExecutionResult `json:"action_execution_results,omitempty"`
RecoverActionResults []ActionExecutionResult `json:"recover_action_results,omitempty"`
EscalationActionResults []ActionExecutionResult `json:"escalation_action_results,omitempty"`
Users []string `json:"users,omitempty"`
State string `json:"state"`
Error string `json:"error,omitempty"`
IsNotified bool `json:"is_notified" elastic_mapping:"is_notified: { type: boolean }"` //标识本次检测是否发送了告警通知
IsEscalated bool `json:"is_escalated" elastic_mapping:"is_escalated: { type: boolean }"` //标识本次检测是否发送了升级告警通知
Conditions Condition `json:"condition"`
ConditionResult *ConditionResult `json:"condition_result,omitempty" elastic_mapping:"condition_result: { type: object,enabled:false }"`
SearchText string `json:"-" elastic_mapping:"search_text:{type:text,index_prefixes:{},index_phrases:true, analyzer:suggest_text_search }"`
Users []string `json:"users,omitempty"`
State string `json:"state"`
Error string `json:"error,omitempty"`
IsNotified bool `json:"is_notified" elastic_mapping:"is_notified: { type: boolean }"` //标识本次检测是否发送了告警通知
IsEscalated bool `json:"is_escalated" elastic_mapping:"is_escalated: { type: boolean }"` //标识本次检测是否发送了升级告警通知
Conditions Condition `json:"condition"`
ConditionResult *ConditionResult `json:"condition_result,omitempty" elastic_mapping:"condition_result: { type: object,enabled:false }"`
SearchText string `json:"-" elastic_mapping:"search_text:{type:text,index_prefixes:{},index_phrases:true, analyzer:suggest_text_search }"`
}

type ActionExecutionResult struct {
ExecutionTime int `json:"execution_time"`
Error string `json:"error"`
Result string `json:"result"`
Message string `json:"message"`
ChannelName string `json:"channel_name"`
ChannelType string `json:"channel_type"`
ChannelID string `json:"channel_id"`
Result string `json:"result"`
Message string `json:"message"`
ChannelName string `json:"channel_name"`
ChannelType string `json:"channel_type"`
ChannelID string `json:"channel_id"`
}

const (
AlertStateAlerting string = "alerting"
AlertStateOK = "ok"
AlertStateError = "error"
AlertStateNodata = "nodata"
AlertStateError = "error"
AlertStateNodata = "nodata"
)

const (
MessageStateAlerting = "alerting"
MessageStateIgnored = "ignored"
MessageStateAlerting = "alerting"
MessageStateIgnored = "ignored"
MessageStateRecovered = "recovered"
)

type AlertMessage struct {
ID string `json:"id,omitempty" elastic_meta:"_id" elastic_mapping:"id: { type: keyword }"`
Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"`
Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"`
RuleID string `json:"rule_id" elastic_mapping:"rule_id: { type: keyword,copy_to:search_text }"`
ResourceID string `json:"resource_id" elastic_mapping:"resource_id: { type: keyword,copy_to:search_text }"`
ResourceName string `json:"resource_name" elastic_mapping:"resource_name: { type: keyword,copy_to:search_text }"`
Title string `json:"title" elastic_mapping:"title: { type: keyword,copy_to:search_text }"`
Message string `json:"message" elastic_mapping:"content: { type: keyword,copy_to:search_text }"`
Status string `json:"status" elastic_mapping:"status: { type: keyword,copy_to:search_text }"`
IgnoredTime time.Time `json:"ignored_time,omitempty" elastic_mapping:"ignored_time: { type: date }"`
IgnoredReason string `json:"ignored_reason,omitempty" elastic_mapping:"ignored_reason: { type: keyword,copy_to:search_text }"`
IgnoredUser string `json:"ignored_user,omitempty" elastic_mapping:"ignored_user: { type: keyword,copy_to:search_text }"`
Priority string `json:"priority" elastic_mapping:"priority: { type: keyword }"`
SearchText string `json:"-" elastic_mapping:"search_text:{type:text,index_prefixes:{},index_phrases:true, analyzer:suggest_text_search }"`
Category string `json:"category,omitempty" elastic_mapping:"category: { type: keyword,copy_to:search_text }"`
Tags []string `json:"tags,omitempty" elastic_mapping:"tags: { type: keyword,copy_to:search_text }"`
ID string `json:"id,omitempty" elastic_meta:"_id" elastic_mapping:"id: { type: keyword }"`
Created time.Time `json:"created,omitempty" elastic_mapping:"created: { type: date }"`
Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated: { type: date }"`
RuleID string `json:"rule_id" elastic_mapping:"rule_id: { type: keyword,copy_to:search_text }"`
ResourceID string `json:"resource_id" elastic_mapping:"resource_id: { type: keyword,copy_to:search_text }"`
ResourceName string `json:"resource_name" elastic_mapping:"resource_name: { type: keyword,copy_to:search_text }"`
Title string `json:"title" elastic_mapping:"title: { type: keyword,copy_to:search_text }"`
Message string `json:"message" elastic_mapping:"content: { type: keyword,copy_to:search_text }"`
Status string `json:"status" elastic_mapping:"status: { type: keyword,copy_to:search_text }"`
IgnoredTime time.Time `json:"ignored_time,omitempty" elastic_mapping:"ignored_time: { type: date }"`
IgnoredReason string `json:"ignored_reason,omitempty" elastic_mapping:"ignored_reason: { type: keyword,copy_to:search_text }"`
IgnoredUser string `json:"ignored_user,omitempty" elastic_mapping:"ignored_user: { type: keyword,copy_to:search_text }"`
Priority string `json:"priority" elastic_mapping:"priority: { type: keyword }"`
SearchText string `json:"-" elastic_mapping:"search_text:{type:text,index_prefixes:{},index_phrases:true, analyzer:suggest_text_search }"`
Category string `json:"category,omitempty" elastic_mapping:"category: { type: keyword,copy_to:search_text }"`
Tags []string `json:"tags,omitempty" elastic_mapping:"tags: { type: keyword,copy_to:search_text }"`
}

/*
Expand All @@ -109,4 +109,4 @@ type AlertMessage struct {
*/

//message status (Active, Ignore, Recover)
//rule status (Active, Error, OK)
//rule status (Active, Error, OK)
39 changes: 20 additions & 19 deletions model/alerting/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ package alerting
import "fmt"

type Condition struct {
Operator string `json:"operator"`
Items []ConditionItem `json:"items"`
Operator string `json:"operator"`
Items []ConditionItem `json:"items"`
}
func (cond *Condition) GetMinimumPeriodMatch() int{

func (cond *Condition) GetMinimumPeriodMatch() int {
var minPeriodMatch = 0
for _, citem := range cond.Items {
if citem.MinimumPeriodMatch > minPeriodMatch {
Expand All @@ -45,14 +46,14 @@ func (cond *Condition) GetMinimumPeriodMatch() int{

type ConditionItem struct {
//MetricName string `json:"metric"`
MinimumPeriodMatch int `json:"minimum_period_match"`
Operator string `json:"operator"`
Values []string `json:"values"`
Priority string `json:"priority"`
Expression string `json:"expression,omitempty"`
MinimumPeriodMatch int `json:"minimum_period_match"`
Operator string `json:"operator"`
Values []string `json:"values"`
Priority string `json:"priority"`
Expression string `json:"expression,omitempty"`
}

func (cond *ConditionItem) GenerateConditionExpression()(conditionExpression string, err error){
func (cond *ConditionItem) GenerateConditionExpression() (conditionExpression string, err error) {
valueLength := len(cond.Values)
if valueLength == 0 {
return conditionExpression, fmt.Errorf("condition values: %v should not be empty", cond.Values)
Expand Down Expand Up @@ -81,20 +82,20 @@ func (cond *ConditionItem) GenerateConditionExpression()(conditionExpression str

type ConditionResult struct {
ResultItems []ConditionResultItem `json:"result_items"`
QueryResult *QueryResult `json:"query_result"`
QueryResult *QueryResult `json:"query_result"`
}
type ConditionResultItem struct {
GroupValues []string `json:"group_values"`
ConditionItem *ConditionItem `json:"condition_item"`
IssueTimestamp interface{} `json:"issue_timestamp"`
ResultValue interface{} `json:"result_value"` //满足条件最后一个值
GroupValues []string `json:"group_values"`
ConditionItem *ConditionItem `json:"condition_item"`
IssueTimestamp interface{} `json:"issue_timestamp"`
ResultValue interface{} `json:"result_value"` //满足条件最后一个值
RelationValues map[string]interface{} `json:"relation_values"`
}

var PriorityWeights = map[string]int{
"info": 1,
"low": 2,
"medium": 3,
"high": 4,
"info": 1,
"low": 2,
"medium": 3,
"high": 4,
"critical": 5,
}
}
19 changes: 9 additions & 10 deletions model/alerting/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ import (

type Channel struct {
orm.ORMObjectBase
Name string `json:"name" elastic_mapping:"name:{type:keyword,copy_to:search_text}"`
Type string `json:"type" elastic_mapping:"type:{type:keyword,copy_to:search_text}"` // email or webhook
Priority int `json:"priority,omitempty"`
Webhook *CustomWebhook `json:"webhook,omitempty" elastic_mapping:"webhook:{type:object}"`
SearchText string `json:"-" elastic_mapping:"search_text:{type:text,index_prefixes:{},index_phrases:true, analyzer:suggest_text_search }"`
SubType string `json:"sub_type" elastic_mapping:"sub_type:{type:keyword,copy_to:search_text}"`
Email *Email `json:"email,omitempty" elastic_mapping:"email:{type:object}"`
Enabled bool `json:"enabled" elastic_mapping:"enabled:{type:boolean}"`
Name string `json:"name" elastic_mapping:"name:{type:keyword,copy_to:search_text}"`
Type string `json:"type" elastic_mapping:"type:{type:keyword,copy_to:search_text}"` // email or webhook
Priority int `json:"priority,omitempty"`
Webhook *CustomWebhook `json:"webhook,omitempty" elastic_mapping:"webhook:{type:object}"`
SearchText string `json:"-" elastic_mapping:"search_text:{type:text,index_prefixes:{},index_phrases:true, analyzer:suggest_text_search }"`
SubType string `json:"sub_type" elastic_mapping:"sub_type:{type:keyword,copy_to:search_text}"`
Email *Email `json:"email,omitempty" elastic_mapping:"email:{type:object}"`
Enabled bool `json:"enabled" elastic_mapping:"enabled:{type:boolean}"`
}


const (
ChannelEmail = "email"
ChannelWebhook = "webhook"
)
)
4 changes: 2 additions & 2 deletions model/alerting/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ package alerting

type Filter struct {
And []FilterQuery `json:"and,omitempty"`
Or []FilterQuery `json:"or,omitempty"`
Or []FilterQuery `json:"or,omitempty"`
Not []FilterQuery `json:"not,omitempty"`
//MinimumShouldMatch int `json:"minimum_should_match"`
}

func (f Filter) IsEmpty() bool {
return len(f.And) == 0 && len(f.Or) == 0 && len(f.Not) == 0
}
}
Loading