optimize code of sql struct and measurement node recommend api

This commit is contained in:
douxu 2025-12-19 17:33:12 +08:00
parent 70bcb00062
commit 9499e579b3
15 changed files with 159 additions and 161 deletions

View File

@ -4,7 +4,6 @@ package database
import (
"context"
"fmt"
"strconv"
"time"
"modelRT/common/errcode"
@ -26,15 +25,15 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
}
component := orm.Component{
GlobalUUID: globalUUID,
GridTag: strconv.FormatInt(componentInfo.GridID, 10),
ZoneTag: strconv.FormatInt(componentInfo.ZoneID, 10),
StationTag: strconv.FormatInt(componentInfo.StationID, 10),
Tag: componentInfo.Tag,
Name: componentInfo.Name,
Context: componentInfo.Context,
Op: componentInfo.Op,
Ts: time.Now(),
GlobalUUID: globalUUID,
GridName: componentInfo.GridName,
ZoneName: componentInfo.ZoneName,
StationName: componentInfo.StationName,
Tag: componentInfo.Tag,
Name: componentInfo.Name,
Context: componentInfo.Context,
Op: componentInfo.Op,
Ts: time.Now(),
}
result := tx.WithContext(cancelCtx).Create(&component)

View File

@ -16,9 +16,9 @@ import (
// FillingShortTokenModel define filling short token model info
func FillingShortTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.ShortIdentityTokenModel) error {
filterComponent := &orm.Component{
GridTag: identModel.GetGridTag(),
ZoneTag: identModel.GetZoneTag(),
StationTag: identModel.GetStationTag(),
GridName: identModel.GetGridName(),
ZoneName: identModel.GetZoneName(),
StationName: identModel.GetStationName(),
}
component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent)
@ -34,10 +34,10 @@ func FillingShortTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.
// FillingLongTokenModel define filling long token model info
func FillingLongTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.LongIdentityTokenModel) error {
filterComponent := &orm.Component{
GridTag: identModel.GetGridTag(),
ZoneTag: identModel.GetZoneTag(),
StationTag: identModel.GetStationTag(),
Tag: identModel.GetComponentTag(),
GridName: identModel.GetGridName(),
ZoneName: identModel.GetZoneName(),
StationName: identModel.GetStationName(),
Tag: identModel.GetComponentTag(),
}
component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent)
if err != nil {

View File

@ -4,9 +4,8 @@ package database
import (
"context"
"fmt"
"time"
"modelRT/orm"
"time"
"github.com/gofrs/uuid"
"gorm.io/gorm"
@ -137,22 +136,3 @@ func QueryShortIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag s
}
return &resultComp, &meauserment, nil
}
// GetGlobalUUIDTagStringMap define func to query global_uuid、tag、nspath field
func GetGlobalUUIDTagStringMap(db *gorm.DB) (map[string]orm.AttributeSet, error) {
var results []orm.Component
resMap := make(map[string]orm.AttributeSet)
err := db.Model(&orm.Component{}).Select("global_uuid", "station", "tag", "nspath").Find(&results).Error
if err != nil {
return nil, err
}
for _, r := range results {
resMap[r.GlobalUUID.String()] = orm.AttributeSet{
Tag: r.Tag,
NSPath: r.NSPath,
}
}
return resMap, nil
}

View File

@ -0,0 +1,27 @@
// Package database define database operation functions
package database
import (
"modelRT/orm"
"gorm.io/gorm"
)
// GenAllAttributeMap define func to query global_uuid、component tag、component nspath field for attribute group
func GenAllAttributeMap(db *gorm.DB) (map[string]orm.AttributeSet, error) {
var compResults []orm.Component
resMap := make(map[string]orm.AttributeSet)
err := db.Model(&orm.Component{}).Select("global_uuid", "station_id", "tag", "nspath").Find(&compResults).Error
if err != nil {
return nil, err
}
for _, r := range compResults {
resMap[r.GlobalUUID.String()] = orm.AttributeSet{
CompTag: r.Tag,
CompNSPath: r.NSPath,
}
}
return resMap, nil
}

View File

