zhangzengfei
2025-02-07 17f45fcc0a062a15372883de8909953071c51b3c
添加场所分析
1个文件已添加
4个文件已修改
164 ■■■■■ 已修改文件
db/db.go 63 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
db/model.go 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/disappear.go 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/locationAnalysis.go 88 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/model.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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 {
db/model.go
@@ -183,7 +183,8 @@
}
const (
    ModelIdDrug      = "drug"      // 涉毒
    ModelIdGather    = "gather"    // 聚集
    ModelIdDisappear = "disappear" // 失踪
    ModelIdDrug             = "drug"             // 涉毒
    ModelIdGather           = "gather"           // 聚集
    ModelIdDisappear        = "disappear"        // 失踪
    ModelIdLocationAnalysis = "locationAnalysis" // 场所分析
)
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()
models/locationAnalysis.go
New file
@@ -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 ""
}
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{} },
    // 添加其他模型
}