zhangqian
2024-12-13 6c38b30be9ff127f200ffbfe75c0dc48612f37a6
models/gather_model.go
@@ -4,6 +4,7 @@
   "bytes"
   "context"
   "encoding/json"
   "errors"
   "fmt"
   "github.com/elastic/go-elasticsearch/v6"
   "log"
@@ -12,6 +13,7 @@
   "model-engine/pkg/set"
   "model-engine/service"
   "strings"
   "sync"
   "time"
)
@@ -29,8 +31,15 @@
   Task           *db.ModelTask
}
func (m *GatherModel) Init(task *db.ModelTask) error {
type ProcessedRecord struct {
   UniqueKey string    // 唯一标识
   Timestamp time.Time // 记录的时间戳
}
func (m *GatherModel) Init(task *db.ModelTask) error {
   if len(task.DomainUnitIds) == 0 {
      return errors.New("empty domain set")
   }
   orgIds, areaIds, err := service.GetOrgIdsAndAreaIdsByDomainUnitIds(task.DomainUnitIds)
   if err != nil {
      return err
@@ -63,7 +72,20 @@
   AppearInterval int    `gorm:"type:int;" json:"appearInterval"` //出现间隔,单位为秒
}
var (
   processed        sync.Map                           // 存储已处理记录
   cleanupThreshold = time.Now().Add(-100 * time.Hour) // 定义一个时间窗口,假设只保存最近100小时的记录
)
func (m *GatherModel) Run() error {
   // 清理过期的记录
   processed.Range(func(key, value any) bool {
      if value.(ProcessedRecord).Timestamp.Before(cleanupThreshold) {
         processed.Delete(key)
      }
      return true
   })
   records, err := queryElasticsearch(db.GetEsClient(), m)
   if err != nil {
      log.Fatalf("Failed to query Elasticsearch: %v", err)
@@ -73,7 +95,30 @@
      return nil
   }
   aggregation, err := analyzeAndAggregate(records)
   newRecords := make([]*GatherRecord, 0)
   // 聚合逻辑
   for _, record := range records {
      // 生成唯一标识
      uniqueKey := fmt.Sprintf("%s-%s", record.DocumentNumber, record.PicDate)
      // 如果已经处理过,跳过
      if _, exists := processed.Load(uniqueKey); exists {
         continue
      }
      // 添加到已处理记录
      processed.Store(uniqueKey, ProcessedRecord{
         UniqueKey: uniqueKey,
         Timestamp: time.Now(),
      })
      newRecords = append(newRecords, record)
   }
   if len(newRecords) == 0 {
      return nil
   }
   aggregation, err := analyzeAndAggregate(newRecords)
   if err != nil {
      log.Fatalf("Failed to analyze and aggregate data: %v", err)
   }
@@ -90,18 +135,23 @@
   }
   event := strings.Join(typeNames, ",")
   for lt, persons := range aggregation {
      if persons.Size() == 0 {
         continue
      }
      personIds := persons.Elements()
      result := &db.ModelTaskResults{
         Title:       m.Task.Name,
         Event:       fmt.Sprintf("%s/%d人", event, len(persons)),
         ModelID:     m.Task.ModelID,
         ModelTaskID: m.Task.ID,
         CommunityId: lt.CommunityId,
         OrgID:       lt.OrgId,
         ObjectIds:   strings.Join(persons.Elements(), ","),
         Location:    lt.Location,
         Building:    lt.Building,
         Floor:       lt.Floor,
         PicDate:     lt.Time,
         Title:         m.Task.Name,
         Event:         fmt.Sprintf("%s/%d人", event, len(persons)),
         ModelID:       m.Task.ModelID,
         ModelTaskID:   m.Task.ID,
         CommunityId:   lt.CommunityId,
         OrgID:         lt.OrgId,
         ObjectIds:     strings.Join(personIds, ","),
         Location:      lt.Location,
         Building:      lt.Building,
         Floor:         lt.Floor,
         PicDate:       lt.Time,
         FirstPersonID: personIds[0],
      }
      results = append(results, result)
   }
@@ -114,7 +164,7 @@
   return nil
}
func queryElasticsearch(esClient *elasticsearch.Client, gatherModel *GatherModel) ([]GatherRecord, error) {
func queryElasticsearch(esClient *elasticsearch.Client, gatherModel *GatherModel) ([]*GatherRecord, error) {
   var buf bytes.Buffer
   now := time.Now()
   start := now.Add(-time.Duration(gatherModel.DaysWindow) * 24 * time.Hour)
@@ -283,7 +333,7 @@
   }
   // 解析聚合结果
   var records []GatherRecord
   var records []*GatherRecord
   if aggs, ok := result["aggregations"].(map[string]interface{}); ok {
      if orgBuckets, ok := aggs["orgs"].(map[string]interface{})["buckets"].([]interface{}); ok {
         for _, orgBucket := range orgBuckets {
@@ -316,7 +366,7 @@
                                          documentNumber := person.(map[string]interface{})["key"].(string)
                                          // 构建 GatherRecord 结构体
                                          record := GatherRecord{
                                          record := &GatherRecord{
                                             PicDate:        timestamp,
                                             DocumentNumber: documentNumber,
                                             CommunityId:    communityId,
@@ -354,7 +404,10 @@
   Time        string
}
func analyzeAndAggregate(records []GatherRecord) (map[GatherLocationTime]set.StringSet, error) {
func analyzeAndAggregate(records []*GatherRecord) (map[GatherLocationTime]set.StringSet, error) {
   if len(records) == 0 {
      return nil, nil
   }
   aggregation := make(map[GatherLocationTime]set.StringSet)
   domainIds := set.NewStringSet()
   for _, record := range records {
@@ -367,6 +420,9 @@
   }
   for _, record := range records {
      if record.DocumentNumber == "" {
         continue
      }
      if domains[record.CommunityId] == nil {
         continue
      }