| | |
| | | "basic.com/pubsub/esutil.git" |
| | | "basic.com/valib/logger.git" |
| | | "encoding/json" |
| | | "fmt" |
| | | "ruleModelEngine/config" |
| | | "ruleModelEngine/util" |
| | | "strconv" |
| | | "strings" |
| | | ) |
| | | |
| | | func decodeDocumentInfos(docInfo []map[string]interface{}) ([]CaptureInfo, error) { |
| | |
| | | var captureInfo = CaptureInfo{} |
| | | captureInfo.DocumentNumber = info["key"].(string) |
| | | buckets := info["top_hits"].(map[string]interface{})["hits"].(map[string]interface{})["hits"].([]interface{}) |
| | | orgId := buckets[0].(map[string]interface{})["_source"].(map[string]interface{})["orgId"].(string) |
| | | captureInfo.OrgId = orgId |
| | | for _, sourceInfo := range buckets { |
| | | rSourceInfo := sourceInfo.(map[string]interface{}) |
| | | source := rSourceInfo["_source"].(map[string]interface{}) |
| | |
| | | return captureInfos, nil |
| | | } |
| | | |
| | | func QueryTimesByDocNumberDays(days int, docNumber string) (int, error) { |
| | | func QueryTimesByDocNumberDays(docNumber string, communityId string, cameraIds []string, days int) (int, error) { |
| | | cameraIdsStr := strings.Replace(strings.Trim(fmt.Sprint(cameraIds), "[]"), " ", "\",\"", -1) |
| | | esURL := "http://" + config.Elastic.Host + ":" + config.Elastic.Port + "/" + config.Elastic.Index + "/_search" |
| | | queryDSL := `{ |
| | | "query": { |
| | |
| | | { |
| | | "term": { |
| | | "documentNumber": "` + docNumber + `" |
| | | } |
| | | }, |
| | | { |
| | | "term": { |
| | | "communityId": "` + communityId + `" |
| | | } |
| | | }, |
| | | { |
| | | "terms": { |
| | | "cameraId": ["` + cameraIdsStr + `"] |
| | | } |
| | | } |
| | | ] |
| | |
| | | return total, nil |
| | | } |
| | | |
| | | func QueryLastIdByDayRange(dayGte, dayLt int) (map[string]string, error) { |
| | | func QueryLastIdByDayRange(communityId string, cameraIds []string, dayGte, dayLt int) (map[string]string, error) { |
| | | cameraIdsStr := strings.Replace(strings.Trim(fmt.Sprint(cameraIds), "[]"), " ", "\",\"", -1) |
| | | esURL := "http://" + config.Elastic.Host + ":" + config.Elastic.Port + "/" + config.Elastic.Index + "/_search" |
| | | queryDSL := `{ |
| | | "query": { |
| | |
| | | "lt": "now-` + strconv.Itoa(dayLt) + `d/d" |
| | | } |
| | | } |
| | | } |
| | | ], |
| | | "must_not": [ |
| | | }, |
| | | { |
| | | "term": { |
| | | "alarmRules.ruleId": 4 |
| | | "communityId": "` + communityId + `" |
| | | } |
| | | }, |
| | | { |
| | | "terms": { |
| | | "cameraId": ["` + cameraIdsStr + `"] |
| | | } |
| | | } |
| | | ] |
| | |
| | | "top_hits": { |
| | | "_source": [ |
| | | "documentNumber", |
| | | "id" |
| | | "id", |
| | | "alarmRules.ruleId" |
| | | ], |
| | | "size": 1, |
| | | "sort": [ |
| | |
| | | } |
| | | } |
| | | }` |
| | | //fmt.Println(queryDSL) |
| | | //fmt.Println(esURL) |
| | | //fmt.Println(queryDSL) |
| | | docNumberMap := make(map[string]string) |
| | | buf, err := esutil.EsReq("POST", esURL, []byte(queryDSL)) |
| | | if err != nil { |
| | |
| | | buckets := info["top_hits"].(map[string]interface{})["hits"].(map[string]interface{})["hits"].([]interface{}) |
| | | for _, sourceInfo := range buckets { |
| | | rSourceInfo := sourceInfo.(map[string]interface{}) |
| | | source := rSourceInfo["_source"].(map[string]interface{}) |
| | | documentNumber := source["documentNumber"].(string) |
| | | id := source["id"].(string) |
| | | rSource := rSourceInfo["_source"].(map[string]interface{}) |
| | | //如果该天最后一条已经预警过进出异常,将过滤掉不再预警 |
| | | alarmFlag := false |
| | | if rSource["alarmRules"] != nil { |
| | | alarmRules := rSource["alarmRules"].([]interface{}) |
| | | for _, alarmRule := range alarmRules { |
| | | ruleId := alarmRule.(map[string]interface{})["ruleId"].(string) |
| | | //fmt.Println("ruleId", ruleId,rSource["documentNumber"].(string),rSource["id"].(string)) |
| | | if ruleId == "4" { |
| | | alarmFlag = true |
| | | break |
| | | } |
| | | } |
| | | } |
| | | if alarmFlag == true { |
| | | continue |
| | | } |
| | | documentNumber := rSource["documentNumber"].(string) |
| | | id := rSource["id"].(string) |
| | | docNumberMap[documentNumber] = id |
| | | } |
| | | } |
| | |
| | | return false, nil |
| | | } |
| | | |
| | | func Query1MDataByCommunityId(communityId string) ([]CaptureInfo, error) { |
| | | func QueryByTimeThresholdDataByCommunityId(communityId string, documentNumber []string, days int) ([]CaptureInfo, error) { |
| | | //fmt.Println(config.Elastic.DocumentSize) |
| | | //fmt.Println(config.Elastic.TopHitsSize) |
| | | documentNumberStr := strings.Replace(strings.Trim(fmt.Sprint(documentNumber), "[]"), " ", "\",\"", -1) |
| | | esURL := "http://" + config.Elastic.Host + ":" + config.Elastic.Port + "/" + config.Elastic.Index + "/_search" |
| | | queryDSL := ` |
| | | { |
| | |
| | | { |
| | | "range": { |
| | | "picDate": { |
| | | "gte": "now-30d/d", |
| | | "gte": "now-` + strconv.Itoa(days) + `d/d", |
| | | "lt": "now/d" |
| | | } |
| | | } |
| | |
| | | "term":{ |
| | | "communityId":"` + communityId + `" |
| | | } |
| | | }, |
| | | { |
| | | "terms":{ |
| | | "documentNumber":["` + documentNumberStr + `"] |
| | | } |
| | | } |
| | | ], |
| | | "must_not": [ |
| | | { |
| | | "term": { |
| | | "documentNumber": "" |
| | | } |
| | | } |
| | | ], |
| | | "should": [] |
| | | ] |
| | | } |
| | | }, |
| | | "size": 0, |
| | |
| | | "_source": [ |
| | | "documentNumber", |
| | | "picDate", |
| | | "orgId", |
| | | "cameraLocation.building", |
| | | "cameraLocation.unit", |
| | | "cameraLocation.floor", |