From 17f45fcc0a062a15372883de8909953071c51b3c Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@smartai.com>
Date: 星期五, 07 二月 2025 18:46:33 +0800
Subject: [PATCH] 添加场所分析

---
 db/model.go                |    7 +-
 models/locationAnalysis.go |   88 +++++++++++++++++++++++++++++
 models/disappear.go        |    5 -
 models/model.go            |    1 
 db/db.go                   |   63 +++++++++++++++++++++
 5 files changed, 158 insertions(+), 6 deletions(-)

diff --git a/db/db.go b/db/db.go
index 695e8d5..4d479ec 100644
--- a/db/db.go
+++ b/db/db.go
@@ -74,6 +74,14 @@
 			Description: "閫氱敤鑴辩妯″瀷",
 			Version:     "v1.1.0",
 			Enabled:     false,
+		}, {
+			BaseModel: BaseModel{
+				ID: ModelIdLocationAnalysis,
+			},
+			Name:        "閲嶇偣鍦烘墍鍒嗘瀽",
+			Description: "閫氱敤鍦烘墍鍒嗘瀽妯″瀷",
+			Version:     "v1.0.0",
+			Enabled:     false,
 		},
 	}
 
@@ -155,6 +163,8 @@
 				Sort:     3,
 			},
 		},
+
+		// 鎵樼
 		{
 			Id:      "7a1f0a3a-c207-4d94-bc28-cc9e017b3628",
 			ModelId: ModelIdDisappear,
@@ -206,6 +216,59 @@
 				Sort:     1,
 			},
 		},
+
+		// 鍦烘墍鍒嗘瀽
+		{
+			Id:      "3f667e5a-bd10-4673-be45-f385e19a9c25",
+			ModelId: ModelIdLocationAnalysis,
+			Scope:   "",
+			RuleArg: RuleArg{
+				Alias:    "duration",
+				Name:     "鏃堕棿娈�",
+				Type:     "input",
+				Must:     false,
+				Unit:     "澶�",
+				Range:    "1,2400",
+				Value:    "30",
+				ValType:  "int",
+				Operator: ">=",
+				Sort:     2,
+			},
+		},
+		{
+			Id:      "ce298639-0cf8-4a8f-89f9-a932034a1e86",
+			ModelId: ModelIdLocationAnalysis,
+			Scope:   "",
+			RuleArg: RuleArg{
+				Alias:    "personCount",
+				Name:     "鍑虹幇浜烘暟",
+				Type:     "input",
+				Must:     true,
+				Unit:     "涓�",
+				Range:    "1,100",
+				Value:    "1, 90",
+				ValType:  "int",
+				Operator: ">=",
+				Sort:     0,
+			},
+		},
+		{
+			Id:      "f85ae400-0114-4862-99f7-14a41530d235",
+			ModelId: ModelIdLocationAnalysis,
+			Scope:   "",
+			RuleArg: RuleArg{
+				Alias:    "appearances",
+				Name:     "鍑虹幇娆℃暟",
+				Type:     "input",
+				Must:     true,
+				Unit:     "",
+				Range:    "",
+				Value:    "",
+				ValType:  "int",
+				Operator: ">=",
+				Sort:     1,
+			},
+		},
 	}
 
 	for i := range rules {
diff --git a/db/model.go b/db/model.go
index bc3ac05..0c1be98 100644
--- a/db/model.go
+++ b/db/model.go
@@ -183,7 +183,8 @@
 }
 
 const (
-	ModelIdDrug      = "drug"      // 娑夋瘨
-	ModelIdGather    = "gather"    // 鑱氶泦
-	ModelIdDisappear = "disappear" // 澶辫釜
+	ModelIdDrug             = "drug"             // 娑夋瘨
+	ModelIdGather           = "gather"           // 鑱氶泦
+	ModelIdDisappear        = "disappear"        // 澶辫釜
+	ModelIdLocationAnalysis = "locationAnalysis" // 鍦烘墍鍒嗘瀽
 )
diff --git a/models/disappear.go b/models/disappear.go
index f4ca22b..3f09e2a 100644
--- a/models/disappear.go
+++ b/models/disappear.go
@@ -68,7 +68,6 @@
 				m.LastDirection = val
 			}
 		}
-
 	}
 
 	logger.Debugf("DisappearModel init finish ...task id:%s, name:%s, rule:%+v", task.ID, task.Name, m)
@@ -231,7 +230,7 @@
 		if isOlderThanGivenHours(p.LastAppearanceTime, m.DisappearTime) {
 			result := &db.ModelTaskResults{
 				Title:         m.Task.Name,
-				Event:         eventFormat(p.LastAppearanceTime, p.LastDirection),
+				Event:         m.eventFormat(p.LastAppearanceTime, p.LastDirection),
 				ModelID:       m.Task.ModelID,
 				ModelTaskID:   m.Task.ID,
 				CommunityId:   p.CommunityId,
@@ -305,7 +304,7 @@
 	return timestampTime.Before(timeThreshold)
 }
 
-func eventFormat(lastAppearTime int64, lastDirection string) string {
+func (m *DisappearModel) eventFormat(lastAppearTime int64, lastDirection string) string {
 	lastTime := time.Unix(lastAppearTime, 0)
 	currentTime := time.Now()
 
diff --git a/models/locationAnalysis.go b/models/locationAnalysis.go
new file mode 100644
index 0000000..e9fbc23
--- /dev/null
+++ b/models/locationAnalysis.go
@@ -0,0 +1,88 @@
+package models
+
+import (
+	"fmt"
+	"time"
+
+	"model-engine/db"
+	"model-engine/pkg/logger"
+	"model-engine/service"
+)
+
+type LocationModel struct {
+	AreaIds       map[string]struct{}
+	Building      string       // 妤兼爧
+	AlarmType     db.AlarmType // 棰勮鏂瑰紡
+	KeyPersonType string       // 浜哄憳绫诲瀷
+	PersonLabel   string
+	PersonCount   int // 浜烘暟, 鍑虹幇鐨勫悓绫诲瀷浜哄憳鏁伴噺
+	Appearances   int // 鍑虹幇娆℃暟,
+	Duration      int // 鏃堕棿鑼冨洿, 鍗曚綅澶�
+	Task          *db.ModelTask
+}
+
+func (m *LocationModel) Init(task *db.ModelTask) error {
+	m.AreaIds = make(map[string]struct{})
+	for _, a := range task.DomainUnitIds {
+		m.AreaIds[a] = struct{}{}
+	}
+
+	m.Task = task
+	m.Building = task.Building
+	m.AlarmType = task.AlarmType
+	m.KeyPersonType = task.PersonType
+	m.PersonLabel = task.PersonLabel
+
+	for _, v := range task.Rules {
+		if v.Alias == "personCount" {
+			if val, ok := v.Value.(float64); ok {
+				m.PersonCount = int(val)
+			}
+		}
+
+		if v.Alias == "appearances" {
+			if val, ok := v.Value.(float64); ok {
+				m.Appearances = int(val)
+			}
+		}
+
+		if v.Alias == "duration" {
+			if val, ok := v.Value.(float64); ok {
+				m.Duration = int(val)
+			}
+		}
+	}
+
+	// 榛樿璁$畻30澶╃殑鏁版嵁
+	if m.Duration == 0 {
+		m.Duration = 30
+	}
+
+	logger.Debugf("LocationModel init finish ...task id:%s, name:%s, rule:%+v", task.ID, task.Name, m)
+
+	return nil
+}
+
+func (m *LocationModel) Run() error {
+	// 鏍规嵁閰嶇疆鐨勬椂闂存澶╂暟, 杩囨护瑙勫垯閰嶇疆鐨勭浉鍚岄噸鐐逛汉鍛樼被鍨嬪拰鐩稿悓鏍囩浜哄憳棰戠箒鍑虹幇鐨勬ゼ灞傚湴鍧�
+
+	results := make([]*db.ModelTaskResults, 0)
+
+	logger.Debugf("task %s last filter result %d", m.Task.Name, len(results))
+	return service.SaveTaskResults(results)
+}
+
+func (m *LocationModel) KeepAlive() error {
+	db.GetDB().Model(m.Task).Where("id = ?", m.Task.ID).Update("last_run_time", time.Now())
+	return nil
+}
+
+func (m *LocationModel) Shutdown() error {
+	// 娓呯悊璧勬簮
+	fmt.Println("Shutting down LocationModel Model")
+	return nil
+}
+
+func (m *LocationModel) eventFormat() string {
+	return ""
+}
diff --git a/models/model.go b/models/model.go
index 5a0e98e..8bc5640 100644
--- a/models/model.go
+++ b/models/model.go
@@ -16,6 +16,7 @@
 var modelRegistry = map[string]func() Model{
 	"gather":    func() Model { return &GatherModel{} },
 	"disappear": func() Model { return &DisappearModel{} },
+	"location":  func() Model { return &LocationModel{} },
 	// 娣诲姞鍏朵粬妯″瀷
 }
 

--
Gitblit v1.8.0