Compare commits
115 Commits
feature-cg
...
develop
| 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 | |
|
|
458f7afdbf | |
|
|
54128bedac | |
|
|
86199269f8 | |
|
|
14d2a7ff65 | |
|
|
3442984657 | |
|
|
68a800ce63 | |
|
|
f0a66263a3 | |
|
|
62e897190d | |
|
|
bcf80842b0 | |
|
|
5d02ca9fca | |
|
|
453e6f9851 | |
|
|
0d7890f6aa | |
|
|
5f5eb22b39 | |
|
|
151f7f22c5 | |
|
|
4ee836c70f | |
|
|
7d8c442f9f | |
|
|
51e8a677ca | |
|
|
71366828f4 | |
|
|
a9532debe9 | |
|
|
0c09e7bd25 | |
|
|
e670720a96 | |
|
|
55a606a3f3 | |
|
|
3120cfc3a5 | |
|
|
727b9a98ec | |
|
|
3aab2c8a37 | |
|
|
37a1ccaadc | |
|
|
858d02f955 | |
|
|
349d3398b2 | |
|
|
f8f83c38d9 | |
|
|
3fa0a8c6ca | |
|
|
f4ab4e4ea4 | |
|
|
f7a1ea2540 | |
|
|
49fbd04644 | |
|
|
426409ed91 | |
|
|
3e833909d1 | |
|
|
1b6211b34b | |
|
|
8520790989 | |
|
|
4975c6a5c1 |
|
|
@ -21,4 +21,9 @@
|
|||
# Go workspace file
|
||||
go.work
|
||||
|
||||
.vscode
|
||||
.vscode
|
||||
.idea
|
||||
# Shield all log files in the log folder
|
||||
/log/
|
||||
# Shield config files in the configs folder
|
||||
/configs/**/*.yaml
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
# ModelRT
|
||||
|
||||
[](http://192.168.46.100:4080/CL-Softwares/modelRT)
|
||||
[](http://192.168.46.100:4080/CL-Softwares/modelRT)
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"sort"
|
||||
"sync"
|
||||
|
||||
constants "modelRT/constant"
|
||||
"modelRT/constants"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -16,11 +16,11 @@ var (
|
|||
|
||||
// Event define alert event struct
|
||||
type Event struct {
|
||||
ComponentID int64
|
||||
AnchorName string
|
||||
Level constants.AlertLevel
|
||||
Message string
|
||||
StartTime int64
|
||||
ComponentUUID string
|
||||
AnchorName string
|
||||
Level constants.AlertLevel
|
||||
Message string
|
||||
StartTime int64
|
||||
}
|
||||
|
||||
// EventManager define store and manager alert event struct
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
constants "modelRT/constant"
|
||||
"modelRT/constants"
|
||||
)
|
||||
|
||||
// AnchorParamListConfig define anchor params list config struct
|
||||
|
|
@ -15,7 +15,7 @@ type AnchorParamListConfig struct {
|
|||
|
||||
// AnchorParamBaseConfig define anchor params base config struct
|
||||
type AnchorParamBaseConfig struct {
|
||||
ComponentID int64 // component表 ID
|
||||
ComponentUUID string // componentUUID
|
||||
AnchorName string // 锚定参量名称
|
||||
CompareValUpperLimit float64 // 比较值上限
|
||||
CompareValLowerLimit float64 // 比较值下限
|
||||
|
|
|
|||
|
|
@ -7,24 +7,31 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// BaseConfig define config stuct of base params config
|
||||
// BaseConfig define config struct of base params config
|
||||
type BaseConfig struct {
|
||||
GridID int64 `mapstructure:"grid_id"`
|
||||
ZoneID int64 `mapstructure:"zone_id"`
|
||||
StationID int64 `mapstructure:"station_id"`
|
||||
}
|
||||
|
||||
// KafkaConfig define config stuct 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"`
|
||||
// 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"`
|
||||
}
|
||||
|
||||
// PostgresConfig define config stuct of postgres config
|
||||
// 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 float32 `mapstructure:"read_message_time_duration"`
|
||||
}
|
||||
|
||||
// PostgresConfig define config struct of postgres config
|
||||
type PostgresConfig struct {
|
||||
Port int `mapstructure:"port"`
|
||||
Host string `mapstructure:"host"`
|
||||
|
|
@ -33,7 +40,7 @@ type PostgresConfig struct {
|
|||
Password string `mapstructure:"password"`
|
||||
}
|
||||
|
||||
// LoggerConfig define config stuct of zap logger config
|
||||
// LoggerConfig define config struct of zap logger config
|
||||
type LoggerConfig struct {
|
||||
Mode string `mapstructure:"mode"`
|
||||
Level string `mapstructure:"level"`
|
||||
|
|
@ -44,7 +51,7 @@ type LoggerConfig struct {
|
|||
Compress bool `mapstructure:"compress"`
|
||||
}
|
||||
|
||||
// RedisConfig define config stuct of redis config
|
||||
// RedisConfig define config struct of redis config
|
||||
type RedisConfig struct {
|
||||
Addr string `mapstructure:"addr"`
|
||||
Password string `mapstructure:"password"`
|
||||
|
|
@ -53,13 +60,13 @@ type RedisConfig struct {
|
|||
Timeout int `mapstructure:"timeout"`
|
||||
}
|
||||
|
||||
// AntsConfig define config stuct of ants pool config
|
||||
// AntsConfig define config struct of ants pool config
|
||||
type AntsConfig struct {
|
||||
ParseConcurrentQuantity int `mapstructure:"parse_concurrent_quantity"` // parse comtrade file concurrent quantity
|
||||
RTDReceiveConcurrentQuantity int `mapstructure:"rtd_receive_concurrent_quantity"` // polling real time data concurrent quantity
|
||||
}
|
||||
|
||||
// DataRTConfig define config stuct of data runtime server api config
|
||||
// DataRTConfig define config struct of data runtime server api config
|
||||
type DataRTConfig struct {
|
||||
Host string `mapstructure:"host"`
|
||||
Port int64 `mapstructure:"port"`
|
||||
|
|
@ -67,9 +74,10 @@ type DataRTConfig struct {
|
|||
Method string `mapstructure:"polling_api_method"`
|
||||
}
|
||||
|
||||
// ModelRTConfig define config stuct of model runtime server
|
||||
// ModelRTConfig define config struct of model runtime server
|
||||
type ModelRTConfig struct {
|
||||
BaseConfig `mapstructure:"base"`
|
||||
ServiceConfig `mapstructure:"service"`
|
||||
PostgresConfig `mapstructure:"postgres"`
|
||||
KafkaConfig `mapstructure:"kafka"`
|
||||
LoggerConfig `mapstructure:"logger"`
|
||||
|
|
|
|||
|
|
@ -1,65 +0,0 @@
|
|||
postgres:
|
||||
host: "192.168.2.103"
|
||||
port: 5432
|
||||
database: "demo"
|
||||
user: "postgres"
|
||||
password: "coslight"
|
||||
|
||||
kafka:
|
||||
servers: "localhost:9092"
|
||||
port: 9092
|
||||
group_id: "modelRT"
|
||||
topic: ""
|
||||
auto_offset_reset: "earliest"
|
||||
enable_auto_commit: "false"
|
||||
read_message_time_duration: ”0.5s"
|
||||
|
||||
# influxdb:
|
||||
# host: "localhost"
|
||||
# port: "8086"
|
||||
# token: "lCuiQ316qlly3iFeoi1EUokPJ0XxW-5lnG-3rXsKaaZSjfuxO5EaZfFdrNGM7Zlrdk1PrN_7TOsM_SCu9Onyew=="
|
||||
# org: "coslight"
|
||||
# bucket: "wave_record"
|
||||
|
||||
# zap logger config
|
||||
logger:
|
||||
mode: "development"
|
||||
level: "debug"
|
||||
filepath: "/home/douxu/log/modelRT-%s.log"
|
||||
maxsize: 1
|
||||
maxbackups: 5
|
||||
maxage: 30
|
||||
compress: false
|
||||
|
||||
# ants config
|
||||
ants:
|
||||
parse_concurrent_quantity: 10
|
||||
rtd_receive_concurrent_quantity: 10
|
||||
|
||||
# redis config
|
||||
locker_redis:
|
||||
addr: "192.168.2.104:6379"
|
||||
password: ""
|
||||
db: 1
|
||||
poolsize: 50
|
||||
timeout: 10
|
||||
|
||||
storage_redis:
|
||||
addr: "192.168.2.104:6379"
|
||||
password: ""
|
||||
db: 0
|
||||
poolsize: 50
|
||||
timeout: 10
|
||||
|
||||
# modelRT base config
|
||||
base:
|
||||
grid_id: 1
|
||||
zone_id: 1
|
||||
station_id: 1
|
||||
|
||||
# dataRT api config
|
||||
dataRT:
|
||||
host: "http://127.0.0.1"
|
||||
port: 8888
|
||||
polling_api: "datart/getPointData"
|
||||
polling_api_method: "GET"
|
||||
|
|
@ -1,21 +0,0 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrUUIDFromCheckT1 define error of check uuid from value failed in uuid from change type
|
||||
ErrUUIDFromCheckT1 = errors.New("in uuid from change type, value of new uuid_from is equal value of old uuid_from")
|
||||
// ErrUUIDToCheckT1 define error of check uuid to value failed in uuid from change type
|
||||
ErrUUIDToCheckT1 = errors.New("in uuid from change type, value of new uuid_to is not equal value of old uuid_to")
|
||||
|
||||
// ErrUUIDFromCheckT2 define error of check uuid from value failed in uuid to change type
|
||||
ErrUUIDFromCheckT2 = errors.New("in uuid to change type, value of new uuid_from is not equal value of old uuid_from")
|
||||
// ErrUUIDToCheckT2 define error of check uuid to value failed in uuid to change type
|
||||
ErrUUIDToCheckT2 = errors.New("in uuid to change type, value of new uuid_to is equal value of old uuid_to")
|
||||
|
||||
// ErrUUIDFromCheckT3 define error of check uuid from value failed in uuid add change type
|
||||
ErrUUIDFromCheckT3 = errors.New("in uuid add change type, value of old uuid_from is not empty")
|
||||
// ErrUUIDToCheckT3 define error of check uuid to value failed in uuid add change type
|
||||
ErrUUIDToCheckT3 = errors.New("in uuid add change type, value of old uuid_to is not empty")
|
||||
)
|
||||
|
|
@ -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,11 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// ShortAttrKeyLenth define short attribute key length
|
||||
ShortAttrKeyLenth int = 4
|
||||
// LongAttrKeyLenth define long attribute key length
|
||||
LongAttrKeyLenth int = 7
|
||||
)
|
||||
|
||||
// component、base_extend、rated、setup、model、stable、bay、craft、integrity、behavior
|
||||
|
|
@ -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"
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
// ErrUUIDFromCheckT1 define error of check uuid from value failed in uuid from change type
|
||||
ErrUUIDFromCheckT1 = errors.New("in uuid from change type, value of new uuid_from is equal value of old uuid_from")
|
||||
// ErrUUIDToCheckT1 define error of check uuid to value failed in uuid from change type
|
||||
ErrUUIDToCheckT1 = errors.New("in uuid from change type, value of new uuid_to is not equal value of old uuid_to")
|
||||
|
||||
// ErrUUIDFromCheckT2 define error of check uuid from value failed in uuid to change type
|
||||
ErrUUIDFromCheckT2 = errors.New("in uuid to change type, value of new uuid_from is not equal value of old uuid_from")
|
||||
// ErrUUIDToCheckT2 define error of check uuid to value failed in uuid to change type
|
||||
ErrUUIDToCheckT2 = errors.New("in uuid to change type, value of new uuid_to is equal value of old uuid_to")
|
||||
|
||||
// ErrUUIDFromCheckT3 define error of check uuid from value failed in uuid add change type
|
||||
ErrUUIDFromCheckT3 = errors.New("in uuid add change type, value of old uuid_from is not empty")
|
||||
// ErrUUIDToCheckT3 define error of check uuid to value failed in uuid add change type
|
||||
ErrUUIDToCheckT3 = errors.New("in uuid add change type, value of old uuid_to is not empty")
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidAddressType define error of invalid io address type
|
||||
ErrInvalidAddressType = errors.New("invalid address type")
|
||||
// ErrUnknownDataType define error of unknown measurement data source type
|
||||
ErrUnknownDataType = errors.New("unknown data type")
|
||||
// ErrExceedsLimitType define error of channel number exceeds limit for telemetry
|
||||
ErrExceedsLimitType = errors.New("channel number exceeds limit for Telemetry")
|
||||
// ErrUnsupportedChannelPrefixType define error of unsupported channel prefix
|
||||
ErrUnsupportedChannelPrefixType = errors.New("unsupported channel prefix")
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrFormatUUID define error of format uuid string to uuid.UUID type failed
|
||||
ErrFormatUUID = errors.New("format string type to uuid.UUID type failed")
|
||||
// ErrFormatCache define error of format cache with any type to cacheItem type failed
|
||||
ErrFormatCache = errors.New("format any teype to cache item type failed")
|
||||
)
|
||||
|
||||
// ErrGetClientToken define error of can not get client_token from context
|
||||
var ErrGetClientToken = errors.New("can not get client_token from context")
|
||||
|
||||
// ErrQueryComponentByUUID define error of query component from db by uuid failed
|
||||
var ErrQueryComponentByUUID = errors.New("query component from db failed by uuid")
|
||||
|
||||
// ErrChanIsNil define error of channel is nil
|
||||
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
|
||||
)
|
||||
|
|
@ -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"
|
||||
)
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// DataSourceTypeCL3611 define CL3611 type
|
||||
DataSourceTypeCL3611 = 1
|
||||
// DataSourceTypePower104 define electricity 104 protocol type
|
||||
DataSourceTypePower104 = 2
|
||||
)
|
||||
|
||||
// channel name prefix
|
||||
const (
|
||||
ChannelPrefixTelemetry = "Telemetry"
|
||||
ChannelPrefixTelesignal = "Telesignal"
|
||||
ChannelPrefixTelecommand = "Telecommand"
|
||||
ChannelPrefixTeleadjusting = "Teleadjusting"
|
||||
ChannelPrefixSetpoints = "Setpoints"
|
||||
)
|
||||
|
||||
// channel name suffix
|
||||
const (
|
||||
ChannelSuffixP = "P"
|
||||
ChannelSuffixQ = "Q"
|
||||
ChannelSuffixS = "S"
|
||||
ChannelSuffixPS = "PS"
|
||||
ChannelSuffixF = "F"
|
||||
ChannelSuffixDeltaF = "deltaF"
|
||||
ChannelSuffixUAB = "UAB"
|
||||
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,7 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
const (
|
||||
// RedisSearchDictName define redis search dictionary name
|
||||
RedisSearchDictName = "search_suggestions_dict"
|
||||
)
|
||||
|
|
@ -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
|
||||
)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
// Package constants define constant variable
|
||||
package constants
|
||||
|
||||
// Assuming the B3 specification
|
||||
const (
|
||||
HeaderTraceID = "X-B3-TraceId"
|
||||
HeaderSpanID = "X-B3-SpanId"
|
||||
HeaderParentSpanID = "X-B3-ParentSpanId"
|
||||
)
|
||||
|
|
@ -4,7 +4,6 @@ package database
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"modelRT/common/errcode"
|
||||
|
|
@ -16,27 +15,25 @@ import (
|
|||
)
|
||||
|
||||
// CreateComponentIntoDB define create component info of the circuit diagram into DB
|
||||
func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentCreateInfo) (int64, error) {
|
||||
func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentCreateInfo) (string, error) {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
globalUUID, err := uuid.FromString(componentInfo.UUID)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
return "", fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
}
|
||||
|
||||
component := orm.Component{
|
||||
GlobalUUID: globalUUID,
|
||||
GridID: strconv.FormatInt(componentInfo.GridID, 10),
|
||||
ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10),
|
||||
StationID: strconv.FormatInt(componentInfo.StationID, 10),
|
||||
PageID: componentInfo.PageID,
|
||||
Tag: componentInfo.Tag,
|
||||
ComponentType: componentInfo.ComponentType,
|
||||
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)
|
||||
|
|
@ -45,7 +42,7 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
|
|||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check insert component slice", errcode.ErrInsertRowUnexpected)
|
||||
}
|
||||
return -1, fmt.Errorf("insert component info failed:%w", err)
|
||||
return "", fmt.Errorf("insert component info failed:%w", err)
|
||||
}
|
||||
return component.ID, nil
|
||||
return component.GlobalUUID.String(), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"modelRT/diagram"
|
||||
"modelRT/model"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ParseAttrToken define return the attribute model interface based on the input attribute token. doc addr http://server.baseware.net:6875/books/product-design-docs/page/d6baf
|
||||
func ParseAttrToken(ctx context.Context, tx *gorm.DB, attrToken, clientToken string) (model.AttrModelInterface, error) {
|
||||
rs := diagram.NewRedisString(ctx, attrToken, clientToken, 10, true)
|
||||
|
||||
attrSlice := strings.Split(attrToken, ".")
|
||||
attrLen := len(attrSlice)
|
||||
if attrLen == 4 {
|
||||
short := &model.ShortAttrInfo{
|
||||
AttrGroupName: attrSlice[2],
|
||||
AttrKey: attrSlice[3],
|
||||
}
|
||||
err := FillingShortAttrModel(ctx, tx, attrSlice, short)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
attrValue, err := rs.Get(attrToken)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
short.AttrValue = attrValue
|
||||
return short, nil
|
||||
} else if attrLen == 7 {
|
||||
long := &model.LongAttrInfo{
|
||||
AttrGroupName: attrSlice[5],
|
||||
AttrKey: attrSlice[6],
|
||||
}
|
||||
err := FillingLongAttrModel(ctx, tx, attrSlice, long)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
attrValue, err := rs.Get(attrToken)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
long.AttrValue = attrValue
|
||||
return long, nil
|
||||
}
|
||||
return nil, errors.New("invalid attribute token format")
|
||||
}
|
||||
|
||||
// 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])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attrModel.ComponentInfo = &component
|
||||
return nil
|
||||
}
|
||||
|
||||
// FillingLongAttrModel define filling long attribute model info
|
||||
func FillingLongAttrModel(ctx context.Context, tx *gorm.DB, attrItems []string, attrModel *model.LongAttrInfo) error {
|
||||
grid, err := QueryGridByTagName(ctx, tx, attrItems[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attrModel.GridInfo = &grid
|
||||
zone, err := QueryZoneByTagName(ctx, tx, attrItems[1])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attrModel.ZoneInfo = &zone
|
||||
station, err := QueryStationByTagName(ctx, tx, attrItems[2])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attrModel.StationInfo = &station
|
||||
component, err := QueryComponentByNSPath(ctx, tx, attrItems[3])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
attrModel.ComponentInfo = &component
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryAttrValueFromRedis define query attribute value from redis by attrKey
|
||||
func QueryAttrValueFromRedis(attrKey string) string {
|
||||
fmt.Println(attrKey)
|
||||
return ""
|
||||
}
|
||||
|
|
@ -3,44 +3,40 @@ package database
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/config"
|
||||
"modelRT/logger"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// QueryCircuitDiagramComponentFromDB return the result of query circuit diagram component info order by page id from postgresDB
|
||||
func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *ants.PoolWithFunc) (map[uuid.UUID]int, error) {
|
||||
var components []orm.Component
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
// func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *ants.PoolWithFunc) (map[uuid.UUID]string, error) {
|
||||
// var components []orm.Component
|
||||
// // ctx超时判断
|
||||
// cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
// defer cancel()
|
||||
|
||||
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&components)
|
||||
if result.Error != nil {
|
||||
logger.Error(ctx, "query circuit diagram component info failed", "error", result.Error)
|
||||
return nil, result.Error
|
||||
}
|
||||
// result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&components)
|
||||
// if result.Error != nil {
|
||||
// logger.Error(ctx, "query circuit diagram component info failed", "error", result.Error)
|
||||
// return nil, result.Error
|
||||
// }
|
||||
|
||||
// TODO 优化componentTypeMap输出
|
||||
componentTypeMap := make(map[uuid.UUID]int, len(components))
|
||||
// componentTypeMap := make(map[uuid.UUID]string, len(components))
|
||||
// for _, component := range components {
|
||||
// pool.Invoke(config.ModelParseConfig{
|
||||
// ComponentInfo: component,
|
||||
// Ctx: ctx,
|
||||
// })
|
||||
|
||||
for _, component := range components {
|
||||
pool.Invoke(config.ModelParseConfig{
|
||||
ComponentInfo: component,
|
||||
Ctx: ctx,
|
||||
})
|
||||
|
||||
componentTypeMap[component.GlobalUUID] = component.ComponentType
|
||||
}
|
||||
return componentTypeMap, nil
|
||||
}
|
||||
// componentTypeMap[component.GlobalUUID] = component.GlobalUUID.String()
|
||||
// }
|
||||
// return componentTypeMap, nil
|
||||
// }
|
||||
|
||||
// QueryComponentByUUID return the result of query circuit diagram component info by uuid from postgresDB
|
||||
func QueryComponentByUUID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm.Component, error) {
|
||||
|
|
@ -48,14 +44,54 @@ func QueryComponentByUUID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm
|
|||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
result := tx.WithContext(cancelCtx).
|
||||
Where("global_uuid = ?", uuid).
|
||||
Clauses(clause.Locking{Strength: "UPDATE"}).
|
||||
First(&component)
|
||||
|
||||
result := tx.WithContext(cancelCtx).Where("global_uuid = ? ", uuid).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&component)
|
||||
if result.Error != nil {
|
||||
return orm.Component{}, result.Error
|
||||
}
|
||||
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
|
||||
|
|
@ -69,3 +105,72 @@ 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) {
|
||||
var component orm.Component
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result := tx.WithContext(cancelCtx).Where("NAME = ? ", nsPath).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&component)
|
||||
if result.Error != nil {
|
||||
return orm.Component{}, result.Error
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// QueryGridByTagName return the result of query circuit diagram grid info by tagName from postgresDB
|
||||
func QueryGridByTagName(ctx context.Context, tx *gorm.DB, tagName string) (orm.Grid, error) {
|
||||
var grid orm.Grid
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result := tx.WithContext(cancelCtx).Where("TAGNAME = ? ", tagName).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&grid)
|
||||
if result.Error != nil {
|
||||
return orm.Grid{}, result.Error
|
||||
}
|
||||
return grid, nil
|
||||
}
|
||||
|
|
@ -0,0 +1,62 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// 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 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 {
|
||||
return orm.Measurement{}, result.Error
|
||||
}
|
||||
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
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// QueryStationByTagName return the result of query circuit diagram Station info by tagName from postgresDB
|
||||
func QueryStationByTagName(ctx context.Context, tx *gorm.DB, tagName string) (orm.Station, error) {
|
||||
var station orm.Station
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result := tx.WithContext(cancelCtx).Where("TAGNAME = ? ", tagName).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&station)
|
||||
if result.Error != nil {
|
||||
return orm.Station{}, result.Error
|
||||
}
|
||||
return station, nil
|
||||
}
|
||||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"fmt"
|
||||
"time"
|
||||
|
||||
constants "modelRT/constant"
|
||||
"modelRT/constants"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/orm"
|
||||
|
|
@ -33,14 +33,14 @@ func QueryTopologic(ctx context.Context, tx *gorm.DB) ([]orm.Topologic, error) {
|
|||
}
|
||||
|
||||
// QueryTopologicFromDB return the result of query topologic info from DB
|
||||
func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB, componentTypeMap map[uuid.UUID]int) (*diagram.MultiBranchTreeNode, error) {
|
||||
func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB) (*diagram.MultiBranchTreeNode, error) {
|
||||
topologicInfos, err := QueryTopologic(ctx, tx)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "query topologic info failed", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tree, err := BuildMultiBranchTree(topologicInfos, componentTypeMap)
|
||||
tree, err := BuildMultiBranchTree(topologicInfos)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "init topologic failed", "error", err)
|
||||
return nil, err
|
||||
|
|
@ -49,17 +49,11 @@ func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB, componentTypeMap map
|
|||
}
|
||||
|
||||
// InitCircuitDiagramTopologic return circuit diagram topologic info from postgres
|
||||
func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMap map[uuid.UUID]int) error {
|
||||
func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic) error {
|
||||
var rootVertex *diagram.MultiBranchTreeNode
|
||||
for _, node := range topologicNodes {
|
||||
if node.UUIDFrom == constants.UUIDNil {
|
||||
// rootVertex = node.UUIDTo
|
||||
var componentType int
|
||||
componentType, ok := componentTypeMap[node.UUIDFrom]
|
||||
if !ok {
|
||||
return fmt.Errorf("can not get component type by uuid: %s", node.UUIDFrom)
|
||||
}
|
||||
rootVertex = diagram.NewMultiBranchTree(node.UUIDFrom, componentType)
|
||||
rootVertex = diagram.NewMultiBranchTree(node.UUIDFrom)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
@ -70,13 +64,7 @@ func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMa
|
|||
|
||||
for _, node := range topologicNodes {
|
||||
if node.UUIDFrom == constants.UUIDNil {
|
||||
var componentType int
|
||||
componentType, ok := componentTypeMap[node.UUIDTo]
|
||||
if !ok {
|
||||
return fmt.Errorf("can not get component type by uuid: %s", node.UUIDTo)
|
||||
}
|
||||
nodeVertex := diagram.NewMultiBranchTree(node.UUIDTo, componentType)
|
||||
|
||||
nodeVertex := diagram.NewMultiBranchTree(node.UUIDTo)
|
||||
rootVertex.AddChild(nodeVertex)
|
||||
}
|
||||
}
|
||||
|
|
@ -91,9 +79,7 @@ func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMa
|
|||
|
||||
// TODO 电流互感器不单独划分间隔,以母线、浇筑母线、变压器为间隔原件
|
||||
func IntervalBoundaryDetermine(uuid uuid.UUID) bool {
|
||||
fmt.Println(uuid)
|
||||
var componentID int64
|
||||
diagram.GetComponentMap(componentID)
|
||||
diagram.GetComponentMap(uuid.String())
|
||||
// TODO 判断 component 的类型是否为间隔
|
||||
// TODO 0xA1B2C3D4,高四位表示可以成为间隔的compoent类型的值为FFFF,普通 component 类型的值为 0000。低四位中前二位表示component的一级类型,例如母线 PT、母联/母分、进线等,低四位中后二位表示一级类型中包含的具体类型,例如母线 PT中包含的电压互感器、隔离开关、接地开关、避雷器、带电显示器等。
|
||||
num := uint32(0xA1B2C3D4) // 八位16进制数
|
||||
|
|
@ -104,23 +90,16 @@ func IntervalBoundaryDetermine(uuid uuid.UUID) bool {
|
|||
}
|
||||
|
||||
// BuildMultiBranchTree return the multi branch tree by topologic info and component type map
|
||||
func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.UUID]int) (*diagram.MultiBranchTreeNode, error) {
|
||||
func BuildMultiBranchTree(topologics []orm.Topologic) (*diagram.MultiBranchTreeNode, error) {
|
||||
nodeMap := make(map[uuid.UUID]*diagram.MultiBranchTreeNode, len(topologics)*2)
|
||||
|
||||
for _, topo := range topologics {
|
||||
if _, exists := nodeMap[topo.UUIDFrom]; !exists {
|
||||
// skip special uuid
|
||||
if topo.UUIDTo != constants.UUIDNil {
|
||||
var ok bool
|
||||
componentType, ok := componentTypeMap[topo.UUIDFrom]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can not get component type by uuid: %s", topo.UUIDFrom)
|
||||
}
|
||||
|
||||
nodeMap[topo.UUIDFrom] = &diagram.MultiBranchTreeNode{
|
||||
ID: topo.UUIDFrom,
|
||||
NodeComponentType: componentType,
|
||||
Children: make([]*diagram.MultiBranchTreeNode, 0),
|
||||
ID: topo.UUIDFrom,
|
||||
Children: make([]*diagram.MultiBranchTreeNode, 0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -128,16 +107,9 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.
|
|||
if _, exists := nodeMap[topo.UUIDTo]; !exists {
|
||||
// skip special uuid
|
||||
if topo.UUIDTo != constants.UUIDNil {
|
||||
var ok bool
|
||||
componentType, ok := componentTypeMap[topo.UUIDTo]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can not get component type by uuid: %s", topo.UUIDTo)
|
||||
}
|
||||
|
||||
nodeMap[topo.UUIDTo] = &diagram.MultiBranchTreeNode{
|
||||
ID: topo.UUIDTo,
|
||||
NodeComponentType: componentType,
|
||||
Children: make([]*diagram.MultiBranchTreeNode, 0),
|
||||
ID: topo.UUIDTo,
|
||||
Children: make([]*diagram.MultiBranchTreeNode, 0),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -146,10 +118,8 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.
|
|||
for _, topo := range topologics {
|
||||
var parent *diagram.MultiBranchTreeNode
|
||||
if topo.UUIDFrom == constants.UUIDNil {
|
||||
var componentType int
|
||||
parent = &diagram.MultiBranchTreeNode{
|
||||
ID: constants.UUIDNil,
|
||||
NodeComponentType: componentType,
|
||||
ID: constants.UUIDNil,
|
||||
}
|
||||
nodeMap[constants.UUIDNil] = parent
|
||||
} else {
|
||||
|
|
@ -158,10 +128,8 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.
|
|||
|
||||
var child *diagram.MultiBranchTreeNode
|
||||
if topo.UUIDTo == constants.UUIDNil {
|
||||
var componentType int
|
||||
child = &diagram.MultiBranchTreeNode{
|
||||
ID: topo.UUIDTo,
|
||||
NodeComponentType: componentType,
|
||||
ID: topo.UUIDTo,
|
||||
}
|
||||
} else {
|
||||
child = nodeMap[topo.UUIDTo]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
// QueryZoneByTagName return the result of query circuit diagram Zone info by tagName from postgresDB
|
||||
func QueryZoneByTagName(ctx context.Context, tx *gorm.DB, tagName string) (orm.Zone, error) {
|
||||
var zone orm.Zone
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result := tx.WithContext(cancelCtx).Where("TAGNAME = ? ", tagName).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&zone)
|
||||
if result.Error != nil {
|
||||
return orm.Zone{}, result.Error
|
||||
}
|
||||
return zone, nil
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ package database
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"modelRT/common/errcode"
|
||||
|
|
@ -16,13 +15,13 @@ import (
|
|||
)
|
||||
|
||||
// UpdateComponentIntoDB define update component info of the circuit diagram into DB
|
||||
func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentUpdateInfo) (int64, error) {
|
||||
func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentUpdateInfo) (string, error) {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
globalUUID, err := uuid.FromString(componentInfo.UUID)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
return "", fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
}
|
||||
|
||||
var component orm.Component
|
||||
|
|
@ -32,31 +31,29 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
|
|||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check update component conditions", errcode.ErrUpdateRowZero)
|
||||
}
|
||||
return -1, fmt.Errorf("query component info failed:%w", err)
|
||||
return "", fmt.Errorf("query component info failed:%w", err)
|
||||
}
|
||||
|
||||
updateParams := orm.Component{
|
||||
GlobalUUID: globalUUID,
|
||||
GridID: strconv.FormatInt(componentInfo.GridID, 10),
|
||||
ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10),
|
||||
StationID: strconv.FormatInt(componentInfo.StationID, 10),
|
||||
PageID: componentInfo.PageID,
|
||||
Tag: componentInfo.Tag,
|
||||
ComponentType: componentInfo.ComponentType,
|
||||
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("id = ?", component.ID).Updates(&updateParams)
|
||||
result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("GLOBAL_UUID = ?", component.GlobalUUID).Updates(&updateParams)
|
||||
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check update component conditions", errcode.ErrUpdateRowZero)
|
||||
}
|
||||
return -1, fmt.Errorf("update component info failed:%w", err)
|
||||
return "", fmt.Errorf("update component info failed:%w", err)
|
||||
}
|
||||
return component.ID, nil
|
||||
return component.GlobalUUID.String(), nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import (
|
|||
"time"
|
||||
|
||||
"modelRT/common/errcode"
|
||||
constants "modelRT/constant"
|
||||
"modelRT/constants"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,351 @@
|
|||
# 项目依赖服务部署指南
|
||||
|
||||
本项目依赖于 $\text{PostgreSQL}$ 数据库和 $\text{Redis Stack Server}$(包含 $\text{Redisearch}$ 等模块)部署文档将使用 $\text{Docker}$ 容器化技术部署这两个依赖服务
|
||||
|
||||
## 前提条件
|
||||
|
||||
1. 已安装 $\text{Docker}$
|
||||
2. 下载相关容器镜像
|
||||
3. 确保主机的 $\text{5432}$ 端口($\text{Postgres}$)和 $\text{6379}$ 端口($\text{Redis}$)未被占用
|
||||
|
||||
### 1\. 部署 PostgreSQL 数据库
|
||||
|
||||
使用官方的 `postgres:13.16` 镜像,并设置默认的用户、密码和端口
|
||||
|
||||
#### 1.1 部署命令
|
||||
|
||||
运行以下命令启动 $\text{PostgreSQL}$ 容器
|
||||
|
||||
```bash
|
||||
docker run --name postgres \
|
||||
-e POSTGRES_USER=postgres \
|
||||
-e POSTGRES_PASSWORD=coslight \
|
||||
-p 5432:5432 \
|
||||
-d postgres:13.16
|
||||
```
|
||||
|
||||
#### 1.2 连接信息
|
||||
|
||||
| 参数 | 值 | 说明 |
|
||||
| :--- | :--- | :--- |
|
||||
| **容器名称** | `postgres` | 容器名 |
|
||||
| **镜像版本** | `postgres:13.16` | 镜像名 |
|
||||
| **主机端口** | `5432` | 外部应用连接使用的端口 |
|
||||
| **用户名** | `postgres` | 默认超级用户 |
|
||||
| **密码** | `coslight` | 配置的密码 |
|
||||
|
||||
#### 1.3 状态检查
|
||||
|
||||
要确认容器是否正在运行,请执行
|
||||
|
||||
```bash
|
||||
# 检查容器启动状态
|
||||
docker ps -a |grep postgres
|
||||
# 检查容器启动日志信息
|
||||
docker logs postgres
|
||||
```
|
||||
|
||||
### 2\. 部署 Redis Stack Server
|
||||
|
||||
我们将使用 `redis/redis-stack-server:latest` 镜像该镜像内置了 $\text{Redisearch}$ 模块,用于 $\text{ModelRT}$ 项目中补全功能
|
||||
|
||||
#### 2.1 部署命令
|
||||
|
||||
运行以下命令启动 $\text{Redis Stack Server}$ 容器
|
||||
|
||||
```bash
|
||||
docker run --name redis -p 6379:6379 \
|
||||
-d redis/redis-stack-server:latest
|
||||
```
|
||||
|
||||
#### 2.2 连接信息
|
||||
|
||||
| 参数 | 值 | 说明 |
|
||||
| :--- | :--- | :--- |
|
||||
| **容器名称** | `redis` | 容器名 |
|
||||
| **镜像版本** | `redis/redis-stack-server:latest` | 镜像名 |
|
||||
| **主机端口** | `6379` | 外部应用连接使用的端口 |
|
||||
| **地址** | `localhost:6379` | |
|
||||
| **密码** | **无** | 默认未设置密码 |
|
||||
|
||||
> **注意:** 生产环境中建议使用 `-e REDIS_PASSWORD=<your_secure_password>` 参数来设置 $\text{Redis}$ 访问密码
|
||||
|
||||
#### 2.3 状态检查
|
||||
|
||||
要确认容器是否正在运行,请执行
|
||||
|
||||
```bash
|
||||
# 检查容器启动状态
|
||||
docker ps -a |grep redis
|
||||
# 检查容器启动日志信息
|
||||
docker logs redis
|
||||
```
|
||||
|
||||
#### 2.4 数据注入
|
||||
|
||||
测试数据注入
|
||||
|
||||
##### 2.4.1 Postgres数据注入
|
||||
|
||||
```SQL
|
||||
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数据脚本
|
||||
|
||||
```shell
|
||||
deploy/redis-test-data/measurments-recommend/measurement_injection.go
|
||||
```
|
||||
|
||||
运行脚本向 Reids 导入数据
|
||||
|
||||
```shell
|
||||
go run deploy/redis-test-data/measurments-recommend/measurement_injection.go
|
||||
```
|
||||
|
||||
### 3\. 启动 ModelRT 服务
|
||||
|
||||
#### 3.1 配置服务配置文件
|
||||
|
||||
以下表格为配置文件参数说明表
|
||||
|
||||
| 类别 | 参数名 | 作用描述 | 示例值 |
|
||||
| :--- | :--- | :--- | :--- |
|
||||
| **Postgres** | `host` | PostgreSQL 数据库服务器的 $\text{IP}$ 地址或域名。 | `"192.168.1.101"` |
|
||||
| | `port` | PostgreSQL 数据库服务器的端口号。 | `5432` |
|
||||
| | `database` | 连接的数据库名称。 | `"demo"` |
|
||||
| | `user` | 连接数据库所使用的用户名。 | `"postgres"` |
|
||||
| | `password` | 连接数据库所使用的密码。 | `"coslight"` |
|
||||
| **Kafka** | `servers` | Kafka 集群的 $\text{Bootstrap Server}$ 地址列表(通常是 $\text{host:port}$ 形式,多个地址用逗号分隔)。 | `"localhost:9092"` |
|
||||
| | `port` | Kafka 服务器的端口号。 | `9092` |
|
||||
| | `group_id` | 消费者组 $\text{ID}$,用于标识和管理一组相关的消费者。 | `"modelRT"` |
|
||||
| | `topic` | Kafka 消息的主题名称。 | `""` |
|
||||
| | `auto_offset_reset` | 消费者首次启动或 $\text{Offset}$ 无效时,从哪个位置开始消费(如 `earliest` 或 `latest`)。 | `"earliest"` |
|
||||
| | `enable_auto_commit` | 是否自动提交 $\text{Offset}$。设为 $\text{false}$ 通常用于手动控制 $\text{Offset}$ 提交。 | `"false"` |
|
||||
| | `read_message_time_duration` | 读取消息时的超时或等待时间。 | `”0.5s"` |
|
||||
| **Logger (Zap)** | `mode` | 日志模式,通常为 `development`(开发)或 `production`(生产)。影响日志格式。 | `"development"` |
|
||||
| | `level` | 最低日志级别(如 $\text{debug, info, warn, error}$)。 | `"debug"` |
|
||||
| | `filepath` | 日志文件的输出路径和名称格式(`%s` 会被替换为日期等)。 | `"/Users/douxu/Workspace/coslight/modelRT/modelRT-%s.log"` |
|
||||
| | `maxsize` | 单个日志文件最大大小(单位:$\text{MB}$)。 | `1` |
|
||||
| | `maxbackups` | 保留旧日志文件的最大个数。 | `5` |
|
||||
| | `maxage` | 保留旧日志文件的最大天数。 | `30` |
|
||||
| | `compress` | 是否压缩备份的日志文件。 | `false` |
|
||||
| **Ants Pool** | `parse_concurrent_quantity` | 用于解析任务的协程池最大并发数量。 | `10` |
|
||||
| | `rtd_receive_concurrent_quantity` | 用于实时数据接收任务的协程池最大并发数量。 | `10` |
|
||||
| **Locker Redis** | `addr` | 分布式锁服务所使用的 $\text{Redis}$ 地址。 | `"127.0.0.1:6379"` |
|
||||
| | `password` | $\text{Locker Redis}$ 的密码。 | `""` |
|
||||
| | `db` | $\text{Locker Redis}$ 使用的数据库编号。 | `1` |
|
||||
| | `poolsize` | $\text{Locker Redis}$ 连接池的最大连接数。 | `50` |
|
||||
| | `timeout` | $\text{Locker Redis}$ 连接操作的超时时间(单位:毫秒)。 | `10` |
|
||||
| **Storage Redis** | `addr` | 数据存储服务所使用的 $\text{Redis}$ 地址(例如 $\text{Redisearch}$)。 | `"127.0.0.1:6379"` |
|
||||
| | `password` | $\text{Storage Redis}$ 的密码。 | `""` |
|
||||
| | `db` | $\text{Storage Redis}$ 使用的数据库编号。 | `0` |
|
||||
| | `poolsize` | $\text{Storage Redis}$ 连接池的最大连接数。 | `50` |
|
||||
| | `timeout` | $\text{Storage Redis}$ 连接操作的超时时间(单位:毫秒)。 | `10` |
|
||||
| **Base Config** | `grid_id` | 项目所操作的默认电网 $\text{ID}$。 | `1` |
|
||||
| | `zone_id` | 项目所操作的默认区域 $\text{ID}$。 | `1` |
|
||||
| | `station_id` | 项目所操作的默认变电站 $\text{ID}$。 | `1` |
|
||||
| **Service Config** | `service_name` | 服务名称,用于日志、监控等标识。 | `"modelRT"` |
|
||||
| | `secret_key` | 服务内部使用的秘钥,用于签名或认证。 | `"modelrt_key"` |
|
||||
| **DataRT API** | `host` | 外部 $\text{DataRT}$ 服务的主机地址。 | `"http://127.0.0.1"` |
|
||||
| | `port` | $\text{DataRT}$ 服务的端口号。 | `8888` |
|
||||
| | `polling_api` | 轮询数据的 $\text{API}$ 路径。 | `"datart/getPointData"` |
|
||||
| | `polling_api_method` | 调用该 $\text{API}$ 使用的 $\text{HTTP}$ 方法。 | `"GET"` |
|
||||
|
||||
#### 3.2 编译 ModelRT 服务
|
||||
|
||||
```bash
|
||||
go build -o model-rt main.go
|
||||
```
|
||||
|
||||
#### 3.3 启动服务
|
||||
|
||||
使用编译好的二进制文件进行启动
|
||||
|
||||
```bash
|
||||
./model-rt
|
||||
```
|
||||
|
||||
#### 3.4 检测服务启动日志
|
||||
|
||||
在发现控制台输出如下信息`starting ModelRT server`
|
||||
后即代表服务启动成功
|
||||
|
||||
### 4\. 后续操作(停止与清理)
|
||||
|
||||
#### 4.1 停止容器
|
||||
|
||||
```bash
|
||||
docker stop postgres redis
|
||||
```
|
||||
|
||||
#### 4.2 删除容器(删除后数据将丢失)
|
||||
|
||||
```bash
|
||||
docker rm postgres redis
|
||||
```
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -10,10 +10,10 @@ import (
|
|||
var anchorValueOverview sync.Map
|
||||
|
||||
// GetAnchorValue define func of get circuit diagram data by componentID
|
||||
func GetAnchorValue(componentID int64) (string, error) {
|
||||
value, ok := diagramsOverview.Load(componentID)
|
||||
func GetAnchorValue(componentUUID string) (string, error) {
|
||||
value, ok := diagramsOverview.Load(componentUUID)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("can not find anchor value by componentID:%d", componentID)
|
||||
return "", fmt.Errorf("can not find anchor value by componentUUID:%s", componentUUID)
|
||||
}
|
||||
anchorValue, ok := value.(string)
|
||||
if !ok {
|
||||
|
|
@ -22,20 +22,20 @@ func GetAnchorValue(componentID int64) (string, error) {
|
|||
return anchorValue, nil
|
||||
}
|
||||
|
||||
// UpdateAnchorValue define func of update anchor value by componentID and anchor name
|
||||
func UpdateAnchorValue(componentID int64, anchorValue string) bool {
|
||||
_, result := anchorValueOverview.Swap(componentID, anchorValue)
|
||||
// UpdateAnchorValue define func of update anchor value by componentUUID and anchor name
|
||||
func UpdateAnchorValue(componentUUID string, anchorValue string) bool {
|
||||
_, result := anchorValueOverview.Swap(componentUUID, anchorValue)
|
||||
return result
|
||||
}
|
||||
|
||||
// StoreAnchorValue define func of store anchor value with componentID and anchor name
|
||||
func StoreAnchorValue(componentID int64, anchorValue string) {
|
||||
anchorValueOverview.Store(componentID, anchorValue)
|
||||
// StoreAnchorValue define func of store anchor value with componentUUID and anchor name
|
||||
func StoreAnchorValue(componentUUID string, anchorValue string) {
|
||||
anchorValueOverview.Store(componentUUID, anchorValue)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteAnchorValue define func of delete anchor value with componentID
|
||||
func DeleteAnchorValue(componentID int64) {
|
||||
anchorValueOverview.Delete(componentID)
|
||||
// DeleteAnchorValue define func of delete anchor value with componentUUID
|
||||
func DeleteAnchorValue(componentUUID string) {
|
||||
anchorValueOverview.Delete(componentUUID)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,38 +4,40 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"modelRT/orm"
|
||||
)
|
||||
|
||||
// diagramsOverview define struct of storage all circuit diagram data
|
||||
var diagramsOverview sync.Map
|
||||
|
||||
// GetComponentMap define func of get circuit diagram data by component id
|
||||
func GetComponentMap(componentID int64) (map[string]interface{}, error) {
|
||||
value, ok := diagramsOverview.Load(componentID)
|
||||
// GetComponentMap define func of get circuit diagram data by component uuid
|
||||
func GetComponentMap(componentUUID string) (*orm.Component, error) {
|
||||
value, ok := diagramsOverview.Load(componentUUID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can not find graph by global uuid:%d", componentID)
|
||||
return nil, fmt.Errorf("can not find graph by global uuid:%s", componentUUID)
|
||||
}
|
||||
paramsMap, ok := value.(map[string]interface{})
|
||||
componentInfo, ok := value.(*orm.Component)
|
||||
if !ok {
|
||||
return nil, errors.New("convert to component map struct failed")
|
||||
}
|
||||
return paramsMap, nil
|
||||
return componentInfo, nil
|
||||
}
|
||||
|
||||
// UpdateComponentMap define func of update circuit diagram data by component id and component info
|
||||
func UpdateComponentMap(componentID int64, componentInfo map[string]interface{}) bool {
|
||||
// UpdateComponentMap define func of update circuit diagram data by component uuid and component info
|
||||
func UpdateComponentMap(componentID int64, componentInfo *orm.Component) bool {
|
||||
_, result := diagramsOverview.Swap(componentID, componentInfo)
|
||||
return result
|
||||
}
|
||||
|
||||
// StoreComponentMap define func of store circuit diagram data with component id and component info
|
||||
func StoreComponentMap(componentID int64, componentInfo map[string]interface{}) {
|
||||
diagramsOverview.Store(componentID, componentInfo)
|
||||
// StoreComponentMap define func of store circuit diagram data with component uuid and component info
|
||||
func StoreComponentMap(componentUUID string, componentInfo *orm.Component) {
|
||||
diagramsOverview.Store(componentUUID, componentInfo)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteComponentMap define func of delete circuit diagram data with component id
|
||||
func DeleteComponentMap(componentID int64) {
|
||||
diagramsOverview.Delete(componentID)
|
||||
// DeleteComponentMap define func of delete circuit diagram data with component uuid
|
||||
func DeleteComponentMap(componentUUID string) {
|
||||
diagramsOverview.Delete(componentUUID)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import (
|
|||
"fmt"
|
||||
"sync"
|
||||
|
||||
constants "modelRT/constant"
|
||||
"modelRT/constants"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
|
|
|
|||
|
|
@ -10,17 +10,15 @@ var GlobalTree *MultiBranchTreeNode
|
|||
|
||||
// MultiBranchTreeNode represents a topological structure using an multi branch tree
|
||||
type MultiBranchTreeNode struct {
|
||||
ID uuid.UUID // 节点唯一标识
|
||||
NodeComponentType int // 节点组件类型
|
||||
Parent *MultiBranchTreeNode // 指向父节点的指针
|
||||
Children []*MultiBranchTreeNode // 指向所有子节点的指针切片
|
||||
ID uuid.UUID // 节点唯一标识
|
||||
Parent *MultiBranchTreeNode // 指向父节点的指针
|
||||
Children []*MultiBranchTreeNode // 指向所有子节点的指针切片
|
||||
}
|
||||
|
||||
func NewMultiBranchTree(id uuid.UUID, componentType int) *MultiBranchTreeNode {
|
||||
func NewMultiBranchTree(id uuid.UUID) *MultiBranchTreeNode {
|
||||
return &MultiBranchTreeNode{
|
||||
ID: id,
|
||||
NodeComponentType: componentType,
|
||||
Children: make([]*MultiBranchTreeNode, 0),
|
||||
ID: id,
|
||||
Children: make([]*MultiBranchTreeNode, 0),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -58,7 +56,7 @@ func (n *MultiBranchTreeNode) PrintTree(level int) {
|
|||
fmt.Print(" ")
|
||||
}
|
||||
|
||||
fmt.Printf("- ComponentType:%d,(ID: %s)\n", n.NodeComponentType, n.ID)
|
||||
fmt.Printf("-ID: %s\n", n.ID)
|
||||
|
||||
for _, child := range n.Children {
|
||||
child.PrintTree(level + 1)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ func initClient(rCfg config.RedisConfig) *redis.Client {
|
|||
return client
|
||||
}
|
||||
|
||||
// InitClientInstance define func of return instance of redis client
|
||||
func InitClientInstance(rCfg config.RedisConfig) *redis.Client {
|
||||
// InitRedisClientInstance define func of return instance of redis client
|
||||
func InitRedisClientInstance(rCfg config.RedisConfig) *redis.Client {
|
||||
once.Do(func() {
|
||||
_globalStorageClient = initClient(rCfg)
|
||||
})
|
||||
|
|
|
|||
|
|
@ -14,50 +14,53 @@ 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
|
||||
}
|
||||
|
||||
// NewRedisSet define func of new redis set instance
|
||||
func NewRedisSet(ctx context.Context, hashKey string, token string, lockLeaseTime uint64, needRefresh bool) *RedisSet {
|
||||
func NewRedisSet(ctx context.Context, setKey string, lockLeaseTime uint64, needRefresh bool) *RedisSet {
|
||||
token := ctx.Value("client_token").(string)
|
||||
return &RedisSet{
|
||||
ctx: ctx,
|
||||
rwLocker: locker.InitRWLocker(hashKey, token, lockLeaseTime, needRefresh),
|
||||
key: setKey,
|
||||
rwLocker: locker.InitRWLocker(setKey, token, lockLeaseTime, needRefresh),
|
||||
storageClient: GetRedisClientInstance(),
|
||||
logger: logger.GetLoggerInstance(),
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
|
@ -65,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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,114 @@
|
|||
package diagram
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
locker "modelRT/distributedlock"
|
||||
"modelRT/logger"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// RedisString defines the encapsulation struct of redis string type
|
||||
type RedisString struct {
|
||||
ctx context.Context
|
||||
rwLocker *locker.RedissionRWLocker
|
||||
storageClient *redis.Client
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewRedisString define func of new redis string instance
|
||||
func NewRedisString(ctx context.Context, stringKey string, token string, lockLeaseTime uint64, needRefresh bool) *RedisString {
|
||||
return &RedisString{
|
||||
ctx: ctx,
|
||||
rwLocker: locker.InitRWLocker(stringKey, token, lockLeaseTime, needRefresh),
|
||||
storageClient: GetRedisClientInstance(),
|
||||
logger: logger.GetLoggerInstance(),
|
||||
}
|
||||
}
|
||||
|
||||
// Get define func of get the value of key
|
||||
func (rs *RedisString) Get(stringKey string) (string, error) {
|
||||
err := rs.rwLocker.RLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock rLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||
return "", err
|
||||
}
|
||||
defer rs.rwLocker.UnRLock(rs.ctx)
|
||||
|
||||
value, err := rs.storageClient.Get(rs.ctx, stringKey).Result()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "get string value by key failed", "string_key", stringKey, "error", err)
|
||||
return "", err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// Set define func of set the value of key
|
||||
func (rs *RedisString) Set(stringKey string, value interface{}) error {
|
||||
err := rs.rwLocker.WLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock wLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||
|
||||
err = rs.storageClient.Set(rs.ctx, stringKey, value, redis.KeepTTL).Err()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "get string value by key failed", "string_key", stringKey, "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Incr define func of increments the number stored at key by one
|
||||
func (rs *RedisString) Incr(stringKey string) error {
|
||||
err := rs.rwLocker.WLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock wLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||
|
||||
err = rs.storageClient.Incr(rs.ctx, stringKey).Err()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "incr the number stored at key by one failed", "string_key", stringKey, "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// IncrBy define func of increments the number stored at key by increment
|
||||
func (rs *RedisString) IncrBy(stringKey string, value int64) error {
|
||||
err := rs.rwLocker.WLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock wLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||
|
||||
err = rs.storageClient.IncrBy(rs.ctx, stringKey, value).Err()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "incr the number stored at key by increment", "string_key", stringKey, "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GETDEL define func of get the value of key and delete the key
|
||||
func (rs *RedisString) GETDEL(stringKey string) error {
|
||||
err := rs.rwLocker.WLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock wLock by stringKey failed", "string_key", stringKey, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||
|
||||
err = rs.storageClient.GetDel(rs.ctx, stringKey).Err()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "del the key failed", "string_key", stringKey, "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,124 @@
|
|||
// Package diagram provide diagram data structure and operation
|
||||
package diagram
|
||||
|
||||
import (
|
||||
"context"
|
||||
"iter"
|
||||
"maps"
|
||||
|
||||
locker "modelRT/distributedlock"
|
||||
"modelRT/logger"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
// RedisZSet defines the encapsulation struct of redis zset type
|
||||
type RedisZSet struct {
|
||||
ctx context.Context
|
||||
rwLocker *locker.RedissionRWLocker
|
||||
storageClient *redis.Client
|
||||
}
|
||||
|
||||
// NewRedisZSet define func of new redis zset instance
|
||||
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),
|
||||
storageClient: GetRedisClientInstance(),
|
||||
}
|
||||
}
|
||||
|
||||
// ZADD define func of add redis zset by members
|
||||
func (rs *RedisZSet) ZADD(setKey string, score float64, member interface{}) error {
|
||||
err := rs.rwLocker.WLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock wLock by setKey failed", "set_key", setKey, "error", err)
|
||||
return err
|
||||
}
|
||||
defer rs.rwLocker.UnWLock(rs.ctx)
|
||||
|
||||
err = rs.storageClient.ZAdd(rs.ctx, setKey, redis.Z{Score: score, Member: member}).Err()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "add set by score and memebers failed", "set_key", setKey, "members", member, "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ZRANGE define func of returns the specified range of elements in the sorted set stored by key
|
||||
func (rs *RedisZSet) ZRANGE(setKey string, start, stop int64) ([]string, error) {
|
||||
var results []string
|
||||
|
||||
err := rs.rwLocker.RLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "lock RLock by setKey failed", "set_key", setKey, "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
err = rs.rwLocker.UnRLock(rs.ctx)
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "unlock RLock by setKey failed", "set_key", setKey, "error", err)
|
||||
}
|
||||
}()
|
||||
|
||||
results, err = rs.storageClient.ZRange(rs.ctx, setKey, start, stop).Result()
|
||||
if err != nil {
|
||||
logger.Error(rs.ctx, "range set by key failed", "set_key", setKey, "start", start, "stop", stop, "error", err)
|
||||
return nil, err
|
||||
}
|
||||
return results, nil
|
||||
}
|
||||
|
||||
type Comparer[T any] interface {
|
||||
Compare(T) int
|
||||
}
|
||||
|
||||
type ComparableComparer[T any] interface {
|
||||
Compare(T) int
|
||||
comparable // 直接嵌入 comparable 约束
|
||||
}
|
||||
|
||||
type methodNode[E Comparer[E]] struct {
|
||||
value E
|
||||
left *methodNode[E]
|
||||
right *methodNode[E]
|
||||
}
|
||||
|
||||
type MethodTree[E Comparer[E]] struct {
|
||||
root *methodNode[E]
|
||||
}
|
||||
|
||||
type OrderedSet[E interface {
|
||||
comparable
|
||||
Comparer[E]
|
||||
}] struct {
|
||||
tree MethodTree[E]
|
||||
elements map[E]bool
|
||||
}
|
||||
|
||||
type ComparableOrderedSet[E ComparableComparer[E]] struct {
|
||||
tree MethodTree[E]
|
||||
elements map[E]bool
|
||||
}
|
||||
|
||||
type Set[E any] interface {
|
||||
Insert(E)
|
||||
Delete(E)
|
||||
Has(E) bool
|
||||
All() iter.Seq[E]
|
||||
}
|
||||
|
||||
func InsertAll[E any](set Set[E], seq iter.Seq[E]) {
|
||||
for v := range seq {
|
||||
set.Insert(v)
|
||||
}
|
||||
}
|
||||
|
||||
type HashSet[E comparable] map[E]bool
|
||||
|
||||
func (s HashSet[E]) Insert(v E) { s[v] = true }
|
||||
func (s HashSet[E]) Delete(v E) { delete(s, v) }
|
||||
func (s HashSet[E]) Has(v E) bool { return s[v] }
|
||||
func (s HashSet[E]) All() iter.Seq[E] { return maps.Keys(s) }
|
||||
231
docs/docs.go
231
docs/docs.go
|
|
@ -9,12 +9,136 @@ const docTemplate = `{
|
|||
"info": {
|
||||
"description": "{{escape .Description}}",
|
||||
"title": "{{.Title}}",
|
||||
"contact": {},
|
||||
"contact": {
|
||||
"name": "douxu",
|
||||
"url": "http://www.swagger.io/support",
|
||||
"email": "douxu@clea.com.cn"
|
||||
},
|
||||
"license": {
|
||||
"name": "Apache 2.0",
|
||||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
|
||||
},
|
||||
"version": "{{.Version}}"
|
||||
},
|
||||
"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 中查询可能的测量点或结构路径,并提供推荐列表。",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"Measurement Recommend"
|
||||
],
|
||||
"summary": "测量点推荐(搜索框自动补全)",
|
||||
"parameters": [
|
||||
{
|
||||
"description": "查询输入参数,例如 'trans' 或 'transformfeeder1_220.'",
|
||||
"name": "request",
|
||||
"in": "body",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"$ref": "#/definitions/network.MeasurementRecommendRequest"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "返回推荐列表成功",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/network.SuccessResponse"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"payload": {
|
||||
"$ref": "#/definitions/network.MeasurementRecommendPayload"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "返回推荐列表失败",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/network.FailureResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/model/diagram_load/{page_id}": {
|
||||
"get": {
|
||||
"description": "load circuit diagram info by page id",
|
||||
|
|
@ -55,56 +179,77 @@ const docTemplate = `{
|
|||
}
|
||||
},
|
||||
"definitions": {
|
||||
"network.FailResponseHeader": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"err_msg": {
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"type": "integer",
|
||||
"example": 400
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.FailureResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"header": {
|
||||
"$ref": "#/definitions/network.FailResponseHeader"
|
||||
"code": {
|
||||
"type": "integer",
|
||||
"example": 500
|
||||
},
|
||||
"msg": {
|
||||
"type": "string",
|
||||
"example": "failed to get recommend data from redis"
|
||||
},
|
||||
"payload": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.MeasurementRecommendPayload": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"input": {
|
||||
"type": "string",
|
||||
"example": "transformfeeder1_220."
|
||||
},
|
||||
"offset": {
|
||||
"type": "integer",
|
||||
"example": 21
|
||||
},
|
||||
"recommended_list": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"example": [
|
||||
"[\"I_A_rms\"",
|
||||
" \"I_B_rms\"",
|
||||
"\"I_C_rms\"]"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.MeasurementRecommendRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"input": {
|
||||
"type": "string",
|
||||
"example": "trans"
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.RealTimeDataPayload": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sub_pos": {
|
||||
"description": "TODO 增加example tag",
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.SuccessResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"header": {
|
||||
"$ref": "#/definitions/network.SuccessResponseHeader"
|
||||
},
|
||||
"payload": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
},
|
||||
"example": {
|
||||
"key": "value"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.SuccessResponseHeader": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"err_msg": {
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"code": {
|
||||
"type": "integer",
|
||||
"example": 200
|
||||
},
|
||||
"msg": {
|
||||
"type": "string",
|
||||
"example": "success"
|
||||
},
|
||||
"payload": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -113,12 +258,12 @@ const docTemplate = `{
|
|||
|
||||
// SwaggerInfo holds exported Swagger Info so clients can modify it
|
||||
var SwaggerInfo = &swag.Spec{
|
||||
Version: "",
|
||||
Host: "",
|
||||
BasePath: "",
|
||||
Version: "1.0",
|
||||
Host: "localhost:8080",
|
||||
BasePath: "/api/v1",
|
||||
Schemes: []string{},
|
||||
Title: "",
|
||||
Description: "",
|
||||
Title: "ModelRT 实时模型服务 API 文档",
|
||||
Description: "实时数据计算和模型运行服务的 API 服务",
|
||||
InfoInstanceName: "swagger",
|
||||
SwaggerTemplate: docTemplate,
|
||||
LeftDelim: "{{",
|
||||
|
|
|
|||
|
|
@ -1,9 +1,138 @@
|
|||
{
|
||||
"swagger": "2.0",
|
||||
"info": {
|
||||
"contact": {}
|
||||
"description": "实时数据计算和模型运行服务的 API 服务",
|
||||
"title": "ModelRT 实时模型服务 API 文档",
|
||||
"contact": {
|
||||
"name": "douxu",
|
||||
"url": "http://www.swagger.io/support",
|
||||
"email": "douxu@clea.com.cn"
|
||||
},
|
||||
"license": {
|
||||
"name": "Apache 2.0",
|
||||
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
|
||||
},
|
||||
"version": "1.0"
|
||||
},
|
||||
"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 中查询可能的测量点或结构路径,并提供推荐列表。",
|
||||
"consumes": [
|
||||
"application/json"
|
||||
],
|
||||
"produces": [
|
||||
"application/json"
|
||||
],
|
||||
"tags": [
|
||||
"Measurement Recommend"
|
||||
],
|
||||
"summary": "测量点推荐(搜索框自动补全)",
|
||||
"parameters": [
|
||||
{
|
||||
"description": "查询输入参数,例如 'trans' 或 'transformfeeder1_220.'",
|
||||
"name": "request",
|
||||
"in": "body",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"$ref": "#/definitions/network.MeasurementRecommendRequest"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "返回推荐列表成功",
|
||||
"schema": {
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/network.SuccessResponse"
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"payload": {
|
||||
"$ref": "#/definitions/network.MeasurementRecommendPayload"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "返回推荐列表失败",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/network.FailureResponse"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/model/diagram_load/{page_id}": {
|
||||
"get": {
|
||||
"description": "load circuit diagram info by page id",
|
||||
|
|
@ -44,56 +173,77 @@
|
|||
}
|
||||
},
|
||||
"definitions": {
|
||||
"network.FailResponseHeader": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"err_msg": {
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"type": "integer",
|
||||
"example": 400
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.FailureResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"header": {
|
||||
"$ref": "#/definitions/network.FailResponseHeader"
|
||||
"code": {
|
||||
"type": "integer",
|
||||
"example": 500
|
||||
},
|
||||
"msg": {
|
||||
"type": "string",
|
||||
"example": "failed to get recommend data from redis"
|
||||
},
|
||||
"payload": {
|
||||
"type": "object",
|
||||
"additionalProperties": true
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.MeasurementRecommendPayload": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"input": {
|
||||
"type": "string",
|
||||
"example": "transformfeeder1_220."
|
||||
},
|
||||
"offset": {
|
||||
"type": "integer",
|
||||
"example": 21
|
||||
},
|
||||
"recommended_list": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
},
|
||||
"example": [
|
||||
"[\"I_A_rms\"",
|
||||
" \"I_B_rms\"",
|
||||
"\"I_C_rms\"]"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.MeasurementRecommendRequest": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"input": {
|
||||
"type": "string",
|
||||
"example": "trans"
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.RealTimeDataPayload": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"sub_pos": {
|
||||
"description": "TODO 增加example tag",
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.SuccessResponse": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"header": {
|
||||
"$ref": "#/definitions/network.SuccessResponseHeader"
|
||||
},
|
||||
"payload": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "string"
|
||||
},
|
||||
"example": {
|
||||
"key": "value"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"network.SuccessResponseHeader": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"err_msg": {
|
||||
"type": "string"
|
||||
},
|
||||
"status": {
|
||||
"code": {
|
||||
"type": "integer",
|
||||
"example": 200
|
||||
},
|
||||
"msg": {
|
||||
"type": "string",
|
||||
"example": "success"
|
||||
},
|
||||
"payload": {
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,42 +1,140 @@
|
|||
basePath: /api/v1
|
||||
definitions:
|
||||
network.FailResponseHeader:
|
||||
properties:
|
||||
err_msg:
|
||||
type: string
|
||||
status:
|
||||
example: 400
|
||||
type: integer
|
||||
type: object
|
||||
network.FailureResponse:
|
||||
properties:
|
||||
header:
|
||||
$ref: '#/definitions/network.FailResponseHeader'
|
||||
code:
|
||||
example: 500
|
||||
type: integer
|
||||
msg:
|
||||
example: failed to get recommend data from redis
|
||||
type: string
|
||||
payload:
|
||||
additionalProperties: true
|
||||
type: object
|
||||
type: object
|
||||
network.MeasurementRecommendPayload:
|
||||
properties:
|
||||
input:
|
||||
example: transformfeeder1_220.
|
||||
type: string
|
||||
offset:
|
||||
example: 21
|
||||
type: integer
|
||||
recommended_list:
|
||||
example:
|
||||
- '["I_A_rms"'
|
||||
- ' "I_B_rms"'
|
||||
- '"I_C_rms"]'
|
||||
items:
|
||||
type: string
|
||||
type: array
|
||||
type: object
|
||||
network.MeasurementRecommendRequest:
|
||||
properties:
|
||||
input:
|
||||
example: trans
|
||||
type: string
|
||||
type: object
|
||||
network.RealTimeDataPayload:
|
||||
properties:
|
||||
sub_pos:
|
||||
description: TODO 增加example tag
|
||||
type: object
|
||||
type: object
|
||||
network.SuccessResponse:
|
||||
properties:
|
||||
header:
|
||||
$ref: '#/definitions/network.SuccessResponseHeader'
|
||||
payload:
|
||||
additionalProperties:
|
||||
type: string
|
||||
example:
|
||||
key: value
|
||||
type: object
|
||||
type: object
|
||||
network.SuccessResponseHeader:
|
||||
properties:
|
||||
err_msg:
|
||||
type: string
|
||||
status:
|
||||
code:
|
||||
example: 200
|
||||
type: integer
|
||||
msg:
|
||||
example: success
|
||||
type: string
|
||||
payload:
|
||||
type: object
|
||||
type: object
|
||||
host: localhost:8080
|
||||
info:
|
||||
contact: {}
|
||||
contact:
|
||||
email: douxu@clea.com.cn
|
||||
name: douxu
|
||||
url: http://www.swagger.io/support
|
||||
description: 实时数据计算和模型运行服务的 API 服务
|
||||
license:
|
||||
name: Apache 2.0
|
||||
url: http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
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:
|
||||
- application/json
|
||||
description: 根据用户输入的字符串,从 Redis 中查询可能的测量点或结构路径,并提供推荐列表。
|
||||
parameters:
|
||||
- description: 查询输入参数,例如 'trans' 或 'transformfeeder1_220.'
|
||||
in: body
|
||||
name: request
|
||||
required: true
|
||||
schema:
|
||||
$ref: '#/definitions/network.MeasurementRecommendRequest'
|
||||
produces:
|
||||
- application/json
|
||||
responses:
|
||||
"200":
|
||||
description: 返回推荐列表成功
|
||||
schema:
|
||||
allOf:
|
||||
- $ref: '#/definitions/network.SuccessResponse'
|
||||
- properties:
|
||||
payload:
|
||||
$ref: '#/definitions/network.MeasurementRecommendPayload'
|
||||
type: object
|
||||
"400":
|
||||
description: 返回推荐列表失败
|
||||
schema:
|
||||
$ref: '#/definitions/network.FailureResponse'
|
||||
summary: 测量点推荐(搜索框自动补全)
|
||||
tags:
|
||||
- Measurement Recommend
|
||||
/model/diagram_load/{page_id}:
|
||||
get:
|
||||
consumes:
|
||||
|
|
|
|||
5
go.mod
5
go.mod
|
|
@ -1,13 +1,14 @@
|
|||
module modelRT
|
||||
|
||||
go 1.22.5
|
||||
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
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
|
|
|
|||
191
go.sum
191
go.sum
|
|
@ -1,16 +1,11 @@
|
|||
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=
|
||||
github.com/DATA-DOG/go-sqlmock v1.5.2/go.mod h1:88MAG/4G7SMwSE3CeA0ZKzrT5CiOU3OJ+JlNzwDqpNU=
|
||||
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
|
||||
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
||||
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/RediSearch/redisearch-go/v2 v2.1.1 h1:cCn3i40uLsVD8cxwrdrGfhdAgbR5Cld9q11eYyVOwpM=
|
||||
github.com/RediSearch/redisearch-go/v2 v2.1.1/go.mod h1:Uw93Wi97QqAsw1DwbQrhVd88dBorGTfSuCS42zfh1iA=
|
||||
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=
|
||||
|
|
@ -22,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=
|
||||
|
|
@ -95,48 +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/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/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.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=
|
||||
|
|
@ -145,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=
|
||||
|
|
@ -192,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=
|
||||
|
|
@ -235,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=
|
||||
|
|
@ -256,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=
|
||||
|
|
@ -268,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=
|
||||
|
|
@ -334,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=
|
||||
|
|
@ -417,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=
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import (
|
|||
"strconv"
|
||||
|
||||
"modelRT/alert"
|
||||
constants "modelRT/constant"
|
||||
"modelRT/constants"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
|
|
@ -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,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,11 +8,9 @@ import (
|
|||
"time"
|
||||
|
||||
"modelRT/common/errcode"
|
||||
constants "modelRT/constant"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/model"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
|
|
@ -65,45 +63,12 @@ func ComponentAnchorReplaceHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
cancelCtx, cancel = context.WithTimeout(c, 5*time.Second)
|
||||
defer cancel()
|
||||
unmarshalMap := make(map[string]interface{})
|
||||
tableName := model.SelectModelNameByType(componentInfo.ComponentType)
|
||||
result = pgClient.WithContext(cancelCtx).Table(tableName).Where("global_uuid = ?", uuid).Find(&unmarshalMap)
|
||||
if result.Error != nil {
|
||||
logger.Error(c, "query model detail info failed", "error", result.Error)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: result.Error.Error(),
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
if unmarshalMap == nil {
|
||||
err := fmt.Errorf("query model detail info by uuid failed:%w", errcode.ErrQueryRowZero)
|
||||
logger.Error(c, "query model detail info from table is empty", "table_name", tableName)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
componentType := unmarshalMap["component_type"].(int)
|
||||
if componentType != constants.DemoType {
|
||||
logger.Error(c, "can not process real time data of component type not equal DemoType", "component_id", componentInfo.ID)
|
||||
}
|
||||
diagram.UpdateAnchorValue(componentInfo.ID, anchorName)
|
||||
diagram.UpdateAnchorValue(componentInfo.GlobalUUID.String(), anchorName)
|
||||
|
||||
resp := network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
PayLoad: map[string]interface{}{
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": request.UUID,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AttrDeleteHandler deletes a data attribute
|
||||
func AttrDeleteHandler(c *gin.Context) {
|
||||
var request network.AttrDeleteRequest
|
||||
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 attribute delete request", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
rs := diagram.NewRedisString(c, request.AttrToken, clientToken, 10, true)
|
||||
if err := rs.GETDEL(request.AttrToken); err != nil {
|
||||
logger.Error(c, "failed to delete attribute from Redis", "attr_token", request.AttrToken, "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: map[string]interface{}{
|
||||
"attr_token": request.AttrToken,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/database"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AttrGetHandler retrieves the value of a data attribute
|
||||
func AttrGetHandler(c *gin.Context) {
|
||||
var request network.AttrGetRequest
|
||||
|
||||
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 attribute get request", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
tx := pgClient.Begin()
|
||||
|
||||
attrModel, err := database.ParseAttrToken(c, tx, request.AttrToken, clientToken)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
logger.Error(c, "failed to parse attribute token", "attr_token", request.AttrToken, "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
})
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
|
||||
// The GetAttrValue method is assumed to exist on the AttrModelInterface.
|
||||
// You need to add this method to your attribute_model.go interface definition.
|
||||
attrValue := attrModel.GetAttrValue()
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: map[string]interface{}{
|
||||
"attr_token": request.AttrToken,
|
||||
"attr_value": attrValue,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// AttrSetHandler sets the value of a data attribute
|
||||
func AttrSetHandler(c *gin.Context) {
|
||||
var request network.AttrSetRequest
|
||||
|
||||
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 attribute set request", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// The logic for handling Redis operations directly from the handler
|
||||
rs := diagram.NewRedisString(c, request.AttrToken, clientToken, 10, true)
|
||||
if err := rs.Set(request.AttrToken, request.AttrValue); err != nil {
|
||||
logger.Error(c, "failed to set attribute value in Redis", "attr_token", request.AttrToken, "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: map[string]interface{}{"attr_token": request.AttrToken},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: map[string]interface{}{
|
||||
"attr_token": request.AttrToken,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -10,7 +10,6 @@ import (
|
|||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/bitly/go-simplejson"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
|
@ -38,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -66,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -90,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -102,8 +101,8 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
|
|||
graph.AddEdge(topologicCreateInfo.UUIDFrom, topologicCreateInfo.UUIDTo)
|
||||
}
|
||||
|
||||
for index, componentInfo := range request.ComponentInfos {
|
||||
componentID, err := database.CreateComponentIntoDB(c, tx, componentInfo)
|
||||
for index, info := range request.ComponentInfos {
|
||||
componentUUID, err := database.CreateComponentIntoDB(c, tx, info)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
|
|
@ -112,71 +111,34 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
|
|||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
"component_infos": request.ComponentInfos,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
request.ComponentInfos[index].ID = componentID
|
||||
|
||||
err = database.CreateModelIntoDB(c, tx, componentID, componentInfo.ComponentType, componentInfo.Params)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error(c, "create component model into DB failed", "component_infos", request.ComponentInfos, "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": request.PageID,
|
||||
Payload: map[string]interface{}{
|
||||
"component_infos": request.ComponentInfos,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
request.ComponentInfos[index].UUID = componentUUID
|
||||
}
|
||||
|
||||
for _, componentInfo := range request.ComponentInfos {
|
||||
paramsJSON, err := simplejson.NewJson([]byte(componentInfo.Params))
|
||||
for _, info := range request.ComponentInfos {
|
||||
// TODO 修复赋值问题
|
||||
component, err := network.ConvertComponentCreateInfosToComponents(info)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error(c, "unmarshal component params info failed", "component_params", componentInfo.Params, "error", err)
|
||||
logger.Error(c, "convert component params info failed", "component_info", info, "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
"component_params": componentInfo.Params,
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": info.UUID,
|
||||
"component_params": info.Params,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
componentMap, err := paramsJSON.Map()
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error(c, "format params json info to map failed", "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
"component_params": componentInfo.Params,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
diagram.StoreComponentMap(componentInfo.ID, componentMap)
|
||||
diagram.StoreComponentMap(info.UUID, component)
|
||||
}
|
||||
|
||||
if len(request.FreeVertexs) > 0 {
|
||||
|
|
@ -190,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,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ import (
|
|||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/model"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
|
|
@ -43,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -71,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -96,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -113,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -139,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -163,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -185,39 +184,14 @@ 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,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
modelStruct := model.SelectModelByType(component.ComponentType)
|
||||
modelStruct.SetComponentID(component.ID)
|
||||
result = tx.WithContext(cancelCtx).Where("component_id = ?", component.ID).Delete(modelStruct)
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
tx.Rollback()
|
||||
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check uuid conditions", errcode.ErrDeleteRowZero)
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("delete component info from table %s failed", modelStruct.ReturnTableName())
|
||||
logger.Error(c, msg, "component_global_uuid", componentInfo.UUID, "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
diagram.DeleteComponentMap(component.ID)
|
||||
diagram.DeleteComponentMap(component.GlobalUUID.String())
|
||||
}
|
||||
|
||||
if len(request.FreeVertexs) > 0 {
|
||||
|
|
@ -231,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -77,14 +77,14 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
componentParams, err := diagram.GetComponentMap(component.ID)
|
||||
componentParams, err := diagram.GetComponentMap(component.GlobalUUID.String())
|
||||
if err != nil {
|
||||
logger.Error(c, "get component data from set by uuid failed", "error", err)
|
||||
|
||||
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,
|
||||
},
|
||||
}
|
||||
|
|
@ -111,14 +111,14 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
rootComponentParam, err := diagram.GetComponentMap(rootComponent.ID)
|
||||
rootComponentParam, err := diagram.GetComponentMap(rootComponent.GlobalUUID.String())
|
||||
if err != nil {
|
||||
logger.Error(c, "get component data from set by uuid failed", "error", err)
|
||||
|
||||
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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ import (
|
|||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/bitly/go-simplejson"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
|
@ -36,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -53,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -76,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -93,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,
|
||||
},
|
||||
}
|
||||
|
|
@ -103,32 +102,14 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
}
|
||||
|
||||
for index, componentInfo := range request.ComponentInfos {
|
||||
componentID, err := database.UpdateComponentIntoDB(c, tx, componentInfo)
|
||||
componentUUID, err := database.UpdateComponentIntoDB(c, tx, componentInfo)
|
||||
if err != nil {
|
||||
logger.Error(c, "udpate component info into DB failed", "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
"component_info": request.ComponentInfos,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
request.ComponentInfos[index].ID = componentID
|
||||
|
||||
err = database.UpdateModelIntoDB(c, tx, componentID, componentInfo.ComponentType, componentInfo.Params)
|
||||
if err != nil {
|
||||
logger.Error(c, "udpate component model info into DB failed", "error", err)
|
||||
|
||||
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,
|
||||
},
|
||||
|
|
@ -136,41 +117,27 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
request.ComponentInfos[index].UUID = componentUUID
|
||||
}
|
||||
|
||||
for _, componentInfo := range request.ComponentInfos {
|
||||
paramsJSON, err := simplejson.NewJson([]byte(componentInfo.Params))
|
||||
for _, info := range request.ComponentInfos {
|
||||
// TODO 修复赋值问题
|
||||
component, err := network.ConvertComponentUpdateInfosToComponents(info)
|
||||
if err != nil {
|
||||
logger.Error(c, "unmarshal component info by concurrent map failed", "component_params", componentInfo.Params, "error", err)
|
||||
logger.Error(c, "convert component params info failed", "component_info", info, "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
"component_params": componentInfo.Params,
|
||||
Payload: map[string]interface{}{
|
||||
"uuid": info.UUID,
|
||||
"component_params": info.Params,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
componentMap, err := paramsJSON.Map()
|
||||
if err != nil {
|
||||
logger.Error(c, "format params json info to map failed", "error", err)
|
||||
|
||||
resp := network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
"component_params": componentInfo.Params,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
diagram.UpdateComponentMap(componentInfo.ID, componentMap)
|
||||
diagram.UpdateComponentMap(info.ID, component)
|
||||
}
|
||||
|
||||
if len(request.FreeVertexs) > 0 {
|
||||
|
|
@ -185,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)
|
||||
}
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/logger"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// MeasurementGetHandler define measurement query API
|
||||
func MeasurementGetHandler(c *gin.Context) {
|
||||
var request network.MeasurementGetRequest
|
||||
|
||||
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 get request", "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
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]any{
|
||||
"measurement_id": request.MeasurementID,
|
||||
"measurement_token": request.MeasurementToken,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
measurementInfo, err := database.QueryMeasurementByID(c, pgClient, request.MeasurementID)
|
||||
if err != nil {
|
||||
logger.Error(c, "failed to query measurement by id", "measurement_id", request.MeasurementID, "error", err)
|
||||
c.JSON(http.StatusOK, network.FailureResponse{
|
||||
Code: http.StatusBadRequest,
|
||||
Msg: err.Error(),
|
||||
Payload: map[string]any{
|
||||
"measurement_id": request.MeasurementID,
|
||||
"measurement_token": request.MeasurementToken,
|
||||
"measurement_value": points,
|
||||
},
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
Code: http.StatusOK,
|
||||
Msg: "success",
|
||||
Payload: map[string]any{
|
||||
"measurement_id": request.MeasurementID,
|
||||
"measurement_token": request.MeasurementToken,
|
||||
"measurement_info": measurementInfo,
|
||||
"measurement_value": points,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
@ -0,0 +1,121 @@
|
|||
// Package handler provides HTTP handlers for various endpoints.
|
||||
package handler
|
||||
|
||||
import (
|
||||
"modelRT/logger"
|
||||
"modelRT/model"
|
||||
"modelRT/network"
|
||||
"modelRT/util"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// MeasurementRecommendHandler define measurement recommend API
|
||||
// @Summary 测量点推荐(搜索框自动补全)
|
||||
// @Description 根据用户输入的字符串,从 Redis 中查询可能的测量点或结构路径,并提供推荐列表。
|
||||
// @Tags Measurement Recommend
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @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": "grid1.zone1.station1.ns1.tag1.bay.",
|
||||
// "offset": 21,
|
||||
// "recommended_list": [
|
||||
// "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",
|
||||
// }
|
||||
//
|
||||
// @Router /measurement/recommend [get]
|
||||
func MeasurementRecommendHandler(c *gin.Context) {
|
||||
var request network.MeasurementRecommendRequest
|
||||
|
||||
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(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, network.SuccessResponse{
|
||||
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"
|
||||
constants "modelRT/constant"
|
||||
"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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -65,8 +65,8 @@ func RealTimeDataReceivehandler(c *gin.Context) {
|
|||
realtimedata.RealTimeDataChan <- request
|
||||
|
||||
payload := map[string]interface{}{
|
||||
"component_id": request.PayLoad.ComponentID,
|
||||
"point": request.PayLoad.Point,
|
||||
"component_uuid": request.PayLoad.ComponentUUID,
|
||||
"point": request.PayLoad.Point,
|
||||
}
|
||||
respByte := processResponse(0, "success", payload)
|
||||
if len(respByte) == 0 {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ import (
|
|||
"path"
|
||||
"runtime"
|
||||
|
||||
"modelRT/constants"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
)
|
||||
|
|
@ -46,7 +48,7 @@ func makeLogFields(ctx context.Context, kv ...any) []zap.Field {
|
|||
kv = append(kv, "unknown")
|
||||
}
|
||||
|
||||
kv = append(kv, "traceID", ctx.Value("traceID"), "spanID", ctx.Value("spanID"), "pspanID", ctx.Value("pspanID"))
|
||||
kv = append(kv, "traceID", ctx.Value(constants.HeaderTraceID), "spanID", ctx.Value(constants.HeaderSpanID), "parentSpanID", ctx.Value(constants.HeaderParentSpanID))
|
||||
|
||||
funcName, file, line := getLoggerCallerInfo()
|
||||
kv = append(kv, "func", funcName, "file", file, "line", line)
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@
|
|||
package logger
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"modelRT/config"
|
||||
constants "modelRT/constant"
|
||||
"modelRT/constants"
|
||||
|
||||
"github.com/natefinch/lumberjack"
|
||||
"go.uber.org/zap"
|
||||
|
|
@ -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.
|
||||
|
|
|
|||
223
main.go
223
main.go
|
|
@ -3,24 +3,28 @@ 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"
|
||||
|
||||
|
|
@ -31,14 +35,8 @@ 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", "./config", "config file dir of model runtime service")
|
||||
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")
|
||||
modelRTConfigType = flag.String("modelRT_config_type", "yaml", "config file type of model runtime service")
|
||||
)
|
||||
|
|
@ -46,18 +44,66 @@ var (
|
|||
var (
|
||||
modelRTConfig config.ModelRTConfig
|
||||
postgresDBClient *gorm.DB
|
||||
alertManager *alert.EventManager
|
||||
// alertManager *alert.EventManager
|
||||
)
|
||||
|
||||
// TODO 使用 wire 依赖注入管理 DVIE 面板注册的 panel
|
||||
// @title ModelRT 实时模型服务 API 文档
|
||||
// @version 1.0
|
||||
// @description 实时数据计算和模型运行服务的 API 服务
|
||||
// TODO termsOfService服务条款待后续优化
|
||||
// // @termsOfService http://swagger.io/terms/
|
||||
//
|
||||
// @contact.name douxu
|
||||
// TODO 修改支持的文档地址
|
||||
// @contact.url http://www.swagger.io/support
|
||||
// @contact.email douxu@clea.com.cn
|
||||
//
|
||||
// @license.name Apache 2.0
|
||||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
//
|
||||
// @host localhost:8080
|
||||
// @BasePath /api/v1
|
||||
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) {
|
||||
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 {
|
||||
panic(fmt.Errorf("failed to copy example config file:%w", err))
|
||||
}
|
||||
} else {
|
||||
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)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
serviceToken, err := util.GenerateClientToken(hostName, modelRTConfig.ServiceConfig.ServiceName, modelRTConfig.ServiceConfig.SecretKey)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "generate client token failed", "error", err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// init postgresDBClient
|
||||
postgresDBClient = database.InitPostgresDBInstance(ctx, modelRTConfig.PostgresDBURI)
|
||||
|
||||
|
|
@ -80,7 +126,11 @@ func main() {
|
|||
}
|
||||
defer parsePool.Release()
|
||||
|
||||
storageClient := diagram.InitClientInstance(modelRTConfig.StorageRedisConfig)
|
||||
searchPool, err := util.NewRedigoPool(modelRTConfig.StorageRedisConfig)
|
||||
defer searchPool.Close()
|
||||
model.InitAutocompleterWithPool(searchPool)
|
||||
|
||||
storageClient := diagram.InitRedisClientInstance(modelRTConfig.StorageRedisConfig)
|
||||
defer storageClient.Close()
|
||||
|
||||
lockerClient := locker.InitClientInstance(modelRTConfig.LockerRedisConfig)
|
||||
|
|
@ -94,22 +144,58 @@ 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)
|
||||
// componentTypeMap, err := database.QueryCircuitDiagramComponentFromDB(cancelCtx, tx, parsePool)
|
||||
// if err != nil {
|
||||
// logger.Error(ctx, "load circuit diagrams from postgres failed", "error", err)
|
||||
// panic(err)
|
||||
// }
|
||||
|
||||
cacheMap, err := model.GetNSpathToIsLocalMap(ctx, postgresDBClient)
|
||||
if err != nil {
|
||||
logger.Error(ctx, "load circuit diagrams from postgres failed", "error", err)
|
||||
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)
|
||||
}
|
||||
|
||||
// TODO 暂时屏蔽完成 swagger 启动测试
|
||||
tree, err := database.QueryTopologicFromDB(ctx, tx, componentTypeMap)
|
||||
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)
|
||||
panic(err)
|
||||
|
|
@ -118,62 +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)
|
||||
server := http.Server{
|
||||
Addr: ":8080",
|
||||
Handler: engine,
|
||||
}
|
||||
router.RegisterRoutes(engine, serviceToken)
|
||||
|
||||
// creating a System Signal Receiver
|
||||
done := make(chan os.Signal, 10)
|
||||
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-done
|
||||
if err := server.Shutdown(context.Background()); err != nil {
|
||||
logger.Error(ctx, "ShutdownServerError", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
logger.Info(ctx, "Starting ModelRT server...")
|
||||
err = server.ListenAndServe()
|
||||
if err != nil {
|
||||
if err == http.ErrServerClosed {
|
||||
// the service receives the shutdown signal normally and then closes
|
||||
logger.Info(ctx, "Server closed under request")
|
||||
} else {
|
||||
// abnormal shutdown of service
|
||||
logger.Error(ctx, "Server closed unexpected", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
// engine.Group()
|
||||
// Swagger UI
|
||||
engine.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
|
||||
|
|
@ -187,10 +222,30 @@ func main() {
|
|||
// }
|
||||
// }
|
||||
|
||||
// start route with 8080 port
|
||||
engine.Run(":8080")
|
||||
server := http.Server{
|
||||
Addr: modelRTConfig.ServiceConfig.ServiceAddr,
|
||||
Handler: engine,
|
||||
}
|
||||
|
||||
// Redis hashmap 母线模型、异步电动机模型
|
||||
// creating a System Signal Receiver
|
||||
done := make(chan os.Signal, 10)
|
||||
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-done
|
||||
if err := server.Shutdown(context.Background()); err != nil {
|
||||
logger.Error(ctx, "shutdown serverError", "err", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// kv key name value busx
|
||||
logger.Info(ctx, "starting ModelRT server")
|
||||
err = server.ListenAndServe()
|
||||
if err != nil {
|
||||
if err == http.ErrServerClosed {
|
||||
// the service receives the shutdown signal normally and then closes
|
||||
logger.Info(ctx, "server closed under request")
|
||||
} else {
|
||||
// abnormal shutdown of service
|
||||
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,11 @@
|
|||
package middleware
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// SetTokenMiddleware define a middleware for set token in context
|
||||
func SetTokenMiddleware(clientToken string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Set("client_token", clientToken)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"modelRT/constants"
|
||||
"modelRT/logger"
|
||||
"modelRT/util"
|
||||
|
||||
|
|
@ -15,15 +16,17 @@ import (
|
|||
// StartTrace define func of set trace info from request header
|
||||
func StartTrace() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
traceID := c.Request.Header.Get("traceid")
|
||||
pSpanID := c.Request.Header.Get("spanid")
|
||||
traceID := c.Request.Header.Get(constants.HeaderTraceID)
|
||||
parentSpanID := c.Request.Header.Get(constants.HeaderSpanID)
|
||||
spanID := util.GenerateSpanID(c.Request.RemoteAddr)
|
||||
if traceID == "" { // 如果traceId 为空,证明是链路的发端,把它设置成此次的spanId,发端的spanId是root spanId
|
||||
traceID = spanID // trace 标识整个请求的链路, span则标识链路中的不同服务
|
||||
// if traceId is empty, it means it is the origin of the link. Set it to the spanId of this time. The originating spanId is the root spanId.
|
||||
if traceID == "" {
|
||||
// traceId identifies the entire request link, and spanId identifies the different services in the link.
|
||||
traceID = spanID
|
||||
}
|
||||
c.Set("traceid", traceID)
|
||||
c.Set("spanid", spanID)
|
||||
c.Set("pspanid", pSpanID)
|
||||
c.Set(constants.HeaderTraceID, traceID)
|
||||
c.Set(constants.HeaderSpanID, spanID)
|
||||
c.Set(constants.HeaderParentSpanID, parentSpanID)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
|
@ -33,7 +36,7 @@ type bodyLogWriter struct {
|
|||
body *bytes.Buffer
|
||||
}
|
||||
|
||||
// 包装一下 gin.ResponseWriter,通过这种方式拦截写响应
|
||||
// TODO 包装一下 gin.ResponseWriter,通过这种方式拦截写响应
|
||||
// 让gin写响应的时候先写到 bodyLogWriter 再写gin.ResponseWriter ,
|
||||
// 这样利用中间件里输出访问日志时就能拿到响应了
|
||||
// https://stackoverflow.com/questions/38501325/how-to-log-response-body-in-gin
|
||||
|
|
@ -42,6 +45,8 @@ func (w bodyLogWriter) Write(b []byte) (int, error) {
|
|||
return w.ResponseWriter.Write(b)
|
||||
}
|
||||
|
||||
// TODO 用于访问request与reponse的日志记录
|
||||
// LogAccess define func of log access info
|
||||
func LogAccess() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 保存body
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,95 @@
|
|||
// Package model define model struct of model runtime service
|
||||
package model
|
||||
|
||||
import (
|
||||
"modelRT/orm"
|
||||
)
|
||||
|
||||
// AttrModelInterface define basic attr model type interface
|
||||
type AttrModelInterface interface {
|
||||
GetGridInfo() *orm.Grid
|
||||
GetZoneInfo() *orm.Zone
|
||||
GetStationInfo() *orm.Station
|
||||
GetComponentInfo() *orm.Component
|
||||
GetAttrValue() interface{} // New method to get the attribute value
|
||||
IsLocal() bool
|
||||
}
|
||||
|
||||
// LongAttrInfo structure define long attribute key info of component
|
||||
type LongAttrInfo struct {
|
||||
AttrGroupName string
|
||||
AttrKey string
|
||||
AttrValue any
|
||||
GridInfo *orm.Grid
|
||||
ZoneInfo *orm.Zone
|
||||
StationInfo *orm.Station
|
||||
ComponentInfo *orm.Component
|
||||
}
|
||||
|
||||
// GetGridInfo define return the grid information in the long attribute
|
||||
func (l *LongAttrInfo) GetGridInfo() *orm.Grid {
|
||||
return l.GridInfo
|
||||
}
|
||||
|
||||
// GetZoneInfo define return the zone information in the long attribute
|
||||
func (l *LongAttrInfo) GetZoneInfo() *orm.Zone {
|
||||
return l.ZoneInfo
|
||||
}
|
||||
|
||||
// GetStationInfo define return the station information in the long attribute
|
||||
func (l *LongAttrInfo) GetStationInfo() *orm.Station {
|
||||
return l.StationInfo
|
||||
}
|
||||
|
||||
// GetComponentInfo define return the component information in the long attribute
|
||||
func (l *LongAttrInfo) GetComponentInfo() *orm.Component {
|
||||
return l.ComponentInfo
|
||||
}
|
||||
|
||||
// IsLocal define return the is_local information in the long attribute
|
||||
func (l *LongAttrInfo) IsLocal() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
// GetAttrValue define return the attribute value
|
||||
func (l *LongAttrInfo) GetAttrValue() any {
|
||||
return l.AttrValue
|
||||
}
|
||||
|
||||
// ShortAttrInfo structure define short attribute key info of component
|
||||
type ShortAttrInfo struct {
|
||||
AttrGroupName string
|
||||
AttrKey string
|
||||
AttrValue any
|
||||
ComponentInfo *orm.Component
|
||||
}
|
||||
|
||||
// GetGridInfo define return the grid information in the short attribute
|
||||
func (s *ShortAttrInfo) GetGridInfo() *orm.Grid {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetZoneInfo define return the zone information in the short attribute
|
||||
func (s *ShortAttrInfo) GetZoneInfo() *orm.Zone {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetStationInfo define return the station information in the short attribute
|
||||
func (s *ShortAttrInfo) GetStationInfo() *orm.Station {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetComponentInfo define return the component information in the short attribute
|
||||
func (s *ShortAttrInfo) GetComponentInfo() *orm.Component {
|
||||
return s.ComponentInfo
|
||||
}
|
||||
|
||||
// IsLocal define return the is_local information in the short attribute
|
||||
func (s *ShortAttrInfo) IsLocal() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// GetAttrValue define return the attribute value
|
||||
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
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue