From 9425f506bf3cc6443960c6f57654fa4739b5b0b7 Mon Sep 17 00:00:00 2001 From: silenceqi Date: Sat, 25 Sep 2021 18:53:15 +0800 Subject: [PATCH] aleting bug fixed --- api/init.go | 1 + main.go | 2 +- model/alerting/alert.go | 2 ++ model/alerting/destination.go | 2 +- model/alerting/monitor.go | 4 ++-- service/alerting/alert.go | 4 ++-- service/alerting/overview.go | 24 +++++++++++++------ .../use_send_current_request_to_es/index.ts | 2 +- .../CreateTrigger/utils/constants.js | 13 +++++----- .../pages/Destinations/utils/constants.js | 2 +- 10 files changed, 35 insertions(+), 21 deletions(-) diff --git a/api/init.go b/api/init.go index 367cca55..52a56356 100644 --- a/api/init.go +++ b/api/init.go @@ -38,6 +38,7 @@ func Init(cfg *config.AppConfig) { ui.HandleUIMethod(api.POST, path.Join(pathPrefix, "index/:index"), handler.HandleCreateIndexAction) //new api + ui.HandleUIMethod(api.GET, "/elasticsearch/:id/alerting/overview", alerting.GetAlertOverview) ui.HandleUIMethod(api.POST, "/elasticsearch/:id/alerting/destinations/email_accounts", alerting.CreateEmailAccount) ui.HandleUIMethod(api.PUT, "/elasticsearch/:id/alerting/email_accounts/:emailAccountId", alerting.UpdateEmailAccount) ui.HandleUIMethod(api.DELETE, "/elasticsearch/:id/alerting/email_accounts/:emailAccountId", alerting.DeleteEmailAccount) diff --git a/main.go b/main.go index 8a0e59fa..6bf8eeed 100644 --- a/main.go +++ b/main.go @@ -76,7 +76,7 @@ func main() { orm.RegisterSchemaWithIndexName(elastic.IndexPattern{}, "view") orm.RegisterSchemaWithIndexName(alerting.Config{}, "alerting-config") orm.RegisterSchemaWithIndexName(alerting.Alert{}, "alerting-alerts") - orm.RegisterSchemaWithIndexName(alerting.AlertHistory{}, "alerting-alert-history") + orm.RegisterSchemaWithIndexName(alerting.AlertingHistory{}, "alerting-alert-history") alertSrv.GetScheduler().Start() }) diff --git a/model/alerting/alert.go b/model/alerting/alert.go index 1d6ced67..161adfcf 100644 --- a/model/alerting/alert.go +++ b/model/alerting/alert.go @@ -18,6 +18,8 @@ type Alert struct { TriggerName string `json:"trigger_name" elastic_mapping:"trigger_name:{type:text,fields:{keyword:{type:keyword,ignore_above:256}}}"` } +type AlertingHistory Alert + type ActionExecutionResult struct { ActionID string `json:"action_id" elastic_mapping:"action_id:{type:keyword}"` LastExecutionTime int64 `json:"last_execution_time" elastic_mapping:"last_execution_time:{type:date}"` diff --git a/model/alerting/destination.go b/model/alerting/destination.go index 6f97cec4..fba4c133 100644 --- a/model/alerting/destination.go +++ b/model/alerting/destination.go @@ -25,7 +25,7 @@ type Recipient struct { type CustomWebhook struct { HeaderParams map[string]string `json:"header_params" elastic_mapping:"header_params:{type:object,enabled:false}"` Host string `json:"host" elastic_mapping:"host:{type:text}"` - Method string `json:"method" elastic_mapping:"host:{type:keyword}"` + Method string `json:"method" elastic_mapping:"method:{type:keyword}"` Password string `json:"password" elastic_mapping:"password:{type:text}"` Path string `json:"path" elastic_mapping:"path:{type:keyword}"` Port int `json:"port" elastic_mapping:"port:{type:integer}"` diff --git a/model/alerting/monitor.go b/model/alerting/monitor.go index 4ef04147..9d2f2727 100644 --- a/model/alerting/monitor.go +++ b/model/alerting/monitor.go @@ -18,7 +18,7 @@ type MonitorInput struct { type MonitorSearch struct { Indices []string `json:"indices" elastic_mapping:"indices:{type:text,fields: {keyword: {type: keyword, ignore_above: 256}}}"` - Query map[string]interface{} `json:"query" elastic_mapping:"query:{type:object,enabled:false}}"` + Query map[string]interface{} `json:"query" elastic_mapping:"query:{type:object,enabled:false}"` } type Cron struct { @@ -57,7 +57,7 @@ type Trigger struct { ID string `json:"id"` Severity string `json:"severity" elastic_mapping:"severity:{type:keyword}"` Name string `json:"name" elastic_mapping:"name:{type:text,fields: {keyword: {type: keyword, ignore_above: 256}}}"` - Condition map[string]interface{} `json:"condition" elastic_mapping:"condition:{type:object, enable:false}"` + Condition map[string]interface{} `json:"condition" elastic_mapping:"condition:{type:object, enabled:false}"` Actions []Action `json:"actions" elastic_mapping:"actions"` MinTimeBetweenExecutions int `json:"min_time_between_executions" elastic_mapping:"min_time_between_executions:{type:integer}"` } \ No newline at end of file diff --git a/service/alerting/alert.go b/service/alerting/alert.go index 85def0a6..0829555b 100644 --- a/service/alerting/alert.go +++ b/service/alerting/alert.go @@ -28,9 +28,9 @@ func getQueryParam(req *http.Request, key string, or ...string) string { func getAlertIndexName(typ string) string { switch typ{ case INDEX_ALL_ALERTS: - return fmt.Sprintf("%s,%s", orm.GetIndexName(alerting.AlertHistory{}), orm.GetIndexName(alerting.Alert{})) + return fmt.Sprintf("%s,%s", orm.GetIndexName(alerting.AlertingHistory{}), orm.GetIndexName(alerting.Alert{})) case INDEX_ALERT_HISTORY: - return orm.GetIndexName(alerting.AlertHistory{}) + return orm.GetIndexName(alerting.AlertingHistory{}) } return orm.GetIndexName(alerting.Alert{}) } diff --git a/service/alerting/overview.go b/service/alerting/overview.go index 4c8981dd..701ef3d7 100644 --- a/service/alerting/overview.go +++ b/service/alerting/overview.go @@ -6,13 +6,22 @@ import ( "net/http" ) -func GetAlertMetric(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { - +func GetAlertOverview(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { + alertDayMetricData, err := getLastAlertDayCount() + if err != nil { + writeError(w, err) + return + } + writeJSON(w, IfaceMap{ + "metrics": IfaceMap{ + "alert_day": alertDayMetricData, + }, + }, http.StatusOK) } -func getLastAlertDayCount() error{ +func getLastAlertDayCount() (interface{}, error){ conf := getDefaultConfig() - reqUrl := fmt.Sprintf("%s/%s", conf.Endpoint, getAlertIndexName(INDEX_ALL_ALERTS)) + reqUrl := fmt.Sprintf("%s/%s/_search", conf.Endpoint, getAlertIndexName(INDEX_ALL_ALERTS)) reqBody := IfaceMap{ "size": 0, "query": IfaceMap{ @@ -35,15 +44,16 @@ func getLastAlertDayCount() error{ }, }, } + res, err := doRequest(reqUrl, http.MethodGet, nil, reqBody) if err != nil { - return err + return nil, err } result := IfaceMap{} defer res.Body.Close() err = decodeJSON(res.Body, &result) if err != nil { - return err + return nil, err } buckets := queryValue(result, "aggregations.alert_day_count.buckets", []interface{}{}) var metricData []interface{} @@ -57,5 +67,5 @@ func getLastAlertDayCount() error{ } } } - return nil + return metricData, nil } diff --git a/web/src/components/kibana/console/hooks/use_send_current_request_to_es/index.ts b/web/src/components/kibana/console/hooks/use_send_current_request_to_es/index.ts index 3aa7e6c7..0f3b04de 100644 --- a/web/src/components/kibana/console/hooks/use_send_current_request_to_es/index.ts +++ b/web/src/components/kibana/console/hooks/use_send_current_request_to_es/index.ts @@ -118,5 +118,5 @@ export const useSendCurrentRequestToES = () => { }); } } - }, [dispatch, history]); + }, [dispatch, history, clusterID]); }; diff --git a/web/src/pages/Alerting/pages/CreateTrigger/containers/CreateTrigger/utils/constants.js b/web/src/pages/Alerting/pages/CreateTrigger/containers/CreateTrigger/utils/constants.js index faf340a1..4d3a8a9e 100644 --- a/web/src/pages/Alerting/pages/CreateTrigger/containers/CreateTrigger/utils/constants.js +++ b/web/src/pages/Alerting/pages/CreateTrigger/containers/CreateTrigger/utils/constants.js @@ -24,8 +24,9 @@ export const FORMIK_INITIAL_VALUES = { minTimeBetweenExecutions: null, rollingWindowSize: null, script: { - lang: 'painless', - source: `ctx.results[0].hits.total.value > 0`, + lang: 'yaml', + source: `range: + _ctx.results.[0].hits.total.value.gt: 0`, }, thresholdValue: 10000, thresholdEnum: 'ABOVE', @@ -39,9 +40,9 @@ export const FORMIK_INITIAL_VALUES = { actions: undefined, }; -export const HITS_TOTAL_RESULTS_PATH = 'ctx.results[0].hits.total.value'; -export const AGGREGATION_RESULTS_PATH = 'ctx.results[0].aggregations.when.value'; -export const ANOMALY_GRADE_RESULT_PATH = 'ctx.results[0].aggregations.max_anomaly_grade.value'; -export const ANOMALY_CONFIDENCE_RESULT_PATH = 'ctx.results[0].hits.hits[0]._source.confidence'; +export const HITS_TOTAL_RESULTS_PATH = '_ctx.results[0].hits.total.value'; +export const AGGREGATION_RESULTS_PATH = '_ctx.results[0].aggregations.when.value'; +export const ANOMALY_GRADE_RESULT_PATH = '_ctx.results[0].aggregations.max_anomaly_grade.value'; +export const ANOMALY_CONFIDENCE_RESULT_PATH = '_ctx.results[0].hits.hits[0]._source.confidence'; export const NOT_EMPTY_RESULT = 'ctx.results != null && ctx.results.length > 0 && ctx.results[0].aggregations != null && ctx.results[0].aggregations.max_anomaly_grade != null && ctx.results[0].hits.total.value > 0 && ctx.results[0].hits.hits[0]._source != null && ctx.results[0].hits.hits[0]._source.confidence != null'; diff --git a/web/src/pages/Alerting/pages/Destinations/utils/constants.js b/web/src/pages/Alerting/pages/Destinations/utils/constants.js index c15796a9..21710766 100644 --- a/web/src/pages/Alerting/pages/Destinations/utils/constants.js +++ b/web/src/pages/Alerting/pages/Destinations/utils/constants.js @@ -26,7 +26,7 @@ export const DESTINATION_TYPE = { export const DESTINATION_OPTIONS = [ { value: DESTINATION_TYPE.EMAIL, text: formatMessage({ id: 'alert.destination.type.email' }) }, { value: DESTINATION_TYPE.CUSTOM_HOOK, text: formatMessage({ id: 'alert.destination.type.custom_webhook' }) }, - { value: DESTINATION_TYPE.SLACK, text: formatMessage({ id: 'alert.destination.type.slack' }) }, + // { value: DESTINATION_TYPE.SLACK, text: formatMessage({ id: 'alert.destination.type.slack' }) }, // { value: DESTINATION_TYPE.CHIME, text: formatMessage({ id: 'alert.destination.type.chime' }) }, ];