zhaoqingang
2025-02-19 fca319958029fa924308e50cb61202d7d6ff5008
db/db.go
@@ -3,20 +3,23 @@
import (
   "github.com/elastic/go-elasticsearch/v6"
   "gorm.io/gorm"
   "model-engine/config"
   "model-engine/db/es"
   "model-engine/pkg/logger"
   "model-engine/pkg/mysqlx"
)
// Init 初始化mysql和es
func Init() error {
   if err := mysqlx.Init(config.MysqlConf, logger.GetLogger()); err != nil {
   if err := mysqlx.Init(config.MysqlConf, nil); err != nil {
      return err
   }
   if err := es.InitClient([]string{"http://" + config.EsInfo.Ip + ":" + config.EsInfo.Port}); err != nil {
      return err
   }
   InitDefaultData()
   return nil
}
@@ -50,3 +53,315 @@
   return nil
}
// InitDefaultData 初始化数据
func InitDefaultData() error {
   var models = []*Model{
      //{
      //   BaseModel: BaseModel{
      //      ID: ModelIdGather,
      //   },
      //   Name:        "疑似聚集",
      //   Description: "通用聚集模型",
      //   Version:     "v1.0.2",
      //   Enabled:     false,
      //},
      {
         BaseModel: BaseModel{
            ID: ModelIdDisappear,
         },
         Name:        "疑似脱管",
         Description: "通用脱管模型",
         Version:     "v1.1.0",
         Enabled:     false,
      }, {
         BaseModel: BaseModel{
            ID: ModelIdLocationAnalysis,
         },
         Name:        "重点场所分析",
         Description: "通用场所分析模型",
         Version:     "v1.0.0",
         Enabled:     false,
      }, {
         BaseModel: BaseModel{
            ID: ModelIdAccessRegularity,
         },
         Name:        "出行规律分析",
         Description: "出行规律分析模型",
         Version:     "v1.0.0",
         Enabled:     false,
      },
      //{
      //   BaseModel: BaseModel{
      //      ID: ModelIdNightAnalysis,
      //   },
      //   Name:        "昼伏夜出分析",
      //   Description: "昼伏夜出分析",
      //   Version:     "v1.0.0",
      //   Enabled:     false,
      //},
   }
   for i := range models {
      var existingModel Model
      if err := GetDB().Model(models[i]).Where("id = ?", models[i].ID).First(&existingModel).Error; err != nil {
         GetDB().Model(models[i]).Create(models[i])
      } else {
         GetDB().Model(models[i]).Update("version", models[i].Version)
      }
   }
   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:     3,
      //   },
      //},
      // 托管
      {
         Id:      "7a1f0a3a-c207-4d94-bc28-cc9e017b3628",
         ModelId: ModelIdDisappear,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "disappearTime",
            Name:     "持续时间",
            Type:     "input",
            Must:     true,
            Unit:     "小时",
            Range:    "1,2400",
            Value:    "24",
            ValType:  "int",
            Operator: ">=",
            Sort:     0,
         },
      },
      {
         Id:      "f1b99f28-1be0-4f78-b7c1-b01b1656b7fa",
         ModelId: ModelIdDisappear,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "age",
            Name:     "年龄段",
            Type:     "range",
            Must:     false,
            Unit:     "岁",
            Range:    "1,100",
            Value:    "60, 90",
            ValType:  "int",
            Operator: "==",
            Sort:     2,
         },
      },
      {
         Id:      "47366fa6-2f61-4fe0-957a-b1e0606bb1f0",
         ModelId: ModelIdDisappear,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "lastDirection",
            Name:     "进出方向",
            Type:     "input",
            Must:     false,
            Unit:     "",
            Range:    "",
            Value:    "",
            ValType:  "string",
            Operator: "==",
            Sort:     1,
         },
      },
      // 场所分析
      {
         Id:      "3f667e5a-bd10-4673-be45-f385e19a9c25",
         ModelId: ModelIdLocationAnalysis,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "duration",
            Name:     "监控时间",
            Type:     "input",
            Must:     true,
            Unit:     "天内",
            Range:    "1,60",
            Value:    "30",
            ValType:  "int",
            Operator: "==",
            Sort:     1,
         },
      },
      {
         Id:      "ce298639-0cf8-4a8f-89f9-a932034a1e86",
         ModelId: ModelIdLocationAnalysis,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "timeRange",
            Name:     "出现时间",
            Type:     "range",
            Must:     true,
            Unit:     "点",
            Range:    "0,23",
            Value:    "9,10",
            ValType:  "int",
            Operator: "==",
            Sort:     2,
         },
      },
      {
         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:     0,
         },
      },
      // 出行规律
      {
         Id:      "6ba7b810-9dad-11d1-80b4-00c04fd430c8",
         ModelId: ModelIdAccessRegularity,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "duration",
            Name:     "监控时间段",
            Type:     "input",
            Must:     true,
            Unit:     "天内",
            Range:    "1,60",
            Value:    "30",
            ValType:  "int",
            Operator: ">=",
            Sort:     0,
         },
      },
      {
         Id:      "f47ac10b-58cc-4372-a567-0e02b2c3d479",
         ModelId: ModelIdAccessRegularity,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "timeRange",
            Name:     "出行时间段",
            Type:     "range",
            Must:     true,
            Unit:     "点",
            Range:    "1,24",
            Value:    "21,3",
            ValType:  "int",
            Operator: ">=",
            Sort:     1,
         },
      },
      {
         Id:      "c9bf9e57-1685-4c89-bafb-ff5af830be8a",
         ModelId: ModelIdAccessRegularity,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "appearances",
            Name:     "出行次数",
            Type:     "input",
            Must:     false,
            Unit:     "次",
            Range:    "1,100",
            Value:    "2",
            ValType:  "int",
            Operator: "==",
            Sort:     2,
         },
      },
      {
         Id:      "e4eaaaf2-d142-11e1-b3e4-080027620cdd",
         ModelId: ModelIdAccessRegularity,
         Scope:   "",
         RuleArg: RuleArg{
            Alias:    "direction",
            Name:     "进出方向",
            Type:     "select",
            Must:     false,
            Unit:     "",
            Range:    "",
            Value:    "",
            ValType:  "string",
            Operator: "==",
            Options:  "[{\"label\":\"进\", \"value\":\"in\"},{\"label\":\"出\", \"value\":\"out\"}]",
            Sort:     3,
         },
      },
   }
   for i := range rules {
      GetDB().Save(&rules[i])
   }
   return nil
}