From fca319958029fa924308e50cb61202d7d6ff5008 Mon Sep 17 00:00:00 2001
From: zhaoqingang <zhaoqg0118@163.com>
Date: 星期三, 19 二月 2025 13:33:37 +0800
Subject: [PATCH] 暂停聚集模型

---
 models/gather_model.go |   78 +++++++++++++++++++++++++++++----------
 1 files changed, 58 insertions(+), 20 deletions(-)

diff --git a/models/gather_model.go b/models/gather_model.go
index c384db2..d0c2756 100644
--- a/models/gather_model.go
+++ b/models/gather_model.go
@@ -6,28 +6,31 @@
 	"encoding/json"
 	"errors"
 	"fmt"
-	"github.com/elastic/go-elasticsearch/v6"
 	"log"
-	"model-engine/config"
-	"model-engine/db"
-	"model-engine/pkg/set"
-	"model-engine/service"
 	"strings"
 	"sync"
 	"time"
+
+	"github.com/elastic/go-elasticsearch/v6"
+
+	"model-engine/config"
+	"model-engine/db"
+	"model-engine/pkg/logger"
+	"model-engine/pkg/set"
+	"model-engine/service"
 )
 
 type GatherModel struct {
 	OrgIds         []interface{} `json:"-"`
 	AreaIds        []interface{} `json:"-"`
-	Building       string        `gorm:"type:varchar(255)" json:"building"`    //妤兼爧
-	Floor          string        `gorm:"type:varchar(255)" json:"floor"`       //妤煎眰
-	AlarmType      db.AlarmType  `gorm:"type:varchar(255);" json:"alarmType"`  //棰勮鏂瑰紡
-	PersonType     string        `gorm:"type:varchar(255);" json:"personType"` //浜哄憳绫诲瀷
-	GatherPersons  int           `gorm:"type:int;" json:"gatherPersons"`       //鑱氶泦浜烘暟
-	AppearInterval int           `gorm:"type:int;" json:"appearInterval"`      //鍑虹幇闂撮殧锛屽崟浣嶄负绉�
-	DaysWindow     int           `gorm:"type:int;" json:"daysWindow" `         //杩戝嚑澶╁唴
-	Threshold      int           `gorm:"type:int;" json:"threshold" `          //杈惧嚑娆�
+	Building       string        `gorm:"type:varchar(255)" json:"building"`    // 妤兼爧
+	Floor          string        `gorm:"type:varchar(255)" json:"floor"`       // 妤煎眰
+	AlarmType      db.AlarmType  `gorm:"type:varchar(255);" json:"alarmType"`  // 棰勮鏂瑰紡
+	PersonType     string        `gorm:"type:varchar(255);" json:"personType"` // 浜哄憳绫诲瀷
+	GatherPersons  int           `gorm:"type:int;" json:"gatherPersons"`       // 鑱氶泦浜烘暟
+	AppearInterval int           `gorm:"type:int;" json:"appearInterval"`      // 锛屽崟浣嶄负绉�
+	DaysWindow     int           `gorm:"type:int;" json:"daysWindow" `         // 杩戝嚑澶╁唴
+	Threshold      int           `gorm:"type:int;" json:"threshold" `          // 杈惧嚑娆�
 	Task           *db.ModelTask
 }
 
@@ -52,11 +55,40 @@
 	m.Floor = task.Floor
 	m.AlarmType = task.AlarmType
 	m.PersonType = task.PersonType
-	m.GatherPersons = task.GatherPersons
-	m.AppearInterval = task.AppearInterval
-	m.DaysWindow = task.DaysWindow
-	m.Threshold = task.Threshold
-	fmt.Println("GatherModel init finish ...")
+
+	for _, v := range task.Rules {
+		if v.Alias == "gatherPersons" {
+			if val, ok := v.Value.(float64); ok {
+				m.GatherPersons = int(val)
+			}
+		}
+
+		if v.Alias == "appearInterval" {
+			if val, ok := v.Value.(float64); ok {
+				m.AppearInterval = int(val)
+			}
+		}
+
+		if v.Alias == "daysWindow" {
+			if val, ok := v.Value.(float64); ok {
+				m.DaysWindow = int(val)
+			}
+		}
+
+		if v.Alias == "threshold" {
+			if val, ok := v.Value.(float64); ok {
+				m.Threshold = int(val)
+			}
+		}
+	}
+
+	logger.Debugf("GatherModel init finish ...task id:%s, name:%s, rule:%+v\n", task.ID, task.Name, m)
+
+	if m.GatherPersons == 0 || m.AppearInterval == 0 || m.DaysWindow == 0 || m.Threshold == 0 {
+		logger.Warnf("invalid parameters. task id:%s, name:%s\n", task.ID, task.Name)
+		return errors.New("invalid parameters")
+	}
+
 	return nil
 }
 
@@ -68,8 +100,8 @@
 	OrgId          string `json:"orgId"`
 	Building       string `json:"building"`
 	Floor          string `json:"floor"`
-	GatherPersons  int    `gorm:"type:int;" json:"gatherPersons"`  //鑱氶泦浜烘暟
-	AppearInterval int    `gorm:"type:int;" json:"appearInterval"` //鍑虹幇闂撮殧锛屽崟浣嶄负绉�
+	GatherPersons  int    `gorm:"type:int;" json:"gatherPersons"`  // 鑱氶泦浜烘暟
+	AppearInterval int    `gorm:"type:int;" json:"appearInterval"` // 鍑虹幇闂撮殧锛屽崟浣嶄负绉�
 }
 
 var (
@@ -153,11 +185,17 @@
 			PicDate:       lt.Time,
 			FirstPersonID: personIds[0],
 		}
+
 		results = append(results, result)
 	}
 	return service.SaveTaskResults(results)
 }
 
+func (m *GatherModel) KeepAlive() error {
+	db.GetDB().Model(m.Task).Where("id = ?", m.Task.ID).Update("last_run_time", time.Now())
+	return nil
+}
+
 func (m *GatherModel) Shutdown() error {
 	// 娓呯悊璧勬簮
 	fmt.Println("Shutting down GatherModel Model")

--
Gitblit v1.8.0