// Package pool define concurrency call function in ants package pool import ( "context" "strconv" "time" "modelRT/config" "modelRT/database" "modelRT/diagram" "modelRT/logger" "modelRT/model" realtimedata "modelRT/real-time-data" "github.com/bitly/go-simplejson" "go.uber.org/zap" ) // ParseFunc defines func that parses the model data from postgres var ParseFunc = func(parseConfig interface{}) { logger := logger.GetLoggerInstance() modelParseConfig, ok := parseConfig.(config.ModelParseConfig) if !ok { logger.Error("conversion model parse config type failed") return } cancelCtx, cancel := context.WithTimeout(modelParseConfig.Context, 5*time.Second) defer cancel() pgClient := database.GetPostgresDBClient() unmarshalMap := make(map[string]interface{}) tableName := model.SelectModelNameByType(modelParseConfig.ComponentInfo.ComponentType) result := pgClient.WithContext(cancelCtx).Table(tableName).Where("component_id = ?", modelParseConfig.ComponentInfo.ID).Find(&unmarshalMap) if result.Error != nil { logger.Error("query component detail info failed", zap.Error(result.Error)) return } else if result.RowsAffected == 0 { logger.Error("query component detail info from table is empty", zap.String("table_name", tableName)) return } uuid := modelParseConfig.ComponentInfo.GlobalUUID.String() configsStr, exist := unmarshalMap["anchor_configs_list"] if exist { anchorConfigsJSON, err := simplejson.NewJson([]byte(configsStr.(string))) if err != nil { logger.Error("formmat anchor configs json failed", zap.Error(err)) return } paramsList := anchorConfigsJSON.Get("params_list").MustArray() for index := range paramsList { anchorName := anchorConfigsJSON.Get("params_list").GetIndex(index).Get("upper_limit").MustString() anchorParam := config.AnchorParamConfig{ AnchorParamBaseConfig: config.AnchorParamBaseConfig{ StationID: modelParseConfig.ComponentInfo.StationID, ComponentID: strconv.FormatInt(modelParseConfig.ComponentInfo.ID, 10), UUID: uuid, AnchorName: anchorName, CompareValUpperLimit: anchorConfigsJSON.Get("params_list").GetIndex(index).Get("upper_limit").MustFloat64(), CompareValLowerLimit: anchorConfigsJSON.Get("params_list").GetIndex(index).Get("lower_limit").MustFloat64(), }, } anchorParam.CalculateFunc, anchorParam.CalculateParams = config.SelectAnchorCalculateFuncAndParams(modelParseConfig.ComponentInfo.ComponentType, anchorName, unmarshalMap) diagram.StoreAnchorValue(modelParseConfig.ComponentInfo.GlobalUUID.String(), anchorName) realtimedata.AnchorParamsChan <- anchorParam } } unmarshalMap["id"] = modelParseConfig.ComponentInfo.ID unmarshalMap["uuid"] = uuid unmarshalMap["uuid"] = modelParseConfig.ComponentInfo.Tag unmarshalMap["grid_id"] = modelParseConfig.ComponentInfo.GridID unmarshalMap["zone_id"] = modelParseConfig.ComponentInfo.ZoneID unmarshalMap["station_id"] = modelParseConfig.ComponentInfo.StationID unmarshalMap["page_id"] = modelParseConfig.ComponentInfo.PageID unmarshalMap["component_type"] = modelParseConfig.ComponentInfo.ComponentType unmarshalMap["name"] = modelParseConfig.ComponentInfo.Name unmarshalMap["context"] = modelParseConfig.ComponentInfo.Context unmarshalMap["op"] = modelParseConfig.ComponentInfo.Op unmarshalMap["ts"] = modelParseConfig.ComponentInfo.Ts diagram.StoreComponentMap(uuid, unmarshalMap) return }