package service import ( "context" "errors" "fmt" "github.com/jinzhu/copier" json "github.com/json-iterator/go" "github.com/olivere/elastic/v7" "github.com/opensearch-project/opensearch-go/opensearchapi" "github.com/spf13/cast" "github.com/thoas/go-funk" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/request" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/vo/response" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/client" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/common/conf" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/util" "go.uber.org/zap" "io" "log" "net/http" "strings" "time" "xorm.io/xorm" ) type AlertSvc struct { User entity.SystemUserInfo } var ( OpenSearchIndex = "so_alert" Mapping = strings.NewReader(`{ "settings": { "number_of_shards": 1, "number_of_replicas": 0, "index.max_result_window": "1000000" }, "mappings": { "properties": { "id": { "type": "integer" }, "alert_point": { "type": "keyword" }, "alert_rules_id": { "type": "keyword" }, "risk_level": { "type": "integer" }, "alert_time": { "type": "date", "ignore_malformed": true, "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "class_id": { "type": "integer" }, "class_parent_name": { "type": "keyword" }, "class_name": { "type": "keyword" }, "metric_config_id": { "type": "keyword" }, "metric_config_name": { "type": "keyword" }, "alert_rule_type": { "type": "keyword" }, "alert_rule_type_name": { "type": "keyword" }, "current_value": { "type": "integer" }, "notification_count": { "type": "integer" }, "push_count": { "type": "integer" }, "last_push_time": { "type": "date", "ignore_malformed": true, "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "status": { "type": "integer" }, "disposed_list": { "type": "nested", "properties": { "is_disposed": { "type": "integer" }, "disposal_content": { "type": "keyword" }, "disposal_user": { "type": "keyword" }, "disposal_time": { "type": "date", "ignore_malformed": true, "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } }, "is_disposed": { "type": "integer" }, "close_remark": { "type": "keyword" }, "close_user": { "type": "keyword" }, "close_time": { "type": "date", "ignore_malformed": true, "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "defer_push": { "type": "integer" }, "created_by": { "type": "keyword" }, "created_at": { "type": "date", "ignore_malformed": true, "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "updated_by": { "type": "keyword" }, "updated_at": { "type": "date", "ignore_malformed": true, "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" }, "alert_condition": { "properties": { "thresholds_min": { "type": "integer" }, "thresholds_max": { "type": "integer" }, "risk_level": { "type": "integer" } } } } } }`) ) func (a *AlertSvc) CreateIndex() error { cli, err := client.GetOpenSearch() if err != nil { return err } res := opensearchapi.IndicesCreateRequest{ Index: OpenSearchIndex, Body: Mapping, } do, err := res.Do(context.Background(), cli) if err != nil { return err } defer do.Body.Close() if do.StatusCode != http.StatusOK { return errors.New(do.String()) } return nil } func (a *AlertSvc) DeleteIndex() error { cli, err := client.GetOpenSearch() if err != nil { return err } res := opensearchapi.IndicesDeleteRequest{ Index: []string{OpenSearchIndex}, } do, err := res.Do(context.Background(), cli) if err != nil { return err } defer do.Body.Close() if do.StatusCode != http.StatusOK { return errors.New(do.String()) } return nil } func (a *AlertSvc) Indices() (indices []string, err error) { var ( catIndicesList []response.CatIndices ) cli, err := client.GetOpenSearch() if err != nil { return } res := opensearchapi.CatIndicesRequest{Format: "json"} do, err := res.Do(context.Background(), cli) if err != nil { return } defer do.Body.Close() if do.StatusCode != http.StatusOK { err = errors.New(do.String()) return } body, err := io.ReadAll(do.Body) if err != nil { return } err = json.Unmarshal(body, &catIndicesList) if err != nil { return } funkGet := funk.Get(catIndicesList, "Index") //取一层 indices, ok := funkGet.([]string) if !ok { err = errors.New("funk.Get failed") return } return } func (a *AlertSvc) IndexSearch(req request.ListAlert) (resp response.AlertList, err error) { var ( sources response.OpenSearchSource ) cli, err := client.GetOpenSearch() if err != nil { return } boolQuery := elastic.NewBoolQuery() if req.Id != 0 { boolQuery.Must(elastic.NewMatchQuery("id", req.Id)) } if req.RiskLevel != 0 { boolQuery.Must(elastic.NewMatchQuery("risk_level", req.RiskLevel)) } if req.Status != 0 { boolQuery.Must(elastic.NewMatchQuery("status", req.Status)) } // 请输入预警点/分类/指标 if req.Keyword != "" { subBoolQuery := elastic.NewBoolQuery() subBoolQuery.Should(elastic.NewMultiMatchQuery(req.Keyword, "alert_point", "class_parent_name", "class_name", "metric_config_name")) //subBoolQuery.Should(elastic.NewMatchQuery("class_name", req.Keyword)) boolQuery.Must(subBoolQuery) } if req.StartTime != "" { boolQuery.Filter(elastic.NewRangeQuery("created_at").Gte(req.StartTime).Lte(req.EndTime)) } querySource, _ := boolQuery.Source() b, _ := json.Marshal(querySource) log.Printf("es statements: %s\n", string(b)) content := strings.NewReader(fmt.Sprintf(`{ "query": %s, "from": %d, "size": %d}`, string(b), req.GetPageSize()*(req.GetPage()-1), req.GetPageSize())) res := opensearchapi.SearchRequest{ Index: []string{OpenSearchIndex}, Body: content, } do, err := res.Do(context.Background(), cli) if err != nil { return } defer do.Body.Close() if do.StatusCode != http.StatusOK { err = errors.New(do.String()) return } body, err := io.ReadAll(do.Body) if err != nil { return } err = json.Unmarshal(body, &sources) if err != nil { return } for _, hit := range sources.Hits.Hits { resp.List = append(resp.List, hit.Source) } return } func (a *AlertSvc) IndexUpdate(req request.UpdateAlert) (err error) { var ( sources response.OpenSearchSource now = jsontime.Now() ) cli, err := client.GetOpenSearch() if err != nil { return } doc := make(map[string]interface{}, 0) if req.CloseRemark != "" { doc["close_remark"] = req.CloseRemark } if req.DeferPush != 0 { doc["defer_push"] = req.DeferPush } if req.RiskLevel != 0 { doc["risk_level"] = req.RiskLevel } if req.Status != 0 { doc["status"] = req.Status } doc["updated_at"] = now if req.DisposalContent != "" { var detailAlert response.AlertItem detailAlert, err = a.GetDataById(request.DetailAlert{Id: req.Id}) if err != nil { return err } disposedList := detailAlert.DisposedList disposedList = append(disposedList, entity.DisposedList{ IsDisposed: req.Status, DisposalContent: req.DisposalContent, DisposalUser: a.User.SystemAccount, DisposalTime: now, }) doc["disposed_list"] = disposedList } if len(doc) == 0 { return errors.New("no updated fields") } b, _ := json.Marshal(doc) docStr := string(b) if docStr == "" { return errors.New("doc marshal failed") } content := strings.NewReader(fmt.Sprintf(`{ "doc": %s }`, docStr)) res := opensearchapi.UpdateRequest{ Index: OpenSearchIndex, DocumentID: cast.ToString(req.Id), Body: content, Source: []string{"true"}, } do, err := res.Do(context.Background(), cli) if err != nil { return } defer do.Body.Close() if do.StatusCode != http.StatusOK { err = errors.New(do.String()) return } body, err := io.ReadAll(do.Body) if err != nil { return } err = json.Unmarshal(body, &sources) if err != nil { return } conf.Logger.Info("--->alert update--->", zap.Any("IndexUpdate", sources)) return } func (a *AlertSvc) Update(req request.UpdateAlert) error { err := a.IndexUpdate(req) return err } func (a *AlertSvc) BatchPushAlert(session *xorm.Session, req request.BatchPushAlert) error { now := jsontime.Now() data := entity.PushRecord{ CreatedBy: a.User.SystemAccount, CreatedAt: now, UpdatedBy: a.User.SystemAccount, UpdatedAt: now, } _ = copier.Copy(&data, &req) data.NotifyMethod = util.ConvertToString(req.NotifyMethod) data.SystemAccount = util.ConvertToString(req.NotifyRecipients) // 循环查询 // TODO 批量推送用户告警 conf.Logger.Info("batch push", zap.Any("payload", req)) time.Sleep(time.Second) return nil } func (a *AlertSvc) BatchCloseAlert(req request.BatchCloseAlert) (err error) { var ids []int if len(req.Ids) > 0 { ids = req.Ids } else { ids = append(ids, req.Id) } for _, id := range ids { err = a.IndexUpdate(request.UpdateAlert{ Id: id, CloseRemark: req.CloseRemark, DeferPush: req.DeferPush, Status: 3, }) if err != nil { return } } conf.Logger.Info("batch close", zap.Any("payload", req)) time.Sleep(time.Second) return } func (a *AlertSvc) GetDataById(req request.DetailAlert) (resp response.AlertItem, err error) { list, err := a.IndexSearch(request.ListAlert{Id: req.Id}) if len(list.List) > 0 { resp = list.List[0] } return } func (a *AlertSvc) List(req request.ListAlert) (resp response.AlertList, err error) { resp, err = a.IndexSearch(req) resp.TotalCount = int64(len(resp.List)) return } func (a *AlertSvc) DisposeAlert(req request.DisposeAlert) (err error) { // TODO 我的预警工单处置 var ( sources response.OpenSearchSource now = jsontime.Now() detailAlert response.AlertItem ) detailAlert, err = a.GetDataById(request.DetailAlert{Id: req.Id}) if err != nil { return err } if detailAlert.Status == 1 { return errors.New("alert has been restored") } if detailAlert.Status == 3 { return errors.New("alert has been closed") } cli, err := client.GetOpenSearch() if err != nil { return } doc := make(map[string]interface{}, 0) doc["status"] = req.Status if req.DisposalContent != "" { disposedList := detailAlert.DisposedList disposedList = append(disposedList, entity.DisposedList{ IsDisposed: req.Status, DisposalContent: req.DisposalContent, DisposalUser: a.User.SystemAccount, DisposalTime: now, }) doc["disposed_list"] = disposedList } b, _ := json.Marshal(doc) docStr := string(b) if docStr == "" { return errors.New("doc marshal failed") } content := strings.NewReader(fmt.Sprintf(`{ "doc": %s }`, docStr)) res := opensearchapi.UpdateRequest{ Index: OpenSearchIndex, DocumentID: cast.ToString(req.Id), Body: content, Source: []string{"true"}, } do, err := res.Do(context.Background(), cli) if err != nil { return } defer do.Body.Close() if do.StatusCode != http.StatusOK { err = errors.New(do.String()) return } body, err := io.ReadAll(do.Body) if err != nil { return } err = json.Unmarshal(body, &sources) if err != nil { return } time.Sleep(time.Second) return nil }