Compare commits
77 Commits
458f7afdbf
...
13433f93e5
| Author | SHA1 | Date |
|---|---|---|
|
|
13433f93e5 | |
|
|
e1886bc347 | |
|
|
ba5e5b3d1c | |
|
|
d3b1f0afbe | |
|
|
cf880279e4 | |
|
|
34684bd5f1 | |
|
|
d75b9a624c | |
|
|
cceffa8219 | |
|
|
d1495b7ab8 | |
|
|
60eab0675e | |
|
|
f47e278f85 | |
|
|
a31bd6f395 | |
|
|
29d0e06c94 | |
|
|
fcf4ef3f7d | |
|
|
e74bedd47f | |
|
|
36e196bedd | |
|
|
941d521328 | |
|
|
7969861746 | |
|
|
8e4bdfd0e9 | |
|
|
42751c1020 | |
|
|
51f65500f3 | |
|
|
7ea38615b4 | |
|
|
6e16a9a39a | |
|
|
c29f58f388 | |
|
|
8313b16dfe | |
|
|
f45f10507b | |
|
|
41e2998739 | |
|
|
c16680d4c2 | |
|
|
9499e579b3 | |
|
|
70bcb00062 | |
|
|
df77f80475 | |
|
|
689d31c246 | |
|
|
4f5d998659 | |
|
|
252699cb77 | |
|
|
0add3cf6db | |
|
|
c92cee9575 | |
|
|
d4d8c2c975 | |
|
|
c68cc9436a | |
|
|
716f56babb | |
|
|
5021e7fda1 | |
|
|
befb4e8971 | |
|
|
2a3852a246 | |
|
|
f48807e5e5 | |
|
|
3f70be0d1c | |
|
|
a21a423624 | |
|
|
666e1a9289 | |
|
|
46e72ce588 | |
|
|
b99c03296a | |
|
|
8a4116879b | |
|
|
10b91abee9 | |
|
|
329b4827f8 | |
|
|
a7d894d2de | |
|
|
fca6905d74 | |
|
|
6f3134b5e9 | |
|
|
b6e47177fb | |
|
|
5e311a7071 | |
|
|
36f267aec7 | |
|
|
357d06868e | |
|
|
46ee2a39f4 | |
|
|
dff74222c6 | |
|
|
9593c77c18 | |
|
|
8cbbfbd695 | |
|
|
d434a7737d | |
|
|
984ee3003d | |
|
|
041d7e5788 | |
|
|
b43adf9b67 | |
|
|
a82e02126d | |
|
|
93d1eea61f | |
|
|
8d6efe8bb1 | |
|
|
6de3c5955b | |
|
|
8090751914 | |
|
|
b75358e676 | |
|
|
f5ea909120 | |
|
|
594dc68ab1 | |
|
|
2584f6dacb | |
|
|
09700a86ee | |
|
|
954203b84d |
|
|
@ -22,6 +22,7 @@
|
|||
go.work
|
||||
|
||||
.vscode
|
||||
.idea
|
||||
# Shield all log files in the log folder
|
||||
/log/
|
||||
# Shield config files in the configs folder
|
||||
|
|
|
|||
|
|
@ -0,0 +1,43 @@
|
|||
// Package errcode provides internal error definition and business error definition
|
||||
package errcode
|
||||
|
||||
var (
|
||||
// ErrProcessSuccess define variable to indicates request process success
|
||||
ErrProcessSuccess = newError(20000, "request process success")
|
||||
|
||||
// ErrInvalidToken define variable to provided token does not conform to the expected format (e.g., missing segments)
|
||||
ErrInvalidToken = newError(40001, "invalid token format")
|
||||
|
||||
// ErrCrossToken define variable to occurs when an update attempt involves multiple components, which is restricted by business logic
|
||||
ErrCrossToken = newError(40002, "cross-component update not allowed")
|
||||
|
||||
// ErrRetrieveFailed define variable to indicates a failure in fetching the project-to-table name mapping from the configuration.
|
||||
ErrRetrieveFailed = newError(40003, "retrieve table mapping failed")
|
||||
|
||||
// ErrFoundTargetFailed define variable to returned when the specific database table cannot be identified using the provided token info.
|
||||
ErrFoundTargetFailed = newError(40004, "found target table by token failed")
|
||||
|
||||
// ErrDBQueryFailed define variable to represents a generic failure during a PostgreSQL SELECT or SCAN operation.
|
||||
ErrDBQueryFailed = newError(50001, "query postgres database data failed")
|
||||
|
||||
// ErrDBUpdateFailed define variable to represents a failure during a PostgreSQL UPDATE or SAVE operation.
|
||||
ErrDBUpdateFailed = newError(50002, "update postgres database data failed")
|
||||
|
||||
// ErrDBzeroAffectedRows define variable to occurs when a database operation executes successfully but modifies no records.
|
||||
ErrDBzeroAffectedRows = newError(50003, "zero affected rows")
|
||||
|
||||
// ErrBeginTxFailed indicates that the system failed to start a new PostgreSQL transaction.
|
||||
ErrBeginTxFailed = newError(50004, "begin postgres transaction failed")
|
||||
|
||||
// ErrCommitTxFailed indicates that the PostgreSQL transaction could not be committed successfully.
|
||||
ErrCommitTxFailed = newError(50005, "postgres database transaction commit failed")
|
||||
|
||||
// ErrCachedQueryFailed define variable to indicates an error occurred while attempting to fetch data from the Redis cache.
|
||||
ErrCachedQueryFailed = newError(60001, "query redis cached data failed")
|
||||
|
||||
// ErrCacheSyncWarn define variable to partial success state: the database was updated, but the subsequent Redis cache refresh failed.
|
||||
ErrCacheSyncWarn = newError(60002, "postgres database updated, but cache sync failed")
|
||||
|
||||
// ErrCacheQueryFailed define variable to indicates query cached data by token failed.
|
||||
ErrCacheQueryFailed = newError(60003, "query cached data by token failed")
|
||||
)
|
||||
|
|
@ -1,72 +0,0 @@
|
|||
package errcode
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// 此处为公共的错误码, 预留 10000000 ~ 10000099 间的 100 个错误码
|
||||
var (
|
||||
Success = newError(0, "success")
|
||||
ErrServer = newError(10000000, "服务器内部错误")
|
||||
ErrParams = newError(10000001, "参数错误, 请检查")
|
||||
ErrNotFound = newError(10000002, "资源未找到")
|
||||
ErrPanic = newError(10000003, "(*^__^*)系统开小差了,请稍后重试") // 无预期的panic错误
|
||||
ErrToken = newError(10000004, "Token无效")
|
||||
ErrForbidden = newError(10000005, "未授权") // 访问一些未授权的资源时的错误
|
||||
ErrTooManyRequests = newError(10000006, "请求过多")
|
||||
ErrCoverData = newError(10000007, "ConvertDataError") // 数据转换错误
|
||||
)
|
||||
|
||||
// 各个业务模块自定义的错误码, 从 10000100 开始, 可以按照不同的业务模块划分不同的号段
|
||||
// Example:
|
||||
//var (
|
||||
// ErrOrderClosed = NewError(10000100, "订单已关闭")
|
||||
//)
|
||||
|
||||
// 用户模块相关错误码 10000100 ~ 1000199
|
||||
var (
|
||||
ErrUserInvalid = newError(10000101, "用户异常")
|
||||
ErrUserNameOccupied = newError(10000102, "用户名已被占用")
|
||||
ErrUserNotRight = newError(10000103, "用户名或密码不正确")
|
||||
)
|
||||
|
||||
// 商品模块相关错误码 10000200 ~ 1000299
|
||||
var (
|
||||
ErrCommodityNotExists = newError(10000200, "商品不存在")
|
||||
ErrCommodityStockOut = newError(10000201, "库存不足")
|
||||
)
|
||||
|
||||
// 购物车模块相关错误码 10000300 ~ 1000399
|
||||
var (
|
||||
ErrCartItemParam = newError(10000300, "购物项参数异常")
|
||||
ErrCartWrongUser = newError(10000301, "用户购物信息不匹配")
|
||||
)
|
||||
|
||||
// 订单模块相关错误码 10000500 ~ 10000599
|
||||
var (
|
||||
ErrOrderParams = newError(10000500, "订单参数异常")
|
||||
ErrOrderCanNotBeChanged = newError(10000501, "订单不可修改")
|
||||
ErrOrderUnsupportedPayScene = newError(10000502, "支付场景暂不支持")
|
||||
)
|
||||
|
||||
func (e *AppError) HttpStatusCode() int {
|
||||
switch e.Code() {
|
||||
case Success.Code():
|
||||
return http.StatusOK
|
||||
case ErrServer.Code(), ErrPanic.Code():
|
||||
return http.StatusInternalServerError
|
||||
case ErrParams.Code(), ErrUserInvalid.Code(), ErrUserNameOccupied.Code(), ErrUserNotRight.Code(),
|
||||
ErrCommodityNotExists.Code(), ErrCommodityStockOut.Code(), ErrCartItemParam.Code(), ErrOrderParams.Code():
|
||||
return http.StatusBadRequest
|
||||
case ErrNotFound.Code():
|
||||
return http.StatusNotFound
|
||||
case ErrTooManyRequests.Code():
|
||||
return http.StatusTooManyRequests
|
||||
case ErrToken.Code():
|
||||
return http.StatusUnauthorized
|
||||
case ErrForbidden.Code(), ErrCartWrongUser.Code(), ErrOrderCanNotBeChanged.Code():
|
||||
return http.StatusForbidden
|
||||
default:
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
// Package errcode provides internal error definition and business error definition
|
||||
package errcode
|
||||
|
||||
import "errors"
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
// Package errcode provides internal error definition and business error definition
|
||||
package errcode
|
||||
|
||||
import (
|
||||
|
|
@ -9,12 +10,12 @@ import (
|
|||
|
||||
var codes = map[int]struct{}{}
|
||||
|
||||
// AppError define struct of internal error
|
||||
// AppError define struct of internal error. occurred field records the location where the error is triggered
|
||||
type AppError struct {
|
||||
code int
|
||||
msg string
|
||||
cause error
|
||||
occurred string // 保存由底层错误导致AppErr发生时的位置
|
||||
occurred string
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
|
|
@ -48,10 +49,6 @@ func (e *AppError) Cause() error {
|
|||
}
|
||||
|
||||
// WithCause define func return top level predefined errors,where the cause field contains the underlying base error
|
||||
// 在逻辑执行中出现错误, 比如dao层返回的数据库查询错误
|
||||
// 可以在领域层返回预定义的错误前附加上导致错误的基础错误。
|
||||
// 如果业务模块预定义的错误码比较详细, 可以使用这个方法, 反之错误码定义的比较笼统建议使用Wrap方法包装底层错误生成项目自定义Error
|
||||
// 并将其记录到日志后再使用预定义错误码返回接口响应
|
||||
func (e *AppError) WithCause(err error) *AppError {
|
||||
newErr := e.Clone()
|
||||
newErr.cause = err
|
||||
|
|
@ -60,8 +57,6 @@ func (e *AppError) WithCause(err error) *AppError {
|
|||
}
|
||||
|
||||
// Wrap define func packaging information and errors returned by the underlying logic
|
||||
// 用于逻辑中包装底层函数返回的error 和 WithCause 一样都是为了记录错误链条
|
||||
// 该方法生成的error 用于日志记录, 返回响应请使用预定义好的error
|
||||
func Wrap(msg string, err error) *AppError {
|
||||
if err == nil {
|
||||
return nil
|
||||
|
|
@ -76,7 +71,7 @@ func (e *AppError) UnWrap() error {
|
|||
return e.cause
|
||||
}
|
||||
|
||||
// Is define func return result of whether any error in err's tree matches target. implemented to support errors.Is(err, target)
|
||||
// Is define func return result of whether any error in err's tree matches target. implemented to support errors.Is(err, target)
|
||||
func (e *AppError) Is(target error) bool {
|
||||
targetErr, ok := target.(*AppError)
|
||||
if !ok {
|
||||
|
|
@ -85,6 +80,17 @@ func (e *AppError) Is(target error) bool {
|
|||
return targetErr.Code() == e.Code()
|
||||
}
|
||||
|
||||
// As define func return result of whether any error in err's tree matches target. implemented to support errors.As(err, target)
|
||||
func (e *AppError) As(target any) bool {
|
||||
t, ok := target.(**AppError)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
*t = e
|
||||
return true
|
||||
}
|
||||
|
||||
// Clone define func return a new AppError with source AppError's code, msg, cause, occurred
|
||||
func (e *AppError) Clone() *AppError {
|
||||
return &AppError{
|
||||
|
|
@ -106,7 +112,7 @@ func newError(code int, msg string) *AppError {
|
|||
return &AppError{code: code, msg: msg}
|
||||
}
|
||||
|
||||
// getAppErrOccurredInfo 获取项目中调用Wrap或者WithCause方法时的程序位置, 方便排查问题
|
||||
// getAppErrOccurredInfo define func return the location where the error is triggered
|
||||
func getAppErrOccurredInfo() string {
|
||||
pc, file, line, ok := runtime.Caller(2)
|
||||
if !ok {
|
||||
|
|
@ -139,7 +145,7 @@ type formattedErr struct {
|
|||
Occurred string `json:"occurred"`
|
||||
}
|
||||
|
||||
// toStructuredError 在JSON Encode 前把Error进行格式化
|
||||
// toStructuredError define func convert AppError to structured error for better readability
|
||||
func (e *AppError) toStructuredError() *formattedErr {
|
||||
fe := new(formattedErr)
|
||||
fe.Code = e.Code()
|
||||
|
|
|
|||
|
|
@ -16,18 +16,19 @@ type BaseConfig struct {
|
|||
|
||||
// ServiceConfig define config struct of service config
|
||||
type ServiceConfig struct {
|
||||
ServiceAddr string `mapstructure:"service_addr"`
|
||||
ServiceName string `mapstructure:"service_name"`
|
||||
SecretKey string `mapstructure:"secret_key"`
|
||||
}
|
||||
|
||||
// KafkaConfig define config struct of kafka config
|
||||
type KafkaConfig struct {
|
||||
Servers string `mapstructure:"Servers"`
|
||||
GroupID string `mapstructure:"group_id"`
|
||||
Topic string `mapstructure:"topic"`
|
||||
AutoOffsetReset string `mapstructure:"auto_offset_reset"`
|
||||
EnableAutoCommit string `mapstructure:"enable_auto_commit"`
|
||||
ReadMessageTimeDuration string `mapstructure:"read_message_time_duration"`
|
||||
Servers string `mapstructure:"Servers"`
|
||||
GroupID string `mapstructure:"group_id"`
|
||||
Topic string `mapstructure:"topic"`
|
||||
AutoOffsetReset string `mapstructure:"auto_offset_reset"`
|
||||
EnableAutoCommit string `mapstructure:"enable_auto_commit"`
|
||||
ReadMessageTimeDuration float32 `mapstructure:"read_message_time_duration"`
|
||||
}
|
||||
|
||||
// PostgresConfig define config struct of postgres config
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// CodeSuccess define constant to indicates that the API was successfully processed
|
||||
CodeSuccess = 20000
|
||||
// CodeInvalidParamFailed define constant to indicates request parameter parsing failed
|
||||
CodeInvalidParamFailed = 40001
|
||||
// CodeDBQueryFailed define constant to indicates database query operation failed
|
||||
CodeDBQueryFailed = 50001
|
||||
// CodeDBUpdateailed define constant to indicates database update operation failed
|
||||
CodeDBUpdateailed = 50002
|
||||
// CodeRedisQueryFailed define constant to indicates redis query operation failed
|
||||
CodeRedisQueryFailed = 60001
|
||||
// CodeRedisUpdateFailed define constant to indicates redis update operation failed
|
||||
CodeRedisUpdateFailed = 60002
|
||||
)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
// FanInChanMaxSize define maximum buffer capacity by fanChannel
|
||||
FanInChanMaxSize = 10000
|
||||
// SendMaxBatchSize define maximum buffer capacity
|
||||
// TODO 后续优化批处理大小
|
||||
SendMaxBatchSize = 100
|
||||
// SendChanBufferSize define maximum buffer capacity by channel
|
||||
SendChanBufferSize = 100
|
||||
|
||||
// SendMaxBatchInterval define maximum aggregate latency
|
||||
SendMaxBatchInterval = 20 * time.Millisecond
|
||||
)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
type contextKey string
|
||||
|
||||
// MeasurementUUIDKey define measurement uuid key into context
|
||||
const MeasurementUUIDKey contextKey = "measurement_uuid"
|
||||
|
|
@ -49,3 +49,9 @@ var ErrChanIsNil = errors.New("this channel is nil")
|
|||
|
||||
// ErrConcurrentModify define error of concurrent modification detected
|
||||
var ErrConcurrentModify = errors.New("existed concurrent modification risk")
|
||||
|
||||
// ErrUnsupportedSubAction define error of unsupported real time data subscription action
|
||||
var ErrUnsupportedSubAction = errors.New("unsupported real time data subscription action")
|
||||
|
||||
// ErrUnsupportedLinkAction define error of unsupported measurement link process action
|
||||
var ErrUnsupportedLinkAction = errors.New("unsupported rmeasurement link process action")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// TIBreachTriggerType define out of bounds type constant
|
||||
TIBreachTriggerType = "trigger"
|
||||
)
|
||||
|
||||
const (
|
||||
// TelemetryUpLimit define telemetry upper limit
|
||||
TelemetryUpLimit = "up"
|
||||
// TelemetryUpUpLimit define telemetry upper upper limit
|
||||
TelemetryUpUpLimit = "upup"
|
||||
|
||||
// TelemetryDownLimit define telemetry limit
|
||||
TelemetryDownLimit = "down"
|
||||
// TelemetryDownDownLimit define telemetry lower lower limit
|
||||
TelemetryDownDownLimit = "downdown"
|
||||
)
|
||||
|
||||
const (
|
||||
// TelesignalRaising define telesignal raising edge
|
||||
TelesignalRaising = "raising"
|
||||
// TelesignalFalling define telesignal falling edge
|
||||
TelesignalFalling = "falling"
|
||||
)
|
||||
|
||||
const (
|
||||
// MinBreachCount define min breach count of real time data
|
||||
MinBreachCount = 10
|
||||
)
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// RedisAllGridSetKey define redis set key which store all grid keys
|
||||
RedisAllGridSetKey = "grid_keys"
|
||||
// RedisSpecGridZoneSetKey define redis set key which store all zone keys under specific grid
|
||||
RedisSpecGridZoneSetKey = "grid_%s_zones_keys"
|
||||
|
||||
// RedisAllZoneSetKey define redis set key which store all zone keys
|
||||
RedisAllZoneSetKey = "zone_keys"
|
||||
// RedisSpecZoneStationSetKey define redis set key which store all station keys under specific zone
|
||||
RedisSpecZoneStationSetKey = "zone_%s_stations_keys"
|
||||
|
||||
// RedisAllStationSetKey define redis set key which store all station keys
|
||||
RedisAllStationSetKey = "station_keys"
|
||||
// RedisSpecStationComponentSetKey define redis set key which store all component keys under specific station
|
||||
RedisSpecStationComponentSetKey = "station_%s_components_keys"
|
||||
|
||||
// RedisAllComponentSetKey define redis set key which store all component keys
|
||||
RedisAllComponentSetKey = "component_keys"
|
||||
// RedisSpecComponentSetKey define redis set key which store all component keys under specific zone
|
||||
RedisSpecComponentSetKey = "zone_%s_components_keys"
|
||||
)
|
||||
|
|
@ -4,6 +4,8 @@ package constants
|
|||
const (
|
||||
// DevelopmentLogMode define development operator environment for modelRT project
|
||||
DevelopmentLogMode = "development"
|
||||
// DebugLogMode define debug operator environment for modelRT project
|
||||
DebugLogMode = "debug"
|
||||
// ProductionLogMode define production operator environment for modelRT project
|
||||
ProductionLogMode = "production"
|
||||
)
|
||||
|
|
|
|||
|
|
@ -29,3 +29,9 @@ const (
|
|||
ChannelSuffixUBC = "UBC"
|
||||
ChannelSuffixUCA = "UCA"
|
||||
)
|
||||
|
||||
const (
|
||||
// MaxIdentifyHierarchy define max data indentify syntax hierarchy
|
||||
MaxIdentifyHierarchy = 7
|
||||
IdentifyHierarchy = 4
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,104 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// DefaultScore define the default score for redissearch suggestion
|
||||
DefaultScore = 1.0
|
||||
)
|
||||
|
||||
const (
|
||||
// RedisAllGridSetKey define redis set key which store all grid tag keys
|
||||
RedisAllGridSetKey = "grid_tag_keys"
|
||||
|
||||
// RedisAllZoneSetKey define redis set key which store all zone tag keys
|
||||
RedisAllZoneSetKey = "zone_tag_keys"
|
||||
|
||||
// RedisAllStationSetKey define redis set key which store all station tag keys
|
||||
RedisAllStationSetKey = "station_tag_keys"
|
||||
|
||||
// RedisAllCompNSPathSetKey define redis set key which store all component nspath keys
|
||||
RedisAllCompNSPathSetKey = "component_nspath_keys"
|
||||
|
||||
// RedisAllCompTagSetKey define redis set key which store all component tag keys
|
||||
RedisAllCompTagSetKey = "component_tag_keys"
|
||||
|
||||
// RedisAllConfigSetKey define redis set key which store all config keys
|
||||
RedisAllConfigSetKey = "config_keys"
|
||||
|
||||
// RedisAllMeasTagSetKey define redis set key which store all measurement tag keys
|
||||
RedisAllMeasTagSetKey = "measurement_tag_keys"
|
||||
|
||||
// RedisSpecGridZoneSetKey define redis set key which store all zone tag keys under specific grid
|
||||
RedisSpecGridZoneSetKey = "%s_zone_tag_keys"
|
||||
|
||||
// RedisSpecZoneStationSetKey define redis set key which store all station tag keys under specific zone
|
||||
RedisSpecZoneStationSetKey = "%s_station_tag_keys"
|
||||
|
||||
// RedisSpecStationCompNSPATHSetKey define redis set key which store all component nspath keys under specific station
|
||||
RedisSpecStationCompNSPATHSetKey = "%s_component_nspath_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"
|
||||
)
|
||||
|
||||
const (
|
||||
// SearchLinkAddAction define search link add action
|
||||
SearchLinkAddAction = "add"
|
||||
// SearchLinkDelAction define search link del action
|
||||
SearchLinkDelAction = "del"
|
||||
)
|
||||
|
||||
// RecommendHierarchyType define the hierarchy levels used for redis recommend search
|
||||
type RecommendHierarchyType int
|
||||
|
||||
const (
|
||||
// GridRecommendHierarchyType define grid hierarch for redis recommend search
|
||||
GridRecommendHierarchyType RecommendHierarchyType = iota + 1
|
||||
// ZoneRecommendHierarchyType define zone hierarch for redis recommend search
|
||||
ZoneRecommendHierarchyType
|
||||
// StationRecommendHierarchyType define station hierarch for redis recommend search
|
||||
StationRecommendHierarchyType
|
||||
// CompNSPathRecommendHierarchyType define component nspath hierarch for redis recommend search
|
||||
CompNSPathRecommendHierarchyType
|
||||
// CompTagRecommendHierarchyType define component tag hierarch for redis recommend search
|
||||
CompTagRecommendHierarchyType
|
||||
// ConfigRecommendHierarchyType define config hierarch for redis recommend search
|
||||
ConfigRecommendHierarchyType
|
||||
// MeasTagRecommendHierarchyType define measurement tag hierarch for redis recommend search
|
||||
MeasTagRecommendHierarchyType
|
||||
)
|
||||
|
||||
// String implements fmt.Stringer interface and returns the string representation of the type.
|
||||
func (r RecommendHierarchyType) String() string {
|
||||
switch r {
|
||||
case GridRecommendHierarchyType:
|
||||
return "grid_tag"
|
||||
case ZoneRecommendHierarchyType:
|
||||
return "zone_tag"
|
||||
case StationRecommendHierarchyType:
|
||||
return "station_tag"
|
||||
case CompNSPathRecommendHierarchyType:
|
||||
return "comp_nspath"
|
||||
case CompTagRecommendHierarchyType:
|
||||
return "comp_tag"
|
||||
case ConfigRecommendHierarchyType:
|
||||
return "config"
|
||||
case MeasTagRecommendHierarchyType:
|
||||
return "meas_tag"
|
||||
default:
|
||||
// 返回一个包含原始数值的默认字符串,以便于调试
|
||||
return "unknown_recommend_type(" + string(rune(r)) + ")"
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
// FullRecommendLength define full recommend length with all tokens
|
||||
FullRecommendLength = "t1.t2.t3.t4.t5.t6.t7"
|
||||
// IsLocalRecommendLength define is local recommend length with specific tokens
|
||||
IsLocalRecommendLength = "t4.t5.t6.t7"
|
||||
// token1.token2.token3.token4.token7
|
||||
// token4.token7
|
||||
)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// RespCodeSuccess define constant to indicates that the API was processed success
|
||||
RespCodeSuccess = 2000
|
||||
|
||||
// RespCodeSuccessWithNoSub define constant to ndicates that the request was processed successfully, with all subscriptions removed for the given client_id.
|
||||
RespCodeSuccessWithNoSub = 2101
|
||||
|
||||
// RespCodeFailed define constant to indicates that the API was processed failed
|
||||
RespCodeFailed = 3000
|
||||
|
||||
// RespCodeInvalidParams define constant to indicates that the request parameters failed to validate, parsing failed, or the action is invalid
|
||||
RespCodeInvalidParams = 4001
|
||||
|
||||
// RespCodeUnauthorized define constant to indicates insufficient permissions or an invalid ClientID
|
||||
RespCodeUnauthorized = 4002
|
||||
|
||||
// RespCodeServerError define constants to indicates a serious internal server error (such as database disconnection or code panic)
|
||||
RespCodeServerError = 5000
|
||||
)
|
||||
|
|
@ -0,0 +1,85 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// SubStartAction define the real time subscription start action
|
||||
SubStartAction string = "start"
|
||||
// SubStopAction define the real time subscription stop action
|
||||
SubStopAction string = "stop"
|
||||
// SubAppendAction define the real time subscription append action
|
||||
SubAppendAction string = "append"
|
||||
// SubUpdateAction define the real time subscription update action
|
||||
SubUpdateAction string = "update"
|
||||
)
|
||||
|
||||
// 定义状态常量
|
||||
// TODO 从4位格式修改为5位格式
|
||||
const (
|
||||
// SubSuccessCode define subscription success code
|
||||
SubSuccessCode = "1001"
|
||||
// SubFailedCode define subscription failed code
|
||||
SubFailedCode = "1002"
|
||||
// RTDSuccessCode define real time data return success code
|
||||
RTDSuccessCode = "1003"
|
||||
// RTDFailedCode define real time data return failed code
|
||||
RTDFailedCode = "1004"
|
||||
// CancelSubSuccessCode define cancel subscription success code
|
||||
CancelSubSuccessCode = "1005"
|
||||
// CancelSubFailedCode define cancel subscription failed code
|
||||
CancelSubFailedCode = "1006"
|
||||
// SubRepeatCode define subscription repeat code
|
||||
SubRepeatCode = "1007"
|
||||
// UpdateSubSuccessCode define update subscription success code
|
||||
UpdateSubSuccessCode = "1008"
|
||||
// UpdateSubFailedCode define update subscription failed code
|
||||
UpdateSubFailedCode = "1009"
|
||||
)
|
||||
|
||||
const (
|
||||
// SysCtrlPrefix define to indicates the prefix for all system control directives,facilitating unified parsing within the sendDataStream goroutine
|
||||
SysCtrlPrefix = "SYS_CTRL_"
|
||||
|
||||
// SysCtrlAllRemoved define to indicates that all active polling targets have been removed for the current client, and no further data streams are active
|
||||
SysCtrlAllRemoved = "SYS_CTRL_ALL_REMOVED"
|
||||
|
||||
// SysCtrlSessionExpired define to indicates reserved for indicating that the current websocket session has timed out or is no longer valid
|
||||
SysCtrlSessionExpired = "SYS_CTRL_SESSION_EXPIRED"
|
||||
)
|
||||
|
||||
const (
|
||||
// SubSuccessMsg define subscription success message
|
||||
SubSuccessMsg = "subscription success"
|
||||
// SubFailedMsg define subscription failed message
|
||||
SubFailedMsg = "subscription failed"
|
||||
// RTDSuccessMsg define real time data return success message
|
||||
RTDSuccessMsg = "real time data return success"
|
||||
// RTDFailedMsg define real time data return failed message
|
||||
RTDFailedMsg = "real time data return failed"
|
||||
// CancelSubSuccessMsg define cancel subscription success message
|
||||
CancelSubSuccessMsg = "cancel subscription success"
|
||||
// CancelSubFailedMsg define cancel subscription failed message
|
||||
CancelSubFailedMsg = "cancel subscription failed"
|
||||
// SubRepeatMsg define subscription repeat message
|
||||
SubRepeatMsg = "subscription repeat in target interval"
|
||||
// UpdateSubSuccessMsg define update subscription success message
|
||||
UpdateSubSuccessMsg = "update subscription success"
|
||||
// UpdateSubFailedMsg define update subscription failed message
|
||||
UpdateSubFailedMsg = "update subscription failed"
|
||||
)
|
||||
|
||||
// TargetOperationType define constant to the target operation type
|
||||
type TargetOperationType int
|
||||
|
||||
const (
|
||||
// OpAppend define append new target to the subscription list
|
||||
OpAppend TargetOperationType = iota
|
||||
// OpRemove define remove exist target from the subscription list
|
||||
OpRemove
|
||||
// OpUpdate define update exist target from the subscription list
|
||||
OpUpdate
|
||||
)
|
||||
|
||||
const (
|
||||
// NoticeChanCap define real time data notice channel capacity
|
||||
NoticeChanCap = 10000
|
||||
)
|
||||
|
|
@ -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,
|
||||
GridID: strconv.FormatInt(componentInfo.GridID, 10),
|
||||
ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10),
|
||||
StationID: 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)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"modelRT/common/errcode"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CreateMeasurement define create measurement info of the circuit diagram into DB
|
||||
func CreateMeasurement(ctx context.Context, tx *gorm.DB, measurementInfo network.MeasurementCreateInfo) (string, error) {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
globalUUID, err := uuid.FromString(measurementInfo.UUID)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
}
|
||||
|
||||
measurement := orm.Measurement{
|
||||
Tag: "",
|
||||
Name: "",
|
||||
Type: -1,
|
||||
Size: -1,
|
||||
DataSource: nil,
|
||||
EventPlan: nil,
|
||||
BayUUID: globalUUID,
|
||||
ComponentUUID: globalUUID,
|
||||
Op: -1,
|
||||
Ts: time.Now(),
|
||||
}
|
||||
|
||||
result := tx.WithContext(cancelCtx).Create(&measurement)
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check insert component slice", errcode.ErrInsertRowUnexpected)
|
||||
}
|
||||
return "", fmt.Errorf("insert component info failed:%w", err)
|
||||
}
|
||||
return strconv.FormatInt(measurement.ID, 10), nil
|
||||
}
|
||||
|
|
@ -24,7 +24,6 @@ func CreateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, topol
|
|||
UUIDFrom: info.UUIDFrom,
|
||||
UUIDTo: info.UUIDTo,
|
||||
Flag: info.Flag,
|
||||
Comment: info.Comment,
|
||||
}
|
||||
topologicSlice = append(topologicSlice, topologicInfo)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"modelRT/logger"
|
||||
"modelRT/model"
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// FillingShortTokenModel define filling short token model info
|
||||
func FillingShortTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.ShortIdentityTokenModel) error {
|
||||
filterComponent := &orm.Component{
|
||||
GridName: identModel.GetGridName(),
|
||||
ZoneName: identModel.GetZoneName(),
|
||||
StationName: identModel.GetStationName(),
|
||||
}
|
||||
|
||||
component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query long identity token model info failed", "error", err)
|
||||
return err
|
||||
}
|
||||
identModel.ComponentInfo = component
|
||||
identModel.MeasurementInfo = measurement
|
||||
return nil
|
||||
}
|
||||
|
||||
// FillingLongTokenModel define filling long token model info
|
||||
func FillingLongTokenModel(ctx context.Context, tx *gorm.DB, identModel *model.LongIdentityTokenModel) error {
|
||||
filterComponent := &orm.Component{
|
||||
GridName: identModel.GetGridName(),
|
||||
ZoneName: identModel.GetZoneName(),
|
||||
StationName: identModel.GetStationName(),
|
||||
Tag: identModel.GetComponentTag(),
|
||||
}
|
||||
component, measurement, err := QueryLongIdentModelInfoByToken(ctx, tx, identModel.MeasurementTag, filterComponent)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query long identity token model info failed", "error", err)
|
||||
return err
|
||||
}
|
||||
identModel.ComponentInfo = component
|
||||
identModel.MeasurementInfo = measurement
|
||||
return nil
|
||||
}
|
||||
|
||||
// ParseDataIdentifierToken define function to parse data identifier token function
|
||||
func ParseDataIdentifierToken(ctx context.Context, tx *gorm.DB, identToken string) (model.IndentityTokenModelInterface, error) {
|
||||
identSlice := strings.Split(identToken, ".")
|
||||
identSliceLen := len(identSlice)
|
||||
if identSliceLen == 4 {
|
||||
// token1.token2.token3.token4.token7
|
||||
shortIndentModel := &model.ShortIdentityTokenModel{
|
||||
GridTag: identSlice[0],
|
||||
ZoneTag: identSlice[1],
|
||||
StationTag: identSlice[2],
|
||||
NamespacePath: identSlice[3],
|
||||
MeasurementTag: identSlice[6],
|
||||
}
|
||||
err := FillingShortTokenModel(ctx, tx, shortIndentModel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return shortIndentModel, nil
|
||||
} else if identSliceLen == 7 {
|
||||
// token1.token2.token3.token4.token5.token6.token7
|
||||
longIndentModel := &model.LongIdentityTokenModel{
|
||||
GridTag: identSlice[0],
|
||||
ZoneTag: identSlice[1],
|
||||
StationTag: identSlice[2],
|
||||
NamespacePath: identSlice[3],
|
||||
ComponentTag: identSlice[4],
|
||||
AttributeGroup: identSlice[5],
|
||||
MeasurementTag: identSlice[6],
|
||||
}
|
||||
err := FillingLongTokenModel(ctx, tx, longIndentModel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return longIndentModel, nil
|
||||
}
|
||||
return nil, fmt.Errorf("invalid identity token format: %s", identToken)
|
||||
}
|
||||
|
|
@ -57,7 +57,7 @@ func ParseAttrToken(ctx context.Context, tx *gorm.DB, attrToken, clientToken str
|
|||
|
||||
// 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])
|
||||
component, err := QueryComponentByNSPath(ctx, tx, attrItems[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
@ -82,7 +82,7 @@ func FillingLongAttrModel(ctx context.Context, tx *gorm.DB, attrItems []string,
|
|||
return err
|
||||
}
|
||||
attrModel.StationInfo = &station
|
||||
component, err := QueryComponentByNsPath(ctx, tx, attrItems[3])
|
||||
component, err := QueryComponentByNSPath(ctx, tx, attrItems[3])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package database
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/orm"
|
||||
|
|
@ -54,6 +55,43 @@ func QueryComponentByUUID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm
|
|||
return component, nil
|
||||
}
|
||||
|
||||
// QueryComponentByCompTag return the result of query circuit diagram component info by component tag from postgresDB
|
||||
func QueryComponentByCompTag(ctx context.Context, tx *gorm.DB, tag string) (orm.Component, error) {
|
||||
var component orm.Component
|
||||
result := tx.WithContext(ctx).
|
||||
Where("tag = ?", tag).
|
||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
First(&component)
|
||||
|
||||
if result.Error != nil {
|
||||
return orm.Component{}, result.Error
|
||||
}
|
||||
return component, nil
|
||||
}
|
||||
|
||||
// QueryComponentByCompTags return the result of query circuit diagram component info by components tag from postgresDB
|
||||
func QueryComponentByCompTags(ctx context.Context, tx *gorm.DB, tags []string) (map[string]orm.Component, error) {
|
||||
if len(tags) == 0 {
|
||||
return make(map[string]orm.Component), nil
|
||||
}
|
||||
|
||||
var results []orm.Component
|
||||
err := tx.WithContext(ctx).
|
||||
Model(orm.Component{}).
|
||||
Select("global_uuid,tag, model_name").
|
||||
Where("tag IN ?", tags).
|
||||
Find(&results).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
compModelMap := make(map[string]orm.Component, len(results))
|
||||
for _, result := range results {
|
||||
compModelMap[result.Tag] = result
|
||||
}
|
||||
return compModelMap, nil
|
||||
}
|
||||
|
||||
// QueryComponentByPageID return the result of query circuit diagram component info by page id from postgresDB
|
||||
func QueryComponentByPageID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm.Component, error) {
|
||||
var component orm.Component
|
||||
|
|
@ -68,8 +106,8 @@ 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) {
|
||||
// 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)
|
||||
|
|
@ -81,3 +119,58 @@ func QueryComponentByNsPath(ctx context.Context, tx *gorm.DB, nsPath string) (or
|
|||
}
|
||||
return component, nil
|
||||
}
|
||||
|
||||
// QueryLongIdentModelInfoByToken define func to query long identity model info by long token
|
||||
func QueryLongIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag string, condition *orm.Component) (*orm.Component, *orm.Measurement, error) {
|
||||
var resultComp orm.Component
|
||||
var meauserment orm.Measurement
|
||||
|
||||
// ctx timeout judgment
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).First(&resultComp, &condition)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil, fmt.Errorf("component record not found by %v:%w", condition, result.Error)
|
||||
}
|
||||
return nil, nil, result.Error
|
||||
}
|
||||
|
||||
filterMap := map[string]any{"component_uuid": resultComp.GlobalUUID, "tag": measTag}
|
||||
result = tx.WithContext(cancelCtx).Where(filterMap).Clauses(clause.Locking{Strength: "UPDATE"}).First(&meauserment)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil, fmt.Errorf("measurement record not found by %v:%w", filterMap, result.Error)
|
||||
}
|
||||
return nil, nil, result.Error
|
||||
}
|
||||
return &resultComp, &meauserment, nil
|
||||
}
|
||||
|
||||
// QueryShortIdentModelInfoByToken define func to query short identity model info by short token
|
||||
func QueryShortIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag string, condition *orm.Component) (*orm.Component, *orm.Measurement, error) {
|
||||
var resultComp orm.Component
|
||||
var meauserment orm.Measurement
|
||||
// ctx timeout judgment
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).First(&resultComp, &condition)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil, fmt.Errorf("component record not found by %v:%w", condition, result.Error)
|
||||
}
|
||||
return nil, nil, result.Error
|
||||
}
|
||||
|
||||
filterMap := map[string]any{"component_uuid": resultComp.GlobalUUID, "tag": measTag}
|
||||
result = tx.WithContext(cancelCtx).Where(filterMap).Clauses(clause.Locking{Strength: "UPDATE"}).First(&meauserment)
|
||||
if result.Error != nil {
|
||||
if result.Error == gorm.ErrRecordNotFound {
|
||||
return nil, nil, fmt.Errorf("measurement record not found by %v:%w", filterMap, result.Error)
|
||||
}
|
||||
return nil, nil, result.Error
|
||||
}
|
||||
return &resultComp, &meauserment, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -0,0 +1,110 @@
|
|||
// 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 grids []orm.Grid
|
||||
if err := db.Table("grid").Select("tagname").Scan(&grids).Error; err == nil {
|
||||
for _, g := range grids {
|
||||
if g.TAGNAME != "" {
|
||||
mSet.AllGridTags = append(mSet.AllGridTags, g.TAGNAME)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mSet.AllConfigTags = append(mSet.AllConfigTags, "bay")
|
||||
|
||||
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
|
||||
}
|
||||
|
|
@ -13,13 +13,32 @@ import (
|
|||
|
||||
// QueryMeasurementByID return the result of query circuit diagram component measurement info by id from postgresDB
|
||||
func QueryMeasurementByID(ctx context.Context, tx *gorm.DB, id int64) (orm.Measurement, error) {
|
||||
var component orm.Measurement
|
||||
var measurement orm.Measurement
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
result := tx.WithContext(cancelCtx).
|
||||
Where("id = ?", id).
|
||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
First(&measurement)
|
||||
|
||||
if result.Error != nil {
|
||||
return orm.Measurement{}, result.Error
|
||||
}
|
||||
return measurement, nil
|
||||
}
|
||||
|
||||
// QueryMeasurementByToken define function query circuit diagram component measurement info by token from postgresDB
|
||||
func QueryMeasurementByToken(ctx context.Context, tx *gorm.DB, token string) (orm.Measurement, error) {
|
||||
// TODO parse token to avoid SQL injection
|
||||
|
||||
var component orm.Measurement
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
result := tx.WithContext(cancelCtx).
|
||||
Where(" = ?", token).
|
||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
First(&component)
|
||||
|
||||
if result.Error != nil {
|
||||
|
|
@ -27,3 +46,17 @@ func QueryMeasurementByID(ctx context.Context, tx *gorm.DB, id int64) (orm.Measu
|
|||
}
|
||||
return component, nil
|
||||
}
|
||||
|
||||
// GetAllMeasurements define func to query all measurement info from postgresDB
|
||||
func GetAllMeasurements(ctx context.Context, tx *gorm.DB) ([]orm.Measurement, error) {
|
||||
var measurements []orm.Measurement
|
||||
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&measurements)
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
return measurements, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,81 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func queryFirstByID(ctx context.Context, tx *gorm.DB, id any, dest any) error {
|
||||
result := tx.WithContext(ctx).Where("id = ?", id).First(dest)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
func queryFirstByTag(ctx context.Context, tx *gorm.DB, tagName any, dest any) error {
|
||||
result := tx.WithContext(ctx).Where("tagname = ?", tagName).First(dest)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// QueryNodeInfoByID return the result of query circuit diagram node info by id and level from postgresDB
|
||||
func QueryNodeInfoByID(ctx context.Context, tx *gorm.DB, id int64, level int) (orm.CircuitDiagramNodeInterface, orm.CircuitDiagramNodeInterface, error) {
|
||||
// 设置 Context 超时
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var currentNodeInfo orm.CircuitDiagramNodeInterface
|
||||
var previousNodeInfo orm.CircuitDiagramNodeInterface
|
||||
var err error
|
||||
|
||||
switch level {
|
||||
case 0:
|
||||
var grid orm.Grid
|
||||
err = queryFirstByID(cancelCtx, tx, id, &grid)
|
||||
currentNodeInfo = grid
|
||||
case 1:
|
||||
// current:Zone,Previous:Grid
|
||||
var zone orm.Zone
|
||||
err = queryFirstByID(cancelCtx, tx, id, &zone)
|
||||
currentNodeInfo = zone
|
||||
if err == nil {
|
||||
var grid orm.Grid
|
||||
err = queryFirstByID(cancelCtx, tx, zone.GridID, &grid)
|
||||
previousNodeInfo = grid
|
||||
}
|
||||
case 2:
|
||||
// current:Station,Previous:Zone
|
||||
var station orm.Station
|
||||
err = queryFirstByID(cancelCtx, tx, id, &station)
|
||||
currentNodeInfo = station
|
||||
if err == nil {
|
||||
var zone orm.Zone
|
||||
err = queryFirstByID(cancelCtx, tx, station.ZoneID, &zone)
|
||||
previousNodeInfo = zone
|
||||
}
|
||||
case 3, 4:
|
||||
// current:Component, Previous:Station
|
||||
var component orm.Component
|
||||
err = queryFirstByID(cancelCtx, tx, id, &component)
|
||||
currentNodeInfo = component
|
||||
if err == nil {
|
||||
var station orm.Station
|
||||
// TODO 修改staion name为通过 station id 查询
|
||||
err = queryFirstByTag(cancelCtx, tx, component.StationName, &station)
|
||||
previousNodeInfo = station
|
||||
}
|
||||
case 5:
|
||||
// TODO[NONEED-ISSUE]暂无此层级增加或删除需求 #2
|
||||
return nil, nil, nil
|
||||
default:
|
||||
return nil, nil, fmt.Errorf("unsupported node level: %d", level)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return previousNodeInfo, currentNodeInfo, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/logger"
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// QueryArrtibuteRecordByUUID return the attribute table record info of the component attribute by uuid
|
||||
func QueryArrtibuteRecordByUUID(ctx context.Context, tx *gorm.DB, gridID, zoneID, stationID int64) ([]orm.Page, error) {
|
||||
var pages []orm.Page
|
||||
// ctx timeout judgment
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result := tx.Model(&orm.Page{}).WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Select(`"page".id, "page".Name, "page".status,"page".context`).Joins(`inner join "station" on "station".id = "page".station_id`).Joins(`inner join "zone" on "zone".id = "station".zone_id`).Joins(`inner join "grid" on "grid".id = "zone".grid_id`).Where(`"grid".id = ? and "zone".id = ? and "station".id = ?`, gridID, zoneID, stationID).Scan(&pages)
|
||||
|
||||
if result.Error != nil {
|
||||
logger.Error(ctx, "query circuit diagram pages by gridID and zoneID and stationID failed", "grid_id", gridID, "zone_id", zoneID, "station_id", stationID, "error", result.Error)
|
||||
return nil, result.Error
|
||||
}
|
||||
return pages, nil
|
||||
}
|
||||
|
||||
// GetProjectNameByTagAndGroupName 根据 tag 和 meta_model 获取项目名称
|
||||
func GetProjectNameByTagAndGroupName(db *gorm.DB, tag string, groupName string) (string, error) {
|
||||
var project orm.ProjectManager
|
||||
|
||||
// 使用 Select 只提取 name 字段,提高查询效率
|
||||
// 使用 Where 进行多列条件过滤
|
||||
err := db.Select("name").
|
||||
Where("tag = ? AND meta_model = ?", tag, groupName).
|
||||
First(&project).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return "", fmt.Errorf("project not found with tag: %s and model: %s", tag, groupName)
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
return project.Name, nil
|
||||
}
|
||||
|
||||
// BatchGetProjectNames define func to batch retrieve name based on multiple tags and metaModel
|
||||
func BatchGetProjectNames(db *gorm.DB, identifiers []orm.ProjectIdentifier) (map[orm.ProjectIdentifier]string, error) {
|
||||
if len(identifiers) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var projects []orm.ProjectManager
|
||||
queryArgs := make([][]any, len(identifiers))
|
||||
for i, id := range identifiers {
|
||||
queryArgs[i] = []any{id.Tag, id.GroupName}
|
||||
}
|
||||
|
||||
err := db.Select("tag", "group_name", "name").
|
||||
Where("(tag, group_name) IN ?", queryArgs).
|
||||
Find(&projects).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resultMap := make(map[orm.ProjectIdentifier]string)
|
||||
for _, p := range projects {
|
||||
key := orm.ProjectIdentifier{Tag: p.Tag, GroupName: p.GroupName}
|
||||
resultMap[key] = p.Name
|
||||
}
|
||||
|
||||
return resultMap, nil
|
||||
}
|
||||
|
|
@ -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,
|
||||
GridID: strconv.FormatInt(componentInfo.GridID, 10),
|
||||
ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10),
|
||||
StationID: 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)
|
||||
|
|
|
|||
|
|
@ -51,7 +51,6 @@ func UpdateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, chang
|
|||
Flag: changeInfo.Flag,
|
||||
UUIDFrom: changeInfo.NewUUIDFrom,
|
||||
UUIDTo: changeInfo.NewUUIDTo,
|
||||
Comment: changeInfo.Comment,
|
||||
}
|
||||
result = tx.WithContext(cancelCtx).Create(&topologic)
|
||||
}
|
||||
|
|
|
|||
200
deploy/deploy.md
200
deploy/deploy.md
|
|
@ -88,45 +88,185 @@ docker logs redis
|
|||
##### 2.4.1 Postgres数据注入
|
||||
|
||||
```SQL
|
||||
INSERT INTO public."Topologic" VALUES (2, 1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', '10f155cf-bd27-4557-85b2-d126b6e2657f', 1, NULL);
|
||||
INSERT INTO public."Topologic" VALUES (3, 1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', 1, NULL);
|
||||
INSERT INTO public."Topologic" VALUES (4, 1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', '70c190f2-8a75-42a9-b166-ec5f87e0aa6b', 1, NULL);
|
||||
INSERT INTO public."Topologic" VALUES (5, 1, 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '70c200f2-8a75-42a9-c166-bf5f87e0aa6b', 1, NULL);
|
||||
INSERT INTO public."Topologic" VALUES (1, 1, '00000000-0000-0000-0000-000000000000', '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', 1, NULL);
|
||||
insert into public.grid(id,tagname,name,description,op,ts) VALUES (1, 'grid1', '网格1', '测试网格1', -1,CURRENT_TIMESTAMP);
|
||||
|
||||
insert into public.zone(id,grid_id,tagname,name,description,op,ts) VALUES (1, 1,'zone1', '区域1_1', '测试区域1_1', -1,CURRENT_TIMESTAMP);
|
||||
|
||||
insert into public.station(id,zone_id,tagname,name,description,is_local,op,ts) VALUES (1, 1,'station1', '站1_1_1', '测试站1_1_1', true, -1,CURRENT_TIMESTAMP),
|
||||
(2, 1, 'station2', '站1_1_2', '测试站1_1_2', false, -1, CURRENT_TIMESTAMP);
|
||||
|
||||
INSERT INTO public.topologic(flag, uuid_from, uuid_to, context, description, op, ts)
|
||||
VALUES
|
||||
(1, '00000000-0000-0000-0000-000000000000', '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', '{}', '', 1, CURRENT_TIMESTAMP),
|
||||
(1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', '10f155cf-bd27-4557-85b2-d126b6e2657f', '{}', '', 1, CURRENT_TIMESTAMP),
|
||||
(1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '{}', '', 1, CURRENT_TIMESTAMP),
|
||||
(1, '70c190f2-8a60-42a9-b143-ec5f87e0aa6b', '70c190f2-8a75-42a9-b166-ec5f87e0aa6b', '{}', '', 1, CURRENT_TIMESTAMP),
|
||||
(1, 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '70c200f2-8a75-42a9-c166-bf5f87e0aa6b', '{}', '', 1, CURRENT_TIMESTAMP),
|
||||
(1, 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '968dd6e6-faec-4f78-b58a-d6e68426b09e', '{}', '', 1, CURRENT_TIMESTAMP),
|
||||
(1, 'e32bc0be-67f4-4d79-a5da-eaa40a5bd77d', '968dd6e6-faec-4f78-b58a-d6e68426b08e', '{}', '', 1, CURRENT_TIMESTAMP);
|
||||
|
||||
INSERT INTO public.bay (bay_uuid, name, tag, type, unom, fla, capacity, description, in_service, state, grid, zone, station, business, context, from_uuids, to_uuids, dev_protect, dev_fault_record, dev_status, dev_dyn_sense, dev_instruct, dev_etc, components, op, ts)
|
||||
VALUES (
|
||||
'18e71a24-694a-43fa-93a7-c4d02a27d1bc',
|
||||
'', '', '',
|
||||
-1, -1, -1,
|
||||
'',
|
||||
false,
|
||||
-1,
|
||||
'', '', '',
|
||||
'{}',
|
||||
'{}',
|
||||
'[]',
|
||||
'[]',
|
||||
'[]',
|
||||
'[]',
|
||||
'[]',
|
||||
'[]',
|
||||
'[]',
|
||||
'[]',
|
||||
ARRAY['968dd6e6-faec-4f78-b58a-d6e68426b09e', '968dd6e6-faec-4f78-b58a-d6e68426b08e']::uuid[],
|
||||
-1,
|
||||
CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
INSERT INTO public.component (global_uuid, nspath, tag, name, model_name, description, grid, zone, station, station_id, type, in_service, state, status, connection, label, context, op, ts)
|
||||
VALUES
|
||||
(
|
||||
'968dd6e6-faec-4f78-b58a-d6e68426b09e',
|
||||
'ns1', 'tag1', 'component1', 'bus_1', '',
|
||||
'grid1', 'zone1', 'station1', 1,
|
||||
-1,
|
||||
false,
|
||||
-1, -1,
|
||||
'{}',
|
||||
'{}',
|
||||
'{}',
|
||||
-1,
|
||||
CURRENT_TIMESTAMP
|
||||
),
|
||||
(
|
||||
'968dd6e6-faec-4f78-b58a-d6e68426b08e',
|
||||
'ns2', 'tag2', 'component2', 'bus_1', '',
|
||||
'grid1', 'zone1', 'station1', 1,
|
||||
-1,
|
||||
false,
|
||||
-1, -1,
|
||||
'{}',
|
||||
'{}',
|
||||
'{}',
|
||||
-1,
|
||||
CURRENT_TIMESTAMP
|
||||
),
|
||||
(
|
||||
'968dd6e6-faec-4f78-b58a-d6e88426b09e',
|
||||
'ns3', 'tag3', 'component3', 'bus_1', '',
|
||||
'grid1', 'zone1', 'station2', 2,
|
||||
-1,
|
||||
false,
|
||||
-1, -1,
|
||||
'{}',
|
||||
'{}',
|
||||
'{}',
|
||||
-1,
|
||||
CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
INSERT INTO public.measurement (id, tag, name, type, size, data_source, event_plan, bay_uuid, component_uuid, op, ts)
|
||||
VALUES
|
||||
(3, 'I11_C_rms', '45母甲侧互连电流C相1', -1, 200, '{"type": 1, "io_address": {"device": "ssu001", "channel": "TM1", "station": "001"}}', '{"cause": {"up": 55.0, "down": 45.0}, "action": {"command": "warning", "parameters": ["I段母线甲侧互连电流C相1"]}, "enable": true}', '18e71a24-694a-43fa-93a7-c4d02a27d1bc', '968dd6e6-faec-4f78-b58a-d6e68426b09e', -1, CURRENT_TIMESTAMP),
|
||||
(4, 'I11_B_rms', '45母甲侧互连电流B相1', -1, 300, '{"type": 1, "io_address": {"device": "ssu001", "channel": "TM2", "station": "001"}}', '{"cause": {"upup": 65, "downdown": 35}, "action": {"command": "warning", "parameters": ["I段母线甲侧互连电流B相1"]}, "enable": true}', '18e71a24-694a-43fa-93a7-c4d02a27d1bc', '968dd6e6-faec-4f78-b58a-d6e68426b09e', -1, CURRENT_TIMESTAMP),
|
||||
(5, 'I11_A_rms', '45母甲侧互连电流A相1', -1, 300, '{"type": 1, "io_address": {"device": "ssu001", "channel": "TM3", "station": "001"}}', '{"cause": {"up": 55, "down": 45, "upup": 65, "downdown": 35}, "action": {"command": "warning", "parameters": ["I段母线甲侧互连电流A相1"]}, "enable": true}', '18e71a24-694a-43fa-93a7-c4d02a27d1bc', '968dd6e6-faec-4f78-b58a-d6e68426b09e', -1, CURRENT_TIMESTAMP);
|
||||
|
||||
INSERT INTO public.project_manager (id, name, tag, meta_model, group_name, link_type, check_state, is_public, op, ts
|
||||
) VALUES
|
||||
(1, 'component', 'component', '', 'component', 0,
|
||||
'{"checkState": [{"name": "global_uuid", "type": "UUID", "checked": 1, "isVisible": 1, "defaultValue": "", "lengthPrecision": -1}, {"name": "nspath", "type": "VARCHAR(32)", "checked": 1, "isVisible": 1, "defaultValue": "", "lengthPrecision": 32}, {"name": "tag", "type": "VARCHAR(32)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 32}, {"name": "name", "type": "VARCHAR(64)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 64}, {"name": "description", "type": "VARCHAR(512)", "checked": 1, "isVisible": 1, "defaultValue": "", "lengthPrecision": 512}, {"name": "station", "type": "VARCHAR(64)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 64}, {"name": "zone", "type": "VARCHAR(64)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 64}, {"name": "grid", "type": "VARCHAR(64)", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": 64}, {"name": "type", "type": "INTEGER", "checked": 1, "isVisible": 0, "defaultValue": "0", "lengthPrecision": -1}, {"name": "in_service", "type": "SMALLINT", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "state", "type": "INTEGER", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "connection", "type": "JSONB", "checked": 1, "isVisible": 1, "defaultValue": "{}", "lengthPrecision": -1}, {"name": "label", "type": "JSONB", "checked": 1, "isVisible": 1, "defaultValue": "{}", "lengthPrecision": -1}, {"name": "context", "type": "JSONB", "checked": 1, "isVisible": 0, "defaultValue": "{}", "lengthPrecision": -1}, {"name": "op", "type": "INTEGER", "checked": 1, "isVisible": 0, "defaultValue": "-1", "lengthPrecision": -1}, {"name": "ts", "type": "TIMESTAMP", "checked": 1, "isVisible": 0, "defaultValue": "null", "lengthPrecision": -1}, {"name": "model_name", "type": "VARCHAR(64)", "checked": 1, "isVisible": 0, "defaultValue": "null", "lengthPrecision": 64}, {"name": "status", "type": "SMALLINT", "checked": 1, "isVisible": 0, "defaultValue": "null", "lengthPrecision": -1}]}', TRUE, -1, CURRENT_TIMESTAMP
|
||||
),
|
||||
(2, 'bus_bus_1_base_extend', 'bus_1', 'bus', 'base_extend', 0,
|
||||
'{"checkState": [{"name": "bus_num", "type": "INTEGER", "checked": 1, "isVisible": 0, "defaultValue": "1", "lengthPrecision": -1}, {"name": "unom_kv", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "null", "lengthPrecision": -1}]}', FALSE, -1, CURRENT_TIMESTAMP
|
||||
),
|
||||
(3, 'bus_bus_1_model', 'bus_1', 'bus', 'model', 0,
|
||||
'{"checkState": [{"name": "ui_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "100", "lengthPrecision": -1}, {"name": "ui_kv", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "35", "lengthPrecision": -1}, {"name": "ui_pa", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "stability_rated_current", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "1000", "lengthPrecision": -1}, {"name": "stability_dynamic_steady_current", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "40", "lengthPrecision": -1}, {"name": "load_adjustment_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "100", "lengthPrecision": -1}, {"name": "load_adjustment_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "100", "lengthPrecision": -1}, {"name": "bus_type", "type": "VARCHAR(10)", "checked": 1, "isVisible": 1, "defaultValue": "PQ母线", "lengthPrecision": 10}, {"name": "csc_s3_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_s3_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_i3_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_i3_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_z3s_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0.05", "lengthPrecision": -1}, {"name": "csc_z3s_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0.1", "lengthPrecision": -1}, {"name": "csc_s1_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_s1_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_i1_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_i1_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "csc_z1s_max", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0.05", "lengthPrecision": -1}, {"name": "csc_z1s_min", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0.1", "lengthPrecision": -1}, {"name": "csc_base_voltage", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "37", "lengthPrecision": -1}, {"name": "csc_base_capacity", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "100", "lengthPrecision": -1}]}', FALSE, -1, CURRENT_TIMESTAMP
|
||||
),
|
||||
(4, 'bus_bus_1_stable', 'bus_1', 'bus', 'stable', 0,
|
||||
'{"checkState": [{"name": "uvpw_threshold_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "95", "lengthPrecision": -1}, {"name": "uvpw_runtime", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "10", "lengthPrecision": -1}, {"name": "uvw_threshold_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "90", "lengthPrecision": -1}, {"name": "uvw_runtime", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "10", "lengthPrecision": -1}, {"name": "ovpw_threshold_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "105", "lengthPrecision": -1}, {"name": "ovpw_runtime", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "60", "lengthPrecision": -1}, {"name": "ovw_threshold_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "110", "lengthPrecision": -1}, {"name": "ovw_runtime", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "10", "lengthPrecision": -1}, {"name": "umargin_pmax", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "umargin_qmax", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "0", "lengthPrecision": -1}, {"name": "umargin_ulim", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "90", "lengthPrecision": -1}, {"name": "umargin_plim_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "15", "lengthPrecision": -1}, {"name": "umargin_qlim_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "15", "lengthPrecision": -1}, {"name": "umargin_ulim_percent", "type": "DOUBLE PRECISION", "checked": 1, "isVisible": 1, "defaultValue": "15", "lengthPrecision": -1}]}', FALSE, -1, CURRENT_TIMESTAMP);
|
||||
|
||||
INSERT INTO public.bus_bus_1_stable (id, global_uuid, attribute_group, uvpw_threshold_percent, uvpw_runtime, uvw_threshold_percent, uvw_runtime, ovpw_threshold_percent, ovpw_runtime, ovw_threshold_percent, ovw_runtime,
|
||||
umargin_pmax, umargin_qmax, umargin_ulim, umargin_plim_percent, umargin_qlim_percent, umargin_ulim_percent
|
||||
) VALUES (
|
||||
1,
|
||||
'968dd6e6-faec-4f78-b58a-d6e68426b08e',
|
||||
'stable',
|
||||
95,
|
||||
10,
|
||||
90,
|
||||
10,
|
||||
105,
|
||||
60,
|
||||
110,
|
||||
10,
|
||||
0,
|
||||
0,
|
||||
90,
|
||||
15,
|
||||
15,
|
||||
15
|
||||
);
|
||||
|
||||
INSERT INTO public.bus_bus_1_model (id, global_uuid, attribute_group,
|
||||
ui_percent, ui_kv, ui_pa, stability_rated_current, stability_dynamic_steady_current, load_adjustment_min, load_adjustment_max, bus_type, csc_s3_max, csc_s3_min, csc_i3_max, csc_i3_min, csc_z3s_max, csc_z3s_min, csc_s1_max, csc_s1_min, csc_i1_max, csc_i1_min, csc_z1s_max, csc_z1s_min, csc_base_voltage, csc_base_capacity
|
||||
) VALUES (
|
||||
1,
|
||||
'968dd6e6-faec-4f78-b58a-d6e68426b08e',
|
||||
'model',
|
||||
100,
|
||||
35,
|
||||
0,
|
||||
1000,
|
||||
40,
|
||||
100,
|
||||
100,
|
||||
'PQ母线',
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0.05,
|
||||
0.1,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0.05,
|
||||
0.1,
|
||||
37,
|
||||
100
|
||||
);
|
||||
|
||||
INSERT INTO public.bus_bus_1_base_extend (id, global_uuid, attribute_group,
|
||||
bus_num, unom_kv
|
||||
) VALUES (
|
||||
1,
|
||||
'968dd6e6-faec-4f78-b58a-d6e68426b08e',
|
||||
'base_extend',
|
||||
1,
|
||||
NULL
|
||||
);
|
||||
```
|
||||
|
||||
##### 2.4.2 Redis数据注入
|
||||
|
||||
Redis数据脚本
|
||||
|
||||
```Lua
|
||||
redis.call('SADD', 'grid_keys', 'transformfeeder1_220', 'transformfeeder1_220_35', 'transformfeeder1_220_36')
|
||||
redis.call('SADD', 'grid_transformfeeder1_220_zones_keys', 'I_A_rms', 'I_B_rms', 'I_C_rms')
|
||||
redis.call('SADD', 'grid_transformfeeder1_220_35_zones_keys', 'I_A_rms', 'I_B_rms', 'I_C_rms')
|
||||
redis.call('SADD', 'grid_transformfeeder1_220_36_zones_keys', 'I_A_rms', 'I_B_rms', 'I_C_rms')
|
||||
|
||||
local dict_key = 'search_suggestions_dict'
|
||||
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220_35', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220_36', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220.I_A_rms', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220.I_B_rms', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220.I_C_rms', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220_35.I_A_rms', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220_35.I_B_rms', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220_35.I_C_rms', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220_36.I_A_rms', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220_36.I_B_rms', 1)
|
||||
redis.call('FT.SUGADD', dict_key, 'transformfeeder1_220_36.I_C_rms', 1)
|
||||
|
||||
return 'OK'
|
||||
```shell
|
||||
deploy/redis-test-data/measurments-recommend/measurement_injection.go
|
||||
```
|
||||
|
||||
在Redis CLI 中导入命令
|
||||
运行脚本向 Reids 导入数据
|
||||
|
||||
1. 使用 `EVAL "lua脚本" 0`即可成功导入数据
|
||||
2. 使用 `SCRIPT LOAD "lua脚本"`加载脚本,然后使用 `EVAL SHA1值 0` 命令执行上一步存储命令返回的哈希值即可
|
||||
```shell
|
||||
go run deploy/redis-test-data/measurments-recommend/measurement_injection.go
|
||||
```
|
||||
|
||||
### 3\. 启动 ModelRT 服务
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
FROM golang:1.24-alpine AS builder
|
||||
|
||||
WORKDIR /app
|
||||
COPY go.mod .
|
||||
COPY go.sum .
|
||||
RUN GOPROXY="https://goproxy.cn,direct" go mod download
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o modelrt main.go
|
||||
|
||||
FROM alpine:latest
|
||||
WORKDIR /app
|
||||
ARG USER_ID=1000
|
||||
RUN adduser -D -u ${USER_ID} modelrt
|
||||
COPY --from=builder /app/modelrt ./modelrt
|
||||
COPY configs/config.example.yaml ./configs/config.example.yaml
|
||||
RUN chown -R modelrt:modelrt /app
|
||||
RUN chmod +x /app/modelrt
|
||||
USER modelrt
|
||||
CMD ["/app/modelrt", "-modelRT_config_dir=/app/configs"]
|
||||
|
|
@ -0,0 +1,327 @@
|
|||
// Package main implement redis test data injection
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/RediSearch/redisearch-go/v2/redisearch"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var ac *redisearch.Autocompleter
|
||||
|
||||
// InitAutocompleterWithPool define func of initialize the Autocompleter with redigo pool
|
||||
func init() {
|
||||
// ac = redisearch.NewAutocompleterFromPool(pool, redisSearchDictName)
|
||||
ac = redisearch.NewAutocompleter("localhost:6379", redisSearchDictName)
|
||||
}
|
||||
|
||||
const (
|
||||
gridKeysSet = "grid_tag_keys"
|
||||
zoneKeysSet = "zone_tag_keys"
|
||||
stationKeysSet = "station_tag_keys"
|
||||
componentNSPathKeysSet = "component_nspath_keys"
|
||||
componentTagKeysSet = "component_tag_keys"
|
||||
configKeysSet = "config_keys"
|
||||
measurementTagKeysSet = "measurement_tag_keys"
|
||||
|
||||
// Grid -> Zone (e.g., grid1_zones_keys)
|
||||
gridZoneSetKeyFormat = "grid%d_zone_tag_keys"
|
||||
// Zone -> Station (e.g., zone1_1_stations_keys)
|
||||
zoneStationSetKeyFormat = "zone%d_%d_station_tag_keys"
|
||||
// Station -> NSPath (e.g., station1_1_1_components_nspath_keys)
|
||||
stationNSPathKeyFormat = "station%d_%d_%d_component_nspath_keys"
|
||||
// NSPath -> CompTag (e.g., ns1_1_1_1_components_tag_keys)
|
||||
nsPathCompTagKeyFormat = "ns%d_%d_%d_%d_component_tag_keys"
|
||||
// CompTag -> Measurement (e.g., comptag1_1_1_1_1_measurement_keys)
|
||||
compTagMeasKeyFormat = "comptag%d_%d_%d_%d_%d_measurement_tag_keys"
|
||||
)
|
||||
|
||||
const (
|
||||
redisSearchDictName = "search_suggestions_dict"
|
||||
defaultScore = 1.0
|
||||
)
|
||||
|
||||
var configMetrics = []any{
|
||||
"component", "base_extend", "rated", "setup", "model",
|
||||
"stable", "bay", "craft", "integrity", "behavior",
|
||||
}
|
||||
|
||||
func bulkInsertAllHierarchySets(ctx context.Context, rdb *redis.Client) error {
|
||||
log.Println("starting bulk insertion of Redis hierarchy sets")
|
||||
|
||||
if err := insertStaticSets(ctx, rdb); err != nil {
|
||||
return fmt.Errorf("static set insertion failed: %w", err)
|
||||
}
|
||||
|
||||
if err := insertDynamicHierarchy(ctx, rdb); err != nil {
|
||||
return fmt.Errorf("dynamic hierarchy insertion failed: %w", err)
|
||||
}
|
||||
|
||||
if err := insertAllHierarchySuggestions(ac); err != nil {
|
||||
return fmt.Errorf("dynamic hierarchy insertion failed: %w", err)
|
||||
}
|
||||
|
||||
log.Println("bulk insertion complete")
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertStaticSets(ctx context.Context, rdb *redis.Client) error {
|
||||
// grid_keys
|
||||
if err := rdb.SAdd(ctx, gridKeysSet, "grid1", "grid2", "grid3").Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", gridKeysSet, err)
|
||||
}
|
||||
|
||||
// zone_keys (3x3 = 9 members)
|
||||
zoneMembers := make([]any, 0, 9)
|
||||
for i := 1; i <= 3; i++ {
|
||||
for j := 1; j <= 3; j++ {
|
||||
zoneMembers = append(zoneMembers, fmt.Sprintf("zone%d_%d", i, j))
|
||||
}
|
||||
}
|
||||
if err := rdb.SAdd(ctx, zoneKeysSet, zoneMembers...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", zoneKeysSet, err)
|
||||
}
|
||||
|
||||
// config_keys
|
||||
if err := rdb.SAdd(ctx, configKeysSet, "bay").Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", configKeysSet, err)
|
||||
}
|
||||
|
||||
log.Println("Static sets (grid_keys, zone_keys, config_keys) inserted.")
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertDynamicHierarchy(ctx context.Context, rdb *redis.Client) error {
|
||||
allStationKeys := make([]any, 0, 27)
|
||||
allNSPathKeys := make([]any, 0, 81)
|
||||
allCompTagKeys := make([]any, 0, 243)
|
||||
allMeasurementTagKeys := make([]any, 0, 729)
|
||||
|
||||
// S: Grid Prefix (1-3)
|
||||
for S := 1; S <= 3; S++ {
|
||||
// Grid-Zone Set Key: gridS_zones_keys
|
||||
gridZoneKey := fmt.Sprintf(gridZoneSetKeyFormat, S)
|
||||
gridZoneMembers := make([]any, 0, 3)
|
||||
|
||||
// Y: Zone Index (1-3)
|
||||
for Y := 1; Y <= 3; Y++ {
|
||||
zoneID := fmt.Sprintf("%d_%d", S, Y)
|
||||
zoneMember := "zone" + zoneID
|
||||
gridZoneMembers = append(gridZoneMembers, zoneMember)
|
||||
|
||||
// Zone-Station Set Key: zoneS_Y_stations_keys
|
||||
zoneStationKey := fmt.Sprintf(zoneStationSetKeyFormat, S, Y)
|
||||
zoneStationMembers := make([]any, 0, 3)
|
||||
|
||||
// Z: Station Index (1-3)
|
||||
for Z := 1; Z <= 3; Z++ {
|
||||
stationID := fmt.Sprintf("%d_%d_%d", S, Y, Z)
|
||||
stationKey := "station" + stationID
|
||||
allStationKeys = append(allStationKeys, stationKey)
|
||||
zoneStationMembers = append(zoneStationMembers, stationKey)
|
||||
|
||||
// Station-NSPath Set Key: stationS_Y_Z_components_nspath_keys
|
||||
stationNSPathKey := fmt.Sprintf(stationNSPathKeyFormat, S, Y, Z)
|
||||
stationNSMembers := make([]any, 0, 3)
|
||||
|
||||
// D: NSPath Index (1-3)
|
||||
for D := 1; D <= 3; D++ {
|
||||
nsPathID := fmt.Sprintf("%s_%d", stationID, D)
|
||||
nsPathKey := "ns" + nsPathID
|
||||
allNSPathKeys = append(allNSPathKeys, nsPathKey)
|
||||
stationNSMembers = append(stationNSMembers, nsPathKey)
|
||||
|
||||
// NSPath-CompTag Set Key: nsS_Y_Z_D_components_tag_keys
|
||||
nsCompTagKey := fmt.Sprintf(nsPathCompTagKeyFormat, S, Y, Z, D)
|
||||
nsCompTagMembers := make([]any, 0, 3)
|
||||
|
||||
// I: CompTag Index (1-3)
|
||||
for I := 1; I <= 3; I++ {
|
||||
compTagID := fmt.Sprintf("%s_%d", nsPathID, I)
|
||||
compTagKey := "comptag" + compTagID
|
||||
allCompTagKeys = append(allCompTagKeys, compTagKey)
|
||||
nsCompTagMembers = append(nsCompTagMembers, compTagKey)
|
||||
|
||||
// CompTag-Measurement Set Key: comptagS_Y_Z_D_I_measurement_keys
|
||||
compTagMeasKey := fmt.Sprintf(compTagMeasKeyFormat, S, Y, Z, D, I)
|
||||
compTagMeasMembers := make([]any, 0, 3)
|
||||
|
||||
// M: Measurement Index (1-3)
|
||||
for M := 1; M <= 3; M++ {
|
||||
measurementID := fmt.Sprintf("%s_%d", compTagID, M)
|
||||
measurementKey := "meas" + measurementID
|
||||
allMeasurementTagKeys = append(allMeasurementTagKeys, measurementKey)
|
||||
compTagMeasMembers = append(compTagMeasMembers, measurementKey)
|
||||
}
|
||||
|
||||
if err := rdb.SAdd(ctx, compTagMeasKey, compTagMeasMembers...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", compTagMeasKey, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := rdb.SAdd(ctx, nsCompTagKey, nsCompTagMembers...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", nsCompTagKey, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := rdb.SAdd(ctx, stationNSPathKey, stationNSMembers...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", stationNSPathKey, err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := rdb.SAdd(ctx, zoneStationKey, zoneStationMembers...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", zoneStationKey, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if err := rdb.SAdd(ctx, gridZoneKey, gridZoneMembers...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", gridZoneKey, err)
|
||||
}
|
||||
}
|
||||
|
||||
// 插入所有顶层动态 Set (将所有成员一次性插入到全局 Set 中)
|
||||
if err := rdb.SAdd(ctx, stationKeysSet, allStationKeys...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", stationKeysSet, err)
|
||||
}
|
||||
if err := rdb.SAdd(ctx, componentNSPathKeysSet, allNSPathKeys...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", componentNSPathKeysSet, err)
|
||||
}
|
||||
if err := rdb.SAdd(ctx, componentTagKeysSet, allCompTagKeys...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", componentTagKeysSet, err)
|
||||
}
|
||||
if err := rdb.SAdd(ctx, measurementTagKeysSet, allMeasurementTagKeys...).Err(); err != nil {
|
||||
return fmt.Errorf("sadd failed for %s: %w", measurementTagKeysSet, err)
|
||||
}
|
||||
|
||||
log.Printf("inserted %d stations, %d nspaths, %d comptags, and %d measurements.\n",
|
||||
len(allStationKeys), len(allNSPathKeys), len(allCompTagKeys), len(allMeasurementTagKeys))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertAllHierarchySuggestions(ac *redisearch.Autocompleter) error {
|
||||
suggestions := make([]redisearch.Suggestion, 0, 10000)
|
||||
// S: grid Index (1-3)
|
||||
for S := 1; S <= 3; S++ {
|
||||
gridStr := fmt.Sprintf("grid%d", S)
|
||||
suggestions = append(suggestions, redisearch.Suggestion{Term: gridStr, Score: defaultScore})
|
||||
|
||||
// Y: zone Index (1-3)
|
||||
for Y := 1; Y <= 3; Y++ {
|
||||
zoneStr := fmt.Sprintf("zone%d_%d", S, Y)
|
||||
gridZonePath := fmt.Sprintf("%s.%s", gridStr, zoneStr)
|
||||
suggestions = append(suggestions, redisearch.Suggestion{Term: gridZonePath, Score: defaultScore})
|
||||
|
||||
// Z: station Index (1-3)
|
||||
for Z := 1; Z <= 3; Z++ {
|
||||
stationStr := fmt.Sprintf("station%d_%d_%d", S, Y, Z)
|
||||
gridZoneStationPath := fmt.Sprintf("%s.%s", gridZonePath, stationStr)
|
||||
suggestions = append(suggestions, redisearch.Suggestion{Term: gridZoneStationPath, Score: defaultScore})
|
||||
|
||||
// D: nsPath Index (1-3)
|
||||
for D := 1; D <= 3; D++ {
|
||||
nsPathStr := fmt.Sprintf("ns%d_%d_%d_%d", S, Y, Z, D)
|
||||
gridZoneStationNSPath := fmt.Sprintf("%s.%s", gridZoneStationPath, nsPathStr)
|
||||
suggestions = append(suggestions, redisearch.Suggestion{Term: gridZoneStationNSPath, Score: defaultScore})
|
||||
|
||||
// I: compTag Index (1-3)
|
||||
for I := 1; I <= 3; I++ {
|
||||
compTagStr := fmt.Sprintf("comptag%d_%d_%d_%d_%d", S, Y, Z, D, I)
|
||||
fullCompTagPath := fmt.Sprintf("%s.%s", gridZoneStationNSPath, compTagStr)
|
||||
suggestions = append(suggestions, redisearch.Suggestion{Term: fullCompTagPath, Score: defaultScore})
|
||||
fullConfigPath := fmt.Sprintf("%s.%s", fullCompTagPath, "bay")
|
||||
suggestions = append(suggestions, redisearch.Suggestion{Term: fullConfigPath, Score: defaultScore})
|
||||
// J: measTag Index (1-3)
|
||||
for J := 1; J <= 3; J++ {
|
||||
measTagStr := fmt.Sprintf("meas%d_%d_%d_%d_%d_%d", S, Y, Z, D, I, J)
|
||||
fullMeasurementPath := fmt.Sprintf("%s.%s", fullCompTagPath, measTagStr)
|
||||
suggestions = append(suggestions, redisearch.Suggestion{Term: fullMeasurementPath, Score: defaultScore})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("generated %d suggestions. starting bulk insertion into dictionary '%s'.", len(suggestions), redisSearchDictName)
|
||||
|
||||
// del ac suggestion
|
||||
ac.Delete()
|
||||
|
||||
err := ac.AddTerms(suggestions...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to add %d suggestions: %w", len(suggestions), err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteAllHierarchySets(ctx context.Context, rdb *redis.Client) error {
|
||||
log.Println("starting to collect all Redis Set keys for deletion...")
|
||||
|
||||
keysToDelete := []string{
|
||||
gridKeysSet,
|
||||
zoneKeysSet,
|
||||
stationKeysSet,
|
||||
componentNSPathKeysSet,
|
||||
componentTagKeysSet,
|
||||
configKeysSet,
|
||||
measurementTagKeysSet,
|
||||
}
|
||||
|
||||
for S := 1; S <= 3; S++ {
|
||||
keysToDelete = append(keysToDelete, fmt.Sprintf(gridZoneSetKeyFormat, S))
|
||||
|
||||
for Y := 1; Y <= 3; Y++ {
|
||||
keysToDelete = append(keysToDelete, fmt.Sprintf(zoneStationSetKeyFormat, S, Y))
|
||||
|
||||
for Z := 1; Z <= 3; Z++ {
|
||||
keysToDelete = append(keysToDelete, fmt.Sprintf(stationNSPathKeyFormat, S, Y, Z))
|
||||
|
||||
for D := 1; D <= 3; D++ {
|
||||
keysToDelete = append(keysToDelete, fmt.Sprintf(nsPathCompTagKeyFormat, S, Y, Z, D))
|
||||
|
||||
for I := 1; I <= 3; I++ {
|
||||
keysToDelete = append(keysToDelete, fmt.Sprintf(compTagMeasKeyFormat, S, Y, Z, D, I))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("collected %d unique keys. Starting batch deletion...", len(keysToDelete))
|
||||
|
||||
deletedCount, err := rdb.Del(ctx, keysToDelete...).Result()
|
||||
if err != nil {
|
||||
return fmt.Errorf("batch deletion failed: %w", err)
|
||||
}
|
||||
|
||||
log.Printf("Successfully deleted %d keys (Sets) from Redis.", deletedCount)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: "localhost:6379",
|
||||
Password: "",
|
||||
DB: 0,
|
||||
})
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||
log.Fatalf("could not connect to Redis: %v", err)
|
||||
}
|
||||
log.Println("connected to Redis successfully")
|
||||
|
||||
if err := deleteAllHierarchySets(ctx, rdb); err != nil {
|
||||
log.Fatalf("error delete exist set before bulk insertion: %v", err)
|
||||
}
|
||||
|
||||
if err := bulkInsertAllHierarchySets(ctx, rdb); err != nil {
|
||||
log.Fatalf("error during bulk insertion: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,224 @@
|
|||
// Package main implement redis test data injection
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"modelRT/orm"
|
||||
|
||||
util "modelRT/deploy/redis-test-data/util"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
redisAddr = "localhost:6379"
|
||||
)
|
||||
|
||||
var globalRedisClient *redis.Client
|
||||
|
||||
var (
|
||||
highEnd, highStart, lowStart, lowEnd int
|
||||
totalLength int
|
||||
highSegmentLength int
|
||||
lowSegmentLength int
|
||||
)
|
||||
|
||||
func selectRandomInt() int {
|
||||
options := []int{0, 2}
|
||||
randomIndex := rand.Intn(len(options))
|
||||
return options[randomIndex]
|
||||
}
|
||||
|
||||
// generateMixedData define func to generate a set of floating-point data that meets specific conditions
|
||||
func generateMixedData(highMin, lowMin, highBase, lowBase, baseValue, normalBase float64) []float64 {
|
||||
totalLength = 500
|
||||
highSegmentLength = 20
|
||||
lowSegmentLength = 20
|
||||
|
||||
seed := time.Now().UnixNano()
|
||||
source := rand.NewSource(seed)
|
||||
r := rand.New(source)
|
||||
|
||||
data := make([]float64, totalLength)
|
||||
highStart = rand.Intn(totalLength - highSegmentLength - lowSegmentLength - 1)
|
||||
highEnd = highStart + highSegmentLength
|
||||
lowStart = rand.Intn(totalLength-lowSegmentLength-highEnd) + highEnd
|
||||
lowEnd = lowStart + lowSegmentLength
|
||||
|
||||
for i := 0; i < totalLength; i++ {
|
||||
if i >= highStart && i < highStart+highSegmentLength {
|
||||
// 数据值均大于 55.0,在 [55.5, 60.0] 范围内随机
|
||||
// rand.Float64() 生成 [0.0, 1.0) 范围的浮点数
|
||||
data[i] = highMin + r.Float64()*(highBase)
|
||||
} else if i >= lowStart && i < lowStart+lowSegmentLength {
|
||||
// 数据值均小于 45.0,在 [40.0, 44.5] 范围内随机
|
||||
data[i] = lowMin + r.Float64()*(lowBase)
|
||||
} else {
|
||||
// 数据在 [45.0, 55.0] 范围内随机 (baseValue ± 5)
|
||||
// 50 + rand.Float64() * 10 - 5
|
||||
change := normalBase - r.Float64()*normalBase*2
|
||||
data[i] = baseValue + change
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func generateNormalData(baseValue, normalBase float64) []float64 {
|
||||
totalLength = 500
|
||||
seed := time.Now().UnixNano()
|
||||
source := rand.NewSource(seed)
|
||||
r := rand.New(source)
|
||||
|
||||
data := make([]float64, totalLength)
|
||||
for i := 0; i < totalLength; i++ {
|
||||
change := normalBase - r.Float64()*normalBase*2
|
||||
data[i] = baseValue + change
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func main() {
|
||||
rootCtx := context.Background()
|
||||
|
||||
pgURI := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", "192.168.1.101", 5432, "postgres", "coslight", "demo")
|
||||
|
||||
postgresDBClient, err := gorm.Open(postgres.Open(pgURI))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
sqlDB, err := postgresDBClient.DB()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sqlDB.Close()
|
||||
}()
|
||||
|
||||
cancelCtx, cancel := context.WithTimeout(rootCtx, 5*time.Second)
|
||||
defer cancel()
|
||||
var measurements []orm.Measurement
|
||||
result := postgresDBClient.WithContext(cancelCtx).Find(&measurements)
|
||||
if result.Error != nil {
|
||||
panic(result.Error)
|
||||
}
|
||||
log.Println("总共读取到测量点数量:", len(measurements))
|
||||
measInfos := util.ProcessMeasurements(measurements)
|
||||
|
||||
globalRedisClient = util.InitRedisClient(redisAddr)
|
||||
rCancelCtx, cancel := context.WithCancel(rootCtx)
|
||||
defer cancel()
|
||||
|
||||
for key, measInfo := range measInfos {
|
||||
randomType := selectRandomType()
|
||||
var datas []float64
|
||||
if randomType {
|
||||
// 生成正常数据
|
||||
log.Printf("key:%s generate normal data\n", key)
|
||||
baseValue := measInfo.BaseValue
|
||||
changes := measInfo.Changes
|
||||
normalBase := changes[0]
|
||||
noramlMin := baseValue - normalBase
|
||||
normalMax := baseValue + normalBase
|
||||
datas = generateNormalData(baseValue, normalBase)
|
||||
allTrue := true
|
||||
|
||||
for i := 0; i < totalLength-1; i++ {
|
||||
value := datas[i]
|
||||
// log.Printf("index:%d, value:%.2f\n", i, value)
|
||||
if value < noramlMin && value > normalMax {
|
||||
allTrue = false
|
||||
}
|
||||
}
|
||||
log.Printf("// 验证结果: 所有值是否 >= %.2f或 <= %.2f %t\n", noramlMin, normalMax, allTrue)
|
||||
} else {
|
||||
// 生成异常数据
|
||||
log.Printf("key:%s generate abnormal data\n", key)
|
||||
var highMin, highBase float64
|
||||
var lowMin, lowBase float64
|
||||
var normalBase float64
|
||||
|
||||
// TODO 生成一次测试数据
|
||||
changes := measInfo.Changes
|
||||
baseValue := measInfo.BaseValue
|
||||
if len(changes) == 2 {
|
||||
highMin = baseValue + changes[0]
|
||||
lowMin = baseValue + changes[1]
|
||||
highBase = changes[0]
|
||||
lowBase = changes[1]
|
||||
normalBase = changes[0]
|
||||
} else {
|
||||
randomIndex := selectRandomInt()
|
||||
highMin = baseValue + changes[randomIndex]
|
||||
lowMin = baseValue + changes[randomIndex+1]
|
||||
highBase = changes[randomIndex]
|
||||
lowBase = changes[randomIndex+1]
|
||||
normalBase = changes[0]
|
||||
}
|
||||
|
||||
datas = generateMixedData(highMin, lowMin, highBase, lowBase, baseValue, normalBase)
|
||||
// log.Printf("key:%s\n datas:%v\n", key, datas)
|
||||
|
||||
allHigh := true
|
||||
for i := highStart; i < highEnd; i++ {
|
||||
if datas[i] <= highMin {
|
||||
allHigh = false
|
||||
break
|
||||
}
|
||||
}
|
||||
log.Printf("// 验证结果 (高值段在 %d-%d): 所有值是否 > %.2f? %t\n", highStart, highEnd-1, highMin, allHigh)
|
||||
|
||||
allLow := true
|
||||
for i := lowStart; i < lowEnd; i++ {
|
||||
if datas[i] >= lowMin {
|
||||
allLow = false
|
||||
break
|
||||
}
|
||||
}
|
||||
log.Printf("// 验证结果 (低值段在 %d-%d): 所有值是否 < %.2f? %t\n", lowStart, lowEnd-1, lowMin, allLow)
|
||||
|
||||
allTrue := true
|
||||
for i := 0; i < totalLength-1; i++ {
|
||||
value := datas[i]
|
||||
if i < highStart || (i >= highEnd && i < lowStart) || i >= lowEnd {
|
||||
// log.Printf("index:%d, value:%.2f\n", i, value)
|
||||
if value >= highMin && value <= lowMin {
|
||||
allTrue = false
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Printf("// 验证结果 (正常段在 %d-%d): 所有值是否 <= %.2f或>= %.2f %t\n", 0, totalLength-1, highMin, lowMin, allTrue)
|
||||
}
|
||||
log.Printf("启动数据写入程序, Redis Key: %s, 基准值: %.4f, 变化范围: %+v\n", key, measInfo.BaseValue, measInfo.Changes)
|
||||
pipe := globalRedisClient.Pipeline()
|
||||
redisZs := make([]redis.Z, 0, totalLength)
|
||||
currentTime := time.Now().UnixNano()
|
||||
for i := range totalLength {
|
||||
sequentialTime := currentTime + int64(i)
|
||||
z := redis.Z{
|
||||
Score: datas[i],
|
||||
Member: strconv.FormatInt(sequentialTime, 10),
|
||||
}
|
||||
redisZs = append(redisZs, z)
|
||||
}
|
||||
log.Printf("启动数据写入程序, Redis Key: %s, 写入数据量: %d\n", key, len(redisZs))
|
||||
pipe.ZAdd(rCancelCtx, key, redisZs...)
|
||||
_, err = pipe.Exec(rCancelCtx)
|
||||
if err != nil {
|
||||
log.Printf("redis pipeline execution failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func selectRandomType() bool {
|
||||
options := []int{0, 2}
|
||||
randomValue := rand.Intn(len(options))
|
||||
return randomValue != 0
|
||||
}
|
||||
|
|
@ -0,0 +1,449 @@
|
|||
// Package main implement redis test data injection
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"modelRT/deploy/redis-test-data/util"
|
||||
"modelRT/orm"
|
||||
|
||||
redis "github.com/redis/go-redis/v9"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Redis配置
|
||||
const (
|
||||
redisAddr = "localhost:6379"
|
||||
)
|
||||
|
||||
var globalRedisClient *redis.Client
|
||||
|
||||
// outlierConfig 异常段配置
|
||||
type outlierConfig struct {
|
||||
Enabled bool // 是否启用异常段
|
||||
Count int // 异常段数量 (0=随机, 1-5=指定数量)
|
||||
MinLength int // 异常段最小长度
|
||||
MaxLength int // 异常段最大长度
|
||||
Intensity float64 // 异常强度系数 (1.0=轻微超出, 2.0=显著超出)
|
||||
Distribution string // 分布类型 "both"-上下都有, "upper"-只向上, "lower"-只向下
|
||||
}
|
||||
|
||||
// GenerateFloatSliceWithOutliers 生成包含连续异常段的数据
|
||||
// baseValue: 基准值
|
||||
// changes: 变化范围,每2个元素为一组 [minChange1, maxChange1, minChange2, maxChange2, ...]
|
||||
// size: 生成的切片长度
|
||||
// variationType: 变化类型
|
||||
// outlierConfig: 异常段配置
|
||||
func generateFloatSliceWithOutliers(baseValue float64, changes []float64, size int, variationType string, outlierConfig outlierConfig) ([]float64, error) {
|
||||
// 先生成正常数据
|
||||
data, err := generateFloatSlice(baseValue, changes, size, variationType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 插入异常段
|
||||
if outlierConfig.Enabled {
|
||||
data = insertOutliers(data, baseValue, changes, outlierConfig)
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// 插入异常段
|
||||
func insertOutliers(data []float64, baseValue float64, changes []float64, config outlierConfig) []float64 {
|
||||
if len(data) == 0 || !config.Enabled {
|
||||
return data
|
||||
}
|
||||
|
||||
// 获取变化范围的边界
|
||||
minBound, maxBound := getChangeBounds(baseValue, changes)
|
||||
// TODO delete
|
||||
log.Printf("获取变化范围的边界,min:%.4f,max:%.4f\n", minBound, maxBound)
|
||||
|
||||
// 确定异常段数量
|
||||
outlierCount := config.Count
|
||||
if outlierCount == 0 {
|
||||
// 随机生成1-3个异常段
|
||||
outlierCount = rand.Intn(3) + 1
|
||||
}
|
||||
|
||||
// 计算最大可能的异常段数量
|
||||
maxPossibleOutliers := len(data) / (config.MinLength + 10)
|
||||
if outlierCount > maxPossibleOutliers {
|
||||
outlierCount = maxPossibleOutliers
|
||||
}
|
||||
|
||||
// 生成异常段位置
|
||||
segments := generateOutlierSegments(len(data), config.MinLength, config.MaxLength, outlierCount, config.Distribution)
|
||||
// TODO 调试信息待删除
|
||||
log.Printf("生成异常段位置:%+v\n", segments)
|
||||
// 插入异常数据
|
||||
for _, segment := range segments {
|
||||
data = insertOutlierSegment(data, segment, minBound, maxBound, config)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// 获取变化范围的边界
|
||||
func getChangeBounds(baseValue float64, changes []float64) (minBound, maxBound float64) {
|
||||
if len(changes) == 0 {
|
||||
return baseValue - 10, baseValue + 10
|
||||
}
|
||||
|
||||
ranges := normalizeRanges(changes)
|
||||
minBound, maxBound = baseValue+ranges[0][0], baseValue+ranges[0][1]
|
||||
|
||||
for _, r := range ranges {
|
||||
if baseValue+r[0] < minBound {
|
||||
minBound = baseValue + r[0]
|
||||
}
|
||||
if baseValue+r[1] > maxBound {
|
||||
maxBound = baseValue + r[1]
|
||||
}
|
||||
}
|
||||
|
||||
return minBound, maxBound
|
||||
}
|
||||
|
||||
// OutlierSegment 异常段定义
|
||||
type OutlierSegment struct {
|
||||
Start int
|
||||
Length int
|
||||
Type string // "upper"-向上异常, "lower"-向下异常
|
||||
}
|
||||
|
||||
func generateOutlierSegments(totalSize, minLength, maxLength, count int, distribution string) []OutlierSegment {
|
||||
if count == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
segments := make([]OutlierSegment, 0, count)
|
||||
usedPositions := make(map[int]bool)
|
||||
|
||||
for i := 0; i < count; i++ {
|
||||
// 尝试多次寻找合适的位置
|
||||
for attempt := 0; attempt < 10; attempt++ {
|
||||
length := rand.Intn(maxLength-minLength+1) + minLength
|
||||
start := rand.Intn(totalSize - length)
|
||||
|
||||
// 检查是否与已有段重叠
|
||||
overlap := false
|
||||
for pos := start; pos < start+length; pos++ {
|
||||
if usedPositions[pos] {
|
||||
overlap = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !overlap {
|
||||
// 标记已使用的位置
|
||||
for pos := start; pos < start+length; pos++ {
|
||||
usedPositions[pos] = true
|
||||
}
|
||||
|
||||
// 根据 distribution 配置决定异常类型
|
||||
var outlierType string
|
||||
switch distribution {
|
||||
case "upper":
|
||||
outlierType = "upper"
|
||||
case "lower":
|
||||
outlierType = "lower"
|
||||
case "both":
|
||||
fallthrough
|
||||
default:
|
||||
if rand.Float64() < 0.5 {
|
||||
outlierType = "upper"
|
||||
} else {
|
||||
outlierType = "lower"
|
||||
}
|
||||
}
|
||||
|
||||
segments = append(segments, OutlierSegment{
|
||||
Start: start,
|
||||
Length: length,
|
||||
Type: outlierType,
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return segments
|
||||
}
|
||||
|
||||
func insertOutlierSegment(data []float64, segment OutlierSegment, minBound, maxBound float64, config outlierConfig) []float64 {
|
||||
rangeWidth := maxBound - minBound
|
||||
|
||||
// 确定整个异常段的方向
|
||||
outlierType := segment.Type
|
||||
if outlierType == "" {
|
||||
switch config.Distribution {
|
||||
case "upper":
|
||||
outlierType = "upper"
|
||||
case "lower":
|
||||
outlierType = "lower"
|
||||
default:
|
||||
if rand.Float64() < 0.5 {
|
||||
outlierType = "upper"
|
||||
} else {
|
||||
outlierType = "lower"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 为整个段生成同方向异常值
|
||||
for i := segment.Start; i < segment.Start+segment.Length && i < len(data); i++ {
|
||||
excess := rangeWidth * (0.3 + rand.Float64()*config.Intensity)
|
||||
|
||||
if outlierType == "upper" {
|
||||
data[i] = maxBound + excess
|
||||
} else {
|
||||
data[i] = minBound - excess
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
func detectOutlierSegments(data []float64, baseValue float64, changes []float64, minSegmentLength int) []OutlierSegment {
|
||||
if len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
minBound, maxBound := getChangeBounds(baseValue, changes)
|
||||
var segments []OutlierSegment
|
||||
currentStart := -1
|
||||
currentType := ""
|
||||
|
||||
for i, value := range data {
|
||||
isOutlier := value > maxBound || value < minBound
|
||||
|
||||
if isOutlier {
|
||||
outlierType := "upper"
|
||||
if value < minBound {
|
||||
outlierType = "lower"
|
||||
}
|
||||
|
||||
if currentStart == -1 {
|
||||
// 开始新的异常段
|
||||
currentStart = i
|
||||
currentType = outlierType
|
||||
} else if currentType != outlierType {
|
||||
// 类型变化,结束当前段
|
||||
if i-currentStart >= minSegmentLength {
|
||||
segments = append(segments, OutlierSegment{
|
||||
Start: currentStart,
|
||||
Length: i - currentStart,
|
||||
Type: currentType,
|
||||
})
|
||||
}
|
||||
currentStart = i
|
||||
currentType = outlierType
|
||||
}
|
||||
} else {
|
||||
if currentStart != -1 {
|
||||
// 结束当前异常段
|
||||
if i-currentStart >= minSegmentLength {
|
||||
segments = append(segments, OutlierSegment{
|
||||
Start: currentStart,
|
||||
Length: i - currentStart,
|
||||
Type: currentType,
|
||||
})
|
||||
}
|
||||
currentStart = -1
|
||||
currentType = ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 处理最后的异常段
|
||||
if currentStart != -1 && len(data)-currentStart >= minSegmentLength {
|
||||
segments = append(segments, OutlierSegment{
|
||||
Start: currentStart,
|
||||
Length: len(data) - currentStart,
|
||||
Type: currentType,
|
||||
})
|
||||
}
|
||||
|
||||
return segments
|
||||
}
|
||||
|
||||
func generateFloatSlice(baseValue float64, changes []float64, size int, variationType string) ([]float64, error) {
|
||||
return generateRandomData(baseValue, changes, size), nil
|
||||
}
|
||||
|
||||
func normalizeRanges(changes []float64) [][2]float64 {
|
||||
ranges := make([][2]float64, len(changes)/2)
|
||||
for i := 0; i < len(changes); i += 2 {
|
||||
min, max := changes[i], changes[i+1]
|
||||
if min > max {
|
||||
min, max = max, min
|
||||
}
|
||||
ranges[i/2] = [2]float64{min, max}
|
||||
}
|
||||
return ranges
|
||||
}
|
||||
|
||||
func generateRandomData(baseValue float64, changes []float64, size int) []float64 {
|
||||
data := make([]float64, size)
|
||||
ranges := normalizeRanges(changes)
|
||||
for i := range data {
|
||||
rangeIdx := rand.Intn(len(ranges))
|
||||
minChange := ranges[rangeIdx][0]
|
||||
maxChange := ranges[rangeIdx][1]
|
||||
change := minChange + rand.Float64()*(maxChange-minChange)
|
||||
data[i] = baseValue + change
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
// simulateDataWrite 定时生成并写入模拟数据到 Redis ZSet
|
||||
func simulateDataWrite(ctx context.Context, rdb *redis.Client, redisKey string, config outlierConfig, measInfo util.CalculationResult) {
|
||||
log.Printf("启动数据写入程序, Redis Key: %s, 基准值: %.4f, 变化范围: %+v\n", redisKey, measInfo.BaseValue, measInfo.Changes)
|
||||
ticker := time.NewTicker(3 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
pipe := rdb.Pipeline()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Printf("\n[%s] 写入程序已停止\n", redisKey)
|
||||
return
|
||||
case <-ticker.C:
|
||||
minBound, maxBound := getChangeBounds(measInfo.BaseValue, measInfo.Changes)
|
||||
log.Printf("计算边界: [%.4f, %.4f]\n", minBound, maxBound)
|
||||
|
||||
// 根据基准值类型决定如何处理
|
||||
switch measInfo.BaseType {
|
||||
case "TI":
|
||||
// 边沿触发类型,生成特殊处理的数据
|
||||
log.Printf("边沿触发类型,跳过异常数据生成\n")
|
||||
return
|
||||
case "TE":
|
||||
// 正常上下限类型,生成包含异常的数据
|
||||
if len(measInfo.Changes) == 0 {
|
||||
log.Printf("无变化范围数据,跳过\n")
|
||||
return
|
||||
}
|
||||
|
||||
// 根据变化范围数量调整异常配置
|
||||
if len(measInfo.Changes) == 2 {
|
||||
// 只有上下限
|
||||
config.Distribution = "both"
|
||||
} else if len(measInfo.Changes) == 4 {
|
||||
// 有上下限和预警上下限
|
||||
config.Distribution = "both"
|
||||
config.Intensity = 2.0 // 增强异常强度
|
||||
}
|
||||
|
||||
// 生成包含异常的数据
|
||||
data, err := generateFloatSliceWithOutliers(
|
||||
measInfo.BaseValue,
|
||||
measInfo.Changes,
|
||||
measInfo.Size,
|
||||
"random",
|
||||
config,
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("生成异常数据失败:%v\n", err)
|
||||
continue
|
||||
}
|
||||
|
||||
segments := detectOutlierSegments(data, measInfo.BaseValue, measInfo.Changes, config.MinLength)
|
||||
log.Printf("检测到异常段数量:%d\n", len(segments))
|
||||
for i, segment := range segments {
|
||||
log.Printf("异常段%d: 位置[%d-%d], 长度=%d, 类型=%s\n",
|
||||
i+1, segment.Start, segment.Start+segment.Length-1, segment.Length, segment.Type)
|
||||
}
|
||||
|
||||
redisZs := make([]redis.Z, 0, len(data))
|
||||
for i := range len(data) {
|
||||
z := redis.Z{
|
||||
Score: data[i],
|
||||
Member: strconv.FormatInt(time.Now().UnixNano(), 10),
|
||||
}
|
||||
redisZs = append(redisZs, z)
|
||||
}
|
||||
pipe.ZAdd(ctx, redisKey, redisZs...)
|
||||
_, err = pipe.Exec(ctx)
|
||||
if err != nil {
|
||||
log.Printf("redis pipeline execution failed: %v", err)
|
||||
}
|
||||
log.Printf("生成 redis 实时数据成功\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func gracefulShutdown() {
|
||||
if globalRedisClient != nil {
|
||||
if err := globalRedisClient.Close(); err != nil {
|
||||
log.Printf("关闭 Redis 客户端失败:%v", err)
|
||||
} else {
|
||||
log.Println("关闭 Redis 客户端成功")
|
||||
}
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func main() {
|
||||
rootCtx := context.Background()
|
||||
|
||||
pgURI := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", "192.168.1.101", 5432, "postgres", "coslight", "demo")
|
||||
|
||||
postgresDBClient, err := gorm.Open(postgres.Open(pgURI))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer func() {
|
||||
sqlDB, err := postgresDBClient.DB()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
sqlDB.Close()
|
||||
}()
|
||||
|
||||
cancelCtx, cancel := context.WithTimeout(rootCtx, 5*time.Second)
|
||||
defer cancel()
|
||||
var measurements []orm.Measurement
|
||||
result := postgresDBClient.WithContext(cancelCtx).Find(&measurements)
|
||||
if result.Error != nil {
|
||||
panic(result.Error)
|
||||
}
|
||||
log.Println("总共读取到测量点数量:", len(measurements))
|
||||
measInfos := util.ProcessMeasurements(measurements)
|
||||
|
||||
// 测量点数据生成(包含异常数据)
|
||||
// 配置异常段参数
|
||||
outlierConfig := outlierConfig{
|
||||
Enabled: true, // 是否产生异常段数据
|
||||
Count: 2, // 异常段数量
|
||||
MinLength: 10, // 异常段最小连续长度
|
||||
MaxLength: 15, // 异常段最大连续长度
|
||||
Intensity: 1.5, // 异常强度
|
||||
Distribution: "both", // 分布类型
|
||||
}
|
||||
|
||||
globalRedisClient = util.InitRedisClient(redisAddr)
|
||||
rCancelCtx, cancel := context.WithCancel(rootCtx)
|
||||
defer cancel()
|
||||
|
||||
for key, measInfo := range measInfos {
|
||||
go simulateDataWrite(rCancelCtx, globalRedisClient, key, outlierConfig, measInfo)
|
||||
}
|
||||
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sigChan
|
||||
gracefulShutdown()
|
||||
}
|
||||
|
|
@ -0,0 +1,266 @@
|
|||
// Package util provide some utility fun
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"modelRT/orm"
|
||||
)
|
||||
|
||||
type CalculationResult struct {
|
||||
BaseValue float64
|
||||
Changes []float64
|
||||
Size int
|
||||
BaseType string // "normal", "warning", "edge"
|
||||
Message string
|
||||
}
|
||||
|
||||
func ProcessMeasurements(measurements []orm.Measurement) map[string]CalculationResult {
|
||||
results := make(map[string]CalculationResult, len(measurements))
|
||||
for _, measurement := range measurements {
|
||||
// 检查 DataSource 是否存在且 type 为 1
|
||||
if measurement.DataSource == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// 检查 type 是否为 1
|
||||
dataType, typeExists := measurement.DataSource["type"]
|
||||
if !typeExists {
|
||||
continue
|
||||
}
|
||||
|
||||
// 类型断言,处理不同的数字类型
|
||||
var typeValue int
|
||||
switch v := dataType.(type) {
|
||||
case int:
|
||||
typeValue = v
|
||||
case float64:
|
||||
typeValue = int(v)
|
||||
case int64:
|
||||
typeValue = int(v)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
if typeValue != 1 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取 io_address
|
||||
ioAddressRaw, ioExists := measurement.DataSource["io_address"]
|
||||
if !ioExists {
|
||||
continue
|
||||
}
|
||||
|
||||
ioAddress, ok := ioAddressRaw.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
station, _ := ioAddress["station"].(string)
|
||||
device, _ := ioAddress["device"].(string)
|
||||
channel, _ := ioAddress["channel"].(string)
|
||||
|
||||
result := fmt.Sprintf("%s:%s:phasor:%s", station, device, channel)
|
||||
if measurement.EventPlan == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
causeValue, causeExist := measurement.EventPlan["cause"]
|
||||
if !causeExist {
|
||||
continue
|
||||
}
|
||||
causeMap, ok := causeValue.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
calResult, err := calculateBaseValueEnhanced(causeMap)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
calResult.Size = measurement.Size
|
||||
results[result] = calResult
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
func calculateBaseValueEnhanced(data map[string]any) (CalculationResult, error) {
|
||||
result := CalculationResult{}
|
||||
if edge, exists := data["edge"]; exists {
|
||||
value, err := calculateEdgeValue(edge)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
if edge == "raising" {
|
||||
result.Changes = []float64{1.0}
|
||||
} else {
|
||||
result.Changes = []float64{0.0}
|
||||
}
|
||||
|
||||
result.BaseValue = value
|
||||
result.BaseType = "TI"
|
||||
result.Message = "边沿触发基准值"
|
||||
return result, nil
|
||||
}
|
||||
|
||||
hasUpDown := HasKeys(data, "up", "down")
|
||||
hasUpUpDownDown := HasKeys(data, "upup", "downdown")
|
||||
result.BaseType = "TE"
|
||||
switch {
|
||||
case hasUpDown && hasUpUpDownDown:
|
||||
value, err := calculateAverage(data, "up", "down")
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.BaseValue = value
|
||||
result.Changes, err = calculateChanges(data, value, false, 4)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Message = "上下限基准值(忽略预警上上下下限)"
|
||||
return result, nil
|
||||
|
||||
case hasUpDown:
|
||||
value, err := calculateAverage(data, "up", "down")
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.BaseValue = value
|
||||
result.Changes, err = calculateChanges(data, value, false, 2)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Message = "上下限基准值"
|
||||
return result, nil
|
||||
|
||||
case hasUpUpDownDown:
|
||||
value, err := calculateAverage(data, "upup", "downdown")
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.BaseValue = value
|
||||
result.Changes, err = calculateChanges(data, value, true, 2)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Message = "上上下下限基准值"
|
||||
return result, nil
|
||||
|
||||
default:
|
||||
return result, fmt.Errorf("不支持的数据结构: %v", data)
|
||||
}
|
||||
}
|
||||
|
||||
func calculateAverage(data map[string]any, key1, key2 string) (float64, error) {
|
||||
val1, err := getFloatValue(data, key1)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
val2, err := getFloatValue(data, key2)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return (val1 + val2) / 2.0, nil
|
||||
}
|
||||
|
||||
func calculateChanges(data map[string]any, baseValue float64, maxLimt bool, limitNum int) ([]float64, error) {
|
||||
results := make([]float64, 0, limitNum)
|
||||
switch limitNum {
|
||||
case 2:
|
||||
var key1, key2 string
|
||||
if maxLimt {
|
||||
key1 = "upup"
|
||||
key2 = "downdown"
|
||||
} else {
|
||||
key1 = "up"
|
||||
key2 = "down"
|
||||
}
|
||||
val1, err := getFloatValue(data, key1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, val1-baseValue)
|
||||
|
||||
val2, err := getFloatValue(data, key2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, val2-baseValue)
|
||||
case 4:
|
||||
key1 := "up"
|
||||
key2 := "down"
|
||||
key3 := "upup"
|
||||
key4 := "downdown"
|
||||
|
||||
val1, err := getFloatValue(data, key1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, val1-baseValue)
|
||||
|
||||
val2, err := getFloatValue(data, key2)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, val2-baseValue)
|
||||
|
||||
val3, err := getFloatValue(data, key3)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, val3-baseValue)
|
||||
|
||||
val4, err := getFloatValue(data, key4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
results = append(results, val4-baseValue)
|
||||
}
|
||||
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func getFloatValue(data map[string]any, key string) (float64, error) {
|
||||
value, exists := data[key]
|
||||
if !exists {
|
||||
return 0, fmt.Errorf("缺少必需的键:%s", key)
|
||||
}
|
||||
|
||||
switch v := value.(type) {
|
||||
case float64:
|
||||
return v, nil
|
||||
case int:
|
||||
return float64(v), nil
|
||||
case float32:
|
||||
return float64(v), nil
|
||||
default:
|
||||
return 0, fmt.Errorf("键 %s 的值类型错误,期望数字类型,得到 %T", key, value)
|
||||
}
|
||||
}
|
||||
|
||||
func HasKeys(data map[string]any, keys ...string) bool {
|
||||
for _, key := range keys {
|
||||
if _, exists := data[key]; !exists {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func calculateEdgeValue(edge any) (float64, error) {
|
||||
edgeStr, ok := edge.(string)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("edge 字段类型错误,期望 string,得到 %T", edge)
|
||||
}
|
||||
|
||||
switch edgeStr {
|
||||
case "raising":
|
||||
return 1.0, nil
|
||||
case "falling":
|
||||
return 0.0, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("不支持的 edge 值: %s", edgeStr)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
// Package util provide some utility fun
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// InitRedisClient define func to initialize and return a redis client
|
||||
func InitRedisClient(redisAddr string) *redis.Client {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: redisAddr,
|
||||
Password: "",
|
||||
DB: 0,
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err := rdb.Ping(ctx).Result()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return rdb
|
||||
}
|
||||
|
|
@ -0,0 +1,36 @@
|
|||
// Package diagram provide diagram data structure and operation
|
||||
package diagram
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// RedisClient define struct to accessing redis data that does not require the use of distributed locks
|
||||
type RedisClient struct {
|
||||
Client *redis.Client
|
||||
}
|
||||
|
||||
// NewRedisClient define func of new redis client instance
|
||||
func NewRedisClient() *RedisClient {
|
||||
return &RedisClient{
|
||||
Client: GetRedisClientInstance(),
|
||||
}
|
||||
}
|
||||
|
||||
// QueryByZRangeByLex define func to query real time data from redis zset
|
||||
func (rc *RedisClient) QueryByZRangeByLex(ctx context.Context, key string, size int64) ([]redis.Z, error) {
|
||||
client := rc.Client
|
||||
args := redis.ZRangeArgs{
|
||||
Key: key,
|
||||
Start: 0,
|
||||
Stop: size,
|
||||
ByScore: false,
|
||||
ByLex: false,
|
||||
Rev: false,
|
||||
Offset: 0,
|
||||
Count: 0,
|
||||
}
|
||||
return client.ZRangeArgsWithScores(ctx, args).Result()
|
||||
}
|
||||
|
|
@ -12,82 +12,85 @@ import (
|
|||
// RedisHash defines the encapsulation struct of redis hash type
|
||||
type RedisHash struct {
|
||||
ctx context.Context
|
||||
hashKey string
|
||||
rwLocker *locker.RedissionRWLocker
|
||||
storageClient *redis.Client
|
||||
}
|
||||
|
||||
// NewRedisHash define func of new redis hash instance
|
||||
func NewRedisHash(ctx context.Context, hashKey string, token string, lockLeaseTime uint64, needRefresh bool) *RedisHash {
|
||||
func NewRedisHash(ctx context.Context, hashKey string, lockLeaseTime uint64, needRefresh bool) *RedisHash {
|
||||
token := ctx.Value("client_token").(string)
|
||||
return &RedisHash{
|
||||
ctx: ctx,
|
||||
hashKey: hashKey,
|
||||
rwLocker: locker.InitRWLocker(hashKey, token, lockLeaseTime, needRefresh),
|
||||
storageClient: GetRedisClientInstance(),
|
||||
}
|
||||
}
|
||||
|
||||
// SetRedisHashByMap define func of set redis hash by map struct
|
||||
func (rh *RedisHash) SetRedisHashByMap(hashKey string, fields map[string]interface{}) error {
|
||||
func (rh *RedisHash) SetRedisHashByMap(fields map[string]interface{}) error {
|
||||
err := rh.rwLocker.WLock(rh.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rh.ctx, "lock wLock by hash_key failed", "hash_key", hashKey, "error", err)
|
||||
logger.Error(rh.ctx, "lock wLock by hash_key failed", "hash_key", rh.hashKey, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rh.rwLocker.UnWLock(rh.ctx)
|
||||
|
||||
err = rh.storageClient.HSet(rh.ctx, hashKey, fields).Err()
|
||||
err = rh.storageClient.HSet(rh.ctx, rh.hashKey, fields).Err()
|
||||
if err != nil {
|
||||
logger.Error(rh.ctx, "set hash by map failed", "hash_key", hashKey, "fields", fields, "error", err)
|
||||
logger.Error(rh.ctx, "set hash by map failed", "hash_key", rh.hashKey, "fields", fields, "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetRedisHashByKV define func of set redis hash by kv struct
|
||||
func (rh *RedisHash) SetRedisHashByKV(hashKey string, field string, value interface{}) error {
|
||||
func (rh *RedisHash) SetRedisHashByKV(field string, value interface{}) error {
|
||||
err := rh.rwLocker.WLock(rh.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rh.ctx, "lock wLock by hash_key failed", "hash_key", hashKey, "error", err)
|
||||
logger.Error(rh.ctx, "lock wLock by hash_key failed", "hash_key", rh.hashKey, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rh.rwLocker.UnWLock(rh.ctx)
|
||||
|
||||
err = rh.storageClient.HSet(rh.ctx, hashKey, field, value).Err()
|
||||
err = rh.storageClient.HSet(rh.ctx, rh.hashKey, field, value).Err()
|
||||
if err != nil {
|
||||
logger.Error(rh.ctx, "set hash by kv failed", "hash_key", hashKey, "field", field, "value", value, "error", err)
|
||||
logger.Error(rh.ctx, "set hash by kv failed", "hash_key", rh.hashKey, "field", field, "value", value, "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// HGet define func of get specified field value from redis hash by key and field name
|
||||
func (rh *RedisHash) HGet(hashKey string, field string) (string, error) {
|
||||
func (rh *RedisHash) HGet(field string) (string, error) {
|
||||
err := rh.rwLocker.RLock(rh.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rh.ctx, "lock rLock by hash_key failed", "hash_key", hashKey, "error", err)
|
||||
logger.Error(rh.ctx, "lock rLock by hash_key failed", "hash_key", rh.hashKey, "error", err)
|
||||
return "", err
|
||||
}
|
||||
defer rh.rwLocker.UnRLock(rh.ctx)
|
||||
|
||||
result, err := rh.storageClient.HGet(rh.ctx, hashKey, field).Result()
|
||||
result, err := rh.storageClient.HGet(rh.ctx, rh.hashKey, field).Result()
|
||||
if err != nil {
|
||||
logger.Error(rh.ctx, "set hash by kv failed", "hash_key", hashKey, "field", field, "error", err)
|
||||
logger.Error(rh.ctx, "set hash by kv failed", "hash_key", rh.hashKey, "field", field, "error", err)
|
||||
return "", err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// HGetAll define func of get all filelds from redis hash by key
|
||||
func (rh *RedisHash) HGetAll(hashKey string) (map[string]string, error) {
|
||||
func (rh *RedisHash) HGetAll() (map[string]string, error) {
|
||||
err := rh.rwLocker.RLock(rh.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rh.ctx, "lock rLock by hash_key failed", "hash_key", hashKey, "error", err)
|
||||
logger.Error(rh.ctx, "lock rLock by hash_key failed", "hash_key", rh.hashKey, "error", err)
|
||||
return nil, err
|
||||
}
|
||||
defer rh.rwLocker.UnRLock(rh.ctx)
|
||||
|
||||
result, err := rh.storageClient.HGetAll(rh.ctx, hashKey).Result()
|
||||
result, err := rh.storageClient.HGetAll(rh.ctx, rh.hashKey).Result()
|
||||
if err != nil {
|
||||
logger.Error(rh.ctx, "get all hash field by hash key failed", "hash_key", hashKey, "error", err)
|
||||
logger.Error(rh.ctx, "get all hash field by hash key failed", "hash_key", rh.hashKey, "error", err)
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
// RedisSet defines the encapsulation struct of redis hash type
|
||||
type RedisSet struct {
|
||||
ctx context.Context
|
||||
key string
|
||||
rwLocker *locker.RedissionRWLocker
|
||||
storageClient *redis.Client
|
||||
logger *zap.Logger
|
||||
|
|
@ -24,6 +25,7 @@ func NewRedisSet(ctx context.Context, setKey string, lockLeaseTime uint64, needR
|
|||
token := ctx.Value("client_token").(string)
|
||||
return &RedisSet{
|
||||
ctx: ctx,
|
||||
key: setKey,
|
||||
rwLocker: locker.InitRWLocker(setKey, token, lockLeaseTime, needRefresh),
|
||||
storageClient: GetRedisClientInstance(),
|
||||
logger: logger.GetLoggerInstance(),
|
||||
|
|
@ -31,34 +33,34 @@ func NewRedisSet(ctx context.Context, setKey string, lockLeaseTime uint64, needR
|
|||
}
|
||||
|
||||
// SADD define func of add redis set by members
|
||||
func (rs *RedisSet) SADD(setKey string, members ...interface{}) error {
|
||||
func (rs *RedisSet) SADD(members ...any) error {
|
||||
err := rs.rwLocker.WLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock wLock by setKey failed", "set_key", setKey, "error", err)
|
||||
logger.Error(rs.ctx, "lock wLock by setKey failed", "set_key", rs.key, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||
|
||||
err = rs.storageClient.SAdd(rs.ctx, setKey, members).Err()
|
||||
err = rs.storageClient.SAdd(rs.ctx, rs.key, members).Err()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "add set by memebers failed", "set_key", setKey, "members", members, "error", err)
|
||||
logger.Error(rs.ctx, "add set by memebers failed", "set_key", rs.key, "members", members, "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SREM define func of remove the specified members from redis set by key
|
||||
func (rs *RedisSet) SREM(setKey string, members ...interface{}) error {
|
||||
func (rs *RedisSet) SREM(members ...any) error {
|
||||
err := rs.rwLocker.WLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock wLock by setKey failed", "set_key", setKey, "error", err)
|
||||
logger.Error(rs.ctx, "lock wLock by setKey failed", "set_key", rs.key, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||
|
||||
count, err := rs.storageClient.SRem(rs.ctx, setKey, members).Result()
|
||||
count, err := rs.storageClient.SRem(rs.ctx, rs.key, members).Result()
|
||||
if err != nil || count != int64(len(members)) {
|
||||
logger.Error(rs.ctx, "rem members from set failed", "set_key", setKey, "members", members, "error", err)
|
||||
logger.Error(rs.ctx, "rem members from set failed", "set_key", rs.key, "members", members, "error", err)
|
||||
|
||||
return fmt.Errorf("rem members from set failed:%w", err)
|
||||
}
|
||||
|
|
@ -66,27 +68,27 @@ func (rs *RedisSet) SREM(setKey string, members ...interface{}) error {
|
|||
}
|
||||
|
||||
// SMembers define func of get all memebers from redis set by key
|
||||
func (rs *RedisSet) SMembers(setKey string) ([]string, error) {
|
||||
func (rs *RedisSet) SMembers() ([]string, error) {
|
||||
err := rs.rwLocker.RLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock rLock by setKey failed", "set_key", setKey, "error", err)
|
||||
logger.Error(rs.ctx, "lock rLock by setKey failed", "set_key", rs.key, "error", err)
|
||||
return nil, err
|
||||
}
|
||||
defer rs.rwLocker.UnRLock(rs.ctx)
|
||||
|
||||
result, err := rs.storageClient.SMembers(rs.ctx, setKey).Result()
|
||||
result, err := rs.storageClient.SMembers(rs.ctx, rs.key).Result()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "get all set field by hash key failed", "set_key", setKey, "error", err)
|
||||
logger.Error(rs.ctx, "get all set field by hash key failed", "set_key", rs.key, "error", err)
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// SIsMember define func of determine whether an member is in set by key
|
||||
func (rs *RedisSet) SIsMember(setKey string, member interface{}) (bool, error) {
|
||||
result, err := rs.storageClient.SIsMember(rs.ctx, setKey, member).Result()
|
||||
func (rs *RedisSet) SIsMember(member any) (bool, error) {
|
||||
result, err := rs.storageClient.SIsMember(rs.ctx, rs.key, member).Result()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "get all set field by hash key failed", "set_key", setKey, "error", err)
|
||||
logger.Error(rs.ctx, "get all set field by hash key failed", "set_key", rs.key, "error", err)
|
||||
return false, err
|
||||
}
|
||||
return result, nil
|
||||
|
|
|
|||
|
|
@ -20,7 +20,8 @@ type RedisZSet struct {
|
|||
}
|
||||
|
||||
// NewRedisZSet define func of new redis zset instance
|
||||
func NewRedisZSet(ctx context.Context, key string, token string, lockLeaseTime uint64, needRefresh bool) *RedisZSet {
|
||||
func NewRedisZSet(ctx context.Context, key string, lockLeaseTime uint64, needRefresh bool) *RedisZSet {
|
||||
token := ctx.Value("client_token").(string)
|
||||
return &RedisZSet{
|
||||
ctx: ctx,
|
||||
rwLocker: locker.InitRWLocker(key, token, lockLeaseTime, needRefresh),
|
||||
|
|
|
|||
73
docs/docs.go
73
docs/docs.go
|
|
@ -23,6 +23,70 @@ const docTemplate = `{
|
|||
"host": "{{.Host}}",
|
||||
"basePath": "{{.BasePath}}",
|
||||
"paths": {
|
||||
"/data/realtime": {
|
||||
"get": {
|
||||
"description": "根据用户输入的组件token,从 dataRT 服务中持续获取测点实时数据",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"RealTime Component"
|
||||
],
|
||||
"summary": "获取实时测点数据",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "测量点唯一标识符 (e.g.grid_1:zone_1:station_1:transformfeeder1_220.I_A_rms)",
|
||||
"name": "token",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "查询起始时间 (Unix时间戳, e.g., 1761008266)",
|
||||
"name": "begin",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "查询结束时间 (Unix时间戳, e.g., 1761526675)",
|
||||
"name": "end",
|
||||
"in": "query",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "返回实时数据成功",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/network.SuccessResponse"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"payload": {
|
||||
"$ref": "#/definitions/network.RealTimeDataPayload"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "返回实时数据失败",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/network.FailureResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/measurement/recommend": {
|
||||
"get": {
|
||||
"description": "根据用户输入的字符串,从 Redis 中查询可能的测量点或结构路径,并提供推荐列表。",
|
||||
|
|
@ -164,6 +228,15 @@ const docTemplate = `{
|
|||
}
|
||||
}
|
||||
},
|
||||
"network.RealTimeDataPayload": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sub_pos": {
|
||||
"description": "TODO 增加example tag",
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.SuccessResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,70 @@
|
|||
"host": "localhost:8080",
|
||||
"basePath": "/api/v1",
|
||||
"paths": {
|
||||
"/data/realtime": {
|
||||
"get": {
|
||||
"description": "根据用户输入的组件token,从 dataRT 服务中持续获取测点实时数据",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"RealTime Component"
|
||||
],
|
||||
"summary": "获取实时测点数据",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "测量点唯一标识符 (e.g.grid_1:zone_1:station_1:transformfeeder1_220.I_A_rms)",
|
||||
"name": "token",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "查询起始时间 (Unix时间戳, e.g., 1761008266)",
|
||||
"name": "begin",
|
||||
"in": "query",
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"type": "integer",
|
||||
"description": "查询结束时间 (Unix时间戳, e.g., 1761526675)",
|
||||
"name": "end",
|
||||
"in": "query",
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "返回实时数据成功",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/network.SuccessResponse"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"payload": {
|
||||
"$ref": "#/definitions/network.RealTimeDataPayload"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "返回实时数据失败",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/network.FailureResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/measurement/recommend": {
|
||||
"get": {
|
||||
"description": "根据用户输入的字符串,从 Redis 中查询可能的测量点或结构路径,并提供推荐列表。",
|
||||
|
|
@ -158,6 +222,15 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
"network.RealTimeDataPayload": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sub_pos": {
|
||||
"description": "TODO 增加example tag",
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.SuccessResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
|
|
|||
|
|
@ -34,6 +34,12 @@ definitions:
|
|||
example: trans
|
||||
type: string
|
||||
type: object
|
||||
network.RealTimeDataPayload:
|
||||
properties:
|
||||
sub_pos:
|
||||
description: TODO 增加example tag
|
||||
type: object
|
||||
type: object
|
||||
network.SuccessResponse:
|
||||
properties:
|
||||
code:
|
||||
|
|
@ -58,6 +64,46 @@ info:
|
|||
title: ModelRT 实时模型服务 API 文档
|
||||
version: "1.0"
|
||||
paths:
|
||||
/data/realtime:
|
||||
get:
|
||||
consumes:
|
||||
- application/json
|
||||
description: 根据用户输入的组件token,从 dataRT 服务中持续获取测点实时数据
|
||||
parameters:
|
||||
- description: 测量点唯一标识符 (e.g.grid_1:zone_1:station_1:transformfeeder1_220.I_A_rms)
|
||||
in: query
|
||||
name: token
|
||||
required: true
|
||||
type: string
|
||||
- description: 查询起始时间 (Unix时间戳, e.g., 1761008266)
|
||||
in: query
|
||||
name: begin
|
||||
required: true
|
||||
type: integer
|
||||
- description: 查询结束时间 (Unix时间戳, e.g., 1761526675)
|
||||
in: query
|
||||
name: end
|
||||
required: true
|
||||
type: integer
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: 返回实时数据成功
|
||||
schema:
|
||||
allOf:
|
||||
- $ref: '#/definitions/network.SuccessResponse'
|
||||
- properties:
|
||||
payload:
|
||||
$ref: '#/definitions/network.RealTimeDataPayload'
|
||||
type: object
|
||||
"400":
|
||||
description: 返回实时数据失败
|
||||
schema:
|
||||
$ref: '#/definitions/network.FailureResponse'
|
||||
summary: 获取实时测点数据
|
||||
tags:
|
||||
- RealTime Component
|
||||
/measurement/recommend:
|
||||
get:
|
||||
consumes:
|
||||
|
|
|
|||
3
go.mod
3
go.mod
|
|
@ -1,12 +1,11 @@
|
|||
module modelRT
|
||||
|
||||
go 1.24.1
|
||||
go 1.24
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2
|
||||
github.com/RediSearch/redisearch-go/v2 v2.1.1
|
||||
github.com/bitly/go-simplejson v0.5.1
|
||||
github.com/confluentinc/confluent-kafka-go v1.9.2
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/gomodule/redigo v1.8.9
|
||||
|
|
|
|||
187
go.sum
187
go.sum
|
|
@ -1,6 +1,3 @@
|
|||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2 h1:OcvFkGmslmlZibjAjaHm3L//6LiuBgolP7OputlJIzU=
|
||||
|
|
@ -9,10 +6,6 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
|
|||
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
||||
github.com/RediSearch/redisearch-go/v2 v2.1.1 h1:cCn3i40uLsVD8cxwrdrGfhdAgbR5Cld9q11eYyVOwpM=
|
||||
github.com/RediSearch/redisearch-go/v2 v2.1.1/go.mod h1:Uw93Wi97QqAsw1DwbQrhVd88dBorGTfSuCS42zfh1iA=
|
||||
github.com/actgardner/gogen-avro/v10 v10.1.0/go.mod h1:o+ybmVjEa27AAr35FRqU98DJu1fXES56uXniYFv4yDA=
|
||||
github.com/actgardner/gogen-avro/v10 v10.2.1/go.mod h1:QUhjeHPchheYmMDni/Nx7VB0RsT/ee8YIgGY/xpEQgQ=
|
||||
github.com/actgardner/gogen-avro/v9 v9.1.0/go.mod h1:nyTj6wPqDJoxM3qdnjcLv+EnMDSDFqE0qDpva2QRmKc=
|
||||
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
|
||||
github.com/bitly/go-simplejson v0.5.1 h1:xgwPbetQScXt1gh9BmoJ6j9JMr3TElvuIyjR8pgdoow=
|
||||
github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
|
|
@ -24,51 +17,24 @@ github.com/bytedance/sonic v1.12.5/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKz
|
|||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E=
|
||||
github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
||||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
|
||||
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
|
||||
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||
github.com/cncf/udpa/go v0.0.0-20210930031921-04548b0d99d4/go.mod h1:6pvJx4me5XPnfI9Z40ddWsdw2W/uZgQLFXToKeRcDiI=
|
||||
github.com/cncf/xds/go v0.0.0-20210922020428-25de7278fc84/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
|
||||
github.com/cncf/xds/go v0.0.0-20211001041855-01bcc9b48dfe/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
|
||||
github.com/cncf/xds/go v0.0.0-20211011173535-cb28da3451f1/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
|
||||
github.com/confluentinc/confluent-kafka-go v1.9.2 h1:gV/GxhMBUb03tFWkN+7kdhg+zf+QUM+wVkI9zwh770Q=
|
||||
github.com/confluentinc/confluent-kafka-go v1.9.2/go.mod h1:ptXNqsuDfYbAE/LBW6pnwWZElUoWxHoV8E43DCrliyo=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
|
||||
github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
|
||||
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20=
|
||||
github.com/frankban/quicktest v1.7.2/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o=
|
||||
github.com/frankban/quicktest v1.10.0/go.mod h1:ui7WezCLWMWxVWr1GETZY3smRy0G4KWq9vcPtJmFl7Y=
|
||||
github.com/frankban/quicktest v1.14.0/go.mod h1:NeW+ay9A/U67EYXNFA1nPE8e/tnQv/09mUdL/ijj8og=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.7 h1:SKFKl7kD0RiPdbht0s7hFtjl489WcQ1VyPW8ZzUMYCA=
|
||||
github.com/gabriel-vasile/mimetype v1.4.7/go.mod h1:GDlAgAyIRT27BhFl53XNAFtfjzOkLaF35JdEG0P7LtU=
|
||||
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
|
||||
github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4=
|
||||
github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
|
|
@ -97,50 +63,15 @@ github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
|||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
|
||||
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.2.1-0.20190312032427-6f77996f0c42/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/pprof v0.0.0-20211008130755-947d60d73cc0/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
|
||||
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
|
||||
github.com/hamba/avro v1.5.6/go.mod h1:3vNT0RLXXpFm2Tb/5KC71ZRJlOroggq1Rcitb6k4Fr8=
|
||||
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/heetch/avro v0.3.1/go.mod h1:4xn38Oz/+hiEUTpbVfGVLfvOg0yKLlRP7Q9+gJJILgA=
|
||||
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
|
||||
github.com/invopop/jsonschema v0.4.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
|
||||
|
|
@ -149,42 +80,25 @@ github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
|
|||
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
|
||||
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
|
||||
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jhump/gopoet v0.0.0-20190322174617-17282ff210b3/go.mod h1:me9yfT6IJSlOL3FCfrg+L6yzUEZ+5jW6WHt4Sk+UPUI=
|
||||
github.com/jhump/gopoet v0.1.0/go.mod h1:me9yfT6IJSlOL3FCfrg+L6yzUEZ+5jW6WHt4Sk+UPUI=
|
||||
github.com/jhump/goprotoc v0.5.0/go.mod h1:VrbvcYrQOrTi3i0Vf+m+oqQWk9l72mjkJCYo7UvLHRQ=
|
||||
github.com/jhump/protoreflect v1.11.0/go.mod h1:U7aMIjN0NWq9swDP7xDdoMfRHb35uiuTd3Z9nFXJf5E=
|
||||
github.com/jhump/protoreflect v1.12.0/go.mod h1:JytZfP5d0r8pVNLZvai7U/MCuTWITgrI4tTg7puQFKI=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
|
||||
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
|
||||
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
|
||||
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/juju/qthttptest v0.1.1/go.mod h1:aTlAv8TYaflIiTDIQYzxnl1QdPjAg8Q8qJMErpKy6A4=
|
||||
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
|
||||
github.com/kisielk/sqlstruct v0.0.0-20201105191214-5f3e10d3ab46/go.mod h1:yyMNCyc/Ib3bDTKd379tNMpB/7/H5TjM2Y9QJ5THLbE=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/linkedin/goavro v2.1.0+incompatible/go.mod h1:bBCwI2eGYpUI/4820s67MElg9tdeLbINjLjiM2xZFYM=
|
||||
github.com/linkedin/goavro/v2 v2.10.0/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA=
|
||||
github.com/linkedin/goavro/v2 v2.10.1/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA=
|
||||
github.com/linkedin/goavro/v2 v2.11.1/go.mod h1:UgQUb2N/pmueQYH9bfqFioWxzYCZXSfF8Jw03O5sjqA=
|
||||
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
|
||||
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
|
||||
|
|
@ -196,35 +110,25 @@ github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RR
|
|||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
||||
github.com/nrwiersma/avro-benchmarks v0.0.0-20210913175520-21aec48c8f76/go.mod h1:iKyFMidsk/sVYONJRE372sJuX/QTRPacU7imPqqsu7g=
|
||||
github.com/panjf2000/ants/v2 v2.10.0 h1:zhRg1pQUtkyRiOFo2Sbqwjp0GfBNo9cUY2/Grpx1p+8=
|
||||
github.com/panjf2000/ants/v2 v2.10.0/go.mod h1:7ZxyxsqE4vvW0M7LSD8aI3cKwgFhBHbxnlN8mDqHa1I=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
||||
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/redis/go-redis/v9 v9.7.3 h1:YpPyAayJV+XErNsatSElgRZZVCwXX9QzkKYNvO7x0wM=
|
||||
github.com/redis/go-redis/v9 v9.7.3/go.mod h1:bGUrSggJ9X9GUmZpZNEOQKaANxSGgOEBRltRTZHSvrA=
|
||||
github.com/rogpeppe/clock v0.0.0-20190514195947-2896927a307a/go.mod h1:4r5QyqhjIWCcK8DO4KMclc5Iknq5qVBAlbYYzAbUScQ=
|
||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
|
||||
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
||||
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
|
||||
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
|
||||
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
|
||||
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0/go.mod h1:FKdcjfQW6rpZSnxxUvEA5H/cDPdvJ/SZJQLWWXWGrZ0=
|
||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
|
||||
|
|
@ -239,8 +143,6 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
|||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
|
|
@ -260,9 +162,7 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
|
|||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
|
||||
|
|
@ -272,62 +172,28 @@ go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
|||
golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg=
|
||||
golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
|
||||
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
|
||||
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
|
||||
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200505041828-1ed23360d12c/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
|
||||
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
|
||||
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
|
|
@ -338,80 +204,29 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
|
|||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200505023115-26f46d2f7ef8/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
|
||||
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||
google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c=
|
||||
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo=
|
||||
google.golang.org/genproto v0.0.0-20220503193339-ba3ae3f07e29/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
|
||||
google.golang.org/grpc v1.33.1/go.mod h1:fr5YgcSWrqhRRxogOsw7RzIpsmvOZ6IcH4kBYTpR3n0=
|
||||
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
|
||||
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
|
||||
google.golang.org/grpc v1.46.0/go.mod h1:vN9eftEi1UMyUsIF80+uQXhHjbXYbm0uXoFCACuMGWk=
|
||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/avro.v0 v0.0.0-20171217001914-a730b5802183/go.mod h1:FvqrFXt+jCsyQibeRv4xxEJBL5iG2DDW5aeJwzDiq4A=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/errgo.v1 v1.0.0/go.mod h1:CxwszS/Xz1C49Ucd2i6Zil5UToP1EmyrFhKaMVbg1mk=
|
||||
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
|
||||
gopkg.in/httprequest.v1 v1.2.1/go.mod h1:x2Otw96yda5+8+6ZeWwHIJTFkEHWP/qP8pJOzqEtWPM=
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/retry.v1 v1.0.3/go.mod h1:FJkXmWiMaAo7xB+xhvDF59zhfjDWyzmyAxiT4dB688g=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
|
||||
|
|
@ -421,6 +236,4 @@ gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkw
|
|||
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
|
||||
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
|
||||
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ func QueryAlertEventHandler(c *gin.Context) {
|
|||
resp := network.SuccessResponse{
|
||||
Code: 0,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]any{
|
||||
"events": events,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,7 +68,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) {
|
|||
resp := network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": request.UUID,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,7 +41,7 @@ func AttrDeleteHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
Payload: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
@ -49,7 +49,7 @@ func AttrDeleteHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"attr_token": request.AttrToken,
|
||||
},
|
||||
})
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ func AttrGetHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
Payload: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
@ -59,7 +59,7 @@ func AttrGetHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"attr_token": request.AttrToken,
|
||||
"attr_value": attrValue,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ func AttrSetHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
Payload: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
|
@ -51,7 +51,7 @@ func AttrSetHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"attr_token": request.AttrToken,
|
||||
},
|
||||
})
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
|
|
@ -65,7 +65,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"topologic_info": topologicLink,
|
||||
},
|
||||
}
|
||||
|
|
@ -89,7 +89,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"topologic_infos": topologicCreateInfos,
|
||||
},
|
||||
}
|
||||
|
|
@ -111,7 +111,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"component_infos": request.ComponentInfos,
|
||||
},
|
||||
}
|
||||
|
|
@ -130,7 +130,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": info.UUID,
|
||||
"component_params": info.Params,
|
||||
},
|
||||
|
|
@ -152,7 +152,7 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
|
|||
resp := network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
|
|
@ -70,7 +70,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"topologic_info": topologicLink,
|
||||
},
|
||||
}
|
||||
|
|
@ -95,7 +95,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"topologic_info": topologicDelInfo,
|
||||
},
|
||||
}
|
||||
|
|
@ -112,7 +112,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"topologic_info": topologicDelInfo,
|
||||
},
|
||||
}
|
||||
|
|
@ -138,7 +138,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
},
|
||||
}
|
||||
|
|
@ -162,7 +162,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
},
|
||||
}
|
||||
|
|
@ -184,7 +184,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
},
|
||||
}
|
||||
|
|
@ -205,7 +205,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
|
|||
resp := network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": pageID,
|
||||
},
|
||||
}
|
||||
|
|
@ -48,16 +48,16 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": pageID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
payLoad := make(map[string]interface{})
|
||||
payLoad["root_vertex"] = topologicInfo.RootVertex
|
||||
payLoad["topologic"] = topologicInfo.VerticeLinks
|
||||
payload := make(map[string]interface{})
|
||||
payload["root_vertex"] = topologicInfo.RootVertex
|
||||
payload["topologic"] = topologicInfo.VerticeLinks
|
||||
|
||||
componentParamMap := make(map[string]any)
|
||||
for _, VerticeLink := range topologicInfo.VerticeLinks {
|
||||
|
|
@ -69,7 +69,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": componentUUID,
|
||||
},
|
||||
}
|
||||
|
|
@ -84,7 +84,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": componentUUID,
|
||||
},
|
||||
}
|
||||
|
|
@ -103,7 +103,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": topologicInfo.RootVertex,
|
||||
},
|
||||
}
|
||||
|
|
@ -118,7 +118,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": rootVertexUUID,
|
||||
},
|
||||
}
|
||||
|
|
@ -127,12 +127,12 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
}
|
||||
componentParamMap[rootVertexUUID] = rootComponentParam
|
||||
|
||||
payLoad["component_params"] = componentParamMap
|
||||
payload["component_params"] = componentParamMap
|
||||
|
||||
resp := network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: payLoad,
|
||||
Payload: payload,
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
|
|
@ -52,7 +52,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"topologic_info": topologicLink,
|
||||
},
|
||||
}
|
||||
|
|
@ -75,7 +75,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"topologic_info": topologicChangeInfo,
|
||||
},
|
||||
}
|
||||
|
|
@ -92,7 +92,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"topologic_info": topologicChangeInfo,
|
||||
},
|
||||
}
|
||||
|
|
@ -109,7 +109,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
"component_info": request.ComponentInfos,
|
||||
},
|
||||
|
|
@ -129,7 +129,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": info.UUID,
|
||||
"component_params": info.Params,
|
||||
},
|
||||
|
|
@ -152,7 +152,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
resp := network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,253 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
|
||||
"modelRT/common/errcode"
|
||||
"modelRT/constants"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ComponentAttributeQueryHandler define circuit diagram component attribute value query process API
|
||||
func ComponentAttributeQueryHandler(c *gin.Context) {
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
|
||||
tokens := c.Param("tokens")
|
||||
if tokens == "" {
|
||||
err := fmt.Errorf("tokens is missing from the path")
|
||||
logger.Error(c, "query tokens from path failed", "error", err, "url", c.Request.RequestURI)
|
||||
renderRespFailure(c, constants.RespCodeInvalidParams, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
tokenSlice := strings.Split(tokens, ",")
|
||||
queryResults := make(map[string]queryResult)
|
||||
cacheQueryMap := make(map[string][]cacheQueryItem)
|
||||
|
||||
for _, token := range tokenSlice {
|
||||
slices := strings.Split(token, ".")
|
||||
if len(slices) < 7 {
|
||||
queryResults[token] = queryResult{err: errcode.ErrInvalidToken}
|
||||
continue
|
||||
}
|
||||
hSetKey := fmt.Sprintf("%s_%s", slices[4], slices[5])
|
||||
cacheQueryMap[hSetKey] = append(cacheQueryMap[hSetKey], cacheQueryItem{
|
||||
token: token,
|
||||
attributeCompTag: slices[4],
|
||||
attributeExtendType: slices[5],
|
||||
attributeName: slices[6],
|
||||
})
|
||||
}
|
||||
|
||||
dbQueryMap := make(map[string][]cacheQueryItem)
|
||||
var secondaryQueryCount int
|
||||
for hSetKey, items := range cacheQueryMap {
|
||||
hset := diagram.NewRedisHash(c, hSetKey, 5000, false)
|
||||
cacheData, err := hset.HGetAll()
|
||||
if err != nil {
|
||||
logger.Warn(c, "redis hgetall failed", "key", hSetKey, "err", err)
|
||||
}
|
||||
for _, item := range items {
|
||||
if val, ok := cacheData[item.attributeName]; ok {
|
||||
queryResults[item.token] = queryResult{err: errcode.ErrProcessSuccess, value: val}
|
||||
} else {
|
||||
dbQueryMap[item.attributeCompTag] = append(dbQueryMap[item.attributeCompTag], item)
|
||||
secondaryQueryCount++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if secondaryQueryCount == 0 {
|
||||
payload := genQueryRespPayload(queryResults, tokenSlice)
|
||||
renderRespSuccess(c, constants.RespCodeSuccess, "query dynamic parameter values success", payload)
|
||||
return
|
||||
}
|
||||
|
||||
tx := pgClient.WithContext(c).Begin()
|
||||
if tx.Error != nil {
|
||||
logger.Error(c, "begin postgres transaction failed", "error", tx.Error)
|
||||
fillRemainingErrors(queryResults, tokenSlice, errcode.ErrBeginTxFailed)
|
||||
payload := genQueryRespPayload(queryResults, tokenSlice)
|
||||
renderRespFailure(c, constants.RespCodeServerError, "begin postgres database transaction failed", payload)
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
allCompTags := slices.Collect(maps.Keys(dbQueryMap))
|
||||
compModelMap, err := database.QueryComponentByCompTags(c, tx, allCompTags)
|
||||
if err != nil {
|
||||
logger.Error(c, "query component info from postgres database failed", "error", err)
|
||||
fillRemainingErrors(queryResults, tokenSlice, errcode.ErrDBQueryFailed)
|
||||
payload := genQueryRespPayload(queryResults, tokenSlice)
|
||||
renderRespFailure(c, constants.RespCodeServerError, "query component meta failed", payload)
|
||||
return
|
||||
}
|
||||
|
||||
// batch retrieve component metadata
|
||||
identifiers := make([]orm.ProjectIdentifier, 0, secondaryQueryCount)
|
||||
for tag, items := range dbQueryMap {
|
||||
comp, ok := compModelMap[tag]
|
||||
if !ok {
|
||||
for _, it := range items {
|
||||
queryResults[it.token] = queryResult{err: errcode.ErrFoundTargetFailed}
|
||||
}
|
||||
continue
|
||||
}
|
||||
for i := range items {
|
||||
items[i].attributeModelName = comp.ModelName
|
||||
items[i].globalUUID = comp.GlobalUUID
|
||||
identifiers = append(identifiers, orm.ProjectIdentifier{
|
||||
Token: items[i].token, Tag: comp.ModelName, GroupName: items[i].attributeExtendType,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
tableNameMap, err := database.BatchGetProjectNames(tx, identifiers)
|
||||
if err != nil {
|
||||
logger.Error(c, "batch get table names from postgres database failed", "error", err)
|
||||
fillRemainingErrors(queryResults, tokenSlice, errcode.ErrRetrieveFailed)
|
||||
payload := genQueryRespPayload(queryResults, tokenSlice)
|
||||
renderRespFailure(c, constants.RespCodeServerError, "batch get table names from postgres database failed", payload)
|
||||
return
|
||||
}
|
||||
|
||||
redisSyncMap := make(map[string][]cacheQueryItem)
|
||||
for _, items := range dbQueryMap {
|
||||
for _, item := range items {
|
||||
if _, exists := queryResults[item.token]; exists {
|
||||
continue
|
||||
}
|
||||
|
||||
tbl, ok := tableNameMap[orm.ProjectIdentifier{Tag: item.attributeModelName, GroupName: item.attributeExtendType}]
|
||||
if !ok {
|
||||
queryResults[item.token] = queryResult{err: errcode.ErrFoundTargetFailed}
|
||||
continue
|
||||
}
|
||||
|
||||
var dbVal string
|
||||
res := tx.Table(tbl).Select(item.attributeName).Where("global_uuid = ?", item.globalUUID).Scan(&dbVal)
|
||||
if res.Error != nil || res.RowsAffected == 0 {
|
||||
queryResults[item.token] = queryResult{err: errcode.ErrDBQueryFailed}
|
||||
continue
|
||||
}
|
||||
|
||||
queryResults[item.token] = queryResult{err: errcode.ErrProcessSuccess, value: dbVal}
|
||||
item.attributeVal = dbVal
|
||||
hKey := fmt.Sprintf("%s_%s", item.attributeCompTag, item.attributeExtendType)
|
||||
redisSyncMap[hKey] = append(redisSyncMap[hKey], item)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
logger.Warn(c, "postgres transaction commit failed, but returning scanned data", "error", err)
|
||||
} else {
|
||||
for hKey, items := range redisSyncMap {
|
||||
go backfillRedis(c.Copy(), hKey, items)
|
||||
}
|
||||
}
|
||||
|
||||
payload := genQueryRespPayload(queryResults, tokenSlice)
|
||||
if hasAnyError(queryResults) {
|
||||
renderRespFailure(c, constants.RespCodeFailed, "query completed with partial failures", payload)
|
||||
} else {
|
||||
renderRespSuccess(c, constants.RespCodeSuccess, "query completed successfully", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func hasAnyError(results map[string]queryResult) bool {
|
||||
for _, res := range results {
|
||||
if res.err != nil && res.err.Code() != constants.RespCodeSuccess {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func fillRemainingErrors(results map[string]queryResult, tokens []string, err *errcode.AppError) {
|
||||
for _, token := range tokens {
|
||||
if _, exists := results[token]; !exists {
|
||||
results[token] = queryResult{err: err}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func backfillRedis(ctx context.Context, hSetKey string, items []cacheQueryItem) {
|
||||
hset := diagram.NewRedisHash(ctx, hSetKey, 5000, false)
|
||||
fields := make(map[string]any, len(items))
|
||||
for _, item := range items {
|
||||
if item.attributeVal != "" {
|
||||
fields[item.attributeName] = item.attributeVal
|
||||
}
|
||||
}
|
||||
|
||||
if len(fields) > 0 {
|
||||
if err := hset.SetRedisHashByMap(fields); err != nil {
|
||||
logger.Error(ctx, "async backfill redis failed", "hash_key", hSetKey, "error", err)
|
||||
} else {
|
||||
logger.Info(ctx, "async backfill redis success", "hash_key", hSetKey, "count", len(fields))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func genQueryRespPayload(queryResults map[string]queryResult, requestTokens []string) map[string]any {
|
||||
attributes := make([]attributeQueryResult, 0, len(requestTokens))
|
||||
|
||||
for _, token := range requestTokens {
|
||||
if queryResult, exists := queryResults[token]; exists {
|
||||
attributes = append(attributes, attributeQueryResult{
|
||||
Token: token,
|
||||
Code: queryResult.err.Code(),
|
||||
Msg: queryResult.err.Msg(),
|
||||
Value: queryResult.value,
|
||||
})
|
||||
} else {
|
||||
err := errcode.ErrCacheQueryFailed
|
||||
attributes = append(attributes, attributeQueryResult{
|
||||
Token: token,
|
||||
Code: err.Code(),
|
||||
Msg: err.Msg(),
|
||||
Value: "",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
payload := map[string]any{
|
||||
"attributes": attributes,
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
||||
type cacheQueryItem struct {
|
||||
globalUUID uuid.UUID
|
||||
token string
|
||||
attributeCompTag string
|
||||
attributeModelName string
|
||||
attributeExtendType string
|
||||
attributeName string
|
||||
attributeVal string
|
||||
}
|
||||
|
||||
type attributeQueryResult struct {
|
||||
Token string `json:"token"`
|
||||
Msg string `json:"msg"`
|
||||
Value string `json:"value"`
|
||||
Code int `json:"code"`
|
||||
}
|
||||
|
||||
type queryResult struct {
|
||||
err *errcode.AppError
|
||||
value string
|
||||
}
|
||||
|
|
@ -0,0 +1,214 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"modelRT/common/errcode"
|
||||
"modelRT/constants"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ComponentAttributeUpdateHandler define circuit diagram component attribute value update process API
|
||||
func ComponentAttributeUpdateHandler(c *gin.Context) {
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
var request network.ComponentAttributeUpdateInfo
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error(c, "unmarshal request params failed", "error", err)
|
||||
renderRespFailure(c, constants.RespCodeInvalidParams, err.Error(), nil)
|
||||
return
|
||||
}
|
||||
|
||||
updateResults := make(map[string]*errcode.AppError)
|
||||
attriModifyConfs := make([]attributeModifyConfig, 0, len(request.AttributeConfigs))
|
||||
var attributeComponentTag string
|
||||
for index, attribute := range request.AttributeConfigs {
|
||||
slices := strings.Split(attribute.AttributeToken, ".")
|
||||
if len(slices) < 7 {
|
||||
updateResults[attribute.AttributeToken] = errcode.ErrInvalidToken
|
||||
continue
|
||||
}
|
||||
|
||||
componentTag := slices[4]
|
||||
if index == 0 {
|
||||
attributeComponentTag = componentTag
|
||||
} else if componentTag != attributeComponentTag {
|
||||
updateResults[attribute.AttributeToken] = errcode.ErrCrossToken
|
||||
continue
|
||||
}
|
||||
|
||||
attriModifyConfs = append(attriModifyConfs, attributeModifyConfig{
|
||||
attributeToken: attribute.AttributeToken,
|
||||
attributeExtendType: slices[5],
|
||||
attributeName: slices[6],
|
||||
attributeOldVal: attribute.AttributeOldVal,
|
||||
attributeNewVal: attribute.AttributeNewVal,
|
||||
})
|
||||
}
|
||||
|
||||
// open transaction
|
||||
tx := pgClient.WithContext(c).Begin()
|
||||
if tx.Error != nil {
|
||||
logger.Error(c, "begin postgres transaction failed", "error", tx.Error)
|
||||
renderRespFailure(c, constants.RespCodeServerError, "begin postgres transaction failed", nil)
|
||||
return
|
||||
}
|
||||
|
||||
compInfo, err := database.QueryComponentByCompTag(c, tx, attributeComponentTag)
|
||||
if err != nil {
|
||||
logger.Error(c, "query component info by component tag failed", "error", err, "tag", attributeComponentTag)
|
||||
|
||||
for _, attribute := range request.AttributeConfigs {
|
||||
if _, exists := updateResults[attribute.AttributeToken]; !exists {
|
||||
updateResults[attribute.AttributeToken] = errcode.ErrDBQueryFailed.WithCause(err)
|
||||
}
|
||||
}
|
||||
|
||||
tx.Rollback()
|
||||
|
||||
payload := genUpdateRespPayload(updateResults, request.AttributeConfigs)
|
||||
renderRespFailure(c, constants.RespCodeFailed, "query component metadata failed", payload)
|
||||
return
|
||||
}
|
||||
|
||||
identifiers := make([]orm.ProjectIdentifier, len(attriModifyConfs))
|
||||
for i, mod := range attriModifyConfs {
|
||||
identifiers[i] = orm.ProjectIdentifier{
|
||||
Token: mod.attributeToken,
|
||||
Tag: compInfo.ModelName,
|
||||
GroupName: mod.attributeExtendType,
|
||||
}
|
||||
}
|
||||
tableNameMap, err := database.BatchGetProjectNames(tx, identifiers)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
for _, id := range identifiers {
|
||||
if _, exists := updateResults[id.Token]; !exists {
|
||||
updateResults[id.Token] = errcode.ErrRetrieveFailed.WithCause(err)
|
||||
}
|
||||
}
|
||||
|
||||
payload := genUpdateRespPayload(updateResults, request.AttributeConfigs)
|
||||
renderRespFailure(c, constants.RespCodeFailed, "batch retrieve table names failed", payload)
|
||||
return
|
||||
}
|
||||
|
||||
redisUpdateMap := make(map[string][]cacheUpdateItem)
|
||||
for _, mod := range attriModifyConfs {
|
||||
id := orm.ProjectIdentifier{Tag: compInfo.ModelName, GroupName: mod.attributeExtendType}
|
||||
tableName, exists := tableNameMap[id]
|
||||
if !exists {
|
||||
updateResults[mod.attributeToken] = errcode.ErrFoundTargetFailed
|
||||
continue
|
||||
}
|
||||
|
||||
result := tx.Table(tableName).
|
||||
Where(fmt.Sprintf("%s = ? AND global_uuid = ?", mod.attributeName), mod.attributeOldVal, compInfo.GlobalUUID).
|
||||
Updates(map[string]any{mod.attributeName: mod.attributeNewVal})
|
||||
|
||||
if result.Error != nil {
|
||||
updateResults[mod.attributeToken] = errcode.ErrDBUpdateFailed
|
||||
continue
|
||||
}
|
||||
if result.RowsAffected == 0 {
|
||||
updateResults[mod.attributeToken] = errcode.ErrDBzeroAffectedRows
|
||||
continue
|
||||
}
|
||||
|
||||
cacheKey := fmt.Sprintf("%s_%s", attributeComponentTag, mod.attributeExtendType)
|
||||
redisUpdateMap[cacheKey] = append(redisUpdateMap[cacheKey],
|
||||
cacheUpdateItem{
|
||||
token: mod.attributeToken,
|
||||
name: mod.attributeName,
|
||||
newVal: mod.attributeNewVal,
|
||||
})
|
||||
}
|
||||
|
||||
// commit transaction
|
||||
if err := tx.Commit().Error; err != nil {
|
||||
renderRespFailure(c, constants.RespCodeServerError, "transaction commit failed", nil)
|
||||
return
|
||||
}
|
||||
|
||||
for key, items := range redisUpdateMap {
|
||||
hset := diagram.NewRedisHash(c, key, 5000, false)
|
||||
|
||||
fields := make(map[string]any, len(items))
|
||||
for _, item := range items {
|
||||
fields[item.name] = item.newVal
|
||||
}
|
||||
|
||||
if err := hset.SetRedisHashByMap(fields); err != nil {
|
||||
logger.Error(c, "batch sync redis failed", "hash_key", key, "error", err)
|
||||
|
||||
for _, item := range items {
|
||||
if _, exists := updateResults[item.token]; exists {
|
||||
updateResults[item.token] = errcode.ErrCacheSyncWarn.WithCause(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
payload := genUpdateRespPayload(updateResults, request.AttributeConfigs)
|
||||
if len(updateResults) > 0 {
|
||||
renderRespFailure(c, constants.RespCodeFailed, "process completed with partial failures", payload)
|
||||
return
|
||||
}
|
||||
renderRespSuccess(c, constants.RespCodeSuccess, "process completed successfully", payload)
|
||||
}
|
||||
|
||||
type attributeModifyConfig struct {
|
||||
attributeToken string
|
||||
attributeExtendType string
|
||||
attributeName string
|
||||
attributeOldVal string
|
||||
attributeNewVal string
|
||||
}
|
||||
|
||||
type cacheUpdateItem struct {
|
||||
token string
|
||||
name string
|
||||
newVal string
|
||||
}
|
||||
|
||||
type attributeUpdateResult struct {
|
||||
Token string `json:"token"`
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
}
|
||||
|
||||
func genUpdateRespPayload(updateResults map[string]*errcode.AppError, originalRequests []network.ComponentAttributeConfig) map[string]any {
|
||||
attributes := make([]attributeUpdateResult, 0, len(originalRequests))
|
||||
|
||||
for _, req := range originalRequests {
|
||||
token := req.AttributeToken
|
||||
|
||||
if appErr, exists := updateResults[token]; exists {
|
||||
attributes = append(attributes, attributeUpdateResult{
|
||||
Token: token,
|
||||
Code: appErr.Code(),
|
||||
Msg: appErr.Msg(),
|
||||
})
|
||||
} else {
|
||||
attributes = append(attributes, attributeUpdateResult{
|
||||
Token: token,
|
||||
Code: constants.CodeSuccess,
|
||||
Msg: "token value update success",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
payload := map[string]any{
|
||||
"attributes": attributes,
|
||||
}
|
||||
|
||||
return payload
|
||||
}
|
||||
|
|
@ -0,0 +1,188 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type linkSetConfig struct {
|
||||
CurrKey string
|
||||
PrevKeyTemplate string
|
||||
PrevIsNil bool
|
||||
}
|
||||
|
||||
var linkSetConfigs = map[int]linkSetConfig{
|
||||
// grid hierarchy
|
||||
0: {CurrKey: constants.RedisAllGridSetKey, PrevIsNil: true},
|
||||
// zone hierarchy
|
||||
1: {CurrKey: constants.RedisAllZoneSetKey, PrevKeyTemplate: constants.RedisSpecGridZoneSetKey},
|
||||
// station hierarchy
|
||||
2: {CurrKey: constants.RedisAllStationSetKey, PrevKeyTemplate: constants.RedisSpecZoneStationSetKey},
|
||||
// component nspath hierarchy
|
||||
3: {CurrKey: constants.RedisAllCompNSPathSetKey, PrevKeyTemplate: constants.RedisSpecStationCompNSPATHSetKey},
|
||||
// component tag hierarchy
|
||||
4: {CurrKey: constants.RedisAllCompTagSetKey, PrevKeyTemplate: constants.RedisSpecCompNSPathCompTagSetKey},
|
||||
// config hierarchy
|
||||
5: {CurrKey: constants.RedisAllConfigSetKey, PrevIsNil: true},
|
||||
}
|
||||
|
||||
// DiagramNodeLinkHandler defines the diagram node link process api
|
||||
func DiagramNodeLinkHandler(c *gin.Context) {
|
||||
var request network.DiagramNodeLinkRequest
|
||||
clientToken := c.GetString("client_token")
|
||||
if clientToken == "" {
|
||||
err := constants.ErrGetClientToken
|
||||
logger.Error(c, "failed to get client token from context", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error(c, "failed to unmarshal diagram node process request", "error", err)
|
||||
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: "invalid request body format: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
nodeID := request.NodeID
|
||||
nodeLevel := request.NodeLevel
|
||||
action := request.Action
|
||||
prevNodeInfo, currNodeInfo, err := database.QueryNodeInfoByID(c, pgClient, nodeID, nodeLevel)
|
||||
if err != nil {
|
||||
logger.Error(c, "failed to query diagram node info by nodeID and level from postgres", "node_id", nodeID, "level", nodeLevel, "error", err)
|
||||
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: "failed to query measurement info record: " + err.Error(),
|
||||
Payload: map[string]any{
|
||||
"node_id": nodeID,
|
||||
"node_level": nodeLevel,
|
||||
"action": action,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
prevLinkSet, currLinkSet := generateLinkSet(c, nodeLevel, prevNodeInfo)
|
||||
err = processLinkSetData(c, action, nodeLevel, prevLinkSet, currLinkSet, prevNodeInfo, currNodeInfo)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: map[string]any{
|
||||
"node_id": nodeID,
|
||||
"node_level": nodeLevel,
|
||||
"action": action,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info(c, "process diagram node link success", "node_id", nodeID, "level", nodeLevel, "action", request.Action)
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "diagram node link process success",
|
||||
Payload: map[string]any{
|
||||
"node_id": nodeID,
|
||||
"node_level": nodeLevel,
|
||||
"action": action,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func generateLinkSet(ctx context.Context, level int, prevNodeInfo orm.CircuitDiagramNodeInterface) (*diagram.RedisSet, *diagram.RedisSet) {
|
||||
config, ok := linkSetConfigs[level]
|
||||
// level not supported
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
currLinkSet := diagram.NewRedisSet(ctx, config.CurrKey, 0, false)
|
||||
if config.PrevIsNil {
|
||||
return nil, currLinkSet
|
||||
}
|
||||
|
||||
prevLinkSetKey := fmt.Sprintf(config.PrevKeyTemplate, prevNodeInfo.GetTagName())
|
||||
prevLinkSet := diagram.NewRedisSet(ctx, prevLinkSetKey, 0, false)
|
||||
return prevLinkSet, currLinkSet
|
||||
}
|
||||
|
||||
func processLinkSetData(ctx context.Context, action string, level int, prevLinkSet, currLinkSet *diagram.RedisSet, prevNodeInfo, currNodeInfo orm.CircuitDiagramNodeInterface) error {
|
||||
var currMember string
|
||||
var prevMember string
|
||||
var err1, err2 error
|
||||
|
||||
switch level {
|
||||
case 0, 1, 2, 4:
|
||||
// grid、zone、station、component tag hierarchy
|
||||
currMember = currNodeInfo.GetTagName()
|
||||
if prevLinkSet != nil {
|
||||
prevMember = prevNodeInfo.GetTagName()
|
||||
}
|
||||
case 3:
|
||||
// component NSPath hierarchy
|
||||
currMember = currNodeInfo.GetNSPath()
|
||||
prevMember = prevNodeInfo.GetTagName()
|
||||
case 5:
|
||||
// TODO[NONEED-ISSUE]暂无此层级增加或删除需求 #2
|
||||
err := fmt.Errorf("currently hierarchy no need to add or delete this level: %d", level)
|
||||
logger.Error(ctx, "no need level for link process", "level", level, "action", action, "error", err)
|
||||
return nil
|
||||
default:
|
||||
err := fmt.Errorf("unsupported diagram node level: %d", level)
|
||||
logger.Error(ctx, "unsupport diagram node level for link process", "level", level, "action", action, "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
switch action {
|
||||
case constants.SearchLinkAddAction:
|
||||
err1 = currLinkSet.SADD(currMember)
|
||||
if prevLinkSet != nil {
|
||||
err2 = prevLinkSet.SADD(prevMember)
|
||||
}
|
||||
case constants.SearchLinkDelAction:
|
||||
err1 = currLinkSet.SREM(currMember)
|
||||
if prevLinkSet != nil {
|
||||
err2 = prevLinkSet.SREM(prevMember)
|
||||
}
|
||||
default:
|
||||
err := constants.ErrUnsupportedLinkAction
|
||||
logger.Error(ctx, "unsupport diagram node link process action", "action", action, "error", err)
|
||||
return err
|
||||
}
|
||||
return processDiagramLinkError(err1, err2, action)
|
||||
}
|
||||
|
||||
func processDiagramLinkError(err1, err2 error, action string) error {
|
||||
var err error
|
||||
if err1 != nil && err2 != nil {
|
||||
err = errors.Join(err1, err2)
|
||||
err = fmt.Errorf("process diagram node link failed, currLinkSet %s operation and prevLinkSet %s operation failed: %w", action, action, err)
|
||||
} else if err1 != nil {
|
||||
err = fmt.Errorf("process diagram node currLinkSet link failed: currLinkSet %s operation failed: %w", action, err1)
|
||||
} else {
|
||||
err = fmt.Errorf("process diagram node prevLinkSet link failed: prevLinkSet %s operation: %w", action, err2)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func renderRespFailure(c *gin.Context, code int, msg string, payload any) {
|
||||
resp := network.FailureResponse{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
}
|
||||
if payload != nil {
|
||||
resp.Payload = payload
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
func renderRespSuccess(c *gin.Context, code int, msg string, payload any) {
|
||||
resp := network.SuccessResponse{
|
||||
Code: code,
|
||||
Msg: msg,
|
||||
}
|
||||
if payload != nil {
|
||||
resp.Payload = payload
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"modelRT/alert"
|
||||
"modelRT/constants"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// QueryHistoryDataHandler define query history data process API
|
||||
func QueryHistoryDataHandler(c *gin.Context) {
|
||||
token := c.Query("token")
|
||||
beginStr := c.Query("begin")
|
||||
begin, err := strconv.Atoi(beginStr)
|
||||
if err != nil {
|
||||
logger.Error(c, "convert begin param from string to int failed", "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
endStr := c.Query("end")
|
||||
end, err := strconv.Atoi(endStr)
|
||||
if err != nil {
|
||||
logger.Error(c, "convert end param from string to int failed", "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
fmt.Println(token, begin, end)
|
||||
// TODO parse token to dataRT query params
|
||||
var level int
|
||||
var targetLevel constants.AlertLevel
|
||||
alertManger := alert.GetAlertMangerInstance()
|
||||
targetLevel = constants.AlertLevel(level)
|
||||
events := alertManger.GetRangeEventsByLevel(targetLevel)
|
||||
|
||||
resp := network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: map[string]interface{}{
|
||||
"events": events,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
|
@ -38,14 +38,14 @@ func MeasurementGetHandler(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
zset := diagram.NewRedisZSet(c, request.MeasurementToken, clientToken, 0, false)
|
||||
zset := diagram.NewRedisZSet(c, request.MeasurementToken, 0, false)
|
||||
points, err := zset.ZRANGE(request.MeasurementToken, 0, -1)
|
||||
if err != nil {
|
||||
logger.Error(c, "failed to get measurement data from redis", "measurement_token", request.MeasurementToken, "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusInternalServerError,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]any{
|
||||
"measurement_id": request.MeasurementID,
|
||||
"measurement_token": request.MeasurementToken,
|
||||
},
|
||||
|
|
@ -60,7 +60,7 @@ func MeasurementGetHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]any{
|
||||
"measurement_id": request.MeasurementID,
|
||||
"measurement_token": request.MeasurementToken,
|
||||
"measurement_value": points,
|
||||
|
|
@ -72,7 +72,7 @@ func MeasurementGetHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]any{
|
||||
"measurement_id": request.MeasurementID,
|
||||
"measurement_token": request.MeasurementToken,
|
||||
"measurement_info": measurementInfo,
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"modelRT/logger"
|
||||
"modelRT/model"
|
||||
"modelRT/network"
|
||||
"modelRT/util"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
|
@ -17,34 +17,36 @@ import (
|
|||
// @Tags Measurement Recommend
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body network.MeasurementRecommendRequest true "查询输入参数,例如 'trans' 或 'transformfeeder1_220.'"
|
||||
// @Param input query string true "推荐关键词,例如 'grid1' 或 'grid1.'" Example("grid1")
|
||||
// @Success 200 {object} network.SuccessResponse{payload=network.MeasurementRecommendPayload} "返回推荐列表成功"
|
||||
//
|
||||
// @Example 200 {
|
||||
// "code": 200,
|
||||
// "msg": "success",
|
||||
// "payload": {
|
||||
// "input": "transformfeeder1_220.",
|
||||
// "input": "grid1.zone1.station1.ns1.tag1.bay.",
|
||||
// "offset": 21,
|
||||
// "recommended_list": [
|
||||
// "I_A_rms",
|
||||
// "I_B_rms",
|
||||
// "I_C_rms",
|
||||
// "I11_A_rms",
|
||||
// "I11_B_rms.",
|
||||
// "I11_C_rms.",
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Failure 400 {object} network.FailureResponse "返回推荐列表失败"
|
||||
// @Example 400 {
|
||||
// "code": 400,
|
||||
// "msg": "failed to get recommend data from redis",
|
||||
// }
|
||||
//
|
||||
// @Example 400 {
|
||||
// "code": 400,
|
||||
// "msg": "failed to get recommend data from redis",
|
||||
// }
|
||||
//
|
||||
// @Router /measurement/recommend [get]
|
||||
func MeasurementRecommendHandler(c *gin.Context) {
|
||||
var request network.MeasurementRecommendRequest
|
||||
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error(c, "failed to unmarshal measurement recommend request", "error", err)
|
||||
if err := c.ShouldBindQuery(&request); err != nil {
|
||||
logger.Error(c, "failed to bind measurement recommend request", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
|
|
@ -52,65 +54,68 @@ func MeasurementRecommendHandler(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
recommends, isFuzzy, err := model.RedisSearchRecommend(c, request.Input)
|
||||
if err != nil {
|
||||
logger.Error(c, "failed to get recommend data from redis", "input", request.Input, "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusInternalServerError,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]any{
|
||||
"input": request.Input,
|
||||
},
|
||||
recommendResults := model.RedisSearchRecommend(c, request.Input)
|
||||
payloads := make([]network.MeasurementRecommendPayload, 0, len(recommendResults))
|
||||
for _, recommendResult := range recommendResults {
|
||||
if recommendResult.Err != nil {
|
||||
err := recommendResult.Err
|
||||
logger.Error(c, "failed to get recommend data from redis", "input", request.Input, "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusInternalServerError,
|
||||
Msg: err.Error(),
|
||||
Payload: map[string]any{
|
||||
"input": request.Input,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var finalOffset int
|
||||
recommends := recommendResult.QueryDatas
|
||||
if recommendResult.IsFuzzy {
|
||||
var maxOffset int
|
||||
for index, recommend := range recommends {
|
||||
offset := util.GetLongestCommonPrefixLength(request.Input, recommend)
|
||||
if index == 0 || offset > maxOffset {
|
||||
maxOffset = offset
|
||||
}
|
||||
}
|
||||
finalOffset = maxOffset
|
||||
} else {
|
||||
var minOffset int
|
||||
for index, recommend := range recommends {
|
||||
offset := util.GetLongestCommonPrefixLength(request.Input, recommend)
|
||||
if index == 0 || offset < minOffset {
|
||||
minOffset = offset
|
||||
}
|
||||
}
|
||||
finalOffset = minOffset
|
||||
}
|
||||
|
||||
resultRecommends := make([]string, 0, len(recommends))
|
||||
seen := make(map[string]struct{})
|
||||
|
||||
for _, recommend := range recommends {
|
||||
recommendTerm := recommend[finalOffset:]
|
||||
if len(recommendTerm) != 0 {
|
||||
if _, exists := seen[recommendTerm]; !exists {
|
||||
seen[recommendTerm] = struct{}{}
|
||||
resultRecommends = append(resultRecommends, recommendTerm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
payloads = append(payloads, network.MeasurementRecommendPayload{
|
||||
Input: request.Input,
|
||||
Offset: finalOffset,
|
||||
RecommendType: recommendResult.RecommendType.String(),
|
||||
RecommendedList: resultRecommends,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var finalOffset int
|
||||
if isFuzzy {
|
||||
var maxOffset int
|
||||
for index, recommend := range recommends {
|
||||
offset := model.GetLongestCommonPrefixLength(request.Input, recommend)
|
||||
if index == 0 || offset > maxOffset {
|
||||
maxOffset = offset
|
||||
}
|
||||
}
|
||||
finalOffset = maxOffset
|
||||
} else {
|
||||
var minOffset int
|
||||
for index, recommend := range recommends {
|
||||
offset := model.GetLongestCommonPrefixLength(request.Input, recommend)
|
||||
if index == 0 || offset < minOffset {
|
||||
minOffset = offset
|
||||
}
|
||||
}
|
||||
finalOffset = minOffset
|
||||
}
|
||||
|
||||
resultRecommends := make([]string, 0, len(recommends))
|
||||
seen := make(map[string]struct{})
|
||||
|
||||
for _, recommend := range recommends {
|
||||
recommendTerm := recommend[finalOffset:]
|
||||
if len(recommendTerm) != 0 {
|
||||
if _, exists := seen[recommendTerm]; !exists {
|
||||
seen[recommendTerm] = struct{}{}
|
||||
resultRecommends = append(resultRecommends, recommendTerm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
// PayLoad: map[string]any{
|
||||
// "input": request.Input,
|
||||
// "offset": finalOffset,
|
||||
// "recommended_list": resultRecommends,
|
||||
// },
|
||||
PayLoad: &network.MeasurementRecommendPayload{
|
||||
Input: request.Input,
|
||||
Offset: finalOffset,
|
||||
RecommendedList: resultRecommends,
|
||||
},
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: &payloads,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,135 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// MeasurementLinkHandler defines the measurement link process api
|
||||
func MeasurementLinkHandler(c *gin.Context) {
|
||||
var request network.MeasurementLinkRequest
|
||||
clientToken := c.GetString("client_token")
|
||||
if clientToken == "" {
|
||||
err := constants.ErrGetClientToken
|
||||
logger.Error(c, "failed to get client token from context", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error(c, "failed to unmarshal measurement process request", "error", err)
|
||||
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: "invalid request body format: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
measurementID := request.MeasurementID
|
||||
action := request.Action
|
||||
measurementInfo, err := database.QueryMeasurementByID(c, pgClient, measurementID)
|
||||
if err != nil {
|
||||
logger.Error(c, "failed to query measurement info by measurement id from postgres", "meauserement_id", measurementID, "error", err)
|
||||
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: "failed to query measurement info record: " + err.Error(),
|
||||
Payload: map[string]any{
|
||||
"id": measurementID,
|
||||
"action": action,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
componentInfo, err := database.QueryComponentByUUID(c, pgClient, measurementInfo.ComponentUUID)
|
||||
if err != nil {
|
||||
logger.Error(c, "failed to query component info by component uuid from postgres", "component_uuid", measurementInfo.ComponentUUID, "error", err)
|
||||
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: "failed to query component info record: " + err.Error(),
|
||||
Payload: map[string]any{
|
||||
"id": measurementID,
|
||||
"action": action,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
allMeasSet := diagram.NewRedisSet(c, constants.RedisAllMeasTagSetKey, 0, false)
|
||||
compMeasLinkKey := fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, componentInfo.Tag)
|
||||
compMeasLinkSet := diagram.NewRedisSet(c, compMeasLinkKey, 0, false)
|
||||
|
||||
switch action {
|
||||
case constants.SearchLinkAddAction:
|
||||
err1 := allMeasSet.SADD(measurementInfo.Tag)
|
||||
err2 := compMeasLinkSet.SADD(measurementInfo.Tag)
|
||||
err = processActionError(err1, err2, action)
|
||||
if err != nil {
|
||||
logger.Error(c, "add measurement link process operation failed", "measurement_id", measurementID, "action", action, "error", err)
|
||||
}
|
||||
case constants.SearchLinkDelAction:
|
||||
err1 := allMeasSet.SREM(measurementInfo.Tag)
|
||||
err2 := compMeasLinkSet.SREM(measurementInfo.Tag)
|
||||
err = processActionError(err1, err2, action)
|
||||
if err != nil {
|
||||
logger.Error(c, "del measurement link process operation failed", "measurement_id", measurementID, "action", action, "error", err)
|
||||
}
|
||||
default:
|
||||
err = constants.ErrUnsupportedLinkAction
|
||||
logger.Error(c, "unsupport measurement link process action", "measurement_id", measurementID, "action", action, "error", err)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: map[string]any{
|
||||
"measurement_id": request.MeasurementID,
|
||||
"action": request.Action,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info(c, "process measurement link success", "measurement_id", measurementID, "action", request.Action)
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "measurement link process success",
|
||||
Payload: map[string]any{
|
||||
"measurement_id": measurementID,
|
||||
"action": request.Action,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func processActionError(err1, err2 error, action string) error {
|
||||
var err error
|
||||
if err1 != nil && err2 != nil {
|
||||
err = errors.Join(err1, err2)
|
||||
err = fmt.Errorf("process measurement link failed, allMeasSet %s operation and compMeasLinkSet %s operation failed: %w", action, action, err)
|
||||
} else if err1 != nil {
|
||||
err = fmt.Errorf("process measurement link failed: allMeasSet %s operation failed: %w", action, err1)
|
||||
} else {
|
||||
err = fmt.Errorf("process measurement link failed: compMeasLinkSet %s operation: %w", action, err2)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
|
@ -0,0 +1,508 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net/http"
|
||||
"slices"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/model"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var pullUpgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
CheckOrigin: func(_ *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
// PullRealTimeDataHandler define real time data pull API
|
||||
// @Summary 实时数据拉取 websocket api
|
||||
// @Description 根据用户输入的clientID拉取对应的实时数据
|
||||
// @Tags RealTime Component Websocket
|
||||
// @Router /monitors/data/realtime/stream/:clientID [get]
|
||||
func PullRealTimeDataHandler(c *gin.Context) {
|
||||
clientID := c.Param("clientID")
|
||||
if clientID == "" {
|
||||
err := fmt.Errorf("clientID is missing from the path")
|
||||
logger.Error(c, "query clientID from path failed", "error", err, "url", c.Request.RequestURI)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
conn, err := pullUpgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
logger.Error(c, "upgrade http protocol to websocket protocal failed", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
ctx, cancel := context.WithCancel(c.Request.Context())
|
||||
defer cancel()
|
||||
|
||||
// TODO[BACKPRESSURE-ISSUE] 先期使用固定大容量对扇入模型进行定义 #1
|
||||
fanInChan := make(chan network.RealTimePullTarget, constants.FanInChanMaxSize)
|
||||
sendChan := make(chan []network.RealTimePullTarget, constants.SendChanBufferSize)
|
||||
|
||||
go processTargetPolling(ctx, globalSubState, clientID, fanInChan, sendChan)
|
||||
go readClientMessages(ctx, conn, clientID, cancel)
|
||||
go sendDataStream(ctx, conn, clientID, sendChan, cancel)
|
||||
defer close(sendChan)
|
||||
|
||||
bufferMaxSize := constants.SendMaxBatchSize
|
||||
sendMaxInterval := constants.SendMaxBatchInterval
|
||||
buffer := make([]network.RealTimePullTarget, 0, bufferMaxSize)
|
||||
ticker := time.NewTicker(sendMaxInterval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case targetData, ok := <-fanInChan:
|
||||
if !ok {
|
||||
logger.Error(ctx, "fanInChan closed unexpectedly", "client_id", clientID)
|
||||
return
|
||||
}
|
||||
buffer = append(buffer, targetData)
|
||||
|
||||
if len(buffer) >= bufferMaxSize {
|
||||
// buffer is full, send immediately
|
||||
select {
|
||||
case sendChan <- buffer:
|
||||
default:
|
||||
logger.Warn(ctx, "sendChan is full, dropping aggregated data batch (buffer is full)", "client_id", clientID)
|
||||
}
|
||||
|
||||
// reset buffer
|
||||
buffer = make([]network.RealTimePullTarget, 0, bufferMaxSize)
|
||||
// reset the ticker to prevent it from triggering immediately after the ticker is sent
|
||||
ticker.Reset(sendMaxInterval)
|
||||
}
|
||||
case <-ticker.C:
|
||||
if len(buffer) > 0 {
|
||||
// when the ticker is triggered, all data in the send buffer is sent
|
||||
select {
|
||||
case sendChan <- buffer:
|
||||
default:
|
||||
logger.Warn(ctx, "sendChan is full, dropping aggregated data batch (ticker is triggered)", "client_id", clientID)
|
||||
}
|
||||
|
||||
// reset buffer
|
||||
buffer = make([]network.RealTimePullTarget, 0, bufferMaxSize)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
// send the last remaining data
|
||||
if len(buffer) > 0 {
|
||||
select {
|
||||
case sendChan <- buffer:
|
||||
default:
|
||||
logger.Warn(ctx, "sendChan is full, cannot send last remaining data during shutdown.", "client_id", clientID)
|
||||
}
|
||||
}
|
||||
logger.Info(ctx, "pullRealTimeDataHandler exiting as context is done.", "client_id", clientID)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// readClientMessages 负责持续监听客户端发送的消息(例如 Ping/Pong, Close Frame, 或控制命令)
|
||||
func readClientMessages(ctx context.Context, conn *websocket.Conn, clientID string, cancel context.CancelFunc) {
|
||||
// conn.SetReadLimit(512)
|
||||
for {
|
||||
msgType, msgBytes, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
||||
logger.Info(ctx, "client actively and normally closed the connection", "client_id", clientID)
|
||||
} else if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||
logger.Error(ctx, "an unexpected error occurred while reading the webSocket connection", "client_id", clientID, "error", err)
|
||||
} else {
|
||||
// handle other read errors (eg, I/O errors)
|
||||
logger.Error(ctx, "an error occurred while reading the webSocket connection", "client_id", clientID, "error", err)
|
||||
}
|
||||
cancel()
|
||||
break
|
||||
}
|
||||
|
||||
// process normal message from client
|
||||
if msgType == websocket.TextMessage || msgType == websocket.BinaryMessage {
|
||||
logger.Info(ctx, "read normal message from client", "client_id", clientID, "content", string(msgBytes))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sendAggregateRealTimeDataStream define func to responsible for continuously pushing aggregate real-time data to the client
|
||||
func sendAggregateRealTimeDataStream(conn *websocket.Conn, targetsData []network.RealTimePullTarget) error {
|
||||
if len(targetsData) == 0 {
|
||||
return nil
|
||||
}
|
||||
response := network.SuccessResponse{
|
||||
Code: 200,
|
||||
Msg: "success",
|
||||
Payload: network.RealTimePullPayload{
|
||||
Targets: targetsData,
|
||||
},
|
||||
}
|
||||
return conn.WriteJSON(response)
|
||||
}
|
||||
|
||||
// sendDataStream define func to manages a dedicated goroutine to push data batches or system signals to the websocket client
|
||||
func sendDataStream(ctx context.Context, conn *websocket.Conn, clientID string, sendChan <-chan []network.RealTimePullTarget, cancel context.CancelFunc) {
|
||||
logger.Info(ctx, "start dedicated websocket sender goroutine", "client_id", clientID)
|
||||
for targetsData := range sendChan {
|
||||
// TODO 使用 constants.SysCtrlPrefix + switch-case 形式应对可能的业务扩展
|
||||
if len(targetsData) == 1 && targetsData[0].ID == constants.SysCtrlAllRemoved {
|
||||
err := conn.WriteJSON(map[string]any{
|
||||
"code": 2101,
|
||||
"msg": "all targets removed in given client_id",
|
||||
"payload": map[string]int{
|
||||
"active_targets_count": 0,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error(ctx, "send all targets removed system signal failed", "client_id", clientID, "error", err)
|
||||
cancel()
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
if err := sendAggregateRealTimeDataStream(conn, targetsData); err != nil {
|
||||
logger.Error(ctx, "send the real time aggregate data failed in sender goroutine", "client_id", clientID, "error", err)
|
||||
cancel()
|
||||
return
|
||||
}
|
||||
}
|
||||
logger.Info(ctx, "sender goroutine exiting as channel is closed", "client_id", clientID)
|
||||
}
|
||||
|
||||
// processTargetPolling define func to process target in subscription map and data is continuously retrieved from redis based on the target
|
||||
func processTargetPolling(ctx context.Context, s *SharedSubState, clientID string, fanInChan chan network.RealTimePullTarget, sendChan chan<- []network.RealTimePullTarget) {
|
||||
// ensure the fanInChan will not leak
|
||||
defer close(fanInChan)
|
||||
logger.Info(ctx, fmt.Sprintf("start processing real time data polling for clientID:%s", clientID))
|
||||
stopChanMap := make(map[string]chan struct{})
|
||||
s.globalMutex.RLock()
|
||||
config, confExist := s.subMap[clientID]
|
||||
if !confExist {
|
||||
logger.Error(ctx, "can not found config into local stored map by clientID", "clientID", clientID)
|
||||
s.globalMutex.RUnlock()
|
||||
return
|
||||
}
|
||||
s.globalMutex.RUnlock()
|
||||
|
||||
config.mutex.RLock()
|
||||
for interval, measurementTargets := range config.measurements {
|
||||
for _, target := range measurementTargets {
|
||||
// add a secondary check to prevent the target from already existing in the stopChanMap
|
||||
if _, exists := stopChanMap[target]; exists {
|
||||
logger.Warn(ctx, "target already exists in polling map, skipping start-up", "target", target)
|
||||
continue
|
||||
}
|
||||
|
||||
targetContext, exist := config.targetContext[target]
|
||||
if !exist {
|
||||
logger.Error(ctx, "can not found subscription node param into param map", "target", target)
|
||||
continue
|
||||
}
|
||||
measurementInfo := targetContext.measurement
|
||||
|
||||
queryGStopChan := make(chan struct{})
|
||||
// store stop channel with target into map
|
||||
stopChanMap[target] = queryGStopChan
|
||||
queryKey, err := model.GenerateMeasureIdentifier(measurementInfo.DataSource)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "generate measurement indentifier by data_source field failed", "data_source", measurementInfo.DataSource, "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
pollingConfig := redisPollingConfig{
|
||||
targetID: target,
|
||||
queryKey: queryKey,
|
||||
interval: interval,
|
||||
dataSize: int64(measurementInfo.Size),
|
||||
}
|
||||
go realTimeDataQueryFromRedis(ctx, pollingConfig, fanInChan, queryGStopChan)
|
||||
}
|
||||
}
|
||||
config.mutex.RUnlock()
|
||||
|
||||
for {
|
||||
select {
|
||||
case transportTargets, ok := <-config.noticeChan:
|
||||
if !ok {
|
||||
logger.Error(ctx, "notice channel was closed unexpectedly", "clientID", clientID)
|
||||
stopAllPolling(ctx, stopChanMap)
|
||||
return
|
||||
}
|
||||
config.mutex.Lock()
|
||||
switch transportTargets.OperationType {
|
||||
case constants.OpAppend:
|
||||
appendTargets(ctx, config, stopChanMap, fanInChan, transportTargets.Targets)
|
||||
case constants.OpRemove:
|
||||
removeTargets(ctx, stopChanMap, transportTargets.Targets, sendChan)
|
||||
case constants.OpUpdate:
|
||||
updateTargets(ctx, config, stopChanMap, fanInChan, transportTargets.Targets)
|
||||
}
|
||||
config.mutex.Unlock()
|
||||
case <-ctx.Done():
|
||||
logger.Info(ctx, fmt.Sprintf("stop all data retrieval goroutines under this clientID:%s", clientID))
|
||||
stopAllPolling(ctx, stopChanMap)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// appendTargets starts new polling goroutines for targets that were just added
|
||||
func appendTargets(ctx context.Context, config *RealTimeSubConfig, stopChanMap map[string]chan struct{}, fanInChan chan network.RealTimePullTarget, appendTargets []string) {
|
||||
appendTargetsSet := make(map[string]struct{}, len(appendTargets))
|
||||
for _, target := range appendTargets {
|
||||
appendTargetsSet[target] = struct{}{}
|
||||
}
|
||||
|
||||
for _, target := range appendTargets {
|
||||
targetContext, exists := config.targetContext[target]
|
||||
if !exists {
|
||||
logger.Error(ctx, "the append target does not exist in the real time data config context map,skipping the startup step", "target", target)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, exists := stopChanMap[target]; exists {
|
||||
logger.Error(ctx, "the append target already has a stop channel, skipping the startup step", "target", target)
|
||||
continue
|
||||
}
|
||||
|
||||
queryGStopChan := make(chan struct{})
|
||||
stopChanMap[target] = queryGStopChan
|
||||
|
||||
interval := targetContext.interval
|
||||
_, exists = config.measurements[interval]
|
||||
if !exists {
|
||||
logger.Error(ctx, "targetContext exist but measurements is missing, cannot update config", "target", target, "interval", interval)
|
||||
continue
|
||||
}
|
||||
delete(appendTargetsSet, target)
|
||||
|
||||
queryKey, err := model.GenerateMeasureIdentifier(targetContext.measurement.DataSource)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "the append target generate redis query key identifier failed", "target", target, "error", err)
|
||||
continue
|
||||
}
|
||||
pollingConfig := redisPollingConfig{
|
||||
targetID: target,
|
||||
queryKey: queryKey,
|
||||
interval: targetContext.interval,
|
||||
dataSize: int64(targetContext.measurement.Size),
|
||||
}
|
||||
go realTimeDataQueryFromRedis(ctx, pollingConfig, fanInChan, queryGStopChan)
|
||||
|
||||
logger.Info(ctx, "started new polling goroutine for appended target", "target", target, "interval", targetContext.interval)
|
||||
}
|
||||
|
||||
// allKeys := util.GetKeysFromSet(appendTargetsSet)
|
||||
allKeys := slices.Sorted(maps.Keys(appendTargetsSet))
|
||||
if len(allKeys) > 0 {
|
||||
logger.Warn(ctx, fmt.Sprintf("the following targets:%v start up fetch real time data process goroutine not started", allKeys))
|
||||
clear(appendTargetsSet)
|
||||
}
|
||||
}
|
||||
|
||||
// updateTargets starts new polling goroutines for targets that were just updated
|
||||
func updateTargets(ctx context.Context, config *RealTimeSubConfig, stopChanMap map[string]chan struct{}, fanInChan chan network.RealTimePullTarget, updateTargets []string) {
|
||||
updateTargetsSet := make(map[string]struct{}, len(updateTargets))
|
||||
for _, target := range updateTargets {
|
||||
updateTargetsSet[target] = struct{}{}
|
||||
}
|
||||
|
||||
for _, target := range updateTargets {
|
||||
targetContext, exists := config.targetContext[target]
|
||||
if !exists {
|
||||
logger.Error(ctx, "the update target does not exist in the real time data config context map,skipping the startup step", "target", target)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, exist := stopChanMap[target]; !exist {
|
||||
logger.Error(ctx, "the update target does not has a stop channel, skipping the startup step", "target", target)
|
||||
continue
|
||||
}
|
||||
|
||||
oldQueryGStopChan := stopChanMap[target]
|
||||
logger.Info(ctx, "stopped old polling goroutine for updated target", "target", target)
|
||||
close(oldQueryGStopChan)
|
||||
|
||||
newQueryGStopChan := make(chan struct{})
|
||||
stopChanMap[target] = newQueryGStopChan
|
||||
|
||||
interval := targetContext.interval
|
||||
_, exists = config.measurements[interval]
|
||||
if !exists {
|
||||
logger.Error(ctx, "targetContext exist but measurements is missing, cannot update config", "target", target, "interval", interval)
|
||||
continue
|
||||
}
|
||||
delete(updateTargetsSet, target)
|
||||
|
||||
queryKey, err := model.GenerateMeasureIdentifier(targetContext.measurement.DataSource)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "the update target generate redis query key identifier failed", "target", target, "error", err)
|
||||
continue
|
||||
}
|
||||
pollingConfig := redisPollingConfig{
|
||||
targetID: target,
|
||||
queryKey: queryKey,
|
||||
interval: targetContext.interval,
|
||||
dataSize: int64(targetContext.measurement.Size),
|
||||
}
|
||||
go realTimeDataQueryFromRedis(ctx, pollingConfig, fanInChan, newQueryGStopChan)
|
||||
|
||||
logger.Info(ctx, "started new polling goroutine for update target", "target", target, "interval", targetContext.interval)
|
||||
}
|
||||
|
||||
// allKeys := util.GetKeysFromSet(updateTargetsSet)
|
||||
allKeys := slices.Sorted(maps.Keys(updateTargetsSet))
|
||||
if len(allKeys) > 0 {
|
||||
logger.Warn(ctx, fmt.Sprintf("the following targets:%v start up fetch real time data process goroutine not started", allKeys))
|
||||
clear(updateTargetsSet)
|
||||
}
|
||||
}
|
||||
|
||||
// removeTargets define func to stops running polling goroutines for targets that were removed
|
||||
func removeTargets(ctx context.Context, stopChanMap map[string]chan struct{}, removeTargets []string, sendChan chan<- []network.RealTimePullTarget) {
|
||||
for _, target := range removeTargets {
|
||||
stopChan, exists := stopChanMap[target]
|
||||
if !exists {
|
||||
logger.Warn(ctx, "removeTarget was not running, skipping remove operation", "target", target)
|
||||
continue
|
||||
}
|
||||
|
||||
close(stopChan)
|
||||
delete(stopChanMap, target)
|
||||
logger.Info(ctx, "stopped polling goroutine for removed target", "target", target)
|
||||
}
|
||||
|
||||
if len(stopChanMap) == 0 {
|
||||
logger.Info(ctx, "all polling goroutines have been stopped for this client")
|
||||
sendSpecialStatusToClient(ctx, sendChan)
|
||||
}
|
||||
}
|
||||
|
||||
func sendSpecialStatusToClient(ctx context.Context, sendChan chan<- []network.RealTimePullTarget) {
|
||||
specialTarget := network.RealTimePullTarget{
|
||||
ID: constants.SysCtrlAllRemoved,
|
||||
Datas: []network.RealTimePullData{},
|
||||
}
|
||||
|
||||
select {
|
||||
case sendChan <- []network.RealTimePullTarget{specialTarget}:
|
||||
logger.Info(ctx, "sent 2101 status request to sendChan")
|
||||
default:
|
||||
logger.Warn(ctx, "sendChan is full, skipping 2101 status message")
|
||||
}
|
||||
}
|
||||
|
||||
// stopAllPolling stops all running query goroutines for a specific client
|
||||
func stopAllPolling(ctx context.Context, stopChanMap map[string]chan struct{}) {
|
||||
for target, stopChan := range stopChanMap {
|
||||
logger.Info(ctx, fmt.Sprintf("stop the data fetching behavior for the corresponding target:%s", target))
|
||||
close(stopChan)
|
||||
}
|
||||
clear(stopChanMap)
|
||||
return
|
||||
}
|
||||
|
||||
// redisPollingConfig define struct for param which query real time data from redis
|
||||
type redisPollingConfig struct {
|
||||
targetID string
|
||||
queryKey string
|
||||
interval string
|
||||
dataSize int64
|
||||
}
|
||||
|
||||
func realTimeDataQueryFromRedis(ctx context.Context, config redisPollingConfig, fanInChan chan network.RealTimePullTarget, stopChan chan struct{}) {
|
||||
logger.Info(ctx, "start a redis query goroutine for real time data pulling", "targetID", config.targetID, "queryKey", config.queryKey, "interval", config.interval, "dataSize", config.dataSize)
|
||||
duration, err := time.ParseDuration(config.interval)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "failed to parse the time string", "interval", config.interval, "error", err)
|
||||
return
|
||||
}
|
||||
ticker := time.NewTicker(duration)
|
||||
defer ticker.Stop()
|
||||
|
||||
client := diagram.NewRedisClient()
|
||||
needPerformQuery := true
|
||||
for {
|
||||
if needPerformQuery {
|
||||
performQuery(ctx, client, config, fanInChan)
|
||||
needPerformQuery = false
|
||||
}
|
||||
|
||||
select {
|
||||
case <-ticker.C:
|
||||
needPerformQuery = true
|
||||
case <-stopChan:
|
||||
logger.Info(ctx, "stop the redis query goroutine via a singal")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func performQuery(ctx context.Context, client *diagram.RedisClient, config redisPollingConfig, fanInChan chan network.RealTimePullTarget) {
|
||||
members, err := client.QueryByZRangeByLex(ctx, config.queryKey, config.dataSize)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query real time data from redis failed", "key", config.queryKey, "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
pullDatas := make([]network.RealTimePullData, 0, len(members))
|
||||
for _, member := range members {
|
||||
pullDatas = append(pullDatas, network.RealTimePullData{
|
||||
Time: member.Member.(string),
|
||||
Value: member.Score,
|
||||
})
|
||||
}
|
||||
sortPullDataByTimeAscending(ctx, pullDatas)
|
||||
targetData := network.RealTimePullTarget{
|
||||
ID: config.targetID,
|
||||
Datas: pullDatas,
|
||||
}
|
||||
|
||||
select {
|
||||
case fanInChan <- targetData:
|
||||
default:
|
||||
// TODO[BACKPRESSURE-ISSUE] 考虑 fanInChan 阻塞,当出现大量数据阻塞查询循环并丢弃时,采取背压方式解决问题 #1
|
||||
logger.Warn(ctx, "fanInChan is full, dropping real-time data frame", "key", config.queryKey, "data_size", len(members))
|
||||
}
|
||||
}
|
||||
|
||||
func sortPullDataByTimeAscending(ctx context.Context, data []network.RealTimePullData) {
|
||||
sort.Slice(data, func(i, j int) bool {
|
||||
t1, err1 := strconv.ParseInt(data[i].Time, 10, 64)
|
||||
if err1 != nil {
|
||||
logger.Error(ctx, "parsing real time data timestamp failed", "index", i, "time", data[i].Time, "error", err1)
|
||||
return false
|
||||
}
|
||||
|
||||
t2, err2 := strconv.ParseInt(data[j].Time, 10, 64)
|
||||
if err2 != nil {
|
||||
logger.Error(ctx, "parsing real time data timestamp failed", "index", j, "time", data[j].Time, "error", err2)
|
||||
return true
|
||||
}
|
||||
return t1 < t2
|
||||
})
|
||||
}
|
||||
|
|
@ -2,43 +2,189 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"modelRT/alert"
|
||||
"modelRT/constants"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/bitly/go-simplejson"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
var wsUpgrader = websocket.Upgrader{
|
||||
ReadBufferSize: 1024,
|
||||
WriteBufferSize: 1024,
|
||||
// CheckOrigin 必须返回 true,否则浏览器会拒绝连接
|
||||
CheckOrigin: func(_ *http.Request) bool {
|
||||
// 在生产环境中,应该更严格地检查 Origin 头部
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
// QueryRealTimeDataHandler define query real time data process API
|
||||
// @Summary 获取实时测点数据
|
||||
// @Description 根据用户输入的组件token,从 dataRT 服务中持续获取测点实时数据
|
||||
// @Tags RealTime Component
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param token query string true "测量点唯一标识符 (e.g.grid_1:zone_1:station_1:transformfeeder1_220.I_A_rms)"
|
||||
// @Param begin query int true "查询起始时间 (Unix时间戳, e.g., 1761008266)"
|
||||
// @Param end query int true "查询结束时间 (Unix时间戳, e.g., 1761526675)"
|
||||
// @Success 200 {object} network.SuccessResponse{payload=network.RealTimeDataPayload} "返回实时数据成功"
|
||||
//
|
||||
// @Example 200 {
|
||||
// "code": 200,
|
||||
// "msg": "success",
|
||||
// "payload": {
|
||||
// "input": "grid1.zone1.station1.ns1.tag1.transformfeeder1_220.I_A_rms",
|
||||
// "sub_pos": [
|
||||
// {
|
||||
// "time": 1736305467506000000,
|
||||
// "value": 1
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Failure 400 {object} network.FailureResponse "返回实时数据失败"
|
||||
//
|
||||
// @Example 400 {
|
||||
// "code": 400,
|
||||
// "msg": "failed to get real time data from dataRT",
|
||||
// }
|
||||
//
|
||||
// @Router /data/realtime [get]
|
||||
func QueryRealTimeDataHandler(c *gin.Context) {
|
||||
var targetLevel constants.AlertLevel
|
||||
var request network.RealTimeQueryRequest
|
||||
|
||||
alertManger := alert.GetAlertMangerInstance()
|
||||
|
||||
levelStr := c.Query("level")
|
||||
level, err := strconv.Atoi(levelStr)
|
||||
if err != nil {
|
||||
logger.Error(c, "convert alert level string to int failed", "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error(c, "failed to unmarshal real time query request", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
})
|
||||
return
|
||||
}
|
||||
targetLevel = constants.AlertLevel(level)
|
||||
events := alertManger.GetRangeEventsByLevel(targetLevel)
|
||||
|
||||
resp := network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
"events": events,
|
||||
},
|
||||
conn, err := wsUpgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
logger.Error(c, "upgrade http protocol to websocket protocal failed", "error", err)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
// start a goroutine to open a websocket service with the dataRT service and use the channel to pass data back. Start and maintain the websocket connection with the front-end UI in the local api
|
||||
transportChannel := make(chan []any, 100)
|
||||
closeChannel := make(chan struct{})
|
||||
|
||||
for {
|
||||
select {
|
||||
case data := <-transportChannel:
|
||||
respByte, err := jsoniter.Marshal(data)
|
||||
if err != nil {
|
||||
logger.Error(c, "marshal real time data to bytes failed", "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
err = conn.WriteMessage(websocket.TextMessage, respByte)
|
||||
if err != nil {
|
||||
logger.Error(c, "write message to websocket connection failed", "error", err)
|
||||
continue
|
||||
}
|
||||
case <-closeChannel:
|
||||
logger.Info(c, "data receiving goroutine has been closed")
|
||||
// TODO 优化时间控制
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
err := conn.WriteControl(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "the session ended normally"), deadline)
|
||||
if err != nil {
|
||||
logger.Error(c, "sending close control message failed", "error", err)
|
||||
}
|
||||
// gracefully close session processing
|
||||
err = conn.Close()
|
||||
if err != nil {
|
||||
logger.Error(c, "websocket conn closed failed", "error", err)
|
||||
}
|
||||
logger.Info(c, "websocket connection closed successfully.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// receiveRealTimeDataByWebSocket define func of receive real time data by websocket
|
||||
func receiveRealTimeDataByWebSocket(ctx context.Context, params url.Values, transportChannel chan []any, closeChannel chan struct{}) {
|
||||
serverURL := "ws://127.0.0.1:8888/ws/points"
|
||||
u, err := url.Parse(serverURL)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "parse url failed", "error", err)
|
||||
}
|
||||
|
||||
q := u.Query()
|
||||
for key, values := range params {
|
||||
for _, value := range values {
|
||||
q.Add(key, value)
|
||||
}
|
||||
}
|
||||
u.RawQuery = q.Encode()
|
||||
finalServerURL := u.String()
|
||||
|
||||
conn, resp, err := websocket.DefaultDialer.Dial(finalServerURL, nil)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "dialing websocket server failed", "error", err)
|
||||
if resp != nil {
|
||||
logger.Error(ctx, "websocket server response", "status", resp.Status)
|
||||
}
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
for {
|
||||
msgType, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
// check if it is an expected shutdown error
|
||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
|
||||
logger.Info(ctx, "connection closed normally")
|
||||
} else {
|
||||
logger.Error(ctx, "abnormal disconnection from websocket server", "err", err)
|
||||
}
|
||||
close(closeChannel)
|
||||
break
|
||||
}
|
||||
logger.Info(ctx, "received info from dataRT server", "msg_type", messageTypeToString(msgType), "message", string(message))
|
||||
|
||||
js, err := simplejson.NewJson(message)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "parse real time data from message failed", "message", string(message), "err", err)
|
||||
continue
|
||||
}
|
||||
|
||||
subPoss, err := js.Get("sub_pos").Array()
|
||||
if err != nil {
|
||||
logger.Error(ctx, "parse sub_pos struct from message json info", "sub_pos", js.Get("sub_pos"), "err", err)
|
||||
continue
|
||||
}
|
||||
transportChannel <- subPoss
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// messageTypeToString define func of auxiliary to convert message type to string
|
||||
func messageTypeToString(t int) string {
|
||||
switch t {
|
||||
case websocket.TextMessage:
|
||||
return "TEXT"
|
||||
case websocket.BinaryMessage:
|
||||
return "BINARY"
|
||||
case websocket.PingMessage:
|
||||
return "PING"
|
||||
case websocket.PongMessage:
|
||||
return "PONG"
|
||||
case websocket.CloseMessage:
|
||||
return "CLOSE"
|
||||
default:
|
||||
return "UNKNOWN"
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,744 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maps"
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/database"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
"modelRT/util"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gofrs/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var globalSubState *SharedSubState
|
||||
|
||||
func init() {
|
||||
globalSubState = NewSharedSubState()
|
||||
}
|
||||
|
||||
// RealTimeSubHandler define real time data subscriptions process API
|
||||
// @Summary 开始或结束订阅实时数据
|
||||
// @Description 根据用户输入的组件token,从 modelRT 服务中开始或结束对于量测节点的实时数据的订阅
|
||||
// @Tags RealTime Component
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body network.RealTimeSubRequest true "量测节点实时数据订阅"
|
||||
// @Success 200 {object} network.SuccessResponse{payload=network.RealTimeSubPayload} "订阅实时数据结果列表"
|
||||
//
|
||||
// @Example 200 {
|
||||
// "code": 200,
|
||||
// "msg": "success",
|
||||
// "payload": {
|
||||
// "targets": [
|
||||
// {
|
||||
// "id": "grid1.zone1.station1.ns1.tag1.bay.I11_C_rms",
|
||||
// "code": "1001",
|
||||
// "msg": "subscription success"
|
||||
// },
|
||||
// {
|
||||
// "id": "grid1.zone1.station1.ns1.tag1.bay.I11_B_rms",
|
||||
// "code": "1002",
|
||||
// "msg": "subscription failed"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Failure 400 {object} network.FailureResponse{payload=network.RealTimeSubPayload} "订阅实时数据结果列表"
|
||||
//
|
||||
// @Example 400 {
|
||||
// "code": 400,
|
||||
// "msg": "failed to get recommend data from redis",
|
||||
// "payload": {
|
||||
// "targets": [
|
||||
// {
|
||||
// "id": "grid1.zone1.station1.ns1.tag1.bay.I11_A_rms",
|
||||
// "code": "1002",
|
||||
// "msg": "subscription failed"
|
||||
// },
|
||||
// {
|
||||
// "id": "grid1.zone1.station1.ns1.tag1.bay.I11_B_rms",
|
||||
// "code": "1002",
|
||||
// "msg": "subscription failed"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Router /monitors/data/subscriptions [post]
|
||||
func RealTimeSubHandler(c *gin.Context) {
|
||||
var request network.RealTimeSubRequest
|
||||
var subAction string
|
||||
var clientID string
|
||||
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error(c, "failed to unmarshal real time query request", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if request.Action == constants.SubStartAction && request.ClientID == "" {
|
||||
subAction = request.Action
|
||||
id, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
logger.Error(c, "failed to generate client id", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
clientID = id.String()
|
||||
} else if request.Action == constants.SubStartAction && request.ClientID != "" {
|
||||
subAction = constants.SubAppendAction
|
||||
clientID = request.ClientID
|
||||
} else if request.Action == constants.SubStopAction && request.ClientID != "" {
|
||||
subAction = request.Action
|
||||
clientID = request.ClientID
|
||||
} else if request.Action == constants.SubUpdateAction && request.ClientID != "" {
|
||||
subAction = request.Action
|
||||
clientID = request.ClientID
|
||||
}
|
||||
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
// open transaction
|
||||
tx := pgClient.Begin()
|
||||
defer tx.Commit()
|
||||
|
||||
switch subAction {
|
||||
case constants.SubStartAction:
|
||||
results, err := globalSubState.CreateConfig(c, tx, clientID, request.Measurements)
|
||||
if err != nil {
|
||||
logger.Error(c, "create real time data subscription config failed", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
case constants.SubStopAction:
|
||||
results, err := globalSubState.RemoveTargets(c, clientID, request.Measurements)
|
||||
if err != nil {
|
||||
logger.Error(c, "remove target to real time data subscription config failed", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
case constants.SubAppendAction:
|
||||
results, err := globalSubState.AppendTargets(c, tx, clientID, request.Measurements)
|
||||
if err != nil {
|
||||
logger.Error(c, "append target to real time data subscription config failed", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
case constants.SubUpdateAction:
|
||||
results, err := globalSubState.UpdateTargets(c, tx, clientID, request.Measurements)
|
||||
if err != nil {
|
||||
logger.Error(c, "update target to real time data subscription config failed", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
default:
|
||||
err := fmt.Errorf("%w: request action is %s", constants.ErrUnsupportedSubAction, request.Action)
|
||||
logger.Error(c, "unsupported action of real time data subscription request", "error", err)
|
||||
requestTargetsCount := processRealTimeRequestCount(request.Measurements)
|
||||
results := processRealTimeRequestTargets(request.Measurements, requestTargetsCount, err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: network.RealTimeSubPayload{
|
||||
ClientID: clientID,
|
||||
TargetResults: results,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// RealTimeSubMeasurement define struct of real time subscription measurement
|
||||
type RealTimeSubMeasurement struct {
|
||||
targets []string
|
||||
}
|
||||
|
||||
// TargetPollingContext define struct of real time pulling reverse context
|
||||
type TargetPollingContext struct {
|
||||
interval string
|
||||
measurement *orm.Measurement
|
||||
}
|
||||
|
||||
// RealTimeSubConfig define struct of real time subscription config
|
||||
type RealTimeSubConfig struct {
|
||||
noticeChan chan *transportTargets
|
||||
mutex sync.RWMutex
|
||||
measurements map[string][]string
|
||||
targetContext map[string]*TargetPollingContext
|
||||
}
|
||||
|
||||
// SharedSubState define struct of shared subscription state with mutex
|
||||
type SharedSubState struct {
|
||||
subMap map[string]*RealTimeSubConfig
|
||||
globalMutex sync.RWMutex
|
||||
}
|
||||
|
||||
// NewSharedSubState define function to create new SharedSubState
|
||||
func NewSharedSubState() *SharedSubState {
|
||||
return &SharedSubState{
|
||||
subMap: make(map[string]*RealTimeSubConfig),
|
||||
}
|
||||
}
|
||||
|
||||
// processAndValidateTargetsForStart define func to perform all database I/O operations in a lock-free state for start action
|
||||
func processAndValidateTargetsForStart(ctx context.Context, tx *gorm.DB, measurements []network.RealTimeMeasurementItem, allReqTargetNum int) (
|
||||
[]network.TargetResult, []string,
|
||||
map[string][]string,
|
||||
map[string]*TargetPollingContext,
|
||||
) {
|
||||
targetProcessResults := make([]network.TargetResult, 0, allReqTargetNum)
|
||||
newMeasMap := make(map[string][]string)
|
||||
successfulTargets := make([]string, 0, allReqTargetNum)
|
||||
newMeasContextMap := make(map[string]*TargetPollingContext)
|
||||
|
||||
for _, measurementItem := range measurements {
|
||||
interval := measurementItem.Interval
|
||||
for _, target := range measurementItem.Targets {
|
||||
var targetResult network.TargetResult
|
||||
targetResult.ID = target
|
||||
targetModel, err := database.ParseDataIdentifierToken(ctx, tx, target)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "parse data indentity token failed", "error", err, "identity_token", target)
|
||||
targetResult.Code = constants.SubFailedCode
|
||||
targetResult.Msg = fmt.Sprintf("%s: %s", constants.SubFailedMsg, err.Error())
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
continue
|
||||
}
|
||||
targetResult.Code = constants.SubSuccessCode
|
||||
targetResult.Msg = constants.SubSuccessMsg
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
successfulTargets = append(successfulTargets, target)
|
||||
|
||||
if _, exists := newMeasMap[interval]; !exists {
|
||||
newMeasMap[interval] = make([]string, 0, len(measurementItem.Targets))
|
||||
}
|
||||
|
||||
meas := newMeasMap[interval]
|
||||
meas = append(meas, target)
|
||||
newMeasMap[interval] = meas
|
||||
newMeasContextMap[target] = &TargetPollingContext{
|
||||
interval: interval,
|
||||
measurement: targetModel.GetMeasurementInfo(),
|
||||
}
|
||||
}
|
||||
}
|
||||
return targetProcessResults, successfulTargets, newMeasMap, newMeasContextMap
|
||||
}
|
||||
|
||||
// processAndValidateTargetsForUpdate define func to perform all database I/O operations in a lock-free state for update action
|
||||
func processAndValidateTargetsForUpdate(ctx context.Context, tx *gorm.DB, config *RealTimeSubConfig, measurements []network.RealTimeMeasurementItem, allReqTargetNum int) (
|
||||
[]network.TargetResult, []string,
|
||||
map[string][]string,
|
||||
map[string]*TargetPollingContext,
|
||||
) {
|
||||
targetProcessResults := make([]network.TargetResult, 0, allReqTargetNum)
|
||||
newMeasMap := make(map[string][]string)
|
||||
successfulTargets := make([]string, 0, allReqTargetNum)
|
||||
newMeasContextMap := make(map[string]*TargetPollingContext)
|
||||
|
||||
for _, measurementItem := range measurements {
|
||||
interval := measurementItem.Interval
|
||||
for _, target := range measurementItem.Targets {
|
||||
targetResult := network.TargetResult{ID: target}
|
||||
if _, exist := config.targetContext[target]; !exist {
|
||||
err := fmt.Errorf("target %s does not exists in subscription list", target)
|
||||
logger.Error(ctx, "update target does not exist in subscription list", "error", err, "target", target)
|
||||
targetResult.Code = constants.UpdateSubFailedCode
|
||||
targetResult.Msg = fmt.Sprintf("%s: %s", constants.UpdateSubFailedMsg, err.Error())
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
continue
|
||||
}
|
||||
|
||||
targetModel, err := database.ParseDataIdentifierToken(ctx, tx, target)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "parse data indentity token failed", "error", err, "identity_token", target)
|
||||
targetResult.Code = constants.UpdateSubFailedCode
|
||||
targetResult.Msg = fmt.Sprintf("%s: %s", constants.UpdateSubFailedMsg, err.Error())
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
continue
|
||||
}
|
||||
|
||||
targetResult.Code = constants.UpdateSubSuccessCode
|
||||
targetResult.Msg = constants.UpdateSubSuccessMsg
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
successfulTargets = append(successfulTargets, target)
|
||||
|
||||
if _, exists := newMeasMap[interval]; !exists {
|
||||
newMeasMap[interval] = make([]string, 0, len(measurementItem.Targets))
|
||||
}
|
||||
|
||||
meas := newMeasMap[interval]
|
||||
meas = append(meas, target)
|
||||
newMeasMap[interval] = meas
|
||||
newMeasContextMap[target] = &TargetPollingContext{
|
||||
interval: interval,
|
||||
measurement: targetModel.GetMeasurementInfo(),
|
||||
}
|
||||
}
|
||||
}
|
||||
return targetProcessResults, successfulTargets, newMeasMap, newMeasContextMap
|
||||
}
|
||||
|
||||
// mergeMeasurementsForStart define func to merge newMeasurementsMap into existingMeasurementsMap for start action
|
||||
func mergeMeasurementsForStart(config *RealTimeSubConfig, newMeasurements map[string][]string, newMeasurementsContextMap map[string]*TargetPollingContext) []string {
|
||||
allDuplicates := make([]string, 0)
|
||||
for interval, newMeas := range newMeasurements {
|
||||
if existingMeas, exists := config.measurements[interval]; exists {
|
||||
// deduplication operations prevent duplicate subscriptions to the same measurement node
|
||||
deduplicated, duplicates := util.DeduplicateAndReportDuplicates(existingMeas, newMeas)
|
||||
|
||||
if len(duplicates) > 0 {
|
||||
for _, duplicate := range duplicates {
|
||||
delete(newMeasurementsContextMap, duplicate)
|
||||
}
|
||||
allDuplicates = append(allDuplicates, duplicates...)
|
||||
}
|
||||
|
||||
if len(deduplicated) > 0 {
|
||||
existingMeas = append(existingMeas, deduplicated...)
|
||||
config.measurements[interval] = existingMeas
|
||||
maps.Copy(config.targetContext, newMeasurementsContextMap)
|
||||
}
|
||||
} else {
|
||||
config.measurements[interval] = newMeas
|
||||
maps.Copy(config.targetContext, newMeasurementsContextMap)
|
||||
}
|
||||
}
|
||||
return allDuplicates
|
||||
}
|
||||
|
||||
// mergeMeasurementsForUpdate define func to merge newMeasurementsMap into existingMeasurementsMap for update action
|
||||
func mergeMeasurementsForUpdate(config *RealTimeSubConfig, newMeasurements map[string][]string, newMeasurementsContextMap map[string]*TargetPollingContext) ([]string, error) {
|
||||
allDuplicates := make([]string, 0)
|
||||
delMeasMap := make(map[string][]string)
|
||||
for _, newMeas := range newMeasurements {
|
||||
for _, measurement := range newMeas {
|
||||
oldInterval := config.targetContext[measurement].interval
|
||||
if _, exists := delMeasMap[oldInterval]; !exists {
|
||||
delMeasurements := []string{measurement}
|
||||
delMeasMap[oldInterval] = delMeasurements
|
||||
} else {
|
||||
delMeasurements := delMeasMap[oldInterval]
|
||||
delMeasurements = append(delMeasurements, measurement)
|
||||
delMeasMap[oldInterval] = delMeasurements
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for interval, delMeas := range delMeasMap {
|
||||
existingMeas, exist := config.measurements[interval]
|
||||
if !exist {
|
||||
return nil, fmt.Errorf("can not find exist measurements in %s interval", interval)
|
||||
}
|
||||
|
||||
measurements := util.RemoveTargetsFromSliceSimple(existingMeas, delMeas)
|
||||
config.measurements[interval] = measurements
|
||||
}
|
||||
|
||||
for interval, newMeas := range newMeasurements {
|
||||
if existingMeas, exists := config.measurements[interval]; exists {
|
||||
deduplicated, duplicates := util.DeduplicateAndReportDuplicates(existingMeas, newMeas)
|
||||
|
||||
if len(duplicates) > 0 {
|
||||
for _, duplicate := range duplicates {
|
||||
delete(newMeasurementsContextMap, duplicate)
|
||||
}
|
||||
allDuplicates = append(allDuplicates, duplicates...)
|
||||
}
|
||||
|
||||
if len(deduplicated) > 0 {
|
||||
existingMeas = append(existingMeas, deduplicated...)
|
||||
config.measurements[interval] = existingMeas
|
||||
maps.Copy(config.targetContext, newMeasurementsContextMap)
|
||||
}
|
||||
}
|
||||
}
|
||||
return allDuplicates, nil
|
||||
}
|
||||
|
||||
// CreateConfig define function to create config in SharedSubState
|
||||
func (s *SharedSubState) CreateConfig(ctx context.Context, tx *gorm.DB, clientID string, measurements []network.RealTimeMeasurementItem) ([]network.TargetResult, error) {
|
||||
requestTargetsCount := processRealTimeRequestCount(measurements)
|
||||
targetProcessResults, _, newMeasurementsMap, measurementContexts := processAndValidateTargetsForStart(ctx, tx, measurements, requestTargetsCount)
|
||||
s.globalMutex.Lock()
|
||||
if _, exist := s.subMap[clientID]; exist {
|
||||
s.globalMutex.Unlock()
|
||||
err := fmt.Errorf("clientID %s already exists. use AppendTargets to modify existing config", clientID)
|
||||
logger.Error(ctx, "clientID already exists. use AppendTargets to modify existing config", "error", err)
|
||||
return targetProcessResults, err
|
||||
}
|
||||
|
||||
config := &RealTimeSubConfig{
|
||||
noticeChan: make(chan *transportTargets, constants.NoticeChanCap),
|
||||
measurements: newMeasurementsMap,
|
||||
targetContext: measurementContexts,
|
||||
}
|
||||
s.subMap[clientID] = config
|
||||
s.globalMutex.Unlock()
|
||||
return targetProcessResults, nil
|
||||
}
|
||||
|
||||
// AppendTargets define function to append targets in SharedSubState
|
||||
func (s *SharedSubState) AppendTargets(ctx context.Context, tx *gorm.DB, clientID string, measurements []network.RealTimeMeasurementItem) ([]network.TargetResult, error) {
|
||||
requestTargetsCount := processRealTimeRequestCount(measurements)
|
||||
|
||||
s.globalMutex.RLock()
|
||||
config, exist := s.subMap[clientID]
|
||||
s.globalMutex.RUnlock()
|
||||
|
||||
if !exist {
|
||||
err := fmt.Errorf("clientID %s not found. use CreateConfig to start a new config", clientID)
|
||||
logger.Error(ctx, "clientID not found. use CreateConfig to start a new config", "error", err)
|
||||
return processRealTimeRequestTargets(measurements, requestTargetsCount, err), err
|
||||
}
|
||||
|
||||
targetProcessResults, successfulTargets, newMeasMap, newMeasContextMap := processAndValidateTargetsForStart(ctx, tx, measurements, requestTargetsCount)
|
||||
|
||||
config.mutex.Lock()
|
||||
allDuplicates := mergeMeasurementsForStart(config, newMeasMap, newMeasContextMap)
|
||||
if len(allDuplicates) > 0 {
|
||||
logger.Warn(ctx, "some targets are duplicate and have been ignored in append operation", "clientID", clientID, "duplicates", allDuplicates)
|
||||
// process repeat target in targetProcessResults and successfulTargets
|
||||
targetProcessResults, successfulTargets = filterAndDeduplicateRepeatTargets(targetProcessResults, successfulTargets, allDuplicates)
|
||||
}
|
||||
config.mutex.Unlock()
|
||||
|
||||
if len(successfulTargets) > 0 {
|
||||
transportTargets := &transportTargets{
|
||||
OperationType: constants.OpAppend,
|
||||
Targets: successfulTargets,
|
||||
}
|
||||
config.noticeChan <- transportTargets
|
||||
}
|
||||
|
||||
return targetProcessResults, nil
|
||||
}
|
||||
|
||||
func filterAndDeduplicateRepeatTargets(resultsSlice []network.TargetResult, idsSlice []string, duplicates []string) ([]network.TargetResult, []string) {
|
||||
filteredIDs := make([]string, 0, len(idsSlice))
|
||||
set := make(map[string]struct{}, len(duplicates))
|
||||
for _, duplicate := range duplicates {
|
||||
set[duplicate] = struct{}{}
|
||||
}
|
||||
|
||||
for index := range resultsSlice {
|
||||
if _, isTarget := set[resultsSlice[index].ID]; isTarget {
|
||||
resultsSlice[index].Code = constants.SubRepeatCode
|
||||
resultsSlice[index].Msg = constants.SubRepeatMsg
|
||||
}
|
||||
}
|
||||
|
||||
for _, id := range idsSlice {
|
||||
if _, isTarget := set[id]; !isTarget {
|
||||
filteredIDs = append(filteredIDs, id)
|
||||
}
|
||||
}
|
||||
|
||||
return resultsSlice, filteredIDs
|
||||
}
|
||||
|
||||
// UpsertTargets define function to upsert targets in SharedSubState
|
||||
func (s *SharedSubState) UpsertTargets(ctx context.Context, tx *gorm.DB, clientID string, measurements []network.RealTimeMeasurementItem) ([]network.TargetResult, error) {
|
||||
requestTargetsCount := processRealTimeRequestCount(measurements)
|
||||
targetProcessResults, successfulTargets, newMeasMap, newMeasContextMap := processAndValidateTargetsForStart(ctx, tx, measurements, requestTargetsCount)
|
||||
|
||||
s.globalMutex.RLock()
|
||||
config, exist := s.subMap[clientID]
|
||||
s.globalMutex.RUnlock()
|
||||
|
||||
var opType constants.TargetOperationType
|
||||
if exist {
|
||||
opType = constants.OpUpdate
|
||||
config.mutex.Lock()
|
||||
mergeMeasurementsForStart(config, newMeasMap, newMeasContextMap)
|
||||
config.mutex.Unlock()
|
||||
} else {
|
||||
opType = constants.OpAppend
|
||||
s.globalMutex.Lock()
|
||||
if config, exist = s.subMap[clientID]; !exist {
|
||||
config = &RealTimeSubConfig{
|
||||
noticeChan: make(chan *transportTargets, constants.NoticeChanCap),
|
||||
measurements: newMeasMap,
|
||||
}
|
||||
s.subMap[clientID] = config
|
||||
} else {
|
||||
s.globalMutex.Unlock()
|
||||
config.mutex.Lock()
|
||||
mergeMeasurementsForStart(config, newMeasMap, newMeasContextMap)
|
||||
config.mutex.Unlock()
|
||||
}
|
||||
s.globalMutex.Unlock()
|
||||
}
|
||||
|
||||
if len(successfulTargets) > 0 {
|
||||
transportTargets := &transportTargets{
|
||||
OperationType: opType,
|
||||
Targets: successfulTargets,
|
||||
}
|
||||
config.noticeChan <- transportTargets
|
||||
}
|
||||
return targetProcessResults, nil
|
||||
}
|
||||
|
||||
// RemoveTargets define function to remove targets in SharedSubState
|
||||
func (s *SharedSubState) RemoveTargets(ctx context.Context, clientID string, measurements []network.RealTimeMeasurementItem) ([]network.TargetResult, error) {
|
||||
requestTargetsCount := processRealTimeRequestCount(measurements)
|
||||
targetProcessResults := make([]network.TargetResult, 0, requestTargetsCount)
|
||||
|
||||
s.globalMutex.RLock()
|
||||
config, exist := s.subMap[clientID]
|
||||
if !exist {
|
||||
s.globalMutex.RUnlock()
|
||||
err := fmt.Errorf("clientID %s not found", clientID)
|
||||
logger.Error(ctx, "clientID not found in remove targets operation", "error", err)
|
||||
return processRealTimeRequestTargets(measurements, requestTargetsCount, err), err
|
||||
}
|
||||
s.globalMutex.RUnlock()
|
||||
|
||||
var shouldRemoveClient bool
|
||||
// measurements is the list of items to be removed passed in the request
|
||||
transportTargets := &transportTargets{
|
||||
OperationType: constants.OpRemove,
|
||||
Targets: make([]string, 0, requestTargetsCount),
|
||||
}
|
||||
config.mutex.Lock()
|
||||
for _, measurement := range measurements {
|
||||
interval := measurement.Interval
|
||||
// meas is the locally running listener configuration
|
||||
measTargets, measExist := config.measurements[interval]
|
||||
if !measExist {
|
||||
logger.Error(ctx, fmt.Sprintf("measurement with interval %s not found under clientID %s", interval, clientID), "clientID", clientID, "interval", interval)
|
||||
for _, target := range measTargets {
|
||||
targetResult := network.TargetResult{
|
||||
ID: target,
|
||||
Code: constants.CancelSubFailedCode,
|
||||
Msg: constants.CancelSubFailedMsg,
|
||||
}
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
targetsToRemoveMap := make(map[string]struct{})
|
||||
for _, target := range measurement.Targets {
|
||||
targetsToRemoveMap[target] = struct{}{}
|
||||
}
|
||||
|
||||
var newTargets []string
|
||||
for _, existingTarget := range measTargets {
|
||||
if _, found := targetsToRemoveMap[existingTarget]; !found {
|
||||
newTargets = append(newTargets, existingTarget)
|
||||
} else {
|
||||
transportTargets.Targets = append(transportTargets.Targets, existingTarget)
|
||||
targetResult := network.TargetResult{
|
||||
ID: existingTarget,
|
||||
Code: constants.CancelSubSuccessCode,
|
||||
Msg: constants.CancelSubSuccessMsg,
|
||||
}
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
delete(targetsToRemoveMap, existingTarget)
|
||||
delete(config.targetContext, existingTarget)
|
||||
}
|
||||
}
|
||||
measTargets = newTargets
|
||||
|
||||
if len(measTargets) == 0 {
|
||||
delete(config.measurements, interval)
|
||||
}
|
||||
|
||||
if len(config.measurements) == 0 {
|
||||
shouldRemoveClient = true
|
||||
}
|
||||
|
||||
if len(targetsToRemoveMap) > 0 {
|
||||
err := fmt.Errorf("target remove were not found under clientID %s and interval %s", clientID, interval)
|
||||
for target := range targetsToRemoveMap {
|
||||
targetResult := network.TargetResult{
|
||||
ID: target,
|
||||
Code: constants.CancelSubFailedCode,
|
||||
Msg: fmt.Sprintf("%s: %s", constants.SubFailedMsg, err.Error()),
|
||||
}
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
}
|
||||
}
|
||||
}
|
||||
config.mutex.Unlock()
|
||||
// pass the removed subscription configuration to the notice channel
|
||||
config.noticeChan <- transportTargets
|
||||
|
||||
if shouldRemoveClient {
|
||||
s.globalMutex.Lock()
|
||||
if currentConfig, exist := s.subMap[clientID]; exist && len(currentConfig.measurements) == 0 {
|
||||
delete(s.subMap, clientID)
|
||||
}
|
||||
s.globalMutex.Unlock()
|
||||
}
|
||||
return targetProcessResults, nil
|
||||
}
|
||||
|
||||
// UpdateTargets define function to update targets in SharedSubState
|
||||
func (s *SharedSubState) UpdateTargets(ctx context.Context, tx *gorm.DB, clientID string, measurements []network.RealTimeMeasurementItem) ([]network.TargetResult, error) {
|
||||
requestTargetsCount := processRealTimeRequestCount(measurements)
|
||||
targetProcessResults := make([]network.TargetResult, 0, requestTargetsCount)
|
||||
|
||||
s.globalMutex.RLock()
|
||||
config, exist := s.subMap[clientID]
|
||||
s.globalMutex.RUnlock()
|
||||
|
||||
if !exist {
|
||||
s.globalMutex.RUnlock()
|
||||
err := fmt.Errorf("clientID %s not found", clientID)
|
||||
logger.Error(ctx, "clientID not found in remove targets operation", "error", err)
|
||||
return processRealTimeRequestTargets(measurements, requestTargetsCount, err), err
|
||||
}
|
||||
|
||||
targetProcessResults, successfulTargets, newMeasMap, newMeasContextMap := processAndValidateTargetsForUpdate(ctx, tx, config, measurements, requestTargetsCount)
|
||||
|
||||
config.mutex.Lock()
|
||||
allDuplicates, err := mergeMeasurementsForUpdate(config, newMeasMap, newMeasContextMap)
|
||||
if err != nil {
|
||||
logger.Warn(ctx, "can not find exist measurements in target interval", "clientID", clientID, "duplicates", allDuplicates, "error", err)
|
||||
}
|
||||
|
||||
if len(allDuplicates) > 0 {
|
||||
logger.Warn(ctx, "some targets are duplicate and have been ignored in append operation", "clientID", clientID, "duplicates", allDuplicates)
|
||||
// process repeat target in targetProcessResults and successfulTargets
|
||||
targetProcessResults, successfulTargets = filterAndDeduplicateRepeatTargets(targetProcessResults, successfulTargets, allDuplicates)
|
||||
}
|
||||
config.mutex.Unlock()
|
||||
|
||||
if len(successfulTargets) > 0 {
|
||||
transportTargets := &transportTargets{
|
||||
OperationType: constants.OpUpdate,
|
||||
Targets: successfulTargets,
|
||||
}
|
||||
config.noticeChan <- transportTargets
|
||||
}
|
||||
|
||||
return targetProcessResults, nil
|
||||
}
|
||||
|
||||
// Get define function to get subscriptions config from SharedSubState
|
||||
func (s *SharedSubState) Get(clientID string) (*RealTimeSubConfig, bool) {
|
||||
s.globalMutex.RLock()
|
||||
defer s.globalMutex.RUnlock()
|
||||
|
||||
config, exists := s.subMap[clientID]
|
||||
if !exists {
|
||||
return nil, false
|
||||
}
|
||||
return config, true
|
||||
}
|
||||
|
||||
func processRealTimeRequestCount(measurements []network.RealTimeMeasurementItem) int {
|
||||
totalTargetsCount := 0
|
||||
for _, measItem := range measurements {
|
||||
totalTargetsCount += len(measItem.Targets)
|
||||
}
|
||||
return totalTargetsCount
|
||||
}
|
||||
|
||||
func processRealTimeRequestTargets(measurements []network.RealTimeMeasurementItem, targetCount int, err error) []network.TargetResult {
|
||||
targetProcessResults := make([]network.TargetResult, 0, targetCount)
|
||||
for _, measurementItem := range measurements {
|
||||
for _, target := range measurementItem.Targets {
|
||||
var targetResult network.TargetResult
|
||||
targetResult.ID = target
|
||||
targetResult.Code = constants.SubFailedCode
|
||||
targetResult.Msg = fmt.Sprintf("%s: %s", constants.SubFailedMsg, err.Error())
|
||||
targetProcessResults = append(targetProcessResults, targetResult)
|
||||
}
|
||||
}
|
||||
return targetProcessResults
|
||||
}
|
||||
|
||||
// transportTargets define struct to transport update or remove target to real
|
||||
// time pull api
|
||||
type transportTargets struct {
|
||||
OperationType constants.TargetOperationType
|
||||
Targets []string
|
||||
}
|
||||
|
|
@ -56,6 +56,6 @@ func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql
|
|||
if duration > l.SlowThreshold.Milliseconds() {
|
||||
Warn(ctx, "SQL SLOW", "sql", sql, "rows", rows, "dur(ms)", duration)
|
||||
} else {
|
||||
Debug(ctx, "SQL DEBUG", "sql", sql, "rows", rows, "dur(ms)", duration)
|
||||
Info(ctx, "SQL INFO", "sql", sql, "rows", rows, "dur(ms)", duration)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"modelRT/config"
|
||||
"modelRT/constants"
|
||||
|
|
@ -32,12 +34,14 @@ func getEncoder() zapcore.Encoder {
|
|||
|
||||
// getLogWriter responsible for setting the location of log storage
|
||||
func getLogWriter(mode, filename string, maxsize, maxBackup, maxAge int, compress bool) zapcore.WriteSyncer {
|
||||
dateStr := time.Now().Format("2006-01-02 15:04:05")
|
||||
finalFilename := fmt.Sprintf(filename, dateStr)
|
||||
lumberJackLogger := &lumberjack.Logger{
|
||||
Filename: filename, // log file position
|
||||
MaxSize: maxsize, // log file maxsize
|
||||
MaxAge: maxAge, // maximum number of day files retained
|
||||
MaxBackups: maxBackup, // maximum number of old files retained
|
||||
Compress: compress, // whether to compress
|
||||
Filename: finalFilename, // log file position
|
||||
MaxSize: maxsize, // log file maxsize
|
||||
MaxAge: maxAge, // maximum number of day files retained
|
||||
MaxBackups: maxBackup, // maximum number of old files retained
|
||||
Compress: compress, // whether to compress
|
||||
}
|
||||
|
||||
syncConsole := zapcore.AddSync(os.Stderr)
|
||||
|
|
@ -73,7 +77,6 @@ func InitLoggerInstance(lCfg config.LoggerConfig) {
|
|||
once.Do(func() {
|
||||
_globalLogger = initLogger(lCfg)
|
||||
})
|
||||
defer _globalLogger.Sync()
|
||||
}
|
||||
|
||||
// GetLoggerInstance define func of returns the global logger instance It's safe for concurrent use.
|
||||
|
|
|
|||
122
main.go
122
main.go
|
|
@ -3,28 +3,29 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"modelRT/alert"
|
||||
"modelRT/config"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
locker "modelRT/distributedlock"
|
||||
_ "modelRT/docs"
|
||||
"modelRT/handler"
|
||||
"modelRT/logger"
|
||||
"modelRT/middleware"
|
||||
"modelRT/model"
|
||||
"modelRT/pool"
|
||||
"modelRT/router"
|
||||
"modelRT/util"
|
||||
|
||||
locker "modelRT/distributedlock"
|
||||
_ "modelRT/docs"
|
||||
|
||||
realtimedata "modelRT/real-time-data"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
|
@ -34,12 +35,6 @@ import (
|
|||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var limiter *middleware.Limiter
|
||||
|
||||
func init() {
|
||||
limiter = middleware.NewLimiter(10, 1*time.Minute) // 设置限流器,允许每分钟最多请求10次
|
||||
}
|
||||
|
||||
var (
|
||||
modelRTConfigDir = flag.String("modelRT_config_dir", "./configs", "config file dir of model runtime service")
|
||||
modelRTConfigName = flag.String("modelRT_config_name", "config", "config file name of model runtime service")
|
||||
|
|
@ -49,7 +44,7 @@ var (
|
|||
var (
|
||||
modelRTConfig config.ModelRTConfig
|
||||
postgresDBClient *gorm.DB
|
||||
alertManager *alert.EventManager
|
||||
// alertManager *alert.EventManager
|
||||
)
|
||||
|
||||
// TODO 使用 wire 依赖注入管理 DVIE 面板注册的 panel
|
||||
|
|
@ -73,27 +68,30 @@ func main() {
|
|||
flag.Parse()
|
||||
ctx := context.TODO()
|
||||
|
||||
// init logger
|
||||
logger.InitLoggerInstance(modelRTConfig.LoggerConfig)
|
||||
|
||||
configPath := filepath.Join(*modelRTConfigDir, *modelRTConfigName+"."+*modelRTConfigType)
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
logger.Info(ctx, "configuration file not found,checking for example file")
|
||||
log.Println("configuration file not found,checking for example file")
|
||||
|
||||
exampleConfigPath := filepath.Join(*modelRTConfigDir, *modelRTConfigName+".example."+*modelRTConfigType)
|
||||
configDir := filepath.Dir(configPath)
|
||||
if err := os.MkdirAll(configDir, 0o755); err != nil {
|
||||
panic(fmt.Errorf("failed to create config directory %s:%w", configDir, err))
|
||||
}
|
||||
if _, err := os.Stat(exampleConfigPath); err == nil {
|
||||
if err := util.CopyFile(exampleConfigPath, configPath); err != nil {
|
||||
logger.Error(ctx, "failed to copy example config file", "error", err)
|
||||
panic(err)
|
||||
panic(fmt.Errorf("failed to copy example config file:%w", err))
|
||||
}
|
||||
logger.Info(ctx, "successfully copied example config to actual config file")
|
||||
} else {
|
||||
logger.Error(ctx, "No config file and no config example file found.")
|
||||
panic(errors.New("no config file and no config example file found"))
|
||||
}
|
||||
}
|
||||
|
||||
modelRTConfig = config.ReadAndInitConfig(*modelRTConfigDir, *modelRTConfigName, *modelRTConfigType)
|
||||
|
||||
// init logger
|
||||
logger.InitLoggerInstance(modelRTConfig.LoggerConfig)
|
||||
defer logger.GetLoggerInstance().Sync()
|
||||
|
||||
hostName, err := os.Hostname()
|
||||
if err != nil {
|
||||
logger.Error(ctx, "get host name failed", "error", err)
|
||||
|
|
@ -146,12 +144,6 @@ func main() {
|
|||
}
|
||||
defer anchorRealTimePool.Release()
|
||||
|
||||
// init cancel context
|
||||
cancelCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
// init real time data receive channel
|
||||
go realtimedata.ReceiveChan(cancelCtx)
|
||||
|
||||
postgresDBClient.Transaction(func(tx *gorm.DB) error {
|
||||
// load circuit diagram from postgres
|
||||
// componentTypeMap, err := database.QueryCircuitDiagramComponentFromDB(cancelCtx, tx, parsePool)
|
||||
|
|
@ -160,7 +152,49 @@ func main() {
|
|||
// panic(err)
|
||||
// }
|
||||
|
||||
// TODO 暂时屏蔽完成 swagger 启动测试
|
||||
cacheMap, err := model.GetNSpathToIsLocalMap(ctx, postgresDBClient)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "get nspath to is_local map failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
model.NSPathToIsLocalMap = cacheMap
|
||||
|
||||
err = model.CleanupRecommendRedisCache(ctx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "clean up component measurement and attribute group failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
measurementSet, err := database.GetFullMeasurementSet(tx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "generate component measurement group failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
fullParentPath, isLocalParentPath, err := model.TraverseMeasurementGroupTables(ctx, *measurementSet)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "store component measurement group into redis failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
compAttrSet, err := database.GenAllAttributeMap(tx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "generate component attribute group failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = model.TraverseAttributeGroupTables(ctx, tx, fullParentPath, isLocalParentPath, compAttrSet)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "store component attribute group into redis failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
allMeasurement, err := database.GetAllMeasurements(ctx, tx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "load topologic info from postgres failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
go realtimedata.StartRealTimeDataComputing(ctx, allMeasurement)
|
||||
|
||||
tree, err := database.QueryTopologicFromDB(ctx, tx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "load topologic info from postgres failed", "error", err)
|
||||
|
|
@ -170,37 +204,11 @@ func main() {
|
|||
return nil
|
||||
})
|
||||
|
||||
// TODO 完成订阅数据分析
|
||||
// TODO 暂时屏蔽完成 swagger 启动测试
|
||||
// go realtimedata.RealTimeDataComputer(ctx, nil, []string{}, "")
|
||||
|
||||
// use release mode in productio
|
||||
// gin.SetMode(gin.ReleaseMode)
|
||||
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", limiter.Middleware)
|
||||
{
|
||||
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))
|
||||
|
||||
|
|
@ -215,7 +223,7 @@ func main() {
|
|||
// }
|
||||
|
||||
server := http.Server{
|
||||
Addr: ":8080",
|
||||
Addr: modelRTConfig.ServiceConfig.ServiceAddr,
|
||||
Handler: engine,
|
||||
}
|
||||
|
||||
|
|
@ -225,7 +233,7 @@ func main() {
|
|||
go func() {
|
||||
<-done
|
||||
if err := server.Shutdown(context.Background()); err != nil {
|
||||
logger.Error(ctx, "ShutdownServerError", "err", err)
|
||||
logger.Error(ctx, "shutdown serverError", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
@ -234,10 +242,10 @@ func main() {
|
|||
if err != nil {
|
||||
if err == http.ErrServerClosed {
|
||||
// the service receives the shutdown signal normally and then closes
|
||||
logger.Info(ctx, "Server closed under request")
|
||||
logger.Info(ctx, "server closed under request")
|
||||
} else {
|
||||
// abnormal shutdown of service
|
||||
logger.Error(ctx, "Server closed unexpected", "err", err)
|
||||
logger.Error(ctx, "server closed unexpected", "err", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,47 @@
|
|||
package middleware
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"modelRT/logger"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GinPanicRecovery define func of customizing gin recover output
|
||||
func GinPanicRecovery() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
// Check for a broken connection, as it is not really a
|
||||
// condition that warrants a panic stack trace.
|
||||
var brokenPipe bool
|
||||
if ne, ok := err.(*net.OpError); ok {
|
||||
if se, ok := ne.Err.(*os.SyscallError); ok {
|
||||
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
|
||||
brokenPipe = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
httpRequest, _ := httputil.DumpRequest(c.Request, false)
|
||||
if brokenPipe {
|
||||
logger.Error(c, "http request broken pipe", "path", c.Request.URL.Path, "error", err, "request", string(httpRequest))
|
||||
// If the connection is dead, we can't write a status to it.
|
||||
c.Error(err.(error))
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
logger.Error(c, "http_request_panic", "path", c.Request.URL.Path, "error", err, "request", string(httpRequest), "stack", string(debug.Stack()))
|
||||
c.AbortWithError(http.StatusInternalServerError, err.(error))
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,173 @@
|
|||
// Package model define model struct of model runtime service
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/RediSearch/redisearch-go/v2/redisearch"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type columnParam struct {
|
||||
AttributeType string
|
||||
AttributeGroup map[string]any
|
||||
}
|
||||
|
||||
// TraverseAttributeGroupTables define func to traverse component attribute group tables
|
||||
func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compTagToFullPath map[string]string, isLocalCompTagToFullPath map[string]string, compAttrSet map[string]orm.AttributeSet) error {
|
||||
var tableNames []string
|
||||
|
||||
excludedTables := []string{"component", ""}
|
||||
result := db.Model(&orm.ProjectManager{}).
|
||||
Where("name NOT IN ?", excludedTables).
|
||||
Pluck("name", &tableNames)
|
||||
if result.Error != nil && result.Error != gorm.ErrRecordNotFound {
|
||||
logger.Error(ctx, "query name column data from postgres table failed", "err", result.Error)
|
||||
return result.Error
|
||||
}
|
||||
|
||||
if len(tableNames) == 0 {
|
||||
logger.Info(ctx, "query from postgres successed, but no records found")
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, tableName := range tableNames {
|
||||
var records []map[string]any
|
||||
err := db.Table(tableName).Find(&records).Error
|
||||
if err != nil && err != gorm.ErrRecordNotFound {
|
||||
logger.Error(ctx, fmt.Sprintf("query table '%s' data failed", tableName), "table_name", tableName, "err", err)
|
||||
return err
|
||||
}
|
||||
|
||||
migrator := db.Migrator()
|
||||
|
||||
if exists := migrator.HasTable(tableName); !exists {
|
||||
err := errors.New("can not find special table into database")
|
||||
logger.Error(ctx, "table does not exist in the database", "table _name", tableName, "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
columnTypes, err := migrator.ColumnTypes(tableName)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "retrieving column structure for table failed", "table _name", tableName, "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, record := range records {
|
||||
attributeGroup := make(map[string]any)
|
||||
var componentUUIDStr string
|
||||
var attrSet orm.AttributeSet
|
||||
var attributeType string
|
||||
for _, col := range columnTypes {
|
||||
colName := col.Name()
|
||||
|
||||
value, exists := record[colName]
|
||||
if !exists {
|
||||
err := errors.New("can not find value from record by column name")
|
||||
logger.Error(ctx, "query value by column name failed", "column_name", colName, "error", err)
|
||||
return err
|
||||
}
|
||||
switch colName {
|
||||
case "id":
|
||||
case "global_uuid":
|
||||
componentUUIDStr = value.(string)
|
||||
attrSet, exists = compAttrSet[componentUUIDStr]
|
||||
if !exists {
|
||||
err := errors.New("can not find component tag from record by global uuid")
|
||||
logger.Error(ctx, "check component info by global uuid failed", "global_uuid", componentUUIDStr, "error", err)
|
||||
return err
|
||||
}
|
||||
case "attribute_group":
|
||||
attributeType = value.(string)
|
||||
default:
|
||||
attributeGroup[colName] = value
|
||||
}
|
||||
}
|
||||
|
||||
fullPath := compTagToFullPath[attrSet.CompTag]
|
||||
isLocalfullPath := isLocalCompTagToFullPath[attrSet.CompTag]
|
||||
if fullPath == "" {
|
||||
err := errors.New("can not find full parent path from mapping by component tag")
|
||||
logger.Error(ctx, "find full parent path by from mapping by component tag failed", "component_tag", attrSet.CompTag, "error", err)
|
||||
return err
|
||||
}
|
||||
columnParam := columnParam{
|
||||
AttributeType: attributeType,
|
||||
AttributeGroup: attributeGroup,
|
||||
}
|
||||
go storeAttributeGroup(ctx, attrSet, fullPath, isLocalfullPath, columnParam)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, fullPath string, isLocalFullPath 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))
|
||||
attrbutesGroups := make([]any, 0, len(colParams.AttributeGroup)*2)
|
||||
attributeGroupKey := fmt.Sprintf("%s_%s", attributeSet.CompTag, colParams.AttributeType)
|
||||
|
||||
sug := make([]redisearch.Suggestion, 0, len(colParams.AttributeGroup)*4)
|
||||
for attrName, attrValue := range colParams.AttributeGroup {
|
||||
attrbutesGroups = append(attrbutesGroups, attrName, attrValue)
|
||||
attrNameMembers = append(attrNameMembers, attrName)
|
||||
|
||||
// add redis fuzzy search suggestion for token1-token7 type
|
||||
configTerm := fmt.Sprintf("%s.%s", fullPath, colParams.AttributeType)
|
||||
sug = append(sug, redisearch.Suggestion{
|
||||
Term: configTerm,
|
||||
Score: constants.DefaultScore,
|
||||
})
|
||||
|
||||
measTerm := fmt.Sprintf("%s.%s.%s", fullPath, colParams.AttributeType, attrName)
|
||||
sug = append(sug, redisearch.Suggestion{
|
||||
Term: measTerm,
|
||||
Score: constants.DefaultScore,
|
||||
})
|
||||
|
||||
// add redis fuzzy search suggestion for token4-token7 type
|
||||
configTerm = fmt.Sprintf("%s.%s", isLocalFullPath, colParams.AttributeType)
|
||||
sug = append(sug, redisearch.Suggestion{
|
||||
Term: configTerm,
|
||||
Score: constants.DefaultScore,
|
||||
})
|
||||
|
||||
measTerm = fmt.Sprintf("%s.%s.%s", isLocalFullPath, colParams.AttributeType, attrName)
|
||||
sug = append(sug, redisearch.Suggestion{
|
||||
Term: measTerm,
|
||||
Score: constants.DefaultScore,
|
||||
})
|
||||
}
|
||||
|
||||
if len(attrbutesGroups) > 0 {
|
||||
pipe.HSet(ctx, attributeGroupKey, attrbutesGroups...)
|
||||
}
|
||||
|
||||
if len(attrNameMembers) > 0 {
|
||||
pipe.SAdd(ctx, constants.RedisAllMeasTagSetKey, attrNameMembers)
|
||||
pipe.SAdd(ctx, specCompMeasKey, attrNameMembers)
|
||||
}
|
||||
|
||||
if len(sug) > 0 {
|
||||
ac.AddTerms(sug...)
|
||||
}
|
||||
|
||||
_, err := pipe.Exec(ctx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "init component attribute group recommend content failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ type AttrModelInterface interface {
|
|||
type LongAttrInfo struct {
|
||||
AttrGroupName string
|
||||
AttrKey string
|
||||
AttrValue interface{}
|
||||
AttrValue any
|
||||
GridInfo *orm.Grid
|
||||
ZoneInfo *orm.Zone
|
||||
StationInfo *orm.Station
|
||||
|
|
@ -52,7 +52,7 @@ func (l *LongAttrInfo) IsLocal() bool {
|
|||
}
|
||||
|
||||
// GetAttrValue define return the attribute value
|
||||
func (l *LongAttrInfo) GetAttrValue() interface{} {
|
||||
func (l *LongAttrInfo) GetAttrValue() any {
|
||||
return l.AttrValue
|
||||
}
|
||||
|
||||
|
|
@ -60,7 +60,7 @@ func (l *LongAttrInfo) GetAttrValue() interface{} {
|
|||
type ShortAttrInfo struct {
|
||||
AttrGroupName string
|
||||
AttrKey string
|
||||
AttrValue interface{}
|
||||
AttrValue any
|
||||
ComponentInfo *orm.Component
|
||||
}
|
||||
|
||||
|
|
@ -90,6 +90,6 @@ func (s *ShortAttrInfo) IsLocal() bool {
|
|||
}
|
||||
|
||||
// GetAttrValue define return the attribute value
|
||||
func (l *ShortAttrInfo) GetAttrValue() interface{} {
|
||||
func (l *ShortAttrInfo) GetAttrValue() any {
|
||||
return l.AttrValue
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,119 @@
|
|||
// Package model define model struct of model runtime service
|
||||
package model
|
||||
|
||||
import "modelRT/orm"
|
||||
|
||||
// IndentityTokenModelInterface define basic identity token model type interface
|
||||
type IndentityTokenModelInterface interface {
|
||||
GetComponentInfo() *orm.Component
|
||||
GetMeasurementInfo() *orm.Measurement
|
||||
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)
|
||||
GetMeasurementTag() string // token7(measurement value or attribute field)
|
||||
IsLocal() bool
|
||||
}
|
||||
|
||||
// LongIdentityTokenModel define struct to long identity token info
|
||||
type LongIdentityTokenModel struct {
|
||||
ComponentInfo *orm.Component
|
||||
MeasurementInfo *orm.Measurement
|
||||
GridTag string
|
||||
ZoneTag string
|
||||
StationTag string
|
||||
NamespacePath string
|
||||
ComponentTag string
|
||||
AttributeGroup string
|
||||
MeasurementTag string
|
||||
}
|
||||
|
||||
// GetComponentInfo define return the component information in the long identity token
|
||||
func (l *LongIdentityTokenModel) GetComponentInfo() *orm.Component {
|
||||
return l.ComponentInfo
|
||||
}
|
||||
|
||||
// GetMeasurementInfo define return the measurement information in the long identity token
|
||||
func (l *LongIdentityTokenModel) GetMeasurementInfo() *orm.Measurement {
|
||||
return l.MeasurementInfo
|
||||
}
|
||||
|
||||
// GetGridName define function to return the grid name information in the long identity token
|
||||
func (l *LongIdentityTokenModel) GetGridName() string { return l.GridTag }
|
||||
|
||||
// GetZoneName define function to return the zone name information in the long identity token
|
||||
func (l *LongIdentityTokenModel) GetZoneName() string { return l.ZoneTag }
|
||||
|
||||
// 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 }
|
||||
|
||||
// GetComponentTag define function to return the component tag information in the long identity token
|
||||
func (l *LongIdentityTokenModel) GetComponentTag() string { return l.ComponentTag }
|
||||
|
||||
// GetAttributeGroup define function to return the attribute group information in the long identity token
|
||||
func (l *LongIdentityTokenModel) GetAttributeGroup() string { return l.AttributeGroup }
|
||||
|
||||
// GetMeasurementTag define function to return the measurement tag information in the long identity token
|
||||
func (l *LongIdentityTokenModel) GetMeasurementTag() string {
|
||||
return l.MeasurementTag
|
||||
}
|
||||
|
||||
// IsLocal define return the is_local information in the long identity token
|
||||
func (l *LongIdentityTokenModel) IsLocal() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// ShortIdentityTokenModel define struct to short identity token info
|
||||
type ShortIdentityTokenModel struct {
|
||||
ComponentInfo *orm.Component
|
||||
MeasurementInfo *orm.Measurement
|
||||
|
||||
GridTag string // token1
|
||||
ZoneTag string // token2
|
||||
StationTag string // token3
|
||||
NamespacePath string // token4
|
||||
MeasurementTag string // token7
|
||||
}
|
||||
|
||||
// GetComponentInfo define return the component information in the short identity token
|
||||
func (s *ShortIdentityTokenModel) GetComponentInfo() *orm.Component {
|
||||
return s.ComponentInfo
|
||||
}
|
||||
|
||||
// GetMeasurementInfo define return the measurement information in the long identity token
|
||||
func (s *ShortIdentityTokenModel) GetMeasurementInfo() *orm.Measurement {
|
||||
return s.MeasurementInfo
|
||||
}
|
||||
|
||||
// GetGridName define function to return the grid name information in the short identity token
|
||||
func (s *ShortIdentityTokenModel) GetGridName() string { return "" }
|
||||
|
||||
// GetZoneName define function to return the zone name information in the short identity token
|
||||
func (s *ShortIdentityTokenModel) GetZoneName() 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 }
|
||||
|
||||
// GetComponentTag define function to return the component tag information in the short identity token
|
||||
func (s *ShortIdentityTokenModel) GetComponentTag() string { return "" }
|
||||
|
||||
// GetAttributeGroup define function to return the attribute group information in the short identity token
|
||||
func (s *ShortIdentityTokenModel) GetAttributeGroup() string { return "" }
|
||||
|
||||
// GetMeasurementTag define function to return the measurement tag information in the short identity token
|
||||
func (s *ShortIdentityTokenModel) GetMeasurementTag() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// IsLocal define return the is_local information in the short identity token
|
||||
func (s *ShortIdentityTokenModel) IsLocal() bool {
|
||||
return true
|
||||
}
|
||||
|
|
@ -0,0 +1,217 @@
|
|||
// Package model define model struct of model runtime service
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/RediSearch/redisearch-go/v2/redisearch"
|
||||
)
|
||||
|
||||
// TraverseMeasurementGroupTables define func to traverse component measurement group tables
|
||||
func TraverseMeasurementGroupTables(ctx context.Context, measSet orm.MeasurementSet) (map[string]string, map[string]string, error) {
|
||||
rdb := diagram.GetRedisClientInstance()
|
||||
pipe := rdb.Pipeline()
|
||||
|
||||
compTagToFullPath := make(map[string]string)
|
||||
isLocalCompTagToFullPath := make(map[string]string)
|
||||
|
||||
zoneToGridPath := make(map[string]string)
|
||||
for gridTag, zoneTags := range measSet.GridToZoneTags {
|
||||
for _, zoneTag := range zoneTags {
|
||||
zoneToGridPath[zoneTag] = gridTag
|
||||
}
|
||||
}
|
||||
|
||||
stationToZonePath := make(map[string]string)
|
||||
for zoneTag, stationTags := range measSet.ZoneToStationTags {
|
||||
gridTag := zoneToGridPath[zoneTag]
|
||||
for _, stationTag := range stationTags {
|
||||
stationToZonePath[stationTag] = fmt.Sprintf("%s.%s", gridTag, zoneTag)
|
||||
}
|
||||
}
|
||||
|
||||
compNSPathToStationPath := make(map[string]string)
|
||||
for stationTag, nsPaths := range measSet.StationToCompNSPaths {
|
||||
zonePath := stationToZonePath[stationTag]
|
||||
for _, nsPath := range nsPaths {
|
||||
compNSPathToStationPath[nsPath] = fmt.Sprintf("%s.%s", zonePath, stationTag)
|
||||
}
|
||||
}
|
||||
|
||||
compTagToNSPathPath := make(map[string]string)
|
||||
isLocalCompTagToNSPathPath := make(map[string]string)
|
||||
for nsPath, compTags := range measSet.CompNSPathToCompTags {
|
||||
stationPath := compNSPathToStationPath[nsPath]
|
||||
for _, compTag := range compTags {
|
||||
compTagToNSPathPath[compTag] = fmt.Sprintf("%s.%s", stationPath, nsPath)
|
||||
isLocalCompTagToNSPathPath[compTag] = nsPath
|
||||
}
|
||||
}
|
||||
|
||||
// define a safe SAdd function to avoid errors caused by empty slices
|
||||
safeSAdd := func(key string, members []string) {
|
||||
if len(members) > 0 {
|
||||
pipe.SAdd(ctx, key, members)
|
||||
}
|
||||
}
|
||||
|
||||
safeSAdd(constants.RedisAllGridSetKey, measSet.AllGridTags)
|
||||
gridSug := make([]redisearch.Suggestion, 0, len(measSet.AllGridTags))
|
||||
for _, gridTag := range measSet.AllGridTags {
|
||||
gridSug = append(gridSug, redisearch.Suggestion{Term: gridTag, Score: constants.DefaultScore})
|
||||
}
|
||||
ac.AddTerms(gridSug...)
|
||||
|
||||
safeSAdd(constants.RedisAllZoneSetKey, measSet.AllZoneTags)
|
||||
safeSAdd(constants.RedisAllStationSetKey, measSet.AllStationTags)
|
||||
safeSAdd(constants.RedisAllCompNSPathSetKey, measSet.AllCompNSPaths)
|
||||
safeSAdd(constants.RedisAllCompTagSetKey, measSet.AllCompTags)
|
||||
safeSAdd(constants.RedisAllConfigSetKey, measSet.AllConfigTags)
|
||||
safeSAdd(constants.RedisAllMeasTagSetKey, measSet.AllMeasTags)
|
||||
|
||||
// building the grid -> zones hierarchy
|
||||
for gridTag, zoneTags := range measSet.GridToZoneTags {
|
||||
sug := make([]redisearch.Suggestion, 0, len(zoneTags))
|
||||
for _, zoneTag := range zoneTags {
|
||||
term := fmt.Sprintf("%s.%s", gridTag, zoneTag)
|
||||
// add redis fuzzy search suggestion for token1-token7 type
|
||||
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
||||
}
|
||||
safeSAdd(fmt.Sprintf(constants.RedisSpecGridZoneSetKey, gridTag), zoneTags)
|
||||
ac.AddTerms(sug...)
|
||||
}
|
||||
|
||||
// building the zone -> stations hierarchy
|
||||
for zoneTag, stationTags := range measSet.ZoneToStationTags {
|
||||
sug := make([]redisearch.Suggestion, 0, len(stationTags))
|
||||
gridTag, exists := zoneToGridPath[zoneTag]
|
||||
if !exists {
|
||||
err := fmt.Errorf("zone tag to grid tag mapping not found for zoneTag: %s", zoneTag)
|
||||
logger.Error(ctx, "zone tag to grid tag mapping not found", "zoneTag", zoneTag, "error", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for _, stationTag := range stationTags {
|
||||
// add redis fuzzy search suggestion for token1-token7 type
|
||||
term := fmt.Sprintf("%s.%s.%s", gridTag, zoneTag, stationTag)
|
||||
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
||||
}
|
||||
|
||||
safeSAdd(fmt.Sprintf(constants.RedisSpecZoneStationSetKey, zoneTag), stationTags)
|
||||
ac.AddTerms(sug...)
|
||||
}
|
||||
|
||||
// building the station -> component nspaths hierarchy
|
||||
for stationTag, compNSPaths := range measSet.StationToCompNSPaths {
|
||||
sug := make([]redisearch.Suggestion, 0, len(compNSPaths)*2)
|
||||
parentPath, exists := stationToZonePath[stationTag]
|
||||
if !exists {
|
||||
err := fmt.Errorf("station tag to zone tag mapping not found for stationTag: %s", stationTag)
|
||||
logger.Error(ctx, "zone tag to grid tag mapping not found", "stationTag", stationTag, "error", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for _, nsPath := range compNSPaths {
|
||||
// add redis fuzzy search suggestion for token1-token7 type
|
||||
term := fmt.Sprintf("%s.%s.%s", parentPath, stationTag, nsPath)
|
||||
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
||||
// add redis fuzzy search suggestion for token4-token7 type
|
||||
sug = append(sug, redisearch.Suggestion{Term: nsPath, Score: constants.DefaultScore})
|
||||
}
|
||||
safeSAdd(fmt.Sprintf(constants.RedisSpecStationCompNSPATHSetKey, stationTag), compNSPaths)
|
||||
ac.AddTerms(sug...)
|
||||
}
|
||||
|
||||
// building the component nspath -> component tags hierarchy
|
||||
for compNSPath, compTags := range measSet.CompNSPathToCompTags {
|
||||
sug := make([]redisearch.Suggestion, 0, len(compTags)*2)
|
||||
parentPath, exists := compNSPathToStationPath[compNSPath]
|
||||
if !exists {
|
||||
err := fmt.Errorf("component nspath tag to station tag mapping not found for compNSPath: %s", compNSPath)
|
||||
logger.Error(ctx, "component nspath tag to station tag mapping not found", "compNSPath", compNSPath, "error", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for _, compTag := range compTags {
|
||||
fullPath := fmt.Sprintf("%s.%s.%s", parentPath, compNSPath, compTag)
|
||||
compTagToFullPath[compTag] = fullPath
|
||||
fullPath = fmt.Sprintf("%s.%s", compNSPath, compTag)
|
||||
isLocalCompTagToFullPath[compTag] = fullPath
|
||||
|
||||
// add redis fuzzy search suggestion for token1-token7 type
|
||||
term := fullPath
|
||||
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
||||
// add redis fuzzy search suggestion for token4-token7 type
|
||||
term = fmt.Sprintf("%s.%s", compNSPath, compTag)
|
||||
sug = append(sug, redisearch.Suggestion{Term: term, Score: constants.DefaultScore})
|
||||
}
|
||||
safeSAdd(fmt.Sprintf(constants.RedisSpecCompNSPathCompTagSetKey, compNSPath), compTags)
|
||||
ac.AddTerms(sug...)
|
||||
}
|
||||
|
||||
// building the component tag -> measurement tags hierarchy
|
||||
for compTag, measTags := range measSet.CompTagToMeasTags {
|
||||
sug := make([]redisearch.Suggestion, 0, len(measTags)*4)
|
||||
parentPath, exists := compTagToNSPathPath[compTag]
|
||||
if !exists {
|
||||
err := fmt.Errorf("component tag to component nspath mapping not found for compTag: %s", compTag)
|
||||
logger.Error(ctx, "component tag to component nspath mapping not found", "compTag", compTag, "error", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
isLocalParentPath, exists := isLocalCompTagToNSPathPath[compTag]
|
||||
if !exists {
|
||||
err := fmt.Errorf("component tag to component nspath is local mapping not found for compTag: %s", compTag)
|
||||
logger.Error(ctx, "component tag to component nspath is local mapping not found", "compTag", compTag, "error", err)
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
for _, measTag := range measTags {
|
||||
// add redis fuzzy search suggestion for token1-token7 type
|
||||
configTerm := fmt.Sprintf("%s.%s.%s", parentPath, compTag, "bay")
|
||||
sug = append(sug, redisearch.Suggestion{Term: configTerm, Score: constants.DefaultScore})
|
||||
|
||||
measTerm := fmt.Sprintf("%s.%s.%s.%s", parentPath, compTag, "bay", measTag)
|
||||
sug = append(sug, redisearch.Suggestion{Term: measTerm, Score: constants.DefaultScore})
|
||||
// add redis fuzzy search suggestion for token4-token7 type
|
||||
configTerm = fmt.Sprintf("%s.%s.%s", isLocalParentPath, compTag, "bay")
|
||||
sug = append(sug, redisearch.Suggestion{Term: configTerm, Score: constants.DefaultScore})
|
||||
|
||||
measTerm = fmt.Sprintf("%s.%s.%s.%s", isLocalParentPath, compTag, "bay", measTag)
|
||||
sug = append(sug, redisearch.Suggestion{Term: measTerm, Score: constants.DefaultScore})
|
||||
}
|
||||
safeSAdd(fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, compTag), measTags)
|
||||
ac.AddTerms(sug...)
|
||||
}
|
||||
|
||||
var allErrs []error
|
||||
cmders, execErr := pipe.Exec(ctx)
|
||||
if execErr != nil {
|
||||
logger.Error(ctx, "pipeline execution failed", "error", execErr)
|
||||
allErrs = append(allErrs, execErr)
|
||||
}
|
||||
|
||||
// check for errors in each subcommand of the pipeline
|
||||
for _, cmder := range cmders {
|
||||
cmdErr := cmder.Err()
|
||||
if cmdErr != nil {
|
||||
logger.Error(ctx, "individual redis command failed",
|
||||
"command", cmder.Name(),
|
||||
"args", cmder.Args(),
|
||||
"error", cmdErr)
|
||||
allErrs = append(allErrs, cmdErr)
|
||||
}
|
||||
}
|
||||
if len(allErrs) > 0 {
|
||||
return nil, nil, errors.Join(allErrs...)
|
||||
}
|
||||
|
||||
return compTagToFullPath, isLocalCompTagToFullPath, nil
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ type MeasurementDataSource struct {
|
|||
}
|
||||
|
||||
// IOAddress define interface of IO address
|
||||
type IOAddress interface{}
|
||||
type IOAddress any
|
||||
|
||||
// CL3611Address define CL3611 protol struct
|
||||
type CL3611Address struct {
|
||||
|
|
@ -174,3 +174,98 @@ func (m MeasurementDataSource) GetIOAddress() (IOAddress, error) {
|
|||
return nil, constants.ErrUnknownDataType
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateMeasureIdentifier define func of generate measurement identifier
|
||||
func GenerateMeasureIdentifier(source map[string]any) (string, error) {
|
||||
regTypeVal, ok := source["type"]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("can not find type in datasource field")
|
||||
}
|
||||
|
||||
var regType int
|
||||
switch v := regTypeVal.(type) {
|
||||
case int:
|
||||
regType = v
|
||||
case float32:
|
||||
if v != float32(int(v)) {
|
||||
return "", fmt.Errorf("invalid type format in datasource field, expected integer value, got float: %f", v)
|
||||
}
|
||||
regType = int(v)
|
||||
case float64:
|
||||
if v != float64(int(v)) {
|
||||
return "", fmt.Errorf("invalid type format in datasource field, expected integer value, got float: %f", v)
|
||||
}
|
||||
regType = int(v)
|
||||
default:
|
||||
return "", fmt.Errorf("invalid type format in datasource field,%T", v)
|
||||
}
|
||||
|
||||
ioAddrVal, ok := source["io_address"]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("can not find io_address from datasource field")
|
||||
}
|
||||
|
||||
ioAddress, ok := ioAddrVal.(map[string]any)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("io_address field is not a valid map")
|
||||
}
|
||||
|
||||
switch regType {
|
||||
case constants.DataSourceTypeCL3611:
|
||||
station, ok := ioAddress["station"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("CL3611:invalid or missing station field")
|
||||
}
|
||||
device, ok := ioAddress["device"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("CL3611:invalid or missing device field")
|
||||
}
|
||||
// 提取 channel (string)
|
||||
channel, ok := ioAddress["channel"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("CL3611:invalid or missing channel field")
|
||||
}
|
||||
return concatCL361WithPlus(station, device, channel), nil
|
||||
case constants.DataSourceTypePower104:
|
||||
station, ok := ioAddress["station"].(string)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("Power104:invalid or missing station field")
|
||||
}
|
||||
packetVal, ok := ioAddress["packet"]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("Power104: missing packet field")
|
||||
}
|
||||
var packet int
|
||||
switch v := packetVal.(type) {
|
||||
case int:
|
||||
packet = v
|
||||
default:
|
||||
return "", fmt.Errorf("Power104:invalid packet format")
|
||||
}
|
||||
|
||||
offsetVal, ok := ioAddress["offset"]
|
||||
if !ok {
|
||||
return "", fmt.Errorf("Power104:missing offset field")
|
||||
}
|
||||
var offset int
|
||||
switch v := offsetVal.(type) {
|
||||
case int:
|
||||
offset = v
|
||||
default:
|
||||
return "", fmt.Errorf("Power104:invalid offset format")
|
||||
}
|
||||
return concatP104WithPlus(station, packet, offset), nil
|
||||
default:
|
||||
return "", fmt.Errorf("unsupport regulation type %d into datasource field", regType)
|
||||
}
|
||||
}
|
||||
|
||||
func concatP104WithPlus(station string, packet int, offset int) string {
|
||||
packetStr := strconv.Itoa(packet)
|
||||
offsetStr := strconv.Itoa(offset)
|
||||
return station + ":" + packetStr + ":" + offsetStr
|
||||
}
|
||||
|
||||
func concatCL361WithPlus(station, device, channel string) string {
|
||||
return station + ":" + device + ":" + "phasor" + ":" + channel
|
||||
}
|
||||
|
|
@ -0,0 +1,89 @@
|
|||
// Package model define model struct of model runtime service
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// CleanupRecommendRedisCache define func to cleanup component measurement and attribute roup in redis cache
|
||||
func CleanupRecommendRedisCache(ctx context.Context) error {
|
||||
rdb := diagram.GetRedisClientInstance()
|
||||
|
||||
attrTypes, err := rdb.SMembers(ctx, constants.RedisAllConfigSetKey).Result()
|
||||
if err != nil {
|
||||
logger.Error(ctx, "failed to get attribute types from redis", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, attrType := range attrTypes {
|
||||
pattern := fmt.Sprintf("*_%s", attrType)
|
||||
if err := deleteKeysByPattern(ctx, rdb, pattern); err != nil {
|
||||
logger.Warn(ctx, "cleanup attribute type pattern failed",
|
||||
"attr_type", attrType, "pattern", pattern, "error", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
fixedKeys := []string{
|
||||
constants.RedisAllGridSetKey,
|
||||
constants.RedisAllZoneSetKey,
|
||||
constants.RedisAllStationSetKey,
|
||||
constants.RedisAllCompNSPathSetKey,
|
||||
constants.RedisAllCompTagSetKey,
|
||||
constants.RedisAllMeasTagSetKey,
|
||||
constants.RedisAllConfigSetKey,
|
||||
constants.RedisSearchDictName,
|
||||
}
|
||||
|
||||
if err := rdb.Del(ctx, fixedKeys...).Err(); err != nil {
|
||||
logger.Error(ctx, "delete fixed redis keys failed", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
patterns := []string{
|
||||
"*_zone_tag_keys", // correspond RedisSpecGridZoneSetKey
|
||||
"*_station_tag_keys", // correspond RedisSpecZoneStationSetKey
|
||||
"*_component_nspath_keys", // correspond RedisSpecStationCompNSPATHSetKey
|
||||
"*_component_tag_keys", // correspond RedisSpecCompNSPathCompTagSetKey
|
||||
"*_measurement_tag_keys", // correspond RedisSpecCompTagMeasSetKey
|
||||
}
|
||||
|
||||
for _, pattern := range patterns {
|
||||
if err := deleteKeysByPattern(ctx, rdb, pattern); err != nil {
|
||||
logger.Warn(ctx, "cleanup pattern keys failed", "pattern", pattern, "error", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
logger.Info(ctx, "cleanup component measurement and attribute group in redis cache completed")
|
||||
return nil
|
||||
}
|
||||
|
||||
func deleteKeysByPattern(ctx context.Context, rdb *redis.Client, pattern string) error {
|
||||
var cursor uint64
|
||||
for {
|
||||
keys, nextCursor, err := rdb.Scan(ctx, cursor, pattern, 100).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(keys) > 0 {
|
||||
if err := rdb.Del(ctx, keys...).Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
cursor = nextCursor
|
||||
if cursor == 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
// 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
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ package model
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"strings"
|
||||
|
|
@ -10,11 +11,22 @@ import (
|
|||
"modelRT/constants"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/util"
|
||||
|
||||
"github.com/RediSearch/redisearch-go/v2/redisearch"
|
||||
redigo "github.com/gomodule/redigo/redis"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// SearchResult define struct to store redis query recommend search result
|
||||
type SearchResult struct {
|
||||
// input redis key, used to distinguish which goroutine the result belongs to
|
||||
RecommendType constants.RecommendHierarchyType
|
||||
QueryDatas []string
|
||||
IsFuzzy bool
|
||||
Err error
|
||||
}
|
||||
|
||||
var ac *redisearch.Autocompleter
|
||||
|
||||
// InitAutocompleterWithPool define func of initialize the Autocompleter with redigo pool
|
||||
|
|
@ -22,192 +34,615 @@ func InitAutocompleterWithPool(pool *redigo.Pool) {
|
|||
ac = redisearch.NewAutocompleterFromPool(pool, constants.RedisSearchDictName)
|
||||
}
|
||||
|
||||
func levelOneRedisSearch(ctx context.Context, rdb *redis.Client, hierarchy constants.RecommendHierarchyType, recommendLenType string, searchInput string, searchRedisKey string, fanInChan chan SearchResult) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger.Error(ctx, "searchFunc panicked", "panic", r)
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: errors.New("search goroutine panicked"),
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
exists, err := rdb.SIsMember(ctx, searchRedisKey, searchInput).Result()
|
||||
if err != nil {
|
||||
logger.Error(ctx, "redis membership check failed", "key", searchRedisKey, "member", searchInput, "op", "SIsMember", "error", err)
|
||||
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: err,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// the input key is the complete hierarchical value
|
||||
if exists {
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: []string{"."},
|
||||
IsFuzzy: false,
|
||||
Err: nil,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// process fuzzy search result
|
||||
recommends, err := runFuzzySearch(ctx, rdb, searchInput, "", hierarchy, recommendLenType)
|
||||
if err != nil {
|
||||
logger.Error(ctx, fmt.Sprintf("fuzzy search failed for %s hierarchical", util.GetLevelStrByRdsKey(searchRedisKey)), "search_input", searchInput, "error", err)
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: err,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if len(recommends) > 0 {
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: recommends,
|
||||
IsFuzzy: true,
|
||||
Err: nil,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: []string{},
|
||||
IsFuzzy: true,
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
// RedisSearchRecommend define func of redis search by input string and return recommend results
|
||||
func RedisSearchRecommend(ctx context.Context, input string) ([]string, bool, error) {
|
||||
func RedisSearchRecommend(ctx context.Context, input string) map[string]SearchResult {
|
||||
rdb := diagram.GetRedisClientInstance()
|
||||
|
||||
if input == "" {
|
||||
// 返回所有 grid 名
|
||||
return getAllGridKeys(ctx, constants.RedisAllGridSetKey)
|
||||
fanInChan := make(chan SearchResult, 2)
|
||||
// return all grid tagname
|
||||
go getAllKeyByGridLevel(ctx, rdb, fanInChan)
|
||||
// return all component nspath
|
||||
go getAllKeyByNSPathLevel(ctx, rdb, fanInChan)
|
||||
results := make(map[string]SearchResult)
|
||||
for range 2 {
|
||||
result := <-fanInChan
|
||||
if result.Err != nil {
|
||||
logger.Error(ctx, "return all keys at the special level from redis failed", "recommend_type", result.RecommendType, "error", result.Err)
|
||||
continue
|
||||
}
|
||||
|
||||
if result.RecommendType == constants.CompNSPathRecommendHierarchyType {
|
||||
filterResults := make([]string, 0, len(result.QueryDatas))
|
||||
// TODO 增加 nspath 过滤
|
||||
for _, queryData := range result.QueryDatas {
|
||||
var nsPath string
|
||||
if lastDotIndex := strings.LastIndex(queryData, "."); lastDotIndex == -1 {
|
||||
nsPath = queryData
|
||||
} else {
|
||||
nsPath = queryData[lastDotIndex+1:]
|
||||
}
|
||||
|
||||
if isLocal, ok := NSPathToIsLocalMap[nsPath]; ok && isLocal {
|
||||
filterResults = append(filterResults, queryData)
|
||||
}
|
||||
}
|
||||
result.QueryDatas = filterResults
|
||||
}
|
||||
results[result.RecommendType.String()] = result
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
inputSlice := strings.Split(input, ".")
|
||||
inputSliceLen := len(inputSlice)
|
||||
originInputLen := len(inputSlice)
|
||||
|
||||
fanInChan := make(chan SearchResult, 4)
|
||||
switch inputSliceLen {
|
||||
case 1:
|
||||
// TODO 优化成NewSet的形式
|
||||
gridExist, err := rdb.SIsMember(ctx, constants.RedisAllGridSetKey, input).Result()
|
||||
if err != nil {
|
||||
logger.Error(ctx, "check grid key exist failed ", "grid_key", input, "error", err)
|
||||
return []string{}, false, err
|
||||
}
|
||||
searchInput := inputSlice[0]
|
||||
// grid tagname search
|
||||
go levelOneRedisSearch(ctx, rdb, constants.GridRecommendHierarchyType, constants.FullRecommendLength, searchInput, constants.RedisAllGridSetKey, fanInChan)
|
||||
// component nspath search
|
||||
go levelOneRedisSearch(ctx, rdb, constants.CompNSPathRecommendHierarchyType, constants.IsLocalRecommendLength, searchInput, constants.RedisAllCompNSPathSetKey, fanInChan)
|
||||
|
||||
searchInput := input
|
||||
inputLen := inputSliceLen
|
||||
for inputLen != 0 && !gridExist {
|
||||
results, err := ac.SuggestOpts(searchInput, redisearch.SuggestOptions{
|
||||
Num: math.MaxInt16,
|
||||
Fuzzy: true,
|
||||
WithScores: false,
|
||||
WithPayloads: false,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query info by fuzzy failed", "query_key", input, "error", err)
|
||||
return []string{}, false, err
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
// TODO 构建 for 循环返回所有可能的补全
|
||||
searchInput = searchInput[:len(searchInput)-1]
|
||||
inputLen = len(searchInput)
|
||||
results := make(map[string]SearchResult)
|
||||
// TODO 后续根据支持的数据标识语法长度,进行值的变更
|
||||
for range 2 {
|
||||
result := <-fanInChan
|
||||
if result.Err != nil {
|
||||
logger.Error(ctx, "exec redis fuzzy search by key failed", "recommend_type", result.RecommendType, "error", result.Err)
|
||||
continue
|
||||
}
|
||||
|
||||
var recommends []string
|
||||
for _, result := range results {
|
||||
termSlice := strings.Split(result.Term, ".")
|
||||
if len(termSlice) <= originInputLen {
|
||||
recommends = append(recommends, result.Term)
|
||||
if result.RecommendType == constants.CompNSPathRecommendHierarchyType {
|
||||
filterResults := make([]string, 0, len(result.QueryDatas))
|
||||
// TODO 增加 nspath 过滤
|
||||
for _, queryData := range result.QueryDatas {
|
||||
if queryData == "." {
|
||||
filterResults = append(filterResults, queryData)
|
||||
continue
|
||||
}
|
||||
|
||||
var nsPath string
|
||||
if lastDotIndex := strings.LastIndex(queryData, "."); lastDotIndex == -1 {
|
||||
nsPath = queryData
|
||||
} else {
|
||||
nsPath = queryData[lastDotIndex+1:]
|
||||
}
|
||||
|
||||
if isLocal, ok := NSPathToIsLocalMap[nsPath]; ok && isLocal {
|
||||
filterResults = append(filterResults, queryData)
|
||||
}
|
||||
}
|
||||
result.QueryDatas = filterResults
|
||||
}
|
||||
results[result.RecommendType.String()] = result
|
||||
}
|
||||
|
||||
return results
|
||||
case 2:
|
||||
// zone tagname search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.ZoneRecommendHierarchyType, constants.FullRecommendLength, constants.RedisAllZoneSetKey, inputSlice, fanInChan)
|
||||
// component tagname search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.CompTagRecommendHierarchyType, constants.IsLocalRecommendLength, constants.RedisAllCompTagSetKey, inputSlice, fanInChan)
|
||||
results := make(map[string]SearchResult)
|
||||
for range 2 {
|
||||
result := <-fanInChan
|
||||
if result.Err != nil {
|
||||
logger.Error(ctx, "query all keys at the special level from redis failed", "query_key", result.RecommendType, "error", result.Err)
|
||||
continue
|
||||
}
|
||||
results[result.RecommendType.String()] = result
|
||||
}
|
||||
|
||||
return results
|
||||
case 3:
|
||||
// station tanname search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.StationRecommendHierarchyType, constants.FullRecommendLength, constants.RedisAllStationSetKey, inputSlice, fanInChan)
|
||||
// config search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.ConfigRecommendHierarchyType, constants.IsLocalRecommendLength, constants.RedisAllConfigSetKey, inputSlice, fanInChan)
|
||||
|
||||
results := make(map[string]SearchResult)
|
||||
for range 2 {
|
||||
result := <-fanInChan
|
||||
if result.Err != nil {
|
||||
logger.Error(ctx, "query all keys at the special level from redis failed", "query_key", result.RecommendType, "error", result.Err)
|
||||
continue
|
||||
}
|
||||
results[result.RecommendType.String()] = result
|
||||
}
|
||||
|
||||
return results
|
||||
case 4:
|
||||
// component nspath search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.CompNSPathRecommendHierarchyType, constants.FullRecommendLength, constants.RedisAllCompNSPathSetKey, inputSlice, fanInChan)
|
||||
// measurement tagname search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.MeasTagRecommendHierarchyType, constants.IsLocalRecommendLength, constants.RedisAllConfigSetKey, inputSlice, fanInChan)
|
||||
|
||||
results := make(map[string]SearchResult)
|
||||
for range 2 {
|
||||
result := <-fanInChan
|
||||
if result.Err != nil {
|
||||
logger.Error(ctx, "query all keys at the special level from redis failed", "query_key", result.RecommendType, "error", result.Err)
|
||||
continue
|
||||
}
|
||||
results[result.RecommendType.String()] = result
|
||||
}
|
||||
|
||||
return results
|
||||
case 5:
|
||||
// component tagname search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.CompTagRecommendHierarchyType, constants.FullRecommendLength, constants.RedisAllCompTagSetKey, inputSlice, fanInChan)
|
||||
|
||||
results := make(map[string]SearchResult)
|
||||
for range 1 {
|
||||
result := <-fanInChan
|
||||
if result.Err != nil {
|
||||
logger.Error(ctx, "query all keys at the special level from redis failed", "query_key", result.RecommendType, "error", result.Err)
|
||||
continue
|
||||
}
|
||||
results[result.RecommendType.String()] = result
|
||||
}
|
||||
|
||||
return results
|
||||
case 6:
|
||||
// config search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.ConfigRecommendHierarchyType, constants.FullRecommendLength, constants.RedisAllConfigSetKey, inputSlice, fanInChan)
|
||||
|
||||
results := make(map[string]SearchResult)
|
||||
for range 1 {
|
||||
result := <-fanInChan
|
||||
if result.Err != nil {
|
||||
logger.Error(ctx, "query all keys at the special level from redis failed", "query_key", result.RecommendType, "error", result.Err)
|
||||
continue
|
||||
}
|
||||
results[result.RecommendType.String()] = result
|
||||
}
|
||||
|
||||
return results
|
||||
case 7:
|
||||
// measurement tagname search
|
||||
go handleLevelFuzzySearch(ctx, rdb, constants.MeasTagRecommendHierarchyType, constants.FullRecommendLength, constants.RedisAllMeasTagSetKey, inputSlice, fanInChan)
|
||||
|
||||
results := make(map[string]SearchResult)
|
||||
for range 1 {
|
||||
result := <-fanInChan
|
||||
if result.Err != nil {
|
||||
logger.Error(ctx, "query all keys at the special level from redis failed", "query_key", result.RecommendType, "error", result.Err)
|
||||
continue
|
||||
}
|
||||
results[result.RecommendType.String()] = result
|
||||
}
|
||||
|
||||
return results
|
||||
default:
|
||||
logger.Error(ctx, "unsupport length of search input", "input_len", inputSliceLen)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func queryMemberFromSpecificsLevel(ctx context.Context, rdb *redis.Client, hierarchy constants.RecommendHierarchyType, keyPrefix string) ([]string, error) {
|
||||
queryKey := getSpecificKeyByLength(hierarchy, keyPrefix)
|
||||
return rdb.SMembers(ctx, queryKey).Result()
|
||||
}
|
||||
|
||||
func getAllKeyByGridLevel(ctx context.Context, rdb *redis.Client, fanInChan chan SearchResult) {
|
||||
setKey := constants.RedisAllGridSetKey
|
||||
hierarchy := constants.GridRecommendHierarchyType
|
||||
members, err := rdb.SMembers(ctx, setKey).Result()
|
||||
if err != nil && err != redigo.ErrNil {
|
||||
logger.Error(ctx, "get all members from redis by special key failed", "key", setKey, "op", "SMembers", "error", err)
|
||||
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: err,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: members,
|
||||
IsFuzzy: false,
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func getAllKeyByNSPathLevel(ctx context.Context, rdb *redis.Client, fanInChan chan SearchResult) {
|
||||
queryKey := constants.RedisAllCompNSPathSetKey
|
||||
hierarchy := constants.CompNSPathRecommendHierarchyType
|
||||
members, err := rdb.SMembers(ctx, queryKey).Result()
|
||||
if err != nil && err != redigo.ErrNil {
|
||||
logger.Error(ctx, "get all members by special key failed", "key", queryKey, "op", "SMembers", "error", err)
|
||||
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: err,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: members,
|
||||
IsFuzzy: false,
|
||||
Err: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func combineQueryResultByInput(hierarchy constants.RecommendHierarchyType, recommendLenType string, inputSlice []string, queryResults []string) []string {
|
||||
prefixs := make([]string, 0, len(inputSlice))
|
||||
recommandResults := make([]string, 0, len(queryResults))
|
||||
switch recommendLenType {
|
||||
case constants.FullRecommendLength:
|
||||
switch hierarchy {
|
||||
case constants.ZoneRecommendHierarchyType:
|
||||
prefixs = []string{inputSlice[0]}
|
||||
case constants.StationRecommendHierarchyType:
|
||||
prefixs = inputSlice[0:2]
|
||||
case constants.CompNSPathRecommendHierarchyType:
|
||||
prefixs = inputSlice[0:3]
|
||||
case constants.CompTagRecommendHierarchyType:
|
||||
prefixs = inputSlice[0:4]
|
||||
case constants.ConfigRecommendHierarchyType:
|
||||
prefixs = inputSlice[0:5]
|
||||
case constants.MeasTagRecommendHierarchyType:
|
||||
prefixs = inputSlice[0:6]
|
||||
default:
|
||||
return []string{}
|
||||
}
|
||||
case constants.IsLocalRecommendLength:
|
||||
switch hierarchy {
|
||||
case constants.CompTagRecommendHierarchyType:
|
||||
prefixs = []string{inputSlice[0]}
|
||||
case constants.ConfigRecommendHierarchyType:
|
||||
prefixs = inputSlice[0:2]
|
||||
case constants.MeasTagRecommendHierarchyType:
|
||||
prefixs = inputSlice[0:3]
|
||||
default:
|
||||
return []string{}
|
||||
}
|
||||
}
|
||||
|
||||
for _, queryResult := range queryResults {
|
||||
combineStrs := make([]string, 0, len(inputSlice))
|
||||
combineStrs = append(combineStrs, prefixs...)
|
||||
combineStrs = append(combineStrs, queryResult)
|
||||
recommandResult := strings.Join(combineStrs, ".")
|
||||
recommandResults = append(recommandResults, recommandResult)
|
||||
}
|
||||
return recommandResults
|
||||
}
|
||||
|
||||
func getSpecificKeyByLength(hierarchy constants.RecommendHierarchyType, keyPrefix string) string {
|
||||
switch hierarchy {
|
||||
case constants.GridRecommendHierarchyType:
|
||||
return constants.RedisAllGridSetKey
|
||||
case constants.ZoneRecommendHierarchyType:
|
||||
return fmt.Sprintf(constants.RedisSpecGridZoneSetKey, keyPrefix)
|
||||
case constants.StationRecommendHierarchyType:
|
||||
return fmt.Sprintf(constants.RedisSpecZoneStationSetKey, keyPrefix)
|
||||
case constants.CompNSPathRecommendHierarchyType:
|
||||
return fmt.Sprintf(constants.RedisSpecStationCompNSPATHSetKey, keyPrefix)
|
||||
case constants.CompTagRecommendHierarchyType:
|
||||
return fmt.Sprintf(constants.RedisSpecCompNSPathCompTagSetKey, keyPrefix)
|
||||
case constants.ConfigRecommendHierarchyType:
|
||||
return constants.RedisAllConfigSetKey
|
||||
case constants.MeasTagRecommendHierarchyType:
|
||||
return fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, keyPrefix)
|
||||
default:
|
||||
return constants.RedisAllGridSetKey
|
||||
}
|
||||
}
|
||||
|
||||
// handleLevelFuzzySearch define func to process recommendation logic for specific levels(level >= 2)
|
||||
func handleLevelFuzzySearch(ctx context.Context, rdb *redis.Client, hierarchy constants.RecommendHierarchyType, recommendLenType string, redisSetKey string, inputSlice []string, fanInChan chan SearchResult) {
|
||||
inputSliceLen := len(inputSlice)
|
||||
searchInputIndex := inputSliceLen - 1
|
||||
searchInput := inputSlice[searchInputIndex]
|
||||
searchPrefix := strings.Join(inputSlice[0:searchInputIndex], ".")
|
||||
|
||||
if searchInput == "" {
|
||||
var specificalKey string
|
||||
specificalKeyIndex := searchInputIndex - 1
|
||||
if hierarchy == constants.MeasTagRecommendHierarchyType {
|
||||
specificalKeyIndex = searchInputIndex - 2
|
||||
}
|
||||
|
||||
if specificalKeyIndex >= 0 {
|
||||
specificalKey = inputSlice[specificalKeyIndex]
|
||||
}
|
||||
|
||||
if recommendLenType == constants.IsLocalRecommendLength && hierarchy == constants.ConfigRecommendHierarchyType {
|
||||
// token4-token7 model and query all config keys
|
||||
redisSetKey = constants.RedisAllCompTagSetKey
|
||||
keyExists, err := rdb.SIsMember(ctx, redisSetKey, specificalKey).Result()
|
||||
if err != nil {
|
||||
logger.Error(ctx, "check key exist from redis set failed ", "key", redisSetKey, "error", err)
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: err,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !keyExists {
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: []string{},
|
||||
IsFuzzy: false,
|
||||
Err: nil,
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
members, err := queryMemberFromSpecificsLevel(ctx, rdb, hierarchy, specificalKey)
|
||||
if err != nil && err != redis.Nil {
|
||||
logger.Error(ctx, "query members from redis by special key failed", "key", specificalKey, "member", searchInput, "op", "SMember", "error", err)
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: err,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
recommandResults := combineQueryResultByInput(hierarchy, recommendLenType, inputSlice, members)
|
||||
if hierarchy == constants.ConfigRecommendHierarchyType || hierarchy == constants.MeasTagRecommendHierarchyType {
|
||||
// check the relevance between the config hierarchy and measurement hierarchy output and the request input, i.e., use FT.SUGGET search_suggestions_dict "recommandResult" max 1 for an exact query to check if the result exists
|
||||
secondConfirmResults := make([]string, 0, len(recommandResults))
|
||||
for _, res := range recommandResults {
|
||||
results, err := ac.SuggestOpts(res, redisearch.SuggestOptions{
|
||||
Num: 1,
|
||||
Fuzzy: false,
|
||||
WithScores: false,
|
||||
WithPayloads: false,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error(ctx, "config hierarchy query key second confirmation failed", "query_key", res, "error", err)
|
||||
continue
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
continue
|
||||
}
|
||||
secondConfirmResults = append(secondConfirmResults, res)
|
||||
}
|
||||
|
||||
recommandResults = secondConfirmResults
|
||||
}
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: recommandResults,
|
||||
IsFuzzy: false,
|
||||
Err: nil,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
keyExists, err := rdb.SIsMember(ctx, redisSetKey, searchInput).Result()
|
||||
if err != nil {
|
||||
logger.Error(ctx, "check key exist from redis set failed ", "key", redisSetKey, "error", err)
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: err,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if keyExists {
|
||||
var QueryData []string
|
||||
if hierarchy == constants.MaxIdentifyHierarchy || (hierarchy == constants.IdentifyHierarchy && redisSetKey == constants.RedisAllMeasTagSetKey) {
|
||||
QueryData = []string{""}
|
||||
} else {
|
||||
QueryData = []string{"."}
|
||||
}
|
||||
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: QueryData,
|
||||
IsFuzzy: false,
|
||||
Err: nil,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// start redis fuzzy search
|
||||
recommends, err := runFuzzySearch(ctx, rdb, searchInput, searchPrefix, hierarchy, recommendLenType)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "fuzzy search failed by hierarchy", "hierarchy", hierarchy, "search_input", searchInput, "error", err)
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: nil,
|
||||
IsFuzzy: false,
|
||||
Err: err,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if len(recommends) == 0 {
|
||||
logger.Info(ctx, "fuzzy search without result", "hierarchy", hierarchy, "search_input", searchInput, "error", err)
|
||||
}
|
||||
|
||||
fanInChan <- SearchResult{
|
||||
RecommendType: hierarchy,
|
||||
QueryDatas: recommends,
|
||||
IsFuzzy: true,
|
||||
Err: nil,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// runFuzzySearch define func to process redis fuzzy search
|
||||
func runFuzzySearch(ctx context.Context, rdb *redis.Client, searchInput string, searchPrefix string, hierarchy constants.RecommendHierarchyType, recommendLenType string) ([]string, error) {
|
||||
var configToken string
|
||||
var comparePrefix string
|
||||
searchInputLen := len(searchInput)
|
||||
compareHierarchyLen := int(hierarchy)
|
||||
comparePrefix = searchPrefix
|
||||
if hierarchy == constants.MeasTagRecommendHierarchyType {
|
||||
compareHierarchyLen = int(hierarchy) - 1
|
||||
lastDotIndex := strings.LastIndex(searchPrefix, ".")
|
||||
if lastDotIndex == -1 {
|
||||
configToken = ""
|
||||
} else {
|
||||
configToken = searchPrefix[lastDotIndex+1:]
|
||||
comparePrefix = searchPrefix[:lastDotIndex]
|
||||
}
|
||||
}
|
||||
|
||||
for searchInputLen != 0 {
|
||||
fuzzyInput := searchInput
|
||||
if comparePrefix != "" {
|
||||
fuzzyInput = comparePrefix + "." + searchInput
|
||||
}
|
||||
results, err := ac.SuggestOpts(fuzzyInput, redisearch.SuggestOptions{
|
||||
Num: math.MaxInt16,
|
||||
Fuzzy: true,
|
||||
WithScores: false,
|
||||
WithPayloads: false,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query key by redis fuzzy search failed", "query_key", fuzzyInput, "error", err)
|
||||
return nil, fmt.Errorf("redisearch suggest failed: %w", err)
|
||||
}
|
||||
|
||||
if len(results) == 0 {
|
||||
// 如果没有结果,退一步(删除最后一个字节)并继续循环
|
||||
// TODO 考虑使用其他方式代替 for 循环退一字节的查询方式
|
||||
searchInput = searchInput[:len(searchInput)-1]
|
||||
searchInputLen = len(searchInput)
|
||||
continue
|
||||
}
|
||||
|
||||
var recommends []string
|
||||
for _, result := range results {
|
||||
term := result.Term
|
||||
var termHierarchyLen int
|
||||
var termPrefix string
|
||||
var termLastPart string
|
||||
|
||||
lastDotIndex := strings.LastIndex(term, ".")
|
||||
if lastDotIndex == -1 {
|
||||
termPrefix = ""
|
||||
termLastPart = term
|
||||
} else {
|
||||
termPrefix = term[:lastDotIndex]
|
||||
termLastPart = term[lastDotIndex+1:]
|
||||
}
|
||||
|
||||
if recommendLenType == constants.FullRecommendLength && hierarchy == constants.GridRecommendHierarchyType {
|
||||
exists, err := rdb.SIsMember(ctx, constants.RedisAllGridSetKey, termLastPart).Result()
|
||||
if err != nil || !exists {
|
||||
logger.Info(ctx, "query key by redis fuzzy search failed or term not in redis all grid set", "query_key", fuzzyInput, "exists", exists, "error", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
// 返回模糊查询结果
|
||||
return recommends, true, nil
|
||||
}
|
||||
|
||||
// 处理 input 不为空、不含有.并且 input 是一个完整的 grid key 的情况
|
||||
if strings.HasSuffix(input, ".") == false {
|
||||
recommend := input + "."
|
||||
return []string{recommend}, false, nil
|
||||
}
|
||||
default:
|
||||
lastInput := inputSlice[inputSliceLen-1]
|
||||
// 判断 queryKey 是否是空值,空值则返回上一级别下的所有key
|
||||
if lastInput == "" {
|
||||
setKey := getCombinedConstantsKeyByLength(inputSlice[inputSliceLen-2], inputSliceLen)
|
||||
targetSet := diagram.NewRedisSet(ctx, setKey, 10, true)
|
||||
keys, err := targetSet.SMembers(setKey)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "get all recommend key by setKey failed", "set_key", setKey, "error", err)
|
||||
return []string{}, false, fmt.Errorf("get all recommend key by setKey failed,%w", err)
|
||||
if recommendLenType == constants.FullRecommendLength {
|
||||
if result.Term == "" {
|
||||
termHierarchyLen = 1
|
||||
} else {
|
||||
termHierarchyLen = strings.Count(result.Term, ".") + 1
|
||||
}
|
||||
} else if recommendLenType == constants.IsLocalRecommendLength {
|
||||
if result.Term == "" {
|
||||
termHierarchyLen = 4
|
||||
} else {
|
||||
termHierarchyLen = strings.Count(result.Term, ".") + 4
|
||||
}
|
||||
}
|
||||
|
||||
var results []string
|
||||
for _, key := range keys {
|
||||
result := input + key
|
||||
results = append(results, result)
|
||||
if termHierarchyLen == compareHierarchyLen && termPrefix == comparePrefix && strings.HasPrefix(termLastPart, searchInput) {
|
||||
recommend := result.Term
|
||||
if hierarchy == constants.MeasTagRecommendHierarchyType {
|
||||
recommend = strings.Join([]string{termPrefix, configToken, termLastPart}, ".")
|
||||
}
|
||||
recommends = append(recommends, recommend)
|
||||
}
|
||||
return results, false, nil
|
||||
}
|
||||
|
||||
setKey := getCombinedConstantsKeyByLength(inputSlice[inputSliceLen-2], inputSliceLen)
|
||||
targetSet := diagram.NewRedisSet(ctx, setKey, 10, true)
|
||||
exist, err := targetSet.SIsMember(setKey, lastInput)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "check keys exist failed", "set_key", setKey, "query_key", lastInput, "error", err)
|
||||
return []string{}, false, fmt.Errorf("check keys failed,%w", err)
|
||||
}
|
||||
|
||||
searchInput := input
|
||||
inputLen := len(searchInput)
|
||||
for inputLen != 0 && !exist {
|
||||
logger.Info(ctx, "use fuzzy query", "input", input)
|
||||
results, err := ac.SuggestOpts(searchInput, redisearch.SuggestOptions{
|
||||
Num: math.MaxInt16,
|
||||
Fuzzy: true,
|
||||
WithScores: false,
|
||||
WithPayloads: false,
|
||||
})
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query info by fuzzy failed", "query_key", input, "error", err)
|
||||
return []string{}, false, err
|
||||
}
|
||||
if len(results) == 0 {
|
||||
// TODO 构建 for 循环返回所有可能的补全
|
||||
searchInput = input[:inputLen-1]
|
||||
inputLen = len(searchInput)
|
||||
continue
|
||||
}
|
||||
|
||||
var terms []string
|
||||
for _, result := range results {
|
||||
terms = append(terms, result.Term)
|
||||
}
|
||||
// 返回模糊查询结果
|
||||
return terms, true, nil
|
||||
}
|
||||
return []string{input}, false, nil
|
||||
return recommends, nil
|
||||
}
|
||||
return []string{}, false, nil
|
||||
}
|
||||
|
||||
func getAllGridKeys(ctx context.Context, setKey string) ([]string, bool, error) {
|
||||
// 从redis set 中获取所有的 grid key
|
||||
gridSets := diagram.NewRedisSet(ctx, setKey, 10, true)
|
||||
keys, err := gridSets.SMembers("grid_keys")
|
||||
if err != nil {
|
||||
return []string{}, false, fmt.Errorf("get all root keys failed, error: %v", err)
|
||||
}
|
||||
return keys, false, nil
|
||||
}
|
||||
|
||||
func getSpecificZoneKeys(ctx context.Context, input string) ([]string, bool, error) {
|
||||
setKey := fmt.Sprintf(constants.RedisSpecGridZoneSetKey, input)
|
||||
// TODO 从redis set 中获取指定 grid 下的 zone key
|
||||
zoneSets := diagram.NewRedisSet(ctx, setKey, 10, true)
|
||||
keys, err := zoneSets.SMembers(setKey)
|
||||
if err != nil {
|
||||
return []string{}, false, fmt.Errorf("get all root keys failed, error: %v", err)
|
||||
}
|
||||
var results []string
|
||||
for _, key := range keys {
|
||||
result := input + "." + key
|
||||
results = append(results, result)
|
||||
}
|
||||
return results, false, nil
|
||||
}
|
||||
|
||||
func getConstantsKeyByLength(inputLen int) string {
|
||||
switch inputLen {
|
||||
case 1:
|
||||
return constants.RedisAllGridSetKey
|
||||
case 2:
|
||||
return constants.RedisAllZoneSetKey
|
||||
case 3:
|
||||
return constants.RedisAllStationSetKey
|
||||
case 4:
|
||||
return constants.RedisAllComponentSetKey
|
||||
default:
|
||||
return constants.RedisAllGridSetKey
|
||||
}
|
||||
}
|
||||
|
||||
func getCombinedConstantsKeyByLength(key string, inputLen int) string {
|
||||
switch inputLen {
|
||||
case 2:
|
||||
return fmt.Sprintf(constants.RedisSpecGridZoneSetKey, key)
|
||||
case 3:
|
||||
return fmt.Sprintf(constants.RedisSpecZoneStationSetKey, key)
|
||||
case 4:
|
||||
return fmt.Sprintf(constants.RedisSpecStationComponentSetKey, key)
|
||||
default:
|
||||
return constants.RedisAllGridSetKey
|
||||
}
|
||||
}
|
||||
|
||||
// GetLongestCommonPrefixLength define func of get longest common prefix length between two strings
|
||||
func GetLongestCommonPrefixLength(input string, recommendResult string) int {
|
||||
if input == "" {
|
||||
return 0
|
||||
}
|
||||
|
||||
minLen := min(len(input), len(recommendResult))
|
||||
|
||||
for i := range minLen {
|
||||
if input[i] != recommendResult[i] {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return minLen
|
||||
return []string{}, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,18 +23,32 @@ type TopologicUUIDCreateInfo struct {
|
|||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
// ComponentCreateInfo defines circuit diagram component create index info
|
||||
// ComponentCreateInfo defines circuit diagram component create info
|
||||
type ComponentCreateInfo struct {
|
||||
UUID string `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
Context string `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"`
|
||||
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
|
||||
|
|
@ -54,9 +68,8 @@ func ConvertComponentCreateInfosToComponents(info ComponentCreateInfo) (*orm.Com
|
|||
|
||||
component := &orm.Component{
|
||||
GlobalUUID: uuidVal,
|
||||
// NsPath: info.NsPath,
|
||||
Tag: info.Tag,
|
||||
Name: info.Name,
|
||||
Tag: info.Tag,
|
||||
Name: info.Name,
|
||||
// ModelName: info.ModelName,
|
||||
// Description: info.Description,
|
||||
// GridID: info.GridID,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,22 @@
|
|||
// Package network define struct of network operation
|
||||
package network
|
||||
|
||||
// MeasurementLinkRequest defines the request payload for process an measurement link
|
||||
type MeasurementLinkRequest struct {
|
||||
// required: true
|
||||
MeasurementID int64 `json:"measurement_id" example:"1001"`
|
||||
// required: true
|
||||
// enum: [add, del]
|
||||
Action string `json:"action" example:"add"`
|
||||
}
|
||||
|
||||
// DiagramNodeLinkRequest defines the request payload for process an diagram node link
|
||||
type DiagramNodeLinkRequest struct {
|
||||
// required: true
|
||||
NodeID int64 `json:"node_id" example:"1001"`
|
||||
// required: true
|
||||
NodeLevel int `json:"node_level" example:"1"`
|
||||
// required: true
|
||||
// enum: [add, del]
|
||||
Action string `json:"action" example:"add"`
|
||||
}
|
||||
|
|
@ -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 string `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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
// Package network define struct of network operation
|
||||
package network
|
||||
|
||||
// ComponentAttributeUpdateInfo defines circuit diagram component attribute params info
|
||||
type ComponentAttributeUpdateInfo struct {
|
||||
AttributeConfigs []ComponentAttributeConfig `json:"attributes"`
|
||||
}
|
||||
|
||||
// ComponentAttributeConfig defines request params of circuit diagram component attribute update config
|
||||
type ComponentAttributeConfig struct {
|
||||
AttributeToken string `json:"token"`
|
||||
AttributeOldVal string `json:"attribute_old_val"`
|
||||
AttributeNewVal string `json:"attribute_new_val"`
|
||||
}
|
||||
|
|
@ -9,5 +9,5 @@ type MeasurementGetRequest struct {
|
|||
|
||||
// MeasurementRecommendRequest defines the request payload for an measurement recommend
|
||||
type MeasurementRecommendRequest struct {
|
||||
Input string `json:"input" example:"trans"`
|
||||
Input string `form:"input,omitempty" example:"grid1"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
// Package network define struct of network operation
|
||||
package network
|
||||
|
||||
// RealTimeDataReceiveRequest defines request params of real time data receive api
|
||||
type RealTimeDataReceiveRequest struct {
|
||||
PayLoad RealTimeDataReceivePayload `json:"payload"`
|
||||
}
|
||||
|
||||
// RealTimeDataReceivePayload defines request payload of real time data receive api
|
||||
type RealTimeDataReceivePayload struct {
|
||||
ComponentUUID string `json:"component_uuid"`
|
||||
Point string `json:"point"`
|
||||
Values []RealTimeDataPoint `json:"values"`
|
||||
}
|
||||
|
||||
// RealTimeDataPoint define struct of real time data point
|
||||
type RealTimeDataPoint struct {
|
||||
Time int64 `json:"time" example:"1678886400"`
|
||||
Value float64 `json:"value" example:"123.1"`
|
||||
}
|
||||
|
||||
// RealTimeDataPayload define struct of real time data payload
|
||||
type RealTimeDataPayload struct {
|
||||
// TODO 增加example tag
|
||||
RealTimeDataPoints []RealTimeDataPoint `json:"sub_pos" swaggertype:"object"`
|
||||
}
|
||||
|
|
@ -1,20 +1,48 @@
|
|||
// Package network define struct of network operation
|
||||
package network
|
||||
|
||||
// RealTimeDataReceiveRequest defines request params of real time data receive api
|
||||
type RealTimeDataReceiveRequest struct {
|
||||
PayLoad RealTimeDataReceivePayload `json:"payload"`
|
||||
// RealTimeQueryRequest define struct of real time data query request
|
||||
type RealTimeQueryRequest struct {
|
||||
// required: true
|
||||
// enum: [start, stop]
|
||||
Action string `json:"action" example:"start" description:"请求的操作,例如 start/stop"`
|
||||
// TODO 增加monitorID的example值说明
|
||||
ClientID string `json:"client_id" example:"xxxx" description:"用于标识不同client的监控请求ID"`
|
||||
|
||||
// required: true
|
||||
Measurements []RealTimeMeasurementItem `json:"measurements" description:"定义不同的数据采集策略和目标"`
|
||||
}
|
||||
|
||||
// RealTimeDataReceivePayload defines request payload of real time data receive api
|
||||
type RealTimeDataReceivePayload struct {
|
||||
ComponentUUID string `json:"component_uuid"`
|
||||
Point string `json:"point"`
|
||||
Values []RealTimeDataReceiveParam `json:"values"`
|
||||
// RealTimeSubRequest define struct of real time data subscription request
|
||||
type RealTimeSubRequest struct {
|
||||
// required: true
|
||||
// enum: [start, stop]
|
||||
Action string `json:"action" example:"start" description:"请求的操作,例如 start/stop"`
|
||||
ClientID string `json:"client_id" example:"5d72f2d9-e33a-4f1b-9c76-88a44b9a953e" description:"用于标识不同client的监控请求ID"`
|
||||
// required: true
|
||||
Measurements []RealTimeMeasurementItem `json:"measurements" description:"定义不同的数据采集策略和目标"`
|
||||
}
|
||||
|
||||
// RealTimeDataReceiveParam defines request param of real time data receive api
|
||||
type RealTimeDataReceiveParam struct {
|
||||
Time int64 `json:"time"`
|
||||
Value float64 `json:"value"`
|
||||
// RealTimeMeasurementItem define struct of real time measurement item
|
||||
type RealTimeMeasurementItem struct {
|
||||
Interval string `json:"interval" example:"1" description:"数据采集的时间间隔(秒)"`
|
||||
Targets []string `json:"targets" example:"[\"grid1.zone1.station1.ns1.tag1.bay.I11_A_rms\",\"grid1.zone1.station1.ns1.tag1.tag1.bay.I11_B_rms\"]" description:"需要采集数据的测点或标签名称列表"`
|
||||
}
|
||||
|
||||
// RealTimePullPayload define struct of pull real time data payload
|
||||
type RealTimePullPayload struct {
|
||||
// required: true
|
||||
Targets []RealTimePullTarget `json:"targets" example:"{\"targets\":[{\"id\":\"grid1.zone1.station1.ns1.tag1.bay.I11_A_rms\",\"datas\":[{\"time\":1736305467506000000,\"value\":1},{\"time\":1736305467506000000,\"value\":1}]},{\"id\":\"grid1.zone1.station1.ns1.tag1.bay.I11_B_rms\",\"datas\":[{\"time\":1736305467506000000,\"value\":1},{\"time\":1736305467506000000,\"value\":1}]}]}" description:"实时数据"`
|
||||
}
|
||||
|
||||
// RealTimePullTarget define struct of pull real time data target
|
||||
type RealTimePullTarget struct {
|
||||
ID string `json:"id" example:"grid1.zone1.station1.ns1.tag1.bay.I11_A_rms" description:"实时数据ID值"`
|
||||
Datas []RealTimePullData `json:"datas" example:"[{\"time\":1736305467506000000,\"value\":220},{\"time\":1736305467506000000,\"value\":220}]" description:"实时数据值数组"`
|
||||
}
|
||||
|
||||
// RealTimePullData define struct of pull real time data param
|
||||
type RealTimePullData struct {
|
||||
Time string `json:"time" example:"1736305467506000000" description:"实时数据时间戳"`
|
||||
Value float64 `json:"value" example:"220" description:"实时数据值"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,20 +5,33 @@ package network
|
|||
type FailureResponse struct {
|
||||
Code int `json:"code" example:"500"`
|
||||
Msg string `json:"msg" example:"failed to get recommend data from redis"`
|
||||
PayLoad any `json:"payload" swaggertype:"object"`
|
||||
Payload any `json:"payload" swaggertype:"object"`
|
||||
}
|
||||
|
||||
// SuccessResponse define struct of standard successful API response format
|
||||
type SuccessResponse struct {
|
||||
Code int `json:"code" example:"200"`
|
||||
Msg string `json:"msg" example:"success"`
|
||||
PayLoad any `json:"payload" swaggertype:"object"`
|
||||
Payload any `json:"payload" swaggertype:"object"`
|
||||
}
|
||||
|
||||
// MeasurementRecommendPayload define struct of represents the data payload for the successful recommendation response.
|
||||
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\"]"`
|
||||
// RecommendedList []string `json:"recommended_list"`
|
||||
}
|
||||
|
||||
// TargetResult define struct of target item in real time data subscription response payload
|
||||
type TargetResult struct {
|
||||
ID string `json:"id" example:"grid1.zone1.station1.ns1.tag1.transformfeeder1_220.I_A_rms"`
|
||||
Code string `json:"code" example:"1001"`
|
||||
Msg string `json:"msg" example:"subscription success"`
|
||||
}
|
||||
|
||||
// RealTimeSubPayload define struct of real time data subscription request
|
||||
type RealTimeSubPayload struct {
|
||||
ClientID string `json:"client_id" example:"5d72f2d9-e33a-4f1b-9c76-88a44b9a953e" description:"用于标识不同client的监控请求ID"`
|
||||
TargetResults []TargetResult `json:"targets"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
// Package orm define database data struct
|
||||
package orm
|
||||
|
||||
// AttributeSet define struct to return component tag、component NSPath field
|
||||
type AttributeSet struct {
|
||||
CompTag string
|
||||
CompNSPath string
|
||||
// StationTag string
|
||||
}
|
||||
|
|
@ -9,62 +9,35 @@ import (
|
|||
|
||||
// Bay structure define abstracted info set of electrical bay
|
||||
type Bay struct {
|
||||
BayUUID uuid.UUID `gorm:"column:BAY_UUID;type:uuid;primaryKey;default:gen_random_uuid()"`
|
||||
Name string `gorm:"column:NAME;size:64;not null;default:''"`
|
||||
Type string `gorm:"column:TYPE;size:64;not null;default:''"`
|
||||
Unom float64 `gorm:"column:UNOM;not null;default:-1"`
|
||||
Fla float64 `gorm:"column:FLA;not null;default:-1"`
|
||||
Capacity float64 `gorm:"column:CAPACITY;not null;default:-1"`
|
||||
Description string `gorm:"column:DESCRIPTION;size:512;not null;default:''"`
|
||||
InService bool `gorm:"column:IN_SERVICE;not null;default:false"`
|
||||
State int `gorm:"column:STATE;not null;default:-1"`
|
||||
Grid string `gorm:"column:GRID;size:64;not null;default:''"`
|
||||
Zone string `gorm:"column:ZONE;size:64;not null;default:''"`
|
||||
Station string `gorm:"column:STATION;size:64;not null;default:''"`
|
||||
Business map[string]interface{} `gorm:"column:BUSINESS;type:jsonb;not null;default:'{}'"`
|
||||
Context map[string]interface{} `gorm:"column:CONTEXT;type:jsonb;not null;default:'{}'"`
|
||||
FromUUIDs []uuid.UUID `gorm:"column:FROM_UUIDS;type:jsonb;not null;default:'[]'"`
|
||||
ToUUIDs []uuid.UUID `gorm:"column:TO_UUIDS;type:jsonb;not null;default:'[]'"`
|
||||
DevProtect []interface{} `gorm:"column:DEV_PROTECT;type:jsonb;not null;default:'[]'"`
|
||||
DevFaultRecord []interface{} `gorm:"column:DEV_FAULT_RECORD;type:jsonb;not null;default:'[]'"`
|
||||
DevStatus []interface{} `gorm:"column:DEV_STATUS;type:jsonb;not null;default:'[]'"`
|
||||
DevDynSense []interface{} `gorm:"column:DEV_DYN_SENSE;type:jsonb;not null;default:'[]'"`
|
||||
DevInstruct []interface{} `gorm:"column:DEV_INSTRUCT;type:jsonb;not null;default:'[]'"`
|
||||
DevEtc []interface{} `gorm:"column:DEV_ETC;type:jsonb;not null;default:'[]'"`
|
||||
Components []uuid.UUID `gorm:"column:COMPONENTS;type:uuid[];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"`
|
||||
BayUUID uuid.UUID `gorm:"column:bay_uuid;type:uuid;primaryKey;default:gen_random_uuid()"`
|
||||
Name string `gorm:"column:name;size:64;not null;default:''"`
|
||||
Tag string `gorm:"column:tag;size:32;not null;default:''"`
|
||||
Type string `gorm:"column:type;size:64;not null;default:''"`
|
||||
Unom float64 `gorm:"column:unom;not null;default:-1"`
|
||||
Fla float64 `gorm:"column:fla;not null;default:-1"`
|
||||
Capacity float64 `gorm:"column:capacity;not null;default:-1"`
|
||||
Description string `gorm:"column:description;size:512;not null;default:''"`
|
||||
InService bool `gorm:"column:in_service;not null;default:false"`
|
||||
State int `gorm:"column:state;not null;default:-1"`
|
||||
Grid string `gorm:"column:grid;size:64;not null;default:''"`
|
||||
Zone string `gorm:"column:zone;size:64;not null;default:''"`
|
||||
Station string `gorm:"column:station;size:64;not null;default:''"`
|
||||
Business JSONMap `gorm:"column:business;type:jsonb;not null;default:'{}'"`
|
||||
Context JSONMap `gorm:"column:context;type:jsonb;not null;default:'{}'"`
|
||||
FromUUIDs []uuid.UUID `gorm:"column:from_uuids;type:jsonb;not null;default:'[]'"`
|
||||
ToUUIDs []uuid.UUID `gorm:"column:to_uuids;type:jsonb;not null;default:'[]'"`
|
||||
DevProtect JSONMap `gorm:"column:dev_protect;type:jsonb;not null;default:'[]'"`
|
||||
DevFaultRecord JSONMap `gorm:"column:dev_fault_record;type:jsonb;not null;default:'[]'"`
|
||||
DevStatus JSONMap `gorm:"column:dev_status;type:jsonb;not null;default:'[]'"`
|
||||
DevDynSense JSONMap `gorm:"column:dev_dyn_sense;type:jsonb;not null;default:'[]'"`
|
||||
DevInstruct JSONMap `gorm:"column:dev_instruct;type:jsonb;not null;default:'[]'"`
|
||||
DevEtc JSONMap `gorm:"column:dev_etc;type:jsonb;not null;default:'[]'"`
|
||||
Components []uuid.UUID `gorm:"column:components;type:uuid[];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"`
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of Bay
|
||||
func (Bay) TableName() string {
|
||||
return "BAY"
|
||||
func (b *Bay) TableName() string {
|
||||
return "bay"
|
||||
}
|
||||
|
||||
// CREATE TABLE PUBLIC.BAY (
|
||||
// BAY_UUID UUID PRIMARY KEY DEFAULT GEN_RANDOM_UUID(),
|
||||
// NAME VARCHAR(64) NOT NULL DEFAULT '',
|
||||
// TYPE VARCHAR(64) NOT NULL DEFAULT '',
|
||||
// UNOM DOUBLE PRECISION NOT NULL DEFAULT -1,
|
||||
// FLA DOUBLE PRECISION NOT NULL DEFAULT -1,
|
||||
// CAPACITY DOUBLE PRECISION NOT NULL DEFAULT -1,
|
||||
// DESCRIPTION VARCHAR(512) NOT NULL DEFAULT '',
|
||||
// IN_SERVICE BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
// STATE INTEGER NOT NULL DEFAULT -1,
|
||||
// GRID VARCHAR(64) NOT NULL DEFAULT '',
|
||||
// ZONE VARCHAR(64) NOT NULL DEFAULT '',
|
||||
// STATION VARCHAR(64) NOT NULL DEFAULT '',
|
||||
// BUSINESS JSONB NOT NULL DEFAULT '{}', -- for Server
|
||||
// CONTEXT JSONB NOT NULL DEFAULT '{}', -- for UI
|
||||
// FROM_UUIDS JSONB NOT NULL DEFAULT '[]', -- uuids
|
||||
// TO_UUIDS JSONB NOT NULL DEFAULT '[]', -- uuids
|
||||
// DEV_PROTECT JSONB NOT NULL DEFAULT '[]', -- devices
|
||||
// DEV_FAULT_RECORD JSONB NOT NULL DEFAULT '[]', -- devices
|
||||
// DEV_STATUS JSONB NOT NULL DEFAULT '[]', -- devices
|
||||
// DEV_DYN_SENSE JSONB NOT NULL DEFAULT '[]', -- devices
|
||||
// DEV_INSTRUCT JSONB NOT NULL DEFAULT '[]', -- devices
|
||||
// DEV_ETC JSONB NOT NULL DEFAULT '[]', -- devices
|
||||
// COMPONENTS UUID[] NOT NULL DEFAULT '{}',
|
||||
// OP INTEGER NOT NULL DEFAULT -1,
|
||||
// TS TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
// );
|
||||
|
|
|
|||
|
|
@ -9,27 +9,38 @@ 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"`
|
||||
GridID string `gorm:"column:GRID"`
|
||||
ZoneID string `gorm:"column:ZONE"`
|
||||
StationID 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 map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"`
|
||||
Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"`
|
||||
Context string `gorm:"column:CONTEXT"`
|
||||
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
|
||||
func (c *Component) TableName() string {
|
||||
return "COMPONENT"
|
||||
return "component"
|
||||
}
|
||||
|
||||
// GetTagName define func to inplement CircuitDiagramNodeInterface interface
|
||||
func (c Component) GetTagName() string {
|
||||
return c.Tag
|
||||
}
|
||||
|
||||
// GetNSPath define func to inplement CircuitDiagramNodeInterface interface
|
||||
func (c Component) GetNSPath() string {
|
||||
return c.NSPath
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,15 +7,25 @@ import (
|
|||
|
||||
// Grid structure define abstracted info set of electrical grid
|
||||
type Grid struct {
|
||||
ID int64 `gorm:"column:ID;primaryKey"`
|
||||
TAGNAME string `gorm:"column:TAGNAME"`
|
||||
Name string `gorm:"column:NAME"`
|
||||
Description string `gorm:"column:DESCRIPTION"`
|
||||
Op int `gorm:"column:OP"`
|
||||
Ts time.Time `gorm:"column:TS"`
|
||||
ID int64 `gorm:"column:id;primaryKey"`
|
||||
TAGNAME string `gorm:"column:tagname"`
|
||||
Name string `gorm:"column:name"`
|
||||
Description string `gorm:"column:description"`
|
||||
Op int `gorm:"column:op"`
|
||||
Ts time.Time `gorm:"column:ts"`
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of Grid
|
||||
func (g *Grid) TableName() string {
|
||||
return "GRID"
|
||||
return "grid"
|
||||
}
|
||||
|
||||
// GetTagName define func to inplement CircuitDiagramNodeInterface interface
|
||||
func (g Grid) GetTagName() string {
|
||||
return g.TAGNAME
|
||||
}
|
||||
|
||||
// GetNSPath define func to inplement CircuitDiagramNodeInterface interface
|
||||
func (g Grid) GetNSPath() string {
|
||||
return ""
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,20 +9,21 @@ import (
|
|||
|
||||
// Measurement structure define abstracted info set of electrical measurement
|
||||
type Measurement struct {
|
||||
ID int64 `gorm:"column:ID;primaryKey;autoIncrement"`
|
||||
Tag string `gorm:"column:TAG;size:64;not null;default:''"`
|
||||
Name string `gorm:"column:NAME;size:64;not null;default:''"`
|
||||
Type int16 `gorm:"column:TYPE;not null;default:-1"`
|
||||
Size int `gorm:"column:SIZE;not null;default:-1"`
|
||||
DataSource map[string]interface{} `gorm:"column:DATA_SOURCE;type:jsonb;not null;default:'{}'"`
|
||||
EventPlan map[string]interface{} `gorm:"column:EVENT_PLAN;type:jsonb;not null;default:'{}'"`
|
||||
BayUUID uuid.UUID `gorm:"column:BAY_UUID;type:uuid;not null"`
|
||||
ComponentUUID uuid.UUID `gorm:"column:COMPONENT_UUID;type:uuid;not null"`
|
||||
Op int `gorm:"column:OP;not null;default:-1"`
|
||||
Ts time.Time `gorm:"column:TS;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
|
||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement"`
|
||||
Tag string `gorm:"column:tag;size:64;not null;default:''"`
|
||||
Name string `gorm:"column:name;size:64;not null;default:''"`
|
||||
Type int16 `gorm:"column:type;not null;default:-1"`
|
||||
Size int `gorm:"column:size;not null;default:-1"`
|
||||
DataSource JSONMap `gorm:"column:data_source;type:jsonb;not null;default:'{}'"`
|
||||
EventPlan JSONMap `gorm:"column:event_plan;type:jsonb;not null;default:'{}'"`
|
||||
Binding JSONMap `gorm:"column:binding;type:jsonb;not null;default:'{\"ct\":{\"ratio\":1.0,\"polarity\":1,\"index\":0},\"pt\":{\"ratio\":1.0,\"polarity\":1,\"index\":0}}'"`
|
||||
BayUUID uuid.UUID `gorm:"column:bay_uuid;type:uuid;not null"`
|
||||
ComponentUUID uuid.UUID `gorm:"column:component_uuid;type:uuid;not null"`
|
||||
Op int `gorm:"column:op;not null;default:-1"`
|
||||
Ts time.Time `gorm:"column:ts;type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of Measurement
|
||||
func (Measurement) TableName() string {
|
||||
return "MEASUREMENT"
|
||||
return "measurement"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
// Package orm define database data struct
|
||||
package orm
|
||||
|
||||
// CircuitDiagramNodeInterface define general node type interface
|
||||
type CircuitDiagramNodeInterface interface {
|
||||
GetTagName() string
|
||||
GetNSPath() string
|
||||
}
|
||||
|
|
@ -5,17 +5,17 @@ import "time"
|
|||
|
||||
// Page structure define circuit diagram page info set
|
||||
type Page struct {
|
||||
ID int64 `gorm:"column:ID;primaryKey"`
|
||||
Tag string `gorm:"column:TAG"`
|
||||
Name string `gorm:"column:NAME"`
|
||||
Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"`
|
||||
Context map[string]interface{} `gorm:"column:CONTEXT;type:jsonb;default:'{}'"`
|
||||
Description string `gorm:"column:DESCRIPTION"`
|
||||
Op int `gorm:"column:OP"`
|
||||
Ts time.Time `gorm:"column:TS"`
|
||||
ID int64 `gorm:"column:id;primaryKey"`
|
||||
Tag string `gorm:"column:tag"`
|
||||
Name string `gorm:"column:name"`
|
||||
Label JSONMap `gorm:"column:label;type:jsonb;default:'{}'"`
|
||||
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
|
||||
Description string `gorm:"column:description"`
|
||||
Op int `gorm:"column:op"`
|
||||
Ts time.Time `gorm:"column:ts"`
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of Page
|
||||
func (p *Page) TableName() string {
|
||||
return "PAGE"
|
||||
return "page"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,17 +7,27 @@ import (
|
|||
|
||||
// Station structure define abstracted info set of electrical Station
|
||||
type Station struct {
|
||||
ID int64 `gorm:"column:ID;primaryKey"`
|
||||
ZoneID int64 `gorm:"column:ZONE_ID"`
|
||||
TAGNAME string `gorm:"column:TAGNAME"`
|
||||
Name string `gorm:"column:NAME"`
|
||||
Description string `gorm:"column:DESCRIPTION"`
|
||||
IsLocal bool `gorm:"column:IS_LOCAL"`
|
||||
Op int `gorm:"column:OP"`
|
||||
Ts time.Time `gorm:"column:TS"`
|
||||
ID int64 `gorm:"column:id;primaryKey"`
|
||||
ZoneID int64 `gorm:"column:zone_id"`
|
||||
TAGNAME string `gorm:"column:tagname"`
|
||||
Name string `gorm:"column:name"`
|
||||
Description string `gorm:"column:description"`
|
||||
IsLocal bool `gorm:"column:is_local"`
|
||||
Op int `gorm:"column:op"`
|
||||
Ts time.Time `gorm:"column:ts"`
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of Station
|
||||
func (s *Station) TableName() string {
|
||||
return "STATION"
|
||||
return "station"
|
||||
}
|
||||
|
||||
// GetTagName define func to inplement CircuitDiagramNodeInterface interface
|
||||
func (s Station) GetTagName() string {
|
||||
return s.TAGNAME
|
||||
}
|
||||
|
||||
// GetNSPath define func to inplement CircuitDiagramNodeInterface interface
|
||||
func (s Station) GetNSPath() string {
|
||||
return ""
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,18 +1,25 @@
|
|||
// Package orm define database data struct
|
||||
package orm
|
||||
|
||||
import "github.com/gofrs/uuid"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
||||
// Topologic structure define topologic info set of circuit diagram
|
||||
type Topologic struct {
|
||||
ID int64 `gorm:"column:id"`
|
||||
Flag int `gorm:"column:flag"`
|
||||
UUIDFrom uuid.UUID `gorm:"column:uuid_from"`
|
||||
UUIDTo uuid.UUID `gorm:"column:uuid_to"`
|
||||
Comment string `gorm:"column:comment"`
|
||||
ID int64 `gorm:"column:id"`
|
||||
UUIDFrom uuid.UUID `gorm:"column:uuid_from"`
|
||||
UUIDTo uuid.UUID `gorm:"column:uuid_to"`
|
||||
Context JSONMap `gorm:"column:context;type:jsonb;default:'{}'"`
|
||||
Flag int `gorm:"column:flag"`
|
||||
Description string `gorm:"column:description;size:512;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"`
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of Page
|
||||
func (t *Topologic) TableName() string {
|
||||
return "Topologic"
|
||||
return "topologic"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,16 +7,26 @@ import (
|
|||
|
||||
// Zone structure define abstracted info set of electrical zone
|
||||
type Zone struct {
|
||||
ID int64 `gorm:"column:ID;primaryKey"`
|
||||
GridID int64 `gorm:"column:GRID_ID"`
|
||||
TAGNAME string `gorm:"column:TAGNAME"`
|
||||
Name string `gorm:"column:NAME"`
|
||||
Description string `gorm:"column:DESCRIPTION"`
|
||||
Op int `gorm:"column:OP"`
|
||||
Ts time.Time `gorm:"column:TS"`
|
||||
ID int64 `gorm:"column:id;primaryKey"`
|
||||
GridID int64 `gorm:"column:grid_id"`
|
||||
TAGNAME string `gorm:"column:tagname"`
|
||||
Name string `gorm:"column:name"`
|
||||
Description string `gorm:"column:description"`
|
||||
Op int `gorm:"column:op"`
|
||||
Ts time.Time `gorm:"column:ts"`
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of Zone
|
||||
func (z *Zone) TableName() string {
|
||||
return "ZONE"
|
||||
return "zone"
|
||||
}
|
||||
|
||||
// GetTagName define func to inplement CircuitDiagramNodeInterface interface
|
||||
func (z Zone) GetTagName() string {
|
||||
return z.TAGNAME
|
||||
}
|
||||
|
||||
// GetNSPath define func to inplement CircuitDiagramNodeInterface interface
|
||||
func (z Zone) GetNSPath() string {
|
||||
return ""
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
// Package orm define database data struct
|
||||
package orm
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// JSONMap define struct of implements the sql.Scanner and driver.Valuer interfaces for handling JSONB fields
|
||||
type JSONMap map[string]any
|
||||
|
||||
// Value define func to convert the JSONMap to driver.Value([]byte) for writing to the database
|
||||
func (j JSONMap) Value() (driver.Value, error) {
|
||||
if j == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return json.Marshal(j)
|
||||
}
|
||||
|
||||
// Scan define to scanned values([]bytes) in the database and parsed into a JSONMap for data retrieval
|
||||
func (j *JSONMap) Scan(value any) error {
|
||||
if value == nil {
|
||||
*j = nil
|
||||
return nil
|
||||
}
|
||||
|
||||
var source []byte
|
||||
switch v := value.(type) {
|
||||
case []byte:
|
||||
source = v
|
||||
case string:
|
||||
source = []byte(v)
|
||||
default:
|
||||
return errors.New("unsupported data type for JSONMap Scan")
|
||||
}
|
||||
return json.Unmarshal(source, j)
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
// 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
|
||||
AllConfigTags []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
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
// Package orm define database data struct
|
||||
package orm
|
||||
|
||||
// ProjectManager define struct to manager component attribute tables
|
||||
type ProjectManager struct {
|
||||
ID int32 `gorm:"primaryKey;column:id"`
|
||||
Name string `gorm:"column:name;type:varchar(64);not null"`
|
||||
Tag string `gorm:"column:tag;type:varchar(64);not null"`
|
||||
MetaModel string `gorm:"column:meta_model;type:varchar(64);not null"`
|
||||
GroupName string `gorm:"column:group_name;type:varchar(64);not null"`
|
||||
LinkType int32 `gorm:"column:link_type;type:integer;not null;default:0"`
|
||||
CheckState JSONMap `gorm:"column:check_state;type:jsonb;not null;default:'{}'"`
|
||||
IsPublic bool `gorm:"column:ispublic;type:boolean;not null;default:false"`
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of Page
|
||||
func (p *ProjectManager) TableName() string {
|
||||
return "project_manager"
|
||||
}
|
||||
|
||||
// ProjectIdentifier define struct to encapsulate query parameter pairs
|
||||
type ProjectIdentifier struct {
|
||||
Token string
|
||||
Tag string
|
||||
GroupName string
|
||||
}
|
||||
|
|
@ -1,200 +1,200 @@
|
|||
// Package realtimedata define real time data operation functions
|
||||
package realtimedata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"time"
|
||||
// import (
|
||||
// "context"
|
||||
// "encoding/json"
|
||||
// "sync"
|
||||
// "time"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/database"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
// "modelRT/constants"
|
||||
// "modelRT/database"
|
||||
// "modelRT/logger"
|
||||
// "modelRT/network"
|
||||
|
||||
"github.com/confluentinc/confluent-kafka-go/kafka"
|
||||
"github.com/gofrs/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
// "github.com/confluentinc/confluent-kafka-go/kafka"
|
||||
// "github.com/gofrs/uuid"
|
||||
// "gorm.io/gorm"
|
||||
// )
|
||||
|
||||
// CacheManager define structure for managing cache items
|
||||
type CacheManager struct {
|
||||
mutex sync.RWMutex
|
||||
store sync.Map
|
||||
pool sync.Pool
|
||||
dbClient *gorm.DB
|
||||
kafkaConsumer *kafka.Consumer
|
||||
ttl time.Duration
|
||||
}
|
||||
// // CacheManager define structure for managing cache items
|
||||
// type CacheManager struct {
|
||||
// mutex sync.RWMutex
|
||||
// store sync.Map
|
||||
// pool sync.Pool
|
||||
// dbClient *gorm.DB
|
||||
// kafkaConsumer *kafka.Consumer
|
||||
// ttl time.Duration
|
||||
// }
|
||||
|
||||
// NewCacheManager define func to create new cache manager
|
||||
func NewCacheManager(db *gorm.DB, kf *kafka.Consumer, ttl time.Duration) *CacheManager {
|
||||
cm := &CacheManager{
|
||||
dbClient: db,
|
||||
kafkaConsumer: kf,
|
||||
ttl: ttl,
|
||||
}
|
||||
cm.pool.New = func() any {
|
||||
item := &CacheItem{}
|
||||
return item
|
||||
}
|
||||
return cm
|
||||
}
|
||||
// // NewCacheManager define func to create new cache manager
|
||||
// func NewCacheManager(db *gorm.DB, kf *kafka.Consumer, ttl time.Duration) *CacheManager {
|
||||
// cm := &CacheManager{
|
||||
// dbClient: db,
|
||||
// kafkaConsumer: kf,
|
||||
// ttl: ttl,
|
||||
// }
|
||||
// cm.pool.New = func() any {
|
||||
// item := &CacheItem{}
|
||||
// return item
|
||||
// }
|
||||
// return cm
|
||||
// }
|
||||
|
||||
// RealTimeComponentMonitor define func to continuously processing component info and build real time component data monitor from kafka specified topic
|
||||
func (cm *CacheManager) RealTimeComponentMonitor(ctx context.Context, topic string, duration string) {
|
||||
// context for graceful shutdown
|
||||
cancelCtx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
// // RealTimeComponentMonitor define func to continuously processing component info and build real time component data monitor from kafka specified topic
|
||||
// func (cm *CacheManager) RealTimeComponentMonitor(ctx context.Context, topic string, duration string) {
|
||||
// // context for graceful shutdown
|
||||
// cancelCtx, cancel := context.WithCancel(ctx)
|
||||
// defer cancel()
|
||||
|
||||
monitorConsumer := cm.kafkaConsumer
|
||||
// monitorConsumer := cm.kafkaConsumer
|
||||
|
||||
// subscribe the monitor topic
|
||||
err := monitorConsumer.SubscribeTopics([]string{topic}, nil)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "subscribe to the monitor topic failed", "topic", topic, "error", err)
|
||||
return
|
||||
}
|
||||
defer monitorConsumer.Close()
|
||||
// // subscribe the monitor topic
|
||||
// err := monitorConsumer.SubscribeTopics([]string{topic}, nil)
|
||||
// if err != nil {
|
||||
// logger.Error(ctx, "subscribe to the monitor topic failed", "topic", topic, "error", err)
|
||||
// return
|
||||
// }
|
||||
// defer monitorConsumer.Close()
|
||||
|
||||
timeoutDuration, err := time.ParseDuration(duration)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "parse duration failed", "duration", duration, "error", err)
|
||||
return
|
||||
}
|
||||
// timeoutDuration, err := time.ParseDuration(duration)
|
||||
// if err != nil {
|
||||
// logger.Error(ctx, "parse duration failed", "duration", duration, "error", err)
|
||||
// return
|
||||
// }
|
||||
|
||||
// continuously read messages from kafka
|
||||
for {
|
||||
select {
|
||||
case <-cancelCtx.Done():
|
||||
logger.Info(ctx, "context canceled, stopping read loop")
|
||||
return
|
||||
default:
|
||||
msg, err := monitorConsumer.ReadMessage(timeoutDuration)
|
||||
if err != nil {
|
||||
if err.(kafka.Error).Code() == kafka.ErrTimedOut {
|
||||
continue
|
||||
}
|
||||
logger.Error(ctx, "consumer read message failed", "error", err)
|
||||
continue
|
||||
}
|
||||
// // continuously read messages from kafka
|
||||
// for {
|
||||
// select {
|
||||
// case <-cancelCtx.Done():
|
||||
// logger.Info(ctx, "context canceled, stopping read loop")
|
||||
// return
|
||||
// default:
|
||||
// msg, err := monitorConsumer.ReadMessage(timeoutDuration)
|
||||
// if err != nil {
|
||||
// if err.(kafka.Error).Code() == kafka.ErrTimedOut {
|
||||
// continue
|
||||
// }
|
||||
// logger.Error(ctx, "consumer read message failed", "error", err)
|
||||
// continue
|
||||
// }
|
||||
|
||||
var realTimeData network.RealTimeDataReceiveRequest
|
||||
if err := json.Unmarshal(msg.Value, &realTimeData); err != nil {
|
||||
logger.Error(ctx, "unmarshal kafka message failed", "message", string(msg.Value), "error", err)
|
||||
continue
|
||||
}
|
||||
// var realTimeData network.RealTimeDataReceiveRequest
|
||||
// if err := json.Unmarshal(msg.Value, &realTimeData); err != nil {
|
||||
// logger.Error(ctx, "unmarshal kafka message failed", "message", string(msg.Value), "error", err)
|
||||
// continue
|
||||
// }
|
||||
|
||||
key := realTimeData.PayLoad.ComponentUUID
|
||||
_, err = cm.StoreIntoCache(ctx, key)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "store into cache failed", "key", key, "error", err)
|
||||
continue
|
||||
}
|
||||
// key := realTimeData.PayLoad.ComponentUUID
|
||||
// _, err = cm.StoreIntoCache(ctx, key)
|
||||
// if err != nil {
|
||||
// logger.Error(ctx, "store into cache failed", "key", key, "error", err)
|
||||
// continue
|
||||
// }
|
||||
|
||||
_, err = monitorConsumer.CommitMessage(msg)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "manual submission information failed", "message", msg, "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// _, err = monitorConsumer.CommitMessage(msg)
|
||||
// if err != nil {
|
||||
// logger.Error(ctx, "manual submission information failed", "message", msg, "error", err)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// GetCacheItemFromPool define func to get a cache item from the pool
|
||||
func (cm *CacheManager) GetCacheItemFromPool() *CacheItem {
|
||||
item := cm.pool.Get().(*CacheItem)
|
||||
item.Reset()
|
||||
return item
|
||||
}
|
||||
// // GetCacheItemFromPool define func to get a cache item from the pool
|
||||
// func (cm *CacheManager) GetCacheItemFromPool() *CacheItem {
|
||||
// item := cm.pool.Get().(*CacheItem)
|
||||
// item.Reset()
|
||||
// return item
|
||||
// }
|
||||
|
||||
// PutCacheItemToPool define func to put a cache item back to the pool
|
||||
func (cm *CacheManager) PutCacheItemToPool(item *CacheItem) {
|
||||
if item != nil {
|
||||
item.Reset()
|
||||
cm.pool.Put(item)
|
||||
}
|
||||
}
|
||||
// // PutCacheItemToPool define func to put a cache item back to the pool
|
||||
// func (cm *CacheManager) PutCacheItemToPool(item *CacheItem) {
|
||||
// if item != nil {
|
||||
// item.Reset()
|
||||
// cm.pool.Put(item)
|
||||
// }
|
||||
// }
|
||||
|
||||
// StoreIntoCache define func to store data into cache, if the key already exists and is not expired, return the existing item
|
||||
func (cm *CacheManager) StoreIntoCache(ctx context.Context, key string) (*CacheItem, error) {
|
||||
cm.mutex.Lock()
|
||||
defer cm.mutex.Unlock()
|
||||
// // StoreIntoCache define func to store data into cache, if the key already exists and is not expired, return the existing item
|
||||
// func (cm *CacheManager) StoreIntoCache(ctx context.Context, key string) (*CacheItem, error) {
|
||||
// cm.mutex.Lock()
|
||||
// defer cm.mutex.Unlock()
|
||||
|
||||
if cachedItemRaw, ok := cm.store.Load(key); ok {
|
||||
cachedItem := cachedItemRaw.(*CacheItem)
|
||||
if !cachedItem.IsExpired(cm.ttl) {
|
||||
cachedItem.lastAccessed = time.Now()
|
||||
return cachedItem, nil
|
||||
}
|
||||
cm.PutCacheItemToPool(cachedItem)
|
||||
cm.store.Delete(key)
|
||||
}
|
||||
// if cachedItemRaw, ok := cm.store.Load(key); ok {
|
||||
// cachedItem := cachedItemRaw.(*CacheItem)
|
||||
// if !cachedItem.IsExpired(cm.ttl) {
|
||||
// cachedItem.lastAccessed = time.Now()
|
||||
// return cachedItem, nil
|
||||
// }
|
||||
// cm.PutCacheItemToPool(cachedItem)
|
||||
// cm.store.Delete(key)
|
||||
// }
|
||||
|
||||
uuid, err := uuid.FromString(key)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "format key to UUID failed", "key", key, "error", err)
|
||||
return nil, constants.ErrFormatUUID
|
||||
}
|
||||
// uuid, err := uuid.FromString(key)
|
||||
// if err != nil {
|
||||
// logger.Error(ctx, "format key to UUID failed", "key", key, "error", err)
|
||||
// return nil, constants.ErrFormatUUID
|
||||
// }
|
||||
|
||||
componentInfo, err := database.QueryComponentByUUID(ctx, cm.dbClient, uuid)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query component from db failed by uuid", "component_uuid", key, "error", err)
|
||||
return nil, constants.ErrQueryComponentByUUID
|
||||
}
|
||||
newItem := cm.GetCacheItemFromPool()
|
||||
newItem.key = key
|
||||
newItem.value = []any{componentInfo.Context}
|
||||
// newItem.calculatorFunc = componentInfo.CalculatorFunc
|
||||
newItem.lastAccessed = time.Now()
|
||||
// componentInfo, err := database.QueryComponentByUUID(ctx, cm.dbClient, uuid)
|
||||
// if err != nil {
|
||||
// logger.Error(ctx, "query component from db failed by uuid", "component_uuid", key, "error", err)
|
||||
// return nil, constants.ErrQueryComponentByUUID
|
||||
// }
|
||||
// newItem := cm.GetCacheItemFromPool()
|
||||
// newItem.key = key
|
||||
// newItem.value = []any{componentInfo.Context}
|
||||
// // newItem.calculatorFunc = componentInfo.CalculatorFunc
|
||||
// newItem.lastAccessed = time.Now()
|
||||
|
||||
// 在存储前启动goroutine
|
||||
if newItem.calculatorFunc != nil {
|
||||
newCtx, newCancel := context.WithCancel(ctx)
|
||||
newItem.cancelFunc = newCancel
|
||||
go newItem.calculatorFunc(newCtx, newItem.topic, newItem.value)
|
||||
}
|
||||
// // 在存储前启动goroutine
|
||||
// if newItem.calculatorFunc != nil {
|
||||
// newCtx, newCancel := context.WithCancel(ctx)
|
||||
// newItem.cancelFunc = newCancel
|
||||
// go newItem.calculatorFunc(newCtx, newItem.topic, newItem.value)
|
||||
// }
|
||||
|
||||
cm.store.Store(key, newItem)
|
||||
return newItem, nil
|
||||
}
|
||||
// cm.store.Store(key, newItem)
|
||||
// return newItem, nil
|
||||
// }
|
||||
|
||||
// UpdateCacheItem define func to update cache item with new data and trigger new calculation
|
||||
func (cm *CacheManager) UpdateCacheItem(ctx context.Context, key string, newData any) error {
|
||||
cm.mutex.Lock()
|
||||
defer cm.mutex.Unlock()
|
||||
// // UpdateCacheItem define func to update cache item with new data and trigger new calculation
|
||||
// func (cm *CacheManager) UpdateCacheItem(ctx context.Context, key string, newData any) error {
|
||||
// cm.mutex.Lock()
|
||||
// defer cm.mutex.Unlock()
|
||||
|
||||
cache, existed := cm.store.Load(key)
|
||||
if !existed {
|
||||
return nil
|
||||
}
|
||||
// cache, existed := cm.store.Load(key)
|
||||
// if !existed {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
cacheItem, ok := cache.(*CacheItem)
|
||||
if !ok {
|
||||
return constants.ErrFormatCache
|
||||
}
|
||||
// cacheItem, ok := cache.(*CacheItem)
|
||||
// if !ok {
|
||||
// return constants.ErrFormatCache
|
||||
// }
|
||||
|
||||
// stop old calculation goroutine
|
||||
if cacheItem.cancelFunc != nil {
|
||||
cacheItem.cancelFunc()
|
||||
}
|
||||
// // stop old calculation goroutine
|
||||
// if cacheItem.cancelFunc != nil {
|
||||
// cacheItem.cancelFunc()
|
||||
// }
|
||||
|
||||
oldValue := cacheItem.value
|
||||
cacheItem.value = []any{newData}
|
||||
cacheItem.lastAccessed = time.Now()
|
||||
// oldValue := cacheItem.value
|
||||
// cacheItem.value = []any{newData}
|
||||
// cacheItem.lastAccessed = time.Now()
|
||||
|
||||
newCtx, newCancel := context.WithCancel(ctx)
|
||||
cacheItem.cancelFunc = newCancel
|
||||
// newCtx, newCancel := context.WithCancel(ctx)
|
||||
// cacheItem.cancelFunc = newCancel
|
||||
|
||||
swapped := cm.store.CompareAndSwap(key, oldValue, cacheItem)
|
||||
if !swapped {
|
||||
cacheValue, _ := cm.store.Load(key)
|
||||
logger.Error(ctx, "store new value into cache failed,existed concurrent modification risk", "key", key, "old_value", oldValue, "cache_value", cacheValue, "store_value", cacheItem.value)
|
||||
return constants.ErrConcurrentModify
|
||||
}
|
||||
// swapped := cm.store.CompareAndSwap(key, oldValue, cacheItem)
|
||||
// if !swapped {
|
||||
// cacheValue, _ := cm.store.Load(key)
|
||||
// logger.Error(ctx, "store new value into cache failed,existed concurrent modification risk", "key", key, "old_value", oldValue, "cache_value", cacheValue, "store_value", cacheItem.value)
|
||||
// return constants.ErrConcurrentModify
|
||||
// }
|
||||
|
||||
// start new calculation goroutine
|
||||
if cacheItem.calculatorFunc != nil {
|
||||
go cacheItem.calculatorFunc(newCtx, cacheItem.topic, cacheItem.value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// // start new calculation goroutine
|
||||
// if cacheItem.calculatorFunc != nil {
|
||||
// go cacheItem.calculatorFunc(newCtx, cacheItem.topic, cacheItem.value)
|
||||
// }
|
||||
// return nil
|
||||
// }
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue