aleting bug fixed

This commit is contained in:
silenceqi 2021-09-25 18:53:15 +08:00
parent c4ad48137d
commit 9425f506bf
10 changed files with 35 additions and 21 deletions

View File

@ -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)

View File

@ -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()
})

View File

@ -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}"`

View File

@ -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}"`

View File

@ -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}"`
}

View File

@ -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{})
}

View File

@ -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
}

View File

@ -118,5 +118,5 @@ export const useSendCurrentRequestToES = () => {
});
}
}
}, [dispatch, history]);
}, [dispatch, history, clusterID]);
};

View File

@ -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';

View File

@ -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' }) },
];