@ -63,7 +63,8 @@ func QueryNodeInfoByID(ctx context.Context, tx *gorm.DB, id int64, level int) (o
currentNodeInfo = component
if err == nil {
var station orm.Station
err = queryFirstByTag(cancelCtx, tx, component.StationTag, &station)
// TODO 修改staion name为通过 station id 查询
err = queryFirstByTag(cancelCtx, tx, component.StationName, &station)
previousNodeInfo = station
}
case 5:

View File

@ -4,7 +4,6 @@ package database
import (
"context"
"fmt"
"strconv"
"time"
"modelRT/common/errcode"
@ -36,15 +35,15 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
}
updateParams := orm.Component{
GlobalUUID: globalUUID,
GridTag: strconv.FormatInt(componentInfo.GridID, 10),
ZoneTag: strconv.FormatInt(componentInfo.ZoneID, 10),
StationTag: strconv.FormatInt(componentInfo.StationID, 10),
Tag: componentInfo.Tag,
Name: componentInfo.Name,
Context: componentInfo.Context,
Op: componentInfo.Op,
Ts: time.Now(),
GlobalUUID: globalUUID,
GridName: componentInfo.GridName,
ZoneName: componentInfo.ZoneName,
StationName: componentInfo.StationName,
Tag: componentInfo.Tag,
Name: componentInfo.Name,
Context: componentInfo.Context,
Op: componentInfo.Op,
Ts: time.Now(),
}
result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("GLOBAL_UUID = ?", component.GlobalUUID).Updates(&updateParams)

View File

@ -2,12 +2,11 @@
package handler
import (
"net/http"
"modelRT/logger"
"modelRT/model"
"modelRT/network"
"modelRT/util"
"net/http"
"github.com/gin-gonic/gin"
)
@ -109,6 +108,7 @@ func MeasurementRecommendHandler(c *gin.Context) {
payloads = append(payloads, network.MeasurementRecommendPayload{
Input: request.Input,
Offset: finalOffset,
RecommendType: recommendResult.RecommendType.String(),
RecommendedList: resultRecommends,
})
}

42
main.go
View File

@ -7,25 +7,25 @@ import (
"flag"
"fmt"
"log"
"modelRT/alert"
"modelRT/config"
"modelRT/database"
"modelRT/diagram"
"modelRT/logger"
"modelRT/model"
"modelRT/pool"
"modelRT/router"
"modelRT/util"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"modelRT/alert"
"modelRT/config"
"modelRT/database"
"modelRT/diagram"
locker "modelRT/distributedlock"
_ "modelRT/docs"
"modelRT/handler"
"modelRT/logger"
"modelRT/model"
"modelRT/pool"
realtimedata "modelRT/real-time-data"
"modelRT/router"
"modelRT/util"
"github.com/gin-gonic/gin"
"github.com/panjf2000/ants/v2"
@ -172,28 +172,6 @@ func main() {
engine := gin.New()
router.RegisterRoutes(engine, serviceToken)
// real time data api
engine.GET("/ws/rtdatas", handler.RealTimeDataReceivehandler)
// anchor api
engine.POST("/model/anchor_replace", handler.ComponentAnchorReplaceHandler)
// alert api
engine.GET("/alert/events/query", handler.QueryAlertEventHandler)
// real time data api
engine.GET("/rt/datas/query", handler.QueryRealTimeDataHandler)
// dashborad api
dashboard := engine.Group("/dashboard")
{
dashboard.GET("/load", nil)
dashboard.GET("/query", nil)
dashboard.POST("/create", nil)
dashboard.POST("/update", nil)
dashboard.POST("/delete", nil)
}
// Swagger UI
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

View File

@ -5,11 +5,11 @@ import (
"context"
"errors"
"fmt"
"modelRT/constants"
"modelRT/diagram"
"modelRT/logger"
"modelRT/orm"
"github.com/gofrs/uuid"
"gorm.io/gorm"
)
@ -94,19 +94,31 @@ func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compParamMap
AttributeType: attributeType,
AttributeGroup: attributeGroup,
}
// TODO 将筛选后的 recordSchema 属性值加入到缓存系统中
// TODO 增加 station tag的获取
go storeAttributeGroup(ctx, attrSet, componentUUIDStr, columnParam)
}
}
return nil
}
func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, params columnParam) {
fmt.Println(ctx)
fmt.Println(attributeSet)
uuid, err := uuid.FromString(compUUIDStr)
fmt.Println(uuid, err)
fmt.Println(params)
func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, colParams columnParam) {
rdb := diagram.GetRedisClientInstance()
pipe := rdb.Pipeline()
// add token6 hierarchy content
pipe.SAdd(ctx, constants.RedisAllConfigSetKey, colParams.AttributeType)
// add token5-token7 hierarchy collaboration content
specCompMeasKey := fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, attributeSet.CompTag)
attrNameMembers := make([]string, 0, len(colParams.AttributeGroup))
for attrName, attrValue := range colParams.AttributeGroup {
// TODO 增加元件属性组表的列与对应值的缓存存储
fmt.Println(attrValue)
fmt.Println(compUUIDStr)
attrNameMembers = append(attrNameMembers, attrName)
}
pipe.SAdd(ctx, specCompMeasKey, attrNameMembers)
_, err := pipe.Exec(ctx)
if err != nil {
logger.Error(ctx, "init component attribute group recommend content failed", "error", err)
}
}

View File

@ -7,9 +7,9 @@ import "modelRT/orm"
type IndentityTokenModelInterface interface {
GetComponentInfo() *orm.Component
GetMeasurementInfo() *orm.Measurement
GetGridTag() string // token1
GetZoneTag() string // token2
GetStationTag() string // token3
GetGridName() string // token1
GetZoneName() string // token2
GetStationName() string // token3
GetNamespacePath() string // token4(COMPONENT TABLE NSPATH)
GetComponentTag() string // token5(COMPONENT TABLE TAG)
GetAttributeGroup() string // token6(component attribute group information)
@ -40,14 +40,14 @@ func (l *LongIdentityTokenModel) GetMeasurementInfo() *orm.Measurement {
return l.MeasurementInfo
}
// GetGridTag define function to return the grid tag information in the long identity token
func (l *LongIdentityTokenModel) GetGridTag() string { return l.GridTag }
// GetGridName define function to return the grid name information in the long identity token
func (l *LongIdentityTokenModel) GetGridName() string { return l.GridTag }
// GetZoneTag define function to return the zone tag information in the long identity token
func (l *LongIdentityTokenModel) GetZoneTag() string { return l.ZoneTag }
// GetZoneName define function to return the zone name information in the long identity token
func (l *LongIdentityTokenModel) GetZoneName() string { return l.ZoneTag }
// GetStationTag define function to return the station tag information in the long identity token
func (l *LongIdentityTokenModel) GetStationTag() string { return l.StationTag }
// GetStationName define function to return the station name information in the long identity token
func (l *LongIdentityTokenModel) GetStationName() string { return l.StationTag }
// GetNamespacePath define function to return the namespace path information in the long identity token
func (l *LongIdentityTokenModel) GetNamespacePath() string { return l.NamespacePath }
@ -90,14 +90,14 @@ func (s *ShortIdentityTokenModel) GetMeasurementInfo() *orm.Measurement {
return s.MeasurementInfo
}
// GetGridTag define function to return the grid tag information in the short identity token
func (s *ShortIdentityTokenModel) GetGridTag() string { return "" }
// GetGridName define function to return the grid name information in the short identity token
func (s *ShortIdentityTokenModel) GetGridName() string { return "" }
// GetZoneTag define function to return the zone tag information in the short identity token
func (s *ShortIdentityTokenModel) GetZoneTag() string { return "" }
// GetZoneName define function to return the zone name information in the short identity token
func (s *ShortIdentityTokenModel) GetZoneName() string { return "" }
// GetStationTag define function to return the station tag information in the short identity token
func (s *ShortIdentityTokenModel) GetStationTag() string { return "" }
// GetStationName define function to return the station name information in the short identity token
func (s *ShortIdentityTokenModel) GetStationName() string { return "" }
// GetNamespacePath define function to return the namespace path information in the short identity token
func (s *ShortIdentityTokenModel) GetNamespacePath() string { return s.NamespacePath }

View File

@ -25,30 +25,30 @@ type TopologicUUIDCreateInfo struct {
// ComponentCreateInfo defines circuit diagram component create info
type ComponentCreateInfo struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Context map[string]any `json:"context"`
GridID int64 `json:"grid_id"`
ZoneID int64 `json:"zone_id"`
StationID int64 `json:"station_id"`
PageID int64 `json:"page_id"`
Tag string `json:"tag"`
Params string `json:"params"`
Op int `json:"op"`
UUID string `json:"uuid"`
Name string `json:"name"`
GridName string `json:"grid_name"`
ZoneName string `json:"zone_name"`
StationName string `json:"station_name"`
Tag string `json:"tag"`
Params string `json:"params"`
PageID int64 `json:"page_id"`
Op int `json:"op"`
Context map[string]any `json:"context"`
}
// MeasurementCreateInfo defines circuit diagram measurement create info
type MeasurementCreateInfo struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Context map[string]any `json:"context"`
GridID int64 `json:"grid_id"`
ZoneID int64 `json:"zone_id"`
StationID int64 `json:"station_id"`
PageID int64 `json:"page_id"`
Tag string `json:"tag"`
Params string `json:"params"`
Op int `json:"op"`
UUID string `json:"uuid"`
Name string `json:"name"`
Context map[string]any `json:"context"`
GridName string `json:"grid_name"`
ZoneName string `json:"zone_name"`
StationName string `json:"station_name"`
PageID int64 `json:"page_id"`
Tag string `json:"tag"`
Params string `json:"params"`
Op int `json:"op"`
}
// CircuitDiagramCreateRequest defines request params of circuit diagram create api

View File

@ -36,16 +36,16 @@ type TopologicUUIDChangeInfos struct {
// ComponentUpdateInfo defines circuit diagram component params info
type ComponentUpdateInfo struct {
ID int64 `json:"id"`
UUID string `json:"uuid"`
Name string `json:"name"`
Context map[string]any `json:"context"`
GridID int64 `json:"grid_id"`
ZoneID int64 `json:"zone_id"`
StationID int64 `json:"station_id"`
Params string `json:"params"`
Op int `json:"op"`
Tag string `json:"tag"`
ID int64 `json:"id"`
UUID string `json:"uuid"`
Name string `json:"name"`
GridName string `json:"grid_name"`
ZoneName string `json:"zone_name"`
StationName string `json:"station_name"`
Params string `json:"params"`
Tag string `json:"tag"`
Op int `json:"op"`
Context map[string]any `json:"context"`
}
// CircuitDiagramUpdateRequest defines request params of circuit diagram update api

View File

@ -19,6 +19,7 @@ type SuccessResponse struct {
type MeasurementRecommendPayload struct {
Input string `json:"input" example:"transformfeeder1_220."`
Offset int `json:"offset" example:"21"`
RecommendType string `json:"recommended_type" example:"grid_tag"`
RecommendedList []string `json:"recommended_list" example:"[\"I_A_rms\", \"I_B_rms\",\"I_C_rms\"]"`
}

View File

@ -1,9 +1,9 @@
// Package orm define database data struct
package orm
// AttributeSet define struct to return station tag、component Tag、 NSPath field
// AttributeSet define struct to return component Tag、 NSPath field
type AttributeSet struct {
Tag string
NSPath string
StationTag string
CompTag string
CompNSPath string
// StationTag string
}

View File

@ -9,24 +9,25 @@ import (
// Component structure define abstracted info set of electrical component
type Component struct {
GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey"`
NSPath string `gorm:"column:nspath"`
Tag string `gorm:"column:tag"`
Name string `gorm:"column:name"`
ModelName string `gorm:"column:model_name"`
Description string `gorm:"column:description"`
GridTag string `gorm:"column:grid"`
ZoneTag string `gorm:"column:zone"`
StationTag string `gorm:"column:station"`
Type int `gorm:"column:type"`
InService bool `gorm:"column:in_service"`
State int `gorm:"column:state"`
Status int `gorm:"column:status"`
Connection JSONMap `gorm:"column:connection;type:jsonb;default:'{}'"`
Label JSONMap `gorm:"column:label;type:jsonb;default:'{}'"`
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
Op int `gorm:"column:op"`
Ts time.Time `gorm:"column:ts"`
GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey;default:gen_random_uuid()"`
NSPath string `gorm:"column:nspath;type:varchar(32);not null;default:''"`
Tag string `gorm:"column:tag;type:varchar(32);not null;uniqueIndex;default:''"`
Name string `gorm:"column:name;type:varchar(64);not null;default:''"`
ModelName string `gorm:"column:model_name;type:varchar(64);not null;default:''"`
Description string `gorm:"column:description;type:varchar(512);not null;default:''"`
GridName string `gorm:"column:grid;type:varchar(64);not null;default:''"`
ZoneName string `gorm:"column:zone;type:varchar(64);not null;default:''"`
StationName string `gorm:"column:station;type:varchar(64);not null;default:''"`
StationID int64 `gorm:"column:station_id;not null"`
Type int `gorm:"column:type;not null;default:-1"`
InService bool `gorm:"column:in_service;not null;default:false"`
State int `gorm:"column:state;not null;default:-1"`
Status int `gorm:"column:status;not null;default:-1"`
Connection JSONMap `gorm:"column:connection;type:jsonb;not null;default:'{}'"`
Label JSONMap `gorm:"column:label;type:jsonb;not null;default:'{}'"`
Context JSONMap `gorm:"column:context;type:jsonb;not null;default:'{}'"`
Op int `gorm:"column:op;not null;default:-1"`
Ts time.Time `gorm:"column:ts;type:timestamptz;not null;default:current_timestamp;autoCreateTime"`
}
// TableName func respresent return table name of Component