42 lines
1.0 KiB
Go
42 lines
1.0 KiB
Go
// Package model define model struct of model runtime service
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
|
|
"modelRT/logger"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var NSPathToIsLocalMap map[string]bool
|
|
|
|
// ComponentStationRelation define struct to hold component nspath and station is_local fields
|
|
type ComponentStationRelation struct {
|
|
NSPath string `gorm:"column:nspath"`
|
|
IsLocal bool `gorm:"column:is_local"`
|
|
}
|
|
|
|
// GetNSpathToIsLocalMap define func to get component nspath to station is_local map
|
|
func GetNSpathToIsLocalMap(ctx context.Context, db *gorm.DB) (map[string]bool, error) {
|
|
var results []ComponentStationRelation
|
|
nspathMap := make(map[string]bool)
|
|
|
|
err := db.Table("component").
|
|
Select("component.nspath, station.is_local").
|
|
Joins("join station on component.station_id = station.id").
|
|
Scan(&results).Error
|
|
if err != nil {
|
|
logger.Error(ctx, "query nspath and is_local relationship failed", "err", err)
|
|
return nil, err
|
|
}
|
|
|
|
for _, res := range results {
|
|
if res.NSPath != "" {
|
|
nspathMap[res.NSPath] = res.IsLocal
|
|
}
|
|
}
|
|
|
|
return nspathMap, nil
|
|
}
|