| | |
| | | m.AreaIds = areaIds |
| | | m.Building = task.Building |
| | | m.AlarmType = task.AlarmType |
| | | m.PersonIdentity = []string{"stranger", "visitor", "resident"} //task.IdentityType |
| | | m.KeyTypes = []string{"1", "2", "3", "4", "5"} //task.IdentityType |
| | | m.KeyTypes = []string{"1", "2", "3", "4", "5"} //task.IdentityType |
| | | m.KeyPersonType = task.PersonType |
| | | m.PersonLabel = task.PersonLabel |
| | | if task.IdentityType != "" { |
| | | m.PersonIdentity = strings.Split(task.IdentityType, ",") |
| | | for _, t := range strings.Split(task.IdentityType, ",") { |
| | | if t == "all" { |
| | | m.PersonIdentity = []string{"stranger", "visitor", "resident"} |
| | | break |
| | | } else { |
| | | m.PersonIdentity = append(m.PersonIdentity, t) |
| | | } |
| | | } |
| | | } else { |
| | | m.PersonIdentity = []string{"stranger", "visitor", "resident"} |
| | | } |
| | | |
| | | if task.PersonType != "" { |
| | | m.KeyTypes = strings.Split(task.PersonType, ",") |
| | | } |
| | |
| | | } |
| | | |
| | | type LocationPersonInfo struct { |
| | | Id string `json:"id"` |
| | | DocumentNumber string `json:"document_number"` |
| | | PersonType string `json:"person_type"` |
| | | //CommunityId string `json:"community_id"` |
| | | //OrgId string `json:"org_id"` |
| | | //PersonName string `json:"person_name"` |
| | | //IdCard string `json:"id_card"` |
| | | LastAppearanceTime int64 `json:"last_appearance_time"` |
| | | //LastDirection string `json:"last_direction"` |
| | | //LastLocation string `json:"last_location"` |
| | | Id string `json:"id"` |
| | | DocumentNumber string `json:"document_number"` |
| | | PersonType string `json:"person_type"` |
| | | LastAppearanceTime int64 `json:"last_appearance_time"` |
| | | } |
| | | |
| | | //var ( |
| | | // processed sync.Map // 存储已处理记录 |
| | | // cleanupThreshold = time.Now().Add(-100 * time.Hour) // 定义一个时间窗口,假设只保存最近100小时的记录 |
| | | //) |
| | | |
| | | func (m *LocationModel) Run() error { |
| | | // 根据配置的时间段天数, 每天的时间范围内, 重点人员类型或者特定标签人员出现的楼层次数超过阈值 |
| | | |
| | | results := make([]*db.ModelTaskResults, 0) |
| | | var baseFilter, labelFilter, keyFilter []LocationPersonInfo |
| | | var document_number_map = make(map[string]LocationPersonInfo) |
| | | var document_number_list []string |
| | | community_ids := m.AreaIds |
| | | if len(community_ids) == 0 { |
| | | community_ids = m.OrgIds |
| | | var documentNumberMap = make(map[string]LocationPersonInfo) |
| | | var documentNumberList []string |
| | | communityIds := m.AreaIds |
| | | if len(communityIds) == 0 { |
| | | communityIds = m.OrgIds |
| | | } |
| | | err := db.GetDB().Raw(` |
| | | SELECT |
| | |
| | | snapshot_count_summary AS s |
| | | JOIN person AS p ON p.id = s.document_number |
| | | WHERE |
| | | p.id_card != "" |
| | | AND (s.community_id IN ? OR s.org_id IN ?) |
| | | (s.community_id IN ? OR s.org_id IN ?) |
| | | AND s.status IN ? |
| | | `, community_ids, m.OrgIds, m.PersonIdentity).Scan(&baseFilter).Error |
| | | `, communityIds, m.OrgIds, m.PersonIdentity).Scan(&baseFilter).Error |
| | | if err != nil { |
| | | logger.Warnf(err.Error()) |
| | | } |
| | | |
| | | if len(baseFilter) == 0 { |
| | | return fmt.Errorf("no results found that match the age condition %s - %s - %s ", m.AreaIds, m.OrgIds, m.PersonIdentity) |
| | | return fmt.Errorf("no results found that match the identity condition %+v", m.Task.Rules) |
| | | } |
| | | |
| | | logger.Debugf("task %s match age result %d", m.Task.Name, len(baseFilter)) |
| | | logger.Debugf("task %s match identity result %d", m.Task.Name, len(baseFilter)) |
| | | for _, i := range baseFilter { |
| | | if _, ok := document_number_map[i.DocumentNumber]; !ok { |
| | | document_number_list = append(document_number_list, i.DocumentNumber) |
| | | if _, ok := documentNumberMap[i.DocumentNumber]; !ok { |
| | | documentNumberList = append(documentNumberList, i.DocumentNumber) |
| | | tmp := i |
| | | document_number_map[i.DocumentNumber] = tmp |
| | | documentNumberMap[i.DocumentNumber] = tmp |
| | | } |
| | | } |
| | | if m.PersonLabel != "" { |
| | |
| | | WHERE |
| | | p.id IN ? |
| | | AND l.label_id IN ? |
| | | `, document_number_list, labels).Scan(&labelFilter).Error |
| | | `, documentNumberList, labels).Scan(&labelFilter).Error |
| | | if err != nil { |
| | | logger.Warnf(err.Error()) |
| | | } |
| | |
| | | } |
| | | |
| | | logger.Debugf("task %s match label result %d", m.Task.Name, len(labelFilter)) |
| | | document_number_list = []string{} |
| | | documentNumberList = []string{} |
| | | for _, i := range labelFilter { |
| | | document_number_list = append(document_number_list, i.Id) |
| | | documentNumberList = append(documentNumberList, i.Id) |
| | | } |
| | | } |
| | | |
| | | if len(m.KeyTypes) > 0 { |
| | | //keyTypes := strings.Split(m.KeyPersonType, ",") |
| | | err := db.GetDB().Raw(` |
| | |
| | | WHERE |
| | | p.id IN ? |
| | | AND k.person_type IN ? |
| | | `, document_number_list, m.KeyTypes).Scan(&keyFilter).Error |
| | | `, documentNumberList, m.KeyTypes).Scan(&keyFilter).Error |
| | | if err != nil { |
| | | logger.Warnf(err.Error()) |
| | | } |
| | | if len(keyFilter) == 0 { |
| | | return fmt.Errorf("no results found that match the key condition%s------ %s", document_number_list, m.KeyTypes) |
| | | return fmt.Errorf("no results found that match the key condition %v------ %s", documentNumberList, m.KeyTypes) |
| | | } |
| | | logger.Debugf("task %s match key person result %d", m.Task.Name, len(keyFilter)) |
| | | } |
| | | //logger.Debugf("task %s last result %d", m.Task.Name, len(lastFilter)) |
| | | document_number_list = []string{} |
| | | documentNumberList = []string{} |
| | | for _, i := range keyFilter { |
| | | document_number_list = append(document_number_list, i.Id) |
| | | person := document_number_map[i.DocumentNumber] |
| | | //logger.Debugf("keyFilter %s ------------- %s", i.Id, i.PersonType) |
| | | documentNumberList = append(documentNumberList, i.Id) |
| | | person := documentNumberMap[i.Id] |
| | | person.PersonType = i.PersonType |
| | | document_number_map[i.DocumentNumber] = person |
| | | documentNumberMap[i.Id] = person |
| | | } |
| | | logger.Debugf("document_number_list %s last result %s", m.Task.Name, document_number_list) |
| | | records, err := queryEsLocation(db.GetEsClient(), m, document_number_list) |
| | | //logger.Debugf("document_number_map %s last result %s", m.Task.Name, document_number_map) |
| | | records, err := queryEsLocation(db.GetEsClient(), m, documentNumberList) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | logger.Debugf("records %s last result %s", m.Task.Name, records) |
| | | logger.Debugf("records %s last result %d", m.Task.Name, len(records)) |
| | | domains, err := domainToLocation(records) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | var tagTypes []string |
| | | var lastAppearanceTime int64 |
| | | //var lastAppearanceTime int64 |
| | | for _, record := range records { |
| | | tagTypes = []string{} |
| | | for _, personId := range record.DocumentNumbers { |
| | | tagTypes = append(tagTypes, document_number_map[personId].PersonType) |
| | | lastAppearanceTime = document_number_map[personId].LastAppearanceTime |
| | | logger.Debugf("document_number_map[personId] %+v", documentNumberMap[personId]) |
| | | tagTypes = append(tagTypes, documentNumberMap[personId].PersonType) |
| | | //lastAppearanceTime = document_number_map[personId].LastAppearanceTime |
| | | } |
| | | logger.Debugf("tagTypes %s ---------------%s--------", tagTypes, record.DocumentNumbers) |
| | | //logger.Debugf("tagTypes %s ---------------%s--------", record.PicDate, record.DocumentNumbers) |
| | | _, typeNames, err := service.GetPersonTypeNameByTypes(tagTypes) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | event := strings.Join(typeNames, ",") |
| | | result := &db.ModelTaskResults{ |
| | | Title: m.Task.Name, |
| | | Event: m.eventFormat(event, record.AppearCount), |
| | | ModelID: m.Task.ModelID, |
| | | ModelTaskID: m.Task.ID, |
| | | CommunityId: record.CommunityId, |
| | | OrgID: record.OrgId, |
| | | ObjectIds: strings.Join(record.DocumentNumbers, ","), |
| | | Location: fmt.Sprintf("%s%s%s", domains[record.CommunityId].Name, record.Building, record.Floor), |
| | | Building: record.Building, |
| | | Floor: record.Floor, |
| | | PicDate: time.Unix(lastAppearanceTime, 0).Format("2006-01-02 15:04:05"), |
| | | Title: m.Task.Name, |
| | | Event: m.eventFormat(event, record.AppearCount), |
| | | ModelID: m.Task.ModelID, |
| | | ModelTaskID: m.Task.ID, |
| | | CommunityId: record.CommunityId, |
| | | OrgID: record.OrgId, |
| | | ObjectIds: strings.Join(record.DocumentNumbers, ","), |
| | | Location: fmt.Sprintf("%s%s%s", domains[record.CommunityId].Name, record.Building, record.Floor), |
| | | Building: record.Building, |
| | | Floor: record.Floor, |
| | | //PicDate: time.Unix(int64(record.PicDate/1000), 0).Format("2006-01-02 15:04:05"), |
| | | PicDate: record.PicDate, |
| | | FirstPersonID: record.DocumentNumbers[0], |
| | | } |
| | | results = append(results, result) |
| | | } |
| | | logger.Debugf("task %s last filter result %d", m.Task.Name, len(results)) |
| | | return service.SaveTaskResults(results) |
| | | return service.SaveLocationTaskResults(results) |
| | | } |
| | | |
| | | func (m *LocationModel) KeepAlive() error { |
| | |
| | | } |
| | | filters = append(filters, addrParams) |
| | | } |
| | | //var queryTimes []map[string]interface{} |
| | | var queryTimes []map[string]interface{} |
| | | for date := startTime; date.Before(nowTime); date = date.Add(24 * time.Hour) { |
| | | start := time.Date(date.Year(), date.Month(), date.Day(), locationModel.StartTime, 0, 0, 0, date.Location()) |
| | | end := time.Date(date.Year(), date.Month(), date.Day(), locationModel.EndTime, 0, 0, 0, date.Location()) |
| | | |
| | | //queryTimes = append(queryTimes, map[string]interface{}{ |
| | | // "range": map[string]interface{}{ |
| | | // "picDate": map[string]interface{}{ |
| | | // "gte": start.Format(time.DateTime), // "2006-01-02 15:04:05d" |
| | | // "lte": end.Format(time.DateTime), |
| | | // }, |
| | | // }, |
| | | //}) |
| | | |
| | | filters = append(filters, map[string]interface{}{ |
| | | queryTimes = append(queryTimes, map[string]interface{}{ |
| | | "range": map[string]interface{}{ |
| | | "picDate": map[string]interface{}{ |
| | | "gte": start.Format(time.DateTime), |
| | | "gte": start.Format(time.DateTime), // "2006-01-02 15:04:05d" |
| | | "lte": end.Format(time.DateTime), |
| | | }, |
| | | }, |
| | | }) |
| | | } |
| | | //filters = append(filters, map[string]interface{}{ |
| | | // "bool": map[string]interface{}{ |
| | | // "filter": queryTimes, |
| | | // }, |
| | | //}) |
| | | logger.Debugf("filters--------------------------------------- %s", filters) |
| | | filters = append(filters, map[string]interface{}{ |
| | | "bool": map[string]interface{}{ |
| | | "should": queryTimes, |
| | | }, |
| | | }) |
| | | //logger.Debugf("filters--------------------------------------- %s", filters) |
| | | query := map[string]interface{}{ |
| | | "query": map[string]interface{}{ |
| | | "bool": map[string]interface{}{ |
| | |
| | | "size": 10000, |
| | | }, |
| | | }, |
| | | "picDate": map[string]interface{}{ // 新增按 picDate 聚合 |
| | | "date_histogram": map[string]interface{}{ |
| | | "field": "picDate", |
| | | "interval": "1s", // 按天聚合 |
| | | //"time_zone": "+08:00", // 设置时区 |
| | | }, |
| | | }, |
| | | }, |
| | | }, |
| | | }, |
| | |
| | | if err := json.NewDecoder(res.Body).Decode(&result); err != nil { |
| | | return nil, fmt.Errorf("error parsing response body: %s", err) |
| | | } |
| | | logger.Debugf("result--------------------------------------- %s", result) |
| | | //logger.Debugf("result--------------------------------------- %s", result) |
| | | // 解析聚合结果 |
| | | var records []*LocationRecord |
| | | if aggs, ok := result["aggregations"].(map[string]interface{}); ok { |
| | |
| | | persons = append(persons, docNumBucket.(map[string]interface{})["key"].(string)) |
| | | } |
| | | } |
| | | |
| | | var PicDate string |
| | | if docNumBuckets, ok := floorBucket.(map[string]interface{})["picDate"].(map[string]interface{})["buckets"].([]interface{}); ok { |
| | | for _, docNumBucket := range docNumBuckets { |
| | | PicDate = docNumBucket.(map[string]interface{})["key_as_string"].(string) |
| | | } |
| | | } |
| | | logger.Debugf("floor, persons--,appearCount------------------------------------- %s-- %s --", floor, persons, appearCount) |
| | | record := &LocationRecord{ |
| | | //PicDate: timestamp, |
| | | PicDate: PicDate, |
| | | DocumentNumbers: persons, |
| | | CommunityId: communityId, |
| | | Building: building, |