diff --git a/modelRT/config/model_config.go b/modelRT/config/model_config.go index 97582fa..6e775a4 100644 --- a/modelRT/config/model_config.go +++ b/modelRT/config/model_config.go @@ -1,9 +1,12 @@ package config import ( + "context" + "modelRT/orm" ) type ModelParseConfig struct { - CircuitDiagramInfos []orm.CircuitDiagram + ComponentInfo orm.Component + Context context.Context } diff --git a/modelRT/database/posgres_operator.go b/modelRT/database/posgres_operator.go index 6b966bc..af01b8d 100644 --- a/modelRT/database/posgres_operator.go +++ b/modelRT/database/posgres_operator.go @@ -3,41 +3,63 @@ package database import ( "context" + "fmt" "strconv" "time" "modelRT/config" + "modelRT/diagram" "modelRT/orm" "github.com/panjf2000/ants/v2" "go.uber.org/zap" + "gorm.io/gorm/clause" ) -// LoadCircuitDiagramFromPostgresDB return the result of query circuit diagram info from postgresDB -func LoadCircuitDiagramFromPostgresDB(ctx context.Context, pool *ants.PoolWithFunc, logger *zap.Logger) error { - var circuitDiagramOverviews []orm.CircuitDiagramOverview +// QueryCircuitDiagramComponentFromDB return the result of query circuit diagram component info order by page id from postgresDB +func QueryCircuitDiagramComponentFromDB(ctx context.Context, pool *ants.PoolWithFunc, logger *zap.Logger) error { + var Components []orm.Component // ctx超时判断 cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() - result := _globalPostgresClient.WithContext(cancelCtx).Find(&circuitDiagramOverviews) + result := _globalPostgresClient.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&Components) + if result.Error != nil { + logger.Error("query circuit diagram component info failed", zap.Error(result.Error)) + return result.Error + } + + for _, component := range Components { + fmt.Println(component) + pool.Invoke(config.ModelParseConfig{ + ComponentInfo: component, + Context: ctx, + }) + } + // TODO 加载 Topologic表拓扑关系 + return nil +} + +// QueryElectricalEquipmentUUID return the result of query electrical equipment uuid from postgresDB by circuit diagram id info +func QueryElectricalEquipmentUUID(ctx context.Context, diagramID int64, logger *zap.Logger) error { + var uuids []string + // ctx超时判断 + cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + tableName := "circuit_diagram_" + strconv.FormatInt(diagramID, 10) + result := _globalPostgresClient.Table(tableName).WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Select("uuid").Find(&uuids) if result.Error != nil { logger.Error("query circuit diagram overview info failed", zap.Error(result.Error)) return result.Error } - for _, overview := range circuitDiagramOverviews { - cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) - defer cancel() - var circuitDiagramInfos []orm.CircuitDiagram - tableName := "circuit_diagram_" + strconv.FormatInt(overview.ID, 10) - result := _globalPostgresClient.Table(tableName).WithContext(cancelCtx).Order("id asc").Find(&circuitDiagramInfos) - if result.Error != nil { - logger.Error("query circuit diagram detail infos failed", zap.Error(result.Error)) + for _, uuid := range uuids { + diagramParamsMap, err := diagram.GetComponentMap(uuid) + if err != nil { + logger.Error("get electrical circuit diagram overview info failed", zap.Error(result.Error)) return result.Error } - pool.Invoke(config.ModelParseConfig{ - CircuitDiagramInfos: circuitDiagramInfos, - }) + fmt.Println(diagramParamsMap, err) } + return nil } diff --git a/modelRT/diagram/diagram_overview_map.go b/modelRT/diagram/diagram_overview_map.go index cece378..1c6fe9b 100644 --- a/modelRT/diagram/diagram_overview_map.go +++ b/modelRT/diagram/diagram_overview_map.go @@ -9,7 +9,7 @@ import ( var DiagramsOverview sync.Map -func GetCircuitDiagramMap(key string) (*cmap.ConcurrentMap[string, any], error) { +func GetComponentMap(key string) (*cmap.ConcurrentMap[string, any], error) { value, ok := DiagramsOverview.Load(key) if !ok { newMap := cmap.New[any]() diff --git a/modelRT/go.mod b/modelRT/go.mod index ea1039a..8ef313f 100644 --- a/modelRT/go.mod +++ b/modelRT/go.mod @@ -5,6 +5,7 @@ go 1.22.5 require ( github.com/gin-gonic/gin v1.10.0 github.com/gofrs/uuid v4.4.0+incompatible + github.com/google/uuid v1.4.0 github.com/json-iterator/go v1.1.12 github.com/natefinch/lumberjack v2.0.0+incompatible github.com/orcaman/concurrent-map/v2 v2.0.1 diff --git a/modelRT/go.sum b/modelRT/go.sum index a0ebc3a..212d76d 100644 --- a/modelRT/go.sum +++ b/modelRT/go.sum @@ -37,6 +37,8 @@ github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRx github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= diff --git a/modelRT/main.go b/modelRT/main.go index 2523e2f..f8b615c 100644 --- a/modelRT/main.go +++ b/modelRT/main.go @@ -57,6 +57,7 @@ func main() { // init logger logger = log.GetLoggerInstance(modelRTConfig.LCfg) defer logger.Sync() + // init ants pool pool, err := ants.NewPoolWithFunc(modelRTConfig.ParseConcurrentQuantity, model.ParseFunc) if err != nil { @@ -66,7 +67,7 @@ func main() { defer ants.Release() // load circuit diagram from postgres - err = database.LoadCircuitDiagramFromPostgresDB(ctx, pool, logger) + err = database.QueryCircuitDiagramComponentFromDB(ctx, pool, logger) if err != nil { logger.Error("load circuit diagrams from postgres failed", zap.Error(err)) panic(err) diff --git a/modelRT/model/model_parse.go b/modelRT/model/model_parse.go index f354e17..0cb9bad 100644 --- a/modelRT/model/model_parse.go +++ b/modelRT/model/model_parse.go @@ -1,13 +1,14 @@ package model import ( + "context" "errors" + "time" "modelRT/config" + "modelRT/database" "modelRT/diagram" - jsoniter "github.com/json-iterator/go" - "go.uber.org/zap" ) @@ -20,24 +21,35 @@ var ParseFunc = func(parseConfig interface{}) { panic(errors.New("conversion model parse config type failed")) } - for _, circuitDiagram := range modelParseConfig.CircuitDiagramInfos { - overviewKey := circuitDiagram.UUID - unmarshalMap, err := diagram.GetCircuitDiagramMap(overviewKey) - if err != nil { - logger.Error("get circuit diagram map from overviewMap failed", zap.String("circuit_diagram_overview_key", overviewKey), zap.Error(err)) - panic(err) - } - - err = jsoniter.Unmarshal([]byte(circuitDiagram.OtherParams), unmarshalMap) - if err != nil { - logger.Error("parsing circuit diagram params failed", zap.Int64("circuit_diagram_id", circuitDiagram.ID), zap.Error(err)) - panic(err) - } - unmarshalMap.Set("id", circuitDiagram.ID) - unmarshalMap.Set("parent_id", circuitDiagram.ParentID) - unmarshalMap.Set("type", circuitDiagram.Type) - unmarshalMap.Set("created_time", circuitDiagram.CreatedTime) - unmarshalMap.Set("updated_time", circuitDiagram.UpdateTime) + cancelCtx, cancel := context.WithTimeout(modelParseConfig.Context, 5*time.Second) + defer cancel() + pgClient := database.PostgresDBClient() + componentKey := modelParseConfig.ComponentInfo.GlobalUUID.String() + unmarshalMap, err := diagram.GetComponentMap(componentKey) + if err != nil { + logger.Error("get component map from overviewMap failed", zap.String("component_key", componentKey), zap.Error(err)) + panic(err) } + + tableName := SelectModelByType(modelParseConfig.ComponentInfo.ComponentType) + result := pgClient.Table(tableName).WithContext(cancelCtx).Find(&unmarshalMap) + if result.Error != nil { + logger.Error("query component detail info failed", zap.Error(result.Error)) + } else if result.RowsAffected == 0 { + logger.Error("query component detail info from table is empty", zap.String("table_name", tableName)) + } + unmarshalMap.Set("id", modelParseConfig.ComponentInfo.ID) + unmarshalMap.Set("created_time", modelParseConfig.ComponentInfo.VisibleID) + unmarshalMap.Set("parent_id", modelParseConfig.ComponentInfo.GridID) + unmarshalMap.Set("type", modelParseConfig.ComponentInfo.ZoneID) + unmarshalMap.Set("created_time", modelParseConfig.ComponentInfo.StationID) + unmarshalMap.Set("updated_time", modelParseConfig.ComponentInfo.ComponentType) + unmarshalMap.Set("id", modelParseConfig.ComponentInfo.State) + unmarshalMap.Set("parent_id", modelParseConfig.ComponentInfo.ConnectedBus) + unmarshalMap.Set("type", modelParseConfig.ComponentInfo.Name) + unmarshalMap.Set("updated_time", modelParseConfig.ComponentInfo.Description) + unmarshalMap.Set("id", modelParseConfig.ComponentInfo.Context) + unmarshalMap.Set("parent_id", modelParseConfig.ComponentInfo.Comment) + unmarshalMap.Set("type", modelParseConfig.ComponentInfo.InService) return } diff --git a/modelRT/model/model_select.go b/modelRT/model/model_select.go index b4499f7..1b454f8 100644 --- a/modelRT/model/model_select.go +++ b/modelRT/model/model_select.go @@ -2,9 +2,9 @@ package model import "modelRT/constant" -func SelectModelByType(modelType int) any { +func SelectModelByType(modelType int) string { if modelType == constant.BusbarType { - return BusbarSection{} + return "BusBarSection" } - return nil + return "" } diff --git a/modelRT/orm/circuit_diagram_component.go b/modelRT/orm/circuit_diagram_component.go new file mode 100644 index 0000000..93b6eb4 --- /dev/null +++ b/modelRT/orm/circuit_diagram_component.go @@ -0,0 +1,29 @@ +// Package orm define database data struct +package orm + +import ( + "github.com/google/uuid" +) + +// Component structure define abstracted info set of electrical component +type Component struct { + ID int64 `gorm:"column:id"` + GlobalUUID uuid.UUID `gorm:"column:global_uuid"` + GridID int64 `gorm:"column:grid"` + ZoneID int64 `gorm:"column:zone"` + StationID int64 `gorm:"column:station"` + ComponentType int `gorm:"column:type"` + State int `gorm:"column:state"` + ConnectedBus int `gorm:"column:connected_bus"` + Name string `gorm:"column:name"` + VisibleID string `gorm:"column:visible_id"` + Description string `gorm:"column:description"` + Context string `gorm:"column:context"` + Comment string `gorm:"column:comment"` + InService bool `gorm:"column:in_service"` +} + +// TableName func respresent return table name of circuit diagram overview +func (c *Component) TableName() string { + return "Component" +} diff --git a/modelRT/orm/circuit_diagram_page.go b/modelRT/orm/circuit_diagram_page.go new file mode 100644 index 0000000..d20a25b --- /dev/null +++ b/modelRT/orm/circuit_diagram_page.go @@ -0,0 +1,16 @@ +// Package orm define database data struct +package orm + +import "time" + +// Page structure define circuit diagram page info set +type Page struct { + ID int64 `gorm:"column:id"` + StationID int64 `gorm:"column:station_id"` + Status int `gorm:"column:status"` + Name string `gorm:"column:name"` + Owner string `gorm:"column:owner"` + Comment string `gorm:"column:comment"` + Context string `gorm:"column:context"` + TSModified time.Time `gorm:"column:ts_modified"` +} diff --git a/modelRT/pg.sql b/modelRT/pg.sql index 9e57930..53c0fe0 100644 --- a/modelRT/pg.sql +++ b/modelRT/pg.sql @@ -1,24 +1,44 @@ -CREATE DATABASE circuit_diagram; +CREATE TABLE + public."Page" ( + id serial NOT NULL, + station_id integer NULL, + name character varying(512) NOT NULL, + owner character varying(256) NOT NULL, + ts_modified timestamp with time zone NOT NULL, + context jsonb NULL + ); + +ALTER TABLE + public."Page" +ADD + CONSTRAINT page_pkey PRIMARY KEY (id) + + +CREATE TABLE + public."Component" ( + id serial NOT NULL, + global_uuid uuid NOT NULL, + visible_id character varying(1024) NOT NULL, + name character varying(1024) NOT NULL, + description character varying(4096) NULL, + grid character varying(512) NULL, + zone character varying(512) NULL, + station character varying(512) NULL, + type integer NULL, + in_service boolean NULL DEFAULT false, + state integer NULL DEFAULT 0, + connected_bus integer NULL, + context jsonb NULL, + comment character varying(4096) NULL + ); + +ALTER TABLE + public."Component" +ADD + CONSTRAINT "Component_pkey" PRIMARY KEY (global_uuid) -CREATE TABLE circuit_diagram_overview -( -id bigserial not null, -name text not null, -created_at timestamp with time zone default CURRENT_TIMESTAMP not null, -updated_at timestamp with time zone default CURRENT_TIMESTAMP not null -); - -CREATE TABLE circuit_diagram_1 -( -id bigserial not null, -name text not null, -created_at timestamp with time zone default CURRENT_TIMESTAMP not null, -updated_at timestamp with time zone default CURRENT_TIMESTAMP not null, -parent_id bigint default 0 not null, -other_params text not null -); INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(1,'母线','2024-11-07 09:37:00','2024-11-07 09:37:00',0,'voltage:35,status:0');