lucas 4 months ago
parent
commit
d4b0f13c2b
  1. 30
      README.md
  2. 93
      configHelper.go
  3. 15
      dbHelper/elasticsearchHelper.go
  4. 2
      go.mod

30
README.md

@ -1,4 +1,28 @@
# common_utils
重建
common_utils
# common_utils 通用工具包
支持:Elasticsearch、InfluxDB、Kafka 的操作
### 开发语言和版本
golang ,版本 go1.23.1
### 平台支持
安心云4.0
### 使用方式:
1. 设置 go 环境
set GOPRIVATE=gitea.anxinyun.cn
2. 查询go 版本
go list -m -versions gitea.anxinyun.cn/container/common_utils
如若 GOPRIVATE=gitea.anxinyun.cn 生效,则会返回 common_utils 的版本信息。
如若无版本信息返回,有可能是没有 container 的权限,也有可能环境变量设置没有生效。
异常情况处理:
1. 无 https://gitea.anxinyun.cn/container 访问权限,可以联系下管理员。
2. 环境变量设置不生效
解决方法,在GoLand工具中进行设置:
1)打开 Settings
2)Go > GOROOT,Download Go SDK
3)Go > Go Modules,设置环境变量 GOPRIVATE=gitea.anxinyun.cn
### 依赖包管理
common_utils 如若有修改,需要同步升级 et-go

93
configHelper.go

@ -2,6 +2,9 @@ package common_utils
import (
"context"
"gitea.anxinyun.cn/container/common_models/constant/settlementParam"
"strings"
//"encoding/json"
"errors"
"fmt"
@ -24,6 +27,7 @@ var IotaDeviceCache map[string]common_models.IotaDevice
var ThingStructCache map[string]common_models.ThingStruct
var DeviceNodeCache map[string]common_models.IotaInstances
var AlarmCodeCache map[string]common_models.AlarmCode
var StructureCache map[int]common_models.Structure
// var stationThreshold map[int]common_models.Threshold
// var aggThreshold map[string]common_models.AggThreshold
@ -85,6 +89,9 @@ func initDeviceInfoMapCache() {
if AlarmCodeCache == nil {
AlarmCodeCache = make(map[string]common_models.AlarmCode)
}
if StructureCache == nil {
StructureCache = make(map[int]common_models.Structure)
}
}
// GetDeviceInfo 通过
@ -318,23 +325,75 @@ func (the *ConfigHelper) SetStationGroup(groupId int, group common_models.Statio
k := fmt.Sprintf("%s:%d", redisKey.Group, groupId)
return the.chainedCache.LoadableChinCache.Set(the.ctx, k, &group)
}
// 最大级联层级
const max_corr_depth int = 15
// 关联分组(e.g 沉降级联)
// depth: 递归查询深度(防止错误配置导致基点互相参考;亦限制级联的最大层级)
func (the *ConfigHelper) corrGroups(gp common_models.StationGroup, depth int) {
if gp.GroupType == "202" && gp.Params != nil && len(gp.Params) == 2 {
ref_base_stationId, ok := gp.Params[settlementParam.Ref_base].(float64)
if !ok {
log.Println("ref_base 转换异常。", gp.Params[settlementParam.Ref_base])
return
}
ref_point_stationId, ok := gp.Params[settlementParam.Ref_point].(float64)
if !ok {
log.Println("ref_base 转换异常。", gp.Params[settlementParam.Ref_point])
return
}
basePtr := gp.GetSettlementBaseItem()
if basePtr == nil {
log.Println("无基点。", gp.R())
return
}
sg, err := the.GetStationGroupInfo(int(ref_base_stationId))
subBase := common_models.GroupItem{
StationId: int(ref_base_stationId),
ParamsValue: map[string]interface{}{"base": true},
SubItems: nil,
}
if err == nil {
refGroup, err := the.GetStationGroup(sg.GroupId)
if err == nil {
if depth+1 <= max_corr_depth {
the.corrGroups(refGroup, depth+1)
}
}
subBase = *refGroup.GetItem(int(ref_base_stationId))
}
subPoint := common_models.GroupItem{
StationId: int(ref_point_stationId),
ParamsValue: map[string]interface{}{"base": false},
SubItems: nil,
}
basePtr.SubItems = map[string]common_models.GroupItem{
settlementParam.Ref_base: subBase,
settlementParam.Ref_point: subPoint,
}
//log.Println(basePtr)
}
}
func (the *ConfigHelper) GetStationGroup(groupId int) (common_models.StationGroup, error) {
var result common_models.StationGroup
var group common_models.StationGroup
// group:35
k := fmt.Sprintf("%s:%d", redisKey.Group, groupId)
value, err := the.chainedCache.LoadableChinCache.Get(the.ctx, k)
if v, ok := value.(string); ok {
if v == "" {
err = errors.New("无测点group 数据")
return result, err
}
err = json.Unmarshal([]byte(v), &result)
err = json.Unmarshal([]byte(v), &group)
if err != nil {
log.Printf("json unmarshal error:%s \n", err.Error())
log.Printf("err => v=%s", v)
}
}
return result, err
the.corrGroups(group, 0)
return group, err
}
// RedisKey.station_group
@ -342,6 +401,7 @@ func (the *ConfigHelper) SetStationGroupInfo(stationId int, info common_models.S
k := fmt.Sprintf("%s:%d", redisKey.Station_group, stationId)
return the.chainedCache.LoadableChinCache.Set(the.ctx, k, &info)
}
func (the *ConfigHelper) GetStationGroupInfo(stationId int) (common_models.StationGroupInfo, error) {
// sg:193
k := fmt.Sprintf("%s:%d", redisKey.Station_group, stationId)
@ -522,7 +582,8 @@ func (the *ConfigHelper) GetIotaScheme(dimensionId string) (common_models.IotaSc
var scheme common_models.IotaScheme
value, err := the.chainedCache.LoadableChinCache.Get(the.ctx, k)
if v, ok := value.(string); ok {
err = json.Unmarshal([]byte(v), &scheme)
formattedStr := strings.Replace(v, "+0800", "+08:00", -1)
err = json.Unmarshal([]byte(formattedStr), &scheme)
if err != nil {
log.Printf("json unmarshal error:%s \n", err.Error())
}
@ -603,3 +664,17 @@ func (the *ConfigHelper) mapToTree(source map[string]common_models.IotaInstance,
}
return deviceNode
}
func (the *ConfigHelper) GetStructure(structId int) (common_models.Structure, error) {
var result common_models.Structure
// structure:1
k := fmt.Sprintf("%s:%d", redisKey.Structure, structId)
value, err := the.chainedCache.LoadableChinCache.Get(the.ctx, k)
if v, ok := value.(string); ok {
err = json.Unmarshal([]byte(v), &result)
if err != nil {
log.Printf("json unmarshal error:%s \n", err.Error())
}
}
return result, err
}

15
dbHelper/elasticsearchHelper.go

@ -215,3 +215,18 @@ func (the *ESHelper) BulkWriteRaws2Es(index string, raws []common_models.EsRaw)
func (the *ESHelper) Close() {
}
// BulkWriteGroup2Es 分组主题数据写入ES
func (the *ESHelper) BulkWriteGroup2Es(index string, themes []common_models.EsGroupTheme) {
body := strings.Builder{}
for _, theme := range themes {
source, _ := json.Marshal(theme)
_id := common_calc.NameUUIDFromString(fmt.Sprintf("%d-%d", theme.GroupId, theme.CollectTime.UnixMilli()))
s := fmt.Sprintf(
`{"index": {"_index": "%s","_id": "%s"}}
%s
`, index, _id, source)
body.WriteString(s)
}
the.BulkWrite(index, body.String())
}

2
go.mod

@ -4,7 +4,7 @@ go 1.23.1
require (
gitea.anxinyun.cn/container/common_calc v0.0.1
gitea.anxinyun.cn/container/common_models v0.0.7
gitea.anxinyun.cn/container/common_models v0.0.8
github.com/IBM/sarama v1.43.0
github.com/allegro/bigcache v1.2.1
github.com/bytedance/sonic v1.12.2

Loading…
Cancel
Save