From 699dbde008569e88d7d53da196656b07be1755eb Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期二, 11 二月 2025 14:22:19 +0800
Subject: [PATCH] Merge branch 'dev' of http://192.168.5.5:10010/r/CloudAI/model-engine into dev

---
 .gitignore                 |    2 
 models/accessRegularity.go |  121 +++++++++++++++++++++++++++++++---------
 service/esSearch.go        |    6 +-
 models/locationAnalysis.go |    2 
 models/model.go            |    9 +-
 db/db.go                   |    3 
 db/model_rule.go           |    5 +
 7 files changed, 109 insertions(+), 39 deletions(-)

diff --git a/.gitignore b/.gitignore
index 460ccad..9f8e32f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,6 +2,6 @@
 go.sum
 .so
 logs
-model-engine
+model-engine*
 vendor
 *.lock
\ No newline at end of file
diff --git a/db/db.go b/db/db.go
index 34ab3bc..7a108d7 100644
--- a/db/db.go
+++ b/db/db.go
@@ -351,8 +351,9 @@
 				Unit:     "",
 				Range:    "",
 				Value:    "",
-				ValType:  "[{\"label\":\"杩沑", \"value\":\"in\"},{\"label\":\"鍑篭", \"value\":\"out\"}]",
+				ValType:  "string",
 				Operator: "==",
+				Options:  "[{\"label\":\"杩沑", \"value\":\"in\"},{\"label\":\"鍑篭", \"value\":\"out\"}]",
 				Sort:     3,
 			},
 		},
diff --git a/db/model_rule.go b/db/model_rule.go
index a6aab41..9244338 100644
--- a/db/model_rule.go
+++ b/db/model_rule.go
@@ -17,19 +17,20 @@
 	RuleArg struct {
 		Alias    string `gorm:"column:alias" json:"alias"`         // 鍙傛暟鐨勫埆鍚�
 		Name     string `gorm:"column:name" json:"name"`           // 鍙傛暟鍚嶇О
-		Type     string `gorm:"column:type" json:"type"`           // 鍙傛暟绫诲瀷: input, option, range, image
+		Type     string `gorm:"column:type" json:"type"`           // 鍙傛暟绫诲瀷: input, select, range, image
 		Must     bool   `gorm:"column:must" json:"must"`           // 鏄惁蹇呭~
 		Unit     string `gorm:"column:unit" json:"unit"`           // 鍗曚綅
 		Range    string `gorm:"column:range" json:"range"`         // 鍊肩殑鑼冨洿锛宔g锛�0,100琛ㄧず浠�0鍒�100
 		Value    string `gorm:"column:value" json:"value"`         // 鍙傛暟鍊�
 		ValType  string `gorm:"column:val_type" json:"valType"`    // 鍙傛暟鍊肩被鍨� int, string, bool
 		Operator string `gorm:"column:operator" json:"operator"`   // 杩愮畻绗�
+		Options  string `gorm:"column:options" json:"options"`     // 閫夐」鐨勫閫夊弬鏁�
 		Sort     int    `gorm:"column:sort;default:0" json:"sort"` // 鍙傛暟椤哄簭
 	}
 
 	ModelRuleSet struct {
 		Alias    string      `json:"alias"`    // 鍙傛暟鐨勫埆鍚�
-		Type     string      `json:"type"`     // 鍙傛暟绫诲瀷: input, option, range, image
+		Type     string      `json:"type"`     // 鍙傛暟绫诲瀷: input, select, range, image
 		Name     string      `json:"name"`     // 鍙傛暟鍚嶇О
 		Value    interface{} `json:"value"`    // 鍙傛暟鍊�
 		ValType  string      `json:"valType"`  // 鍊肩被鍨�
diff --git a/models/accessRegularity.go b/models/accessRegularity.go
index 54d5850..70e23d8 100644
--- a/models/accessRegularity.go
+++ b/models/accessRegularity.go
@@ -94,7 +94,7 @@
 		return fmt.Errorf("task id:%s, %s timeRange Time range setting error. %+v", task.ID, task.Name, task.Rules)
 	}
 
-	logger.Debugf("LocationModel init finish ...task id:%s, name:%s, rule:%+v", task.ID, task.Name, m)
+	logger.Debugf("AccessRegularity init finish ...task id:%s, name:%s, rule:%+v", task.ID, task.Name, m)
 
 	return nil
 }
@@ -106,55 +106,61 @@
 	// 鏌ユ壘鎸囧畾鏃堕棿鑼冨洿鍐呭嚭琛岃繃鐨勬。妗堢紪鍙�
 	now := time.Now()
 	startDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).AddDate(0, 0, -m.Duration)
+	endDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
+
 	err := db.GetDB().Raw(`
 		SELECT
 			document_number,
 			frequent_address,
 			community_id,
+			org_id
 		FROM
 			snapshot_count_summary
 		WHERE
-			last_appearance_time > ? "" 
-			AND (p.community_id IN ?
-			OR p.org_id IN ?)
-			AND p.status IN ?
+			last_appearance_time > ?
+			AND (community_id IN ?
+			OR org_id IN ?)
+			AND status IN ?
 		`, startDate.Unix(), m.AreaIds, m.OrgIds, m.IdentityType).Scan(&baseFilter).Error
 	if err != nil {
 		logger.Warnf(err.Error())
 	}
 
+	logger.Debugf("task %s base filter result %d", m.Task.Name, len(baseFilter))
 	esCli := db.GetEsClient()
+	// 璋冪敤es鍒嗘瀽姝や汉鐨勫嚭琛岃寰嬫槸鍚︾鍚堟潯浠讹紝 杩斿洖绗﹀悎鏉′欢鐨勬鏁板拰鏈�鍚庝竴娆$鍚堟潯浠剁殑鏃堕棿
+	sTime := startDate.Format(time.DateTime)
+	eTime := endDate.Format(time.DateTime)
+
 	for _, p := range baseFilter {
-		// 璋冪敤es鍒嗘瀽姝や汉鐨勫嚭琛岃寰嬫槸鍚︾鍚堟潯浠讹紝 杩斿洖绗﹀悎鏉′欢鐨勬鏁板拰鏈�鍚庝竴娆$鍚堟潯浠剁殑鏃堕棿
-		sTime := startDate.Format(time.DateTime)
-		eTime := time.Now().Format(time.DateTime)
 		captures, err := service.QueryEsRecord(esCli, sTime, eTime, nil, []interface{}{p.CommunityId}, []string{p.DocumentNumber})
+		//logger.Debugf("task %s person %s captures %d", m.Task.Name, p.DocumentNumber, len(captures))
+
 		if len(captures) == 0 || err != nil {
 			continue
 		}
+		//logger.Debugf("task %s person %s captures %+v", m.Task.Name, p.DocumentNumber, captures[0])
 
 		// 鏍规嵁鎶撴媿鏃堕棿鍜屽嚭鍏ユ柟鍚戯紝璁$畻绗﹀悎瑙勫垯鍐呯殑鍑哄叆娆℃暟
-		hitCount := 0
+		hitCount, pd := countValidDays(captures, m.StartHour, m.EndHour, m.LastDirection)
 
-		//for _, c := range captures {
-		//	captureTime, err := time.ParseInLocation(time.DateTime, c.PicDate, time.Local)
-		//}
+		if hitCount > m.Appearances {
+			// 鍐欐暟鎹簱
+			result := &db.ModelTaskResults{
+				Title:         m.Task.Name,
+				Event:         fmt.Sprintf("%s - %s 鏃堕棿娈靛唴, %s %d娆�", sTime, eTime, m.Task.Name, hitCount),
+				ModelID:       m.Task.ModelID,
+				ModelTaskID:   m.Task.ID,
+				CommunityId:   p.CommunityId,
+				OrgID:         p.OrgId,
+				ObjectIds:     p.DocumentNumber,
+				Location:      p.FrequentAddress,
+				PicDate:       pd,
+				FirstPersonID: p.DocumentNumber,
+			}
 
-		// 鍐欐暟鎹簱
-		result := &db.ModelTaskResults{
-			Title:         m.Task.Name,
-			Event:         fmt.Sprintf("%s %d娆�", m.Task.Name, hitCount),
-			ModelID:       m.Task.ModelID,
-			ModelTaskID:   m.Task.ID,
-			CommunityId:   p.CommunityId,
-			OrgID:         p.OrgId,
-			ObjectIds:     p.DocumentNumber,
-			Location:      p.FrequentAddress,
-			PicDate:       time.Unix(p.LastAppearanceTime, 0).Format(time.DateTime),
-			FirstPersonID: p.DocumentNumber,
+			results = append(results, result)
 		}
-
-		results = append(results, result)
 	}
 
 	logger.Debugf("task %s last filter result %d", m.Task.Name, len(results))
@@ -168,10 +174,71 @@
 
 func (m *RegularityModel) Shutdown() error {
 	// 娓呯悊璧勬簮
-	fmt.Println("Shutting down LocationModel Model")
+	fmt.Println("Shutting down accessRegularityS Model")
 	return nil
 }
 
 func (m *RegularityModel) eventFormat() string {
 	return ""
 }
+
+func countValidDays(records []*service.ESRecordInfo, startHour, endHour int, direction string) (int, string) {
+	layout := "2006-01-02 15:04:05"             // 鏃堕棿鏍煎紡
+	lastDirectionMap := make(map[string]string) // 璁板綍鏈�鍚庝竴鏉� Direction
+	lastTimeMap := make(map[string]time.Time)   // 璁板綍鏈�鍚庝竴鏉℃椂闂�
+	lastPicDate := ""
+
+	// 鍒ゆ柇鏄惁璺ㄥぉ
+	var isCrossDay bool
+	if endHour < startHour {
+		isCrossDay = true
+	}
+
+	for _, record := range records {
+		// 瑙f瀽鏃堕棿
+		t, err := time.ParseInLocation(layout, record.PicDate, time.Local)
+		if err != nil {
+			fmt.Println("瑙f瀽鏃堕棿澶辫触:", err)
+			continue
+		}
+
+		// 鑾峰彇灏忔椂
+		hour := t.Hour()
+		var key string
+
+		if !isCrossDay {
+			// 鍒ゆ柇鏃堕棿鑼冨洿锛屽苟褰掑睘鍒版煇涓�澶�
+			if hour >= startHour && hour <= endHour {
+				key = t.Format("2006-01-02")
+			} else {
+				continue // 涓嶅湪缁熻鑼冨洿鍐�
+			}
+		} else {
+			// 鍒ゆ柇鏃堕棿鑼冨洿锛屽苟褰掑睘鍒版煇涓�澶�
+			if hour >= startHour { // 21:00-23:59 褰掑睘褰撳ぉ
+				key = t.Format("2006-01-02")
+			} else if hour <= endHour { // 00:00-02:59 褰掑睘鍓嶄竴澶�
+				key = t.AddDate(0, 0, -1).Format("2006-01-02")
+			} else {
+				continue // 涓嶅湪缁熻鑼冨洿鍐�
+			}
+		}
+
+		// 璁板綍璇ユ椂闂存鍐呯殑鏈�鍚庝竴鏉℃暟鎹�
+		if lastTime, exists := lastTimeMap[key]; !exists || t.After(lastTime) {
+			lastTimeMap[key] = t
+			lastDirectionMap[key] = record.CameraLocation.Direction
+			lastPicDate = record.PicDate
+		}
+	}
+
+	// 缁熻绗﹀悎鏉′欢
+	count := 0
+	for _, dir := range lastDirectionMap {
+		if dir == direction || direction == "" {
+			count++
+		}
+	}
+
+	return count, lastPicDate
+}
diff --git a/models/locationAnalysis.go b/models/locationAnalysis.go
index 37d42ed..bdb4efa 100644
--- a/models/locationAnalysis.go
+++ b/models/locationAnalysis.go
@@ -230,7 +230,7 @@
 	if err != nil {
 		return err
 	}
-	logger.Debugf("records %s last result %s", m.Task.Name, records)
+	logger.Debugf("records %s last result %+v", m.Task.Name, records)
 	domains, err := domainToLocation(records)
 	if err != nil {
 		return err
diff --git a/models/model.go b/models/model.go
index 7d9d983..4b1a51a 100644
--- a/models/model.go
+++ b/models/model.go
@@ -14,10 +14,11 @@
 }
 
 var modelRegistry = map[string]func() Model{
-	"gather":    func() Model { return &GatherModel{} },
-	"disappear": func() Model { return &DisappearModel{} },
-	"location":  func() Model { return &LocationModel{} },
-	"night":     func() Model { return &nightModel{} },
+	"gather":           func() Model { return &GatherModel{} },
+	"disappear":        func() Model { return &DisappearModel{} },
+	"location":         func() Model { return &LocationModel{} },
+	"night":            func() Model { return &nightModel{} },
+	"accessRegularity": func() Model { return &RegularityModel{} },
 	// 娣诲姞鍏朵粬妯″瀷
 }
 
diff --git a/service/esSearch.go b/service/esSearch.go
index c5a14e9..e4b05ac 100644
--- a/service/esSearch.go
+++ b/service/esSearch.go
@@ -89,9 +89,9 @@
 				"filter": filters,
 			},
 		},
-		"size":    0,
+		"size":    10000000,
 		"sort":    []interface{}{map[string]interface{}{"picDate": map[string]interface{}{"order": "asc"}}},
-		"_source": map[string]interface{}{"includes": []interface{}{}, "excludes": []interface{}{"*.feature"}},
+		"_source": map[string]interface{}{"includes": []interface{}{}, "excludes": []interface{}{"*.feature", "targetInfo"}},
 	}
 
 	if err := json.NewEncoder(&buf).Encode(query); err != nil {
@@ -148,7 +148,7 @@
 			PicMaxUrl:      source["picMaxUrl"].(string),
 		}
 
-		cameraLocation := source["cameraId"].(map[string]interface{})
+		cameraLocation := source["cameraLocation"].(map[string]interface{})
 		record.CameraLocation = CameraLocation{
 			Building:  cameraLocation["building"].(string),
 			Unit:      cameraLocation["unit"].(string),

--
Gitblit v1.8.0