From 51f65500f38da5e2af7a154cdbdd1a47c826bc5f Mon Sep 17 00:00:00 2001 From: douxu Date: Wed, 24 Dec 2025 16:55:55 +0800 Subject: [PATCH] add func of init component measurement recommend --- constants/recommend_keys.go | 4 +- ...t_attr.go => query_component_attribute.go} | 0 database/query_component_measurement.go | 99 +++++++++++++++++++ handler/diagram_node_link.go | 2 +- model/measurement_group_model.go | 63 ++++++++++++ ...t_model.go => measurement_protol_model.go} | 0 model/redis_recommend.go | 2 +- orm/{attribute_group.go => attribute_set.go} | 2 +- orm/measurement_set.go | 18 ++++ 9 files changed, 185 insertions(+), 5 deletions(-) rename database/{query_component_attr.go => query_component_attribute.go} (100%) create mode 100644 database/query_component_measurement.go create mode 100644 model/measurement_group_model.go rename model/{measurement_model.go => measurement_protol_model.go} (100%) rename orm/{attribute_group.go => attribute_set.go} (64%) create mode 100644 orm/measurement_set.go diff --git a/constants/recommend_keys.go b/constants/recommend_keys.go index 39a59c4..0f0c4a1 100644 --- a/constants/recommend_keys.go +++ b/constants/recommend_keys.go @@ -32,8 +32,8 @@ const ( // RedisSpecStationCompNSPATHSetKey define redis set key which store all component nspath keys under specific station RedisSpecStationCompNSPATHSetKey = "%s_component_nspath_keys" - // RedisSpecStationCompTagSetKey define redis set key which store all component tag keys under specific station - RedisSpecStationCompTagSetKey = "%s_component_tag_keys" + // RedisSpecCompNSPathCompTagSetKey define redis set key which store all component tag keys under specific component nspath + RedisSpecCompNSPathCompTagSetKey = "%s_component_tag_keys" // RedisSpecCompTagMeasSetKey define redis set key which store all measurement tag keys under specific component tag RedisSpecCompTagMeasSetKey = "%s_measurement_tag_keys" diff --git a/database/query_component_attr.go b/database/query_component_attribute.go similarity index 100% rename from database/query_component_attr.go rename to database/query_component_attribute.go diff --git a/database/query_component_measurement.go b/database/query_component_measurement.go new file mode 100644 index 0000000..fd327f4 --- /dev/null +++ b/database/query_component_measurement.go @@ -0,0 +1,99 @@ +// Package database define database operation functions +package database + +import ( + "modelRT/orm" + + "gorm.io/gorm" +) + +type ZoneWithParent struct { + orm.Zone + GridTag string `gorm:"column:grid_tag"` +} + +type StationWithParent struct { + orm.Zone + ZoneTag string `gorm:"column:zone_tag"` +} + +func GetFullMeasurementSet(db *gorm.DB) (*orm.MeasurementSet, error) { + mSet := &orm.MeasurementSet{ + GridToZoneTags: make(map[string][]string), + ZoneToStationTags: make(map[string][]string), + StationToCompNSPaths: make(map[string][]string), + CompNSPathToCompTags: make(map[string][]string), + CompTagToMeasTags: make(map[string][]string), + } + + var zones []struct { + orm.Zone + GridTag string `gorm:"column:grid_tag"` + } + if err := db.Table("zone"). + Select("zone.*, grid.tagname as grid_tag"). + Joins("left join grid on zone.grid_id = grid.id"). + Scan(&zones).Error; err == nil { + for _, z := range zones { + mSet.AllZoneTags = append(mSet.AllZoneTags, z.TAGNAME) + if z.GridTag != "" { + mSet.GridToZoneTags[z.GridTag] = append(mSet.GridToZoneTags[z.GridTag], z.TAGNAME) + } + } + } + + var stations []struct { + orm.Station + ZoneTag string `gorm:"column:zone_tag"` + } + if err := db.Table("station"). + Select("station.*, zone.tagname as zone_tag"). + Joins("left join zone on station.zone_id = zone.id"). + Scan(&stations).Error; err == nil { + for _, s := range stations { + mSet.AllStationTags = append(mSet.AllStationTags, s.TAGNAME) + if s.ZoneTag != "" { + mSet.ZoneToStationTags[s.ZoneTag] = append(mSet.ZoneToStationTags[s.ZoneTag], s.TAGNAME) + } + } + } + + var comps []struct { + orm.Component + StationTag string `gorm:"column:station_tag"` + } + if err := db.Table("component"). + Select("component.*, station.tagname as station_tag"). + Joins("left join station on component.station_id = station.id"). + Scan(&comps).Error; err == nil { + for _, c := range comps { + mSet.AllCompNSPaths = append(mSet.AllCompNSPaths, c.NSPath) + mSet.AllCompTags = append(mSet.AllCompTags, c.Tag) + + if c.StationTag != "" { + mSet.StationToCompNSPaths[c.StationTag] = append(mSet.StationToCompNSPaths[c.StationTag], c.NSPath) + } + + if c.NSPath != "" { + mSet.CompNSPathToCompTags[c.NSPath] = append(mSet.CompNSPathToCompTags[c.NSPath], c.Tag) + } + } + } + + var measurements []struct { + orm.Measurement + CompTag string `gorm:"column:comp_tag"` + } + if err := db.Table("measurement"). + Select("measurement.*, component.tag as comp_tag"). + Joins("left join component on measurement.component_uuid = component.global_uuid"). + Scan(&measurements).Error; err == nil { + for _, m := range measurements { + mSet.AllMeasTags = append(mSet.AllMeasTags, m.Tag) + if m.CompTag != "" { + mSet.CompTagToMeasTags[m.CompTag] = append(mSet.CompTagToMeasTags[m.CompTag], m.Tag) + } + } + } + return mSet, nil +} diff --git a/handler/diagram_node_link.go b/handler/diagram_node_link.go index 0c2bd5c..80850e7 100644 --- a/handler/diagram_node_link.go +++ b/handler/diagram_node_link.go @@ -33,7 +33,7 @@ var linkSetConfigs = map[int]linkSetConfig{ // component nspath hierarchy 3: {CurrKey: constants.RedisAllCompNSPathSetKey, PrevKeyTemplate: constants.RedisSpecStationCompNSPATHSetKey}, // component tag hierarchy - 4: {CurrKey: constants.RedisAllCompTagSetKey, PrevKeyTemplate: constants.RedisSpecStationCompTagSetKey}, + 4: {CurrKey: constants.RedisAllCompTagSetKey, PrevKeyTemplate: constants.RedisSpecCompNSPathCompTagSetKey}, // config hierarchy 5: {CurrKey: constants.RedisAllConfigSetKey, PrevIsNil: true}, } diff --git a/model/measurement_group_model.go b/model/measurement_group_model.go new file mode 100644 index 0000000..a6f1cdb --- /dev/null +++ b/model/measurement_group_model.go @@ -0,0 +1,63 @@ +// Package model define model struct of model runtime service +package model + +import ( + "context" + "fmt" + + "modelRT/constants" + "modelRT/diagram" + "modelRT/logger" + "modelRT/orm" +) + +// TraverseMeasurementGroupTables define func to traverse component measurement group tables +func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.MeasurementSet) error { + rdb := diagram.GetRedisClientInstance() + pipe := rdb.Pipeline() + + pipe.SAdd(ctx, constants.RedisAllGridSetKey, measSet.AllGridTags) + pipe.SAdd(ctx, constants.RedisAllZoneSetKey, measSet.AllZoneTags) + pipe.SAdd(ctx, constants.RedisAllStationSetKey, measSet.AllStationTags) + pipe.SAdd(ctx, constants.RedisAllGridSetKey, measSet.AllCompNSPaths) + pipe.SAdd(ctx, constants.RedisAllZoneSetKey, measSet.AllCompTags) + pipe.SAdd(ctx, constants.RedisAllConfigSetKey, "bay") + pipe.SAdd(ctx, constants.RedisAllStationSetKey, measSet.AllMeasTags) + + // building the grid -> zones hierarchy + for gridTag, zoneTags := range measSet.GridToZoneTags { + key := fmt.Sprintf(constants.RedisSpecGridZoneSetKey, gridTag) + pipe.SAdd(ctx, key, zoneTags) + } + + // building the zone -> stations hierarchy + for zoneTag, stationTags := range measSet.ZoneToStationTags { + key := fmt.Sprintf(constants.RedisSpecZoneStationSetKey, zoneTag) + pipe.SAdd(ctx, key, stationTags) + } + + // building the station -> component nspaths hierarchy + for stationTag, compNSPaths := range measSet.StationToCompNSPaths { + key := fmt.Sprintf(constants.RedisSpecStationCompNSPATHSetKey, stationTag) + pipe.SAdd(ctx, key, compNSPaths) + } + + // building the component nspath -> component tags hierarchy + for compNSPath, compTags := range measSet.CompNSPathToCompTags { + key := fmt.Sprintf(constants.RedisSpecCompNSPathCompTagSetKey, compNSPath) + pipe.SAdd(ctx, key, compTags) + } + + // building the component tag -> measurement tags hierarchy + for compTag, measTags := range measSet.CompTagToMeasTags { + key := fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, compTag) + pipe.SAdd(ctx, key, measTags) + } + + _, err := pipe.Exec(ctx) + if err != nil { + logger.Error(ctx, "init component measurement group recommend content failed", "error", err) + return err + } + return nil +} diff --git a/model/measurement_model.go b/model/measurement_protol_model.go similarity index 100% rename from model/measurement_model.go rename to model/measurement_protol_model.go diff --git a/model/redis_recommend.go b/model/redis_recommend.go index 19f17e3..381a243 100644 --- a/model/redis_recommend.go +++ b/model/redis_recommend.go @@ -367,7 +367,7 @@ func getSpecificKeyByLength(hierarchy constants.RecommendHierarchyType, keyPrefi case constants.CompNSPathRecommendHierarchyType: return fmt.Sprintf(constants.RedisSpecStationCompNSPATHSetKey, keyPrefix) case constants.CompTagRecommendHierarchyType: - return fmt.Sprintf(constants.RedisSpecStationCompTagSetKey, keyPrefix) + return fmt.Sprintf(constants.RedisSpecCompNSPathCompTagSetKey, keyPrefix) case constants.ConfigRecommendHierarchyType: return constants.RedisAllConfigSetKey case constants.MeasTagRecommendHierarchyType: diff --git a/orm/attribute_group.go b/orm/attribute_set.go similarity index 64% rename from orm/attribute_group.go rename to orm/attribute_set.go index c0a9731..d55395c 100644 --- a/orm/attribute_group.go +++ b/orm/attribute_set.go @@ -1,7 +1,7 @@ // Package orm define database data struct package orm -// AttributeSet define struct to return component Tag、 NSPath field +// AttributeSet define struct to return component tag、component NSPath field type AttributeSet struct { CompTag string CompNSPath string diff --git a/orm/measurement_set.go b/orm/measurement_set.go new file mode 100644 index 0000000..a1134f6 --- /dev/null +++ b/orm/measurement_set.go @@ -0,0 +1,18 @@ +// Package orm define database data struct +package orm + +// MeasurementSet define struct to return grid tag、zone tag、station tag、 component NSPath、component tag、measurement tag field +type MeasurementSet struct { + AllGridTags []string + AllZoneTags []string + AllStationTags []string + AllCompNSPaths []string + AllCompTags []string + AllMeasTags []string + + GridToZoneTags map[string][]string // Key: GridTag, Value: ZoneTags + ZoneToStationTags map[string][]string // Key: ZoneTag, Value: StationTags + StationToCompNSPaths map[string][]string // Key: StationTag, Value: NSPaths + CompNSPathToCompTags map[string][]string // Key: NSPaths, Value: CompTags + CompTagToMeasTags map[string][]string // Key: CompTag, Value: MeasTags +}