diff --git a/alert/init.go b/alert/init.go index e6e7ba3..96e7da0 100644 --- a/alert/init.go +++ b/alert/init.go @@ -5,7 +5,7 @@ import ( "sort" "sync" - constants "modelRT/constant" + "modelRT/constants" ) var ( diff --git a/config/anchor_param_config.go b/config/anchor_param_config.go index e0a9c15..7921518 100644 --- a/config/anchor_param_config.go +++ b/config/anchor_param_config.go @@ -2,7 +2,7 @@ package config import ( - constants "modelRT/constant" + "modelRT/constants" ) // AnchorParamListConfig define anchor params list config struct diff --git a/constant/alert.go b/constants/alert.go similarity index 100% rename from constant/alert.go rename to constants/alert.go diff --git a/constant/attrs_key.go b/constants/attrs_key.go similarity index 70% rename from constant/attrs_key.go rename to constants/attrs_key.go index 26206fe..2b22a9f 100644 --- a/constant/attrs_key.go +++ b/constants/attrs_key.go @@ -7,3 +7,5 @@ const ( // LongAttrKeyLenth define long attribute key length LongAttrKeyLenth int = 7 ) + +// component、base_extend、rated、setup、model、stable、bay、craft、integrity、behavior diff --git a/constant/busbar_section.go b/constants/busbar_section.go similarity index 100% rename from constant/busbar_section.go rename to constants/busbar_section.go diff --git a/constant/electrical_components.go b/constants/electrical_components.go similarity index 100% rename from constant/electrical_components.go rename to constants/electrical_components.go diff --git a/constant/error.go b/constants/error.go similarity index 100% rename from constant/error.go rename to constants/error.go diff --git a/constant/log_mode.go b/constants/log_mode.go similarity index 100% rename from constant/log_mode.go rename to constants/log_mode.go diff --git a/constant/time.go b/constants/time.go similarity index 100% rename from constant/time.go rename to constants/time.go diff --git a/constant/togologic.go b/constants/togologic.go similarity index 100% rename from constant/togologic.go rename to constants/togologic.go diff --git a/database/filling_attr_model_info.go b/database/filling_attr_model_info.go new file mode 100644 index 0000000..fb38669 --- /dev/null +++ b/database/filling_attr_model_info.go @@ -0,0 +1,79 @@ +// Package database define database operation functions +package database + +import ( + "context" + "errors" + "strings" + + "modelRT/model" + + "gorm.io/gorm" +) + +// ParseAttrToken define return the attribute model interface based on the input attribute token +func ParseAttrToken(ctx context.Context, tx *gorm.DB, attrToken string) (model.AttrModelInterface, error) { + attrSlice := strings.Split(attrToken, ".") + attrLen := len(attrSlice) + if attrLen == 4 { + short := &model.ShortAttrInfo{ + AttrGroupName: attrSlice[2], + AttrKey: attrSlice[3], + // TODO use redis query + // AttrValue: attrSlice[2], + } + err := FillingShortAttrModel(ctx, tx, attrSlice, short) + if err != nil { + return nil, err + } + return short, nil + } else if attrLen == 7 { + long := &model.LongAttrInfo{ + AttrGroupName: attrSlice[5], + AttrKey: attrSlice[6], + // TODO use redis query + // AttrValue: attrSlice[5], + } + err := FillingLongAttrModel(ctx, tx, attrSlice, long) + if err != nil { + return nil, err + } + return long, nil + } + return nil, errors.New("invalid attribute token format") +} + +// FillingShortAttrModel define filling short attribute model info +func FillingShortAttrModel(ctx context.Context, tx *gorm.DB, attrItems []string, attrModel *model.ShortAttrInfo) error { + component, err := QueryComponentByNsPath(ctx, tx, attrItems[0]) + if err != nil { + return err + } + attrModel.ComponentInfo = &component + return nil +} + +// FillingLongAttrModel define filling long attribute model info +func FillingLongAttrModel(ctx context.Context, tx *gorm.DB, attrItems []string, attrModel *model.LongAttrInfo) error { + grid, err := QueryGridByName(ctx, tx, attrItems[0]) + if err != nil { + return err + } + attrModel.GridInfo = &grid + zone, err := QueryZoneByName(ctx, tx, attrItems[1]) + if err != nil { + return err + } + attrModel.ZoneInfo = &zone + station, err := QueryStationByName(ctx, tx, attrItems[2]) + if err != nil { + return err + } + attrModel.StationInfo = &station + component, err := QueryComponentByNsPath(ctx, tx, attrItems[3]) + if err != nil { + return err + } + attrModel.ComponentInfo = &component + return nil +} diff --git a/database/query_component.go b/database/query_component.go index 4c8245f..785b8c6 100644 --- a/database/query_component.go +++ b/database/query_component.go @@ -69,3 +69,17 @@ func QueryComponentByPageID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (o } return component, nil } + +// QueryComponentByNsPath return the result of query circuit diagram component info by ns path from postgresDB +func QueryComponentByNsPath(ctx context.Context, tx *gorm.DB, nsPath string) (orm.Component, error) { + var component orm.Component + // ctx超时判断 + cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + + result := tx.WithContext(cancelCtx).Where("NAME = ? ", nsPath).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&component) + if result.Error != nil { + return orm.Component{}, result.Error + } + return component, nil +} diff --git a/database/query_grid.go b/database/query_grid.go new file mode 100644 index 0000000..8038327 --- /dev/null +++ b/database/query_grid.go @@ -0,0 +1,26 @@ +// Package database define database operation functions +package database + +import ( + "context" + "time" + + "modelRT/orm" + + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +// QueryGridByName return the result of query circuit diagram grid info by name from postgresDB +func QueryGridByName(ctx context.Context, tx *gorm.DB, name string) (orm.Grid, error) { + var grid orm.Grid + // ctx超时判断 + cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + + result := tx.WithContext(cancelCtx).Where("NAME = ? ", name).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&grid) + if result.Error != nil { + return orm.Grid{}, result.Error + } + return grid, nil +} diff --git a/database/query_station.go b/database/query_station.go new file mode 100644 index 0000000..f7a2e63 --- /dev/null +++ b/database/query_station.go @@ -0,0 +1,26 @@ +// Package database define database operation functions +package database + +import ( + "context" + "time" + + "modelRT/orm" + + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +// QueryStationByName return the result of query circuit diagram Station info by name from postgresDB +func QueryStationByName(ctx context.Context, tx *gorm.DB, name string) (orm.Station, error) { + var station orm.Station + // ctx超时判断 + cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + + result := tx.WithContext(cancelCtx).Where("NAME = ? ", name).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&station) + if result.Error != nil { + return orm.Station{}, result.Error + } + return station, nil +} diff --git a/database/query_topologic.go b/database/query_topologic.go index 9a72426..35bb92d 100644 --- a/database/query_topologic.go +++ b/database/query_topologic.go @@ -6,7 +6,7 @@ import ( "fmt" "time" - constants "modelRT/constant" + "modelRT/constants" "modelRT/diagram" "modelRT/logger" "modelRT/orm" diff --git a/database/query_zone.go b/database/query_zone.go new file mode 100644 index 0000000..7779273 --- /dev/null +++ b/database/query_zone.go @@ -0,0 +1,26 @@ +// Package database define database operation functions +package database + +import ( + "context" + "time" + + "modelRT/orm" + + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +// QueryZoneByName return the result of query circuit diagram Zone info by name from postgresDB +func QueryZoneByName(ctx context.Context, tx *gorm.DB, name string) (orm.Zone, error) { + var zone orm.Zone + // ctx超时判断 + cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + + result := tx.WithContext(cancelCtx).Where("NAME = ? ", name).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&zone) + if result.Error != nil { + return orm.Zone{}, result.Error + } + return zone, nil +} diff --git a/database/update_topologic.go b/database/update_topologic.go index 009b3ce..7348a52 100644 --- a/database/update_topologic.go +++ b/database/update_topologic.go @@ -7,7 +7,7 @@ import ( "time" "modelRT/common/errcode" - constants "modelRT/constant" + "modelRT/constants" "modelRT/network" "modelRT/orm" diff --git a/diagram/graph.go b/diagram/graph.go index e540e33..1d9ee6e 100644 --- a/diagram/graph.go +++ b/diagram/graph.go @@ -5,7 +5,7 @@ import ( "fmt" "sync" - constants "modelRT/constant" + "modelRT/constants" "modelRT/network" "github.com/gofrs/uuid" diff --git a/handler/alert_event_query.go b/handler/alert_event_query.go index 1ddadc6..2365a1b 100644 --- a/handler/alert_event_query.go +++ b/handler/alert_event_query.go @@ -6,7 +6,7 @@ import ( "strconv" "modelRT/alert" - constants "modelRT/constant" + "modelRT/constants" "modelRT/logger" "modelRT/network" diff --git a/handler/anchor_point_replace.go b/handler/anchor_point_replace.go index 2b3956e..7fd4e94 100644 --- a/handler/anchor_point_replace.go +++ b/handler/anchor_point_replace.go @@ -8,7 +8,7 @@ import ( "time" "modelRT/common/errcode" - constants "modelRT/constant" + "modelRT/constants" "modelRT/database" "modelRT/diagram" "modelRT/logger" diff --git a/handler/real_time_data_query.go b/handler/real_time_data_query.go index 5c4ed3c..5152b8b 100644 --- a/handler/real_time_data_query.go +++ b/handler/real_time_data_query.go @@ -6,7 +6,7 @@ import ( "strconv" "modelRT/alert" - constants "modelRT/constant" + "modelRT/constants" "modelRT/logger" "modelRT/network" diff --git a/logger/zap.go b/logger/zap.go index f46c315..da03adb 100644 --- a/logger/zap.go +++ b/logger/zap.go @@ -6,7 +6,7 @@ import ( "sync" "modelRT/config" - constants "modelRT/constant" + "modelRT/constants" "github.com/natefinch/lumberjack" "go.uber.org/zap" diff --git a/model/attribute_model.go b/model/attribute_model.go index 853d718..8a08df0 100644 --- a/model/attribute_model.go +++ b/model/attribute_model.go @@ -1,7 +1,9 @@ // Package model define model struct of model runtime service package model -import "modelRT/orm" +import ( + "modelRT/orm" +) // AttrModelInterface define basic attr model type interface type AttrModelInterface interface { @@ -14,10 +16,69 @@ type AttrModelInterface interface { // LongAttrInfo structure define long attribute key info of component type LongAttrInfo struct { - AttrKey string `json:"attr_key"` + AttrGroupName string + AttrKey string + AttrValue interface{} + GridInfo *orm.Grid + ZoneInfo *orm.Zone + StationInfo *orm.Station + ComponentInfo *orm.Component +} + +// GetGridInfo define return the grid information in the long attribute +func (l *LongAttrInfo) GetGridInfo() *orm.Grid { + return l.GridInfo +} + +// GetZoneInfo define return the zone information in the long attribute +func (l *LongAttrInfo) GetZoneInfo() *orm.Zone { + return l.ZoneInfo +} + +// GetStationInfo define return the station information in the long attribute +func (l *LongAttrInfo) GetStationInfo() *orm.Station { + return l.StationInfo +} + +// GetComponentInfo define return the component information in the long attribute +func (l *LongAttrInfo) GetComponentInfo() *orm.Component { + return l.ComponentInfo +} + +// IsLocal define return the is_local information in the long attribute +func (l *LongAttrInfo) IsLocal() bool { + return true } // ShortAttrInfo structure define short attribute key info of component type ShortAttrInfo struct { - AttrKey string `json:"attr_key"` + AttrGroupName string + AttrKey string + AttrValue interface{} + ComponentInfo *orm.Component +} + +// GetGridInfo define return the grid information in the short attribute +func (s *ShortAttrInfo) GetGridInfo() *orm.Grid { + return nil +} + +// GetZoneInfo define return the zone information in the short attribute +func (s *ShortAttrInfo) GetZoneInfo() *orm.Zone { + return nil +} + +// GetStationInfo define return the station information in the short attribute +func (s *ShortAttrInfo) GetStationInfo() *orm.Station { + return nil +} + +// GetComponentInfo define return the component information in the short attribute +func (s *ShortAttrInfo) GetComponentInfo() *orm.Component { + return s.ComponentInfo +} + +// IsLocal define return the is_local information in the short attribute +func (s *ShortAttrInfo) IsLocal() bool { + return false } diff --git a/model/model_select.go b/model/model_select.go index d6eb7ef..042a867 100644 --- a/model/model_select.go +++ b/model/model_select.go @@ -1,8 +1,7 @@ -// Package model define model struct of model runtime service package model import ( - constants "modelRT/constant" + "modelRT/constants" "modelRT/orm" ) diff --git a/network/circuit_diagram_update_request.go b/network/circuit_diagram_update_request.go index 1a6804c..db55852 100644 --- a/network/circuit_diagram_update_request.go +++ b/network/circuit_diagram_update_request.go @@ -5,7 +5,7 @@ import ( "fmt" "modelRT/common/errcode" - constants "modelRT/constant" + "modelRT/constants" "github.com/gofrs/uuid" ) diff --git a/pool/concurrency_anchor_parse.go b/pool/concurrency_anchor_parse.go index 59411e3..a73bb0b 100644 --- a/pool/concurrency_anchor_parse.go +++ b/pool/concurrency_anchor_parse.go @@ -7,7 +7,7 @@ import ( "modelRT/alert" "modelRT/config" - constants "modelRT/constant" + "modelRT/constants" "modelRT/diagram" "modelRT/logger" diff --git a/real-time-data/real_time_data_receive.go b/real-time-data/real_time_data_receive.go index 61bc2f5..205d63d 100644 --- a/real-time-data/real_time_data_receive.go +++ b/real-time-data/real_time_data_receive.go @@ -5,7 +5,7 @@ import ( "context" "modelRT/config" - constants "modelRT/constant" + "modelRT/constants" "modelRT/diagram" "modelRT/logger" "modelRT/network"