New file |
| | |
| | | package models |
| | | |
| | | import ( |
| | | "errors" |
| | | "fmt" |
| | | "strconv" |
| | | "strings" |
| | | "time" |
| | | |
| | | "model-engine/db" |
| | | "model-engine/pkg/logger" |
| | | "model-engine/service" |
| | | ) |
| | | |
| | | type RegularityModel struct { |
| | | OrgIds []interface{} `json:"-"` |
| | | AreaIds []interface{} `json:"-"` |
| | | IdentityType string // 人员身份类型 陌生人, 访客, 住户 |
| | | KeyPersonType string // 重点人员类型 |
| | | PersonLabel string // 人员身份标签 |
| | | Task *db.ModelTask |
| | | AlarmType db.AlarmType // 预警方式 |
| | | |
| | | Appearances int // 出现次数 |
| | | Duration int // 时间范围, 单位天 |
| | | StartHour int // 开始计算的时间 配置为小时 23 - 02 表示第一天23点 - 第二天的2点 |
| | | EndHour int // 结束时间 |
| | | } |
| | | |
| | | func (m *RegularityModel) Init(task *db.ModelTask) error { |
| | | if len(task.DomainUnitIds) == 0 { |
| | | return errors.New("empty domain set") |
| | | } |
| | | orgIds, areaIds, err := service.GetOrgIdsAndAreaIdsByDomainUnitIds(task.DomainUnitIds) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | |
| | | m.Task = task |
| | | m.OrgIds = orgIds |
| | | m.AreaIds = areaIds |
| | | m.AlarmType = task.AlarmType |
| | | m.KeyPersonType = task.PersonType |
| | | m.PersonLabel = task.PersonLabel |
| | | m.IdentityType = task.IdentityType |
| | | |
| | | for _, v := range task.Rules { |
| | | if v.Alias == "timeRange" { |
| | | if val, ok := v.Value.(string); ok { |
| | | ages := strings.Split(val, ",") |
| | | m.StartHour, _ = strconv.Atoi(ages[0]) |
| | | m.EndHour, _ = strconv.Atoi(ages[1]) |
| | | } |
| | | } |
| | | |
| | | 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 |
| | | } |
| | | |
| | | if m.StartHour == 0 || m.EndHour == 0 { |
| | | 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) |
| | | |
| | | return nil |
| | | } |
| | | |
| | | func (m *RegularityModel) 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 *RegularityModel) KeepAlive() error { |
| | | db.GetDB().Model(m.Task).Where("id = ?", m.Task.ID).Update("last_run_time", time.Now()) |
| | | return nil |
| | | } |
| | | |
| | | func (m *RegularityModel) Shutdown() error { |
| | | // 清理资源 |
| | | fmt.Println("Shutting down LocationModel Model") |
| | | return nil |
| | | } |
| | | |
| | | func (m *RegularityModel) eventFormat() string { |
| | | return "" |
| | | } |