diff --git a/go.mod b/go.mod index 1b52e4c7581a0c194bbd30a17f522f44cacbe4e5..8cd956c9245218da1868fe583072fc3287a737fb 100644 --- a/go.mod +++ b/go.mod @@ -54,10 +54,12 @@ require ( github.com/henrylee2cn/ameda v1.5.0 // indirect github.com/henrylee2cn/goutil v0.0.0-20220704075712-42f2ec55fe8d // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/josharian/intern v1.0.0 // indirect github.com/klauspost/compress v1.16.5 // indirect github.com/klauspost/cpuid/v2 v2.2.4 // indirect github.com/leodido/go-urn v1.2.3 // indirect github.com/lestrrat-go/strftime v1.0.6 // indirect + github.com/mailru/easyjson v0.7.7 // indirect github.com/mattn/go-isatty v0.0.17 // indirect github.com/minio/md5-simd v1.1.2 // indirect github.com/minio/sha256-simd v1.0.1 // indirect @@ -65,6 +67,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/nyaruka/phonenumbers v1.1.7 // indirect + github.com/olivere/elastic/v7 v7.0.32 // indirect github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/rs/xid v1.5.0 // indirect diff --git a/go.sum b/go.sum index bc9646e514176a49a0d3431d0e4c40fe3d6d0127..8a65a31368baf0f9088f9f7099a0c9473372df51 100644 --- a/go.sum +++ b/go.sum @@ -249,6 +249,8 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGw github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= @@ -295,6 +297,8 @@ github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM= github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4= github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ= +github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= +github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcnceauSikq3lYCQ= github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= @@ -350,6 +354,8 @@ github.com/nyaruka/phonenumbers v1.1.7/go.mod h1:DC7jZd321FqUe+qWSNcHi10tyIyGNXG github.com/oklog/oklog v0.3.2/go.mod h1:FCV+B7mhrz4o+ueLpx+KqkyXRGMWOYEvfiXtdGtbWGs= github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA= github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5/go.mod h1:vsDQFd/mU46D+Z4whnwzcISnGGzXWMclvtLoiIKAKIo= +github.com/olivere/elastic/v7 v7.0.32 h1:R7CXvbu8Eq+WlsLgxmKVKPox0oOwAE/2T9Si5BnvK6E= +github.com/olivere/elastic/v7 v7.0.32/go.mod h1:c7PVmLe3Fxq77PIfY/bZmxY/TAamBhCzZ8xDOE09a9k= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= diff --git a/src/service/alert_list.go b/src/service/alert_list.go index 332c967937a5810b98150b0d3e86b51523d24400..6059e87afdb3bee2a4db0894b87d07aefc0dc5a8 100644 --- a/src/service/alert_list.go +++ b/src/service/alert_list.go @@ -6,6 +6,7 @@ import ( "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/thoas/go-funk" "gitlab.wodcloud.com/smart-operation/so-operation-api/src/bean/entity" @@ -16,6 +17,7 @@ import ( "gitlab.wodcloud.com/smart-operation/so-operation-api/src/pkg/beagle/jsontime" "go.uber.org/zap" "io" + "log" "net/http" "strings" "xorm.io/xorm" @@ -246,13 +248,40 @@ func (a *AlertListSvc) IndexSearch(req request.ListAlertList) (resp response.Ale 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": { - "match_all": {} - }, - "from": %d, - "size": %d -}`, req.GetPageSize()*(req.GetPage()-1), req.GetPageSize())) + "query": %s, + "from": %d, + "size": %d}`, string(b), req.GetPageSize()*(req.GetPage()-1), req.GetPageSize())) res := opensearchapi.SearchRequest{ Index: []string{OpenSearchIndex}, @@ -314,7 +343,7 @@ func (a *AlertListSvc) BatchCloseAlertList(session *xorm.Session, req request.Ba } func (a *AlertListSvc) GetDataById(req request.DetailAlertList) (resp response.AlertListItem, err error) { - now := jsontime.Now() + /*now := jsontime.Now() data := response.AlertListItem{ AlertList: entity.AlertList{ Id: 123, @@ -393,8 +422,11 @@ func (a *AlertListSvc) GetDataById(req request.DetailAlertList) (resp response.A NotifyMethod: []string{"dingtalk", "sms"}, }, }, + }*/ + list, err := a.IndexSearch(request.ListAlertList{Id: req.Id}) + if len(list.List) > 0 { + resp = list.List[0] } - resp = data return }