// 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("global_uuid = ?", modelParseConfig.ComponentInfo.GlobalUUID).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: strconv.FormatInt(modelParseConfig.ComponentInfo.StationID, 10), 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["created_time"] = modelParseConfig.ComponentInfo.VisibleID unmarshalMap["parent_id"] = modelParseConfig.ComponentInfo.GridID unmarshalMap["type"] = modelParseConfig.ComponentInfo.ZoneID unmarshalMap["created_time"] = modelParseConfig.ComponentInfo.StationID unmarshalMap["updated_time"] = modelParseConfig.ComponentInfo.ComponentType unmarshalMap["id"] = modelParseConfig.ComponentInfo.State unmarshalMap["parent_id"] = modelParseConfig.ComponentInfo.ConnectedBus unmarshalMap["type"] = modelParseConfig.ComponentInfo.Name unmarshalMap["updated_time"] = modelParseConfig.ComponentInfo.Description unmarshalMap["id"] = modelParseConfig.ComponentInfo.Context unmarshalMap["parent_id"] = modelParseConfig.ComponentInfo.Comment unmarshalMap["type"] = modelParseConfig.ComponentInfo.InService diagram.StoreComponentMap(uuid, unmarshalMap) return }