zhangzengfei
2024-12-18 39d53cf7a2080b49224dec6c395fc4ed2463a424
修改参数的获取逻辑
1个文件已添加
4个文件已修改
317 ■■■■ 已修改文件
cron/cron.go 10 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
db/db.go 104 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
db/model_rule.go 86 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
db/task.go 54 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
models/gather_model.go 63 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
cron/cron.go
@@ -1,12 +1,14 @@
package cron
import (
    "time"
    "github.com/go-co-op/gocron"
    "model-engine/models"
    "model-engine/pkg/logger"
    "model-engine/pkg/safe"
    "model-engine/service"
    "time"
)
var s *gocron.Scheduler
@@ -20,15 +22,17 @@
    if err != nil {
        panic(err)
    }
    for _, task := range tasks {
        model, err := models.GetModel(task.ModelID)
        if err != nil {
            logger.Errorf("can not find model for id:%v", task.ModelID)
            continue
        }
        task := task
        t := task
        safe.Go(func() {
            if err := model.Init(task); err != nil {
            if err := model.Init(t); err != nil {
                return
            }
            if err := model.Run(); err != nil {
db/db.go
@@ -1,8 +1,11 @@
package db
import (
    "time"
    "github.com/elastic/go-elasticsearch/v6"
    "gorm.io/gorm"
    "model-engine/config"
    "model-engine/db/es"
    "model-engine/pkg/logger"
@@ -17,6 +20,9 @@
    if err := es.InitClient([]string{"http://" + config.EsInfo.Ip + ":" + config.EsInfo.Port}); err != nil {
        return err
    }
    InitDefaultData()
    return nil
}
@@ -50,3 +56,101 @@
    return nil
}
// InitDefaultData 初始化数据
func InitDefaultData() error {
    var models = []Model{
        {
            BaseModel: BaseModel{
                ID:        ModelIdGather,
                CreatedAt: time.Time{},
                UpdatedAt: time.Time{},
            },
            Name:        "疑似聚集",
            Description: "通用聚集模型",
            Version:     "v1.0.0",
            Enabled:     false,
        },
    }
    for i := range models {
        GetDB().Save(&models[i])
    }
    var rules = []ModelRule{
        {
            Id:      "bfbdba7f-ee39-41fb-b188-b4c114a51eaa",
            ModelId: ModelIdGather,
            Scope:   "",
            RuleArg: RuleArg{
                Alias:    "gatherPersons",
                Name:     "聚集人数",
                Type:     "input",
                Must:     true,
                Unit:     "人",
                Range:    "1,100",
                Value:    "2",
                ValType:  "int",
                Operator: ">=",
                Sort:     0,
            },
        },
        {
            Id:      "941bef84-ff7f-4460-b5dc-2ac6060304a4",
            ModelId: ModelIdGather,
            Scope:   "",
            RuleArg: RuleArg{
                Alias:    "appearInterval",
                Name:     "出现间隔",
                Type:     "input",
                Must:     true,
                Unit:     "秒",
                Range:    "1,7200",
                Value:    "60",
                ValType:  "int",
                Operator: ">=",
                Sort:     1,
            },
        },
        {
            Id:      "a9b50bae-2c40-40a1-9ebc-ac34850db964",
            ModelId: ModelIdGather,
            Scope:   "",
            RuleArg: RuleArg{
                Alias:    "threshold",
                Name:     "出现次数",
                Type:     "input",
                Must:     true,
                Unit:     "次",
                Range:    "1,60",
                Value:    "1",
                ValType:  "int",
                Operator: ">=",
                Sort:     2,
            },
        },
        {
            Id:      "aed7f95e-1ce6-4fa2-b1b3-aaf59ed86c50",
            ModelId: ModelIdGather,
            Scope:   "",
            RuleArg: RuleArg{
                Alias:    "daysWindow",
                Name:     "监控时间",
                Type:     "input",
                Must:     true,
                Unit:     "天内",
                Range:    "1,7",
                Value:    "1",
                ValType:  "int",
                Operator: "==",
                Sort:     2,
            },
        },
    }
    for i := range rules {
        GetDB().Save(&rules[i])
    }
    return nil
}
db/model_rule.go
New file
@@ -0,0 +1,86 @@
package db
import (
    "fmt"
    "gorm.io/gorm"
)
type (
    ModelRule struct {
        Id      string `gorm:"primary_key;column:id" json:"id"`
        ModelId string `gorm:"column:model_id" json:"modelId"`
        Scope   string `gorm:"column:scope" json:"scope"`
        RuleArg
    }
    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
        Must     bool   `gorm:"column:must" json:"must"`           // 是否必填
        Unit     string `gorm:"column:unit" json:"unit"`           // 单位
        Range    string `gorm:"column:range" json:"range"`         // 值的范围,eg: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"`   // 运算符
        Sort     int    `gorm:"column:sort;default:0" json:"sort"` // 参数顺序
    }
    ModelRuleSet struct {
        Alias    string      `json:"alias"`    // 参数的别名
        Type     string      `json:"type"`     // 参数类型: input, option, range, image
        Name     string      `json:"name"`     // 参数名称
        Value    interface{} `json:"value"`    // 参数值
        ValType  string      `json:"valType"`  // 值类型
        Operator string      `json:"operator"` // 运算符
    }
    ModelRuleSearch struct {
        ModelRule
        Orm *gorm.DB
    }
)
func (e *ModelRule) TableName() string {
    return "model_rule"
}
func NewModelRuleSearch() *ModelRuleSearch {
    return &ModelRuleSearch{
        Orm: GetDB(),
    }
}
func (slf *ModelRuleSearch) SetOrm(tx *gorm.DB) *ModelRuleSearch {
    slf.Orm = tx
    return slf
}
func (slf *ModelRuleSearch) SetModelID(id string) *ModelRuleSearch {
    slf.ModelId = id
    return slf
}
func (slf *ModelRuleSearch) build() *gorm.DB {
    var db = slf.Orm.Table(slf.TableName())
    if slf.ModelId != "" {
        db = db.Where("model_id = ?", slf.ModelId)
    }
    return db
}
func (slf *ModelRuleSearch) Find() ([]*ModelRule, error) {
    var (
        records = make([]*ModelRule, 0)
        db      = slf.build()
    )
    if err := db.Find(&records).Error; err != nil {
        return records, fmt.Errorf("find records err: %v", err)
    }
    return records, nil
}
db/task.go
@@ -1,48 +1,55 @@
package db
import (
    "encoding/json"
    "fmt"
    "gorm.io/gorm"
    "strings"
    "time"
    "gorm.io/gorm"
)
type (
    ModelTask struct {
        BaseModel
        Name           string    `gorm:"type:varchar(255)" json:"name"`    //任务名称
        ModelID        string    `gorm:"type:varchar(255)" json:"modelID"` //模型ID
        BeginTime      time.Time //工作时间开始
        EndTime        time.Time //公祖时间结束
        DomainUnitIds  []string  `gorm:"-" json:"domainUnitIds"` //区域id列表
        DomainIds      string    `gorm:"type:varchar(1000)" json:"-"`
        Building       string    `gorm:"type:varchar(255)" json:"building"`    //楼栋
        Floor          string    `gorm:"type:varchar(255)" json:"floor"`       //楼层
        AlarmType      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" `          //达几次
        Enabled        bool      `json:"enabled"`                              //是否开启
        Name           string         `gorm:"type:varchar(255)" json:"name"`    // 任务名称
        ModelID        string         `gorm:"type:varchar(255)" json:"modelID"` // 模型ID
        BeginTime      time.Time      `json:"beginTime"`                        // 工作时间开始
        EndTime        time.Time      `json:"endTime"`                          // 工作时间结束
        DomainUnitIds  []string       `gorm:"-" json:"domainUnitIds"`           // 区域id列表
        DomainIds      string         `gorm:"type:varchar(1000)" json:"-"`
        Building       string         `gorm:"type:varchar(255)" json:"building"`      // 楼栋
        Floor          string         `gorm:"type:varchar(255)" json:"floor"`         // 楼层
        AlarmType      AlarmType      `gorm:"type:varchar(255);" json:"alarmType"`    // 预警方式
        PersonType     string         `gorm:"type:varchar(255);" json:"personType"`   // 人员类型
        PersonLabel    string         `gorm:"type:varchar(255);" json:"personLabel"`  // 人员标签
        IdentityType   string         `gorm:"type:varchar(255);" json:"identityType"` // 身份标签, 陌生人, 访客, 住户
        AlarmLevel     string         `gorm:"type:varchar(255);" json:"alarmLevel"`   // 预警级别
        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" `            // 达几次
        Enabled        bool           `json:"enabled"`                                // 是否开启
        RuleSet        string         `gorm:"type:text" json:"-"`                     // 存储模型任务设置的规则参数
        Rules          []ModelRuleSet `gorm:"-" json:"rules"`                         // 规则配置
    }
    ModelTaskSearch struct {
        ModelTask
        Orm       *gorm.DB
        ModelIDs  []string
        Unexpired bool
        PageNum   int
        PageSize  int
        Keyword   string
        ModelIDs  []string
        Unexpired bool
    }
)
type AlarmType string
const (
    AlarmTypeSave        AlarmType = "save"    //仅保存
    AlarmTypeSendMessage AlarmType = "message" //发消息
    AlarmTypeSave        AlarmType = "save"    // 仅保存
    AlarmTypeSendMessage AlarmType = "message" // 发消息
)
func (slf *ModelTask) TableName() string {
@@ -78,6 +85,13 @@
    if slf.DomainIds != "" {
        slf.DomainUnitIds = strings.Split(slf.DomainIds, ",")
    }
    if slf.DomainIds != "" {
        slf.DomainUnitIds = strings.Split(slf.DomainIds, ",")
    }
    if slf.RuleSet != "" {
        _ = json.Unmarshal([]byte(slf.RuleSet), &slf.Rules)
    }
    return nil
}
models/gather_model.go
@@ -6,28 +6,30 @@
    "encoding/json"
    "errors"
    "fmt"
    "github.com/elastic/go-elasticsearch/v6"
    "log"
    "strings"
    "sync"
    "time"
    "github.com/elastic/go-elasticsearch/v6"
    "model-engine/config"
    "model-engine/db"
    "model-engine/pkg/set"
    "model-engine/service"
    "strings"
    "sync"
    "time"
)
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 +54,34 @@
    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)
            }
        }
    }
    fmt.Printf("GatherModel init finish ... rule:%+v\n", m)
    return nil
}
@@ -68,8 +93,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 (