chore: 初始开源发布
iotlcm-worker-svr:异步执行中枢(RabbitMQ 消费者 + 全量同步编排)。 - Go module:iotlcm-worker-svr → git.5iot.com/iotlcm-public/iotlcm-worker-svr(214 文件) - 凭据与内部标识脱敏:MySQL/Redis 凭据、私网与生产 IP、内网域名、 库名 xiujie_admin → iotlcm、哨兵名占位 - 159 处真实手机号 → 合法 11 位编造号(保留号段与后 4 位,维持互异, 不破坏「校验 MSISDN 格式」等测试断言) - 移除内部资产:CI 触发器、Jenkins 触发测试文档、预演门禁与镜像 digest 记录 - Dockerfile 改用公共基础镜像(golang:1.25-alpine → alpine:3.23), 移除写死的 git 凭据 - README 重写(架构/全量同步模型/配置/已知失败),补 Apache-2.0 LICENSE,LF 行尾 + .gitattributes 已知:3 个测试包在源仓同一提交上同样失败(client/upstream、 service/allocate、service/inbound),属既有缺陷,本仓未引入亦未修复。
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
package trafficusage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"hash/fnv"
|
||||
"math"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/library/tdengine"
|
||||
"git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model"
|
||||
)
|
||||
|
||||
var identifierPattern = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
||||
|
||||
type Repository struct{}
|
||||
|
||||
func New() *Repository {
|
||||
return &Repository{}
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateDaily(ctx context.Context, items []model.TrafficUsageDailyItem) error {
|
||||
config := tdengine.GetConfig(ctx)
|
||||
return writeUsage(ctx, config, config.DbNameFor(config.DailyStable), config.DailyStable, items, func(item model.TrafficUsageDailyItem) time.Time {
|
||||
return snapshotDay(item.Date)
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateMonthly(ctx context.Context, items []model.TrafficUsageMonthlyItem) error {
|
||||
config := tdengine.GetConfig(ctx)
|
||||
return writeUsage(ctx, config, config.DbNameFor(config.MonthlyStable), config.MonthlyStable, items, func(item model.TrafficUsageMonthlyItem) time.Time {
|
||||
return snapshotMonth(item.Month)
|
||||
})
|
||||
}
|
||||
|
||||
type usageItem interface {
|
||||
model.TrafficUsageDailyItem | model.TrafficUsageMonthlyItem
|
||||
}
|
||||
|
||||
func writeUsage[T usageItem](ctx context.Context, config tdengine.Config, dbName, stable string, items []T, timestamp func(T) time.Time) error {
|
||||
if len(items) == 0 {
|
||||
return nil
|
||||
}
|
||||
if !tdengine.Enabled(ctx) {
|
||||
return gerror.New("TDengine disabled; usage was not persisted")
|
||||
}
|
||||
if !identifierPattern.MatchString(dbName) || !identifierPattern.MatchString(stable) {
|
||||
return gerror.New("invalid tdengine database or stable name")
|
||||
}
|
||||
batchSize := config.BatchSize
|
||||
if batchSize <= 0 {
|
||||
batchSize = 200
|
||||
}
|
||||
for start := 0; start < len(items); start += batchSize {
|
||||
end := min(start+batchSize, len(items))
|
||||
var builder strings.Builder
|
||||
builder.WriteString("INSERT INTO ")
|
||||
for index, item := range items[start:end] {
|
||||
base := usageBase(item)
|
||||
if base.Iccid == "" {
|
||||
return gerror.New("traffic usage item ICCID is empty")
|
||||
}
|
||||
if math.IsNaN(base.UsageMb) || math.IsInf(base.UsageMb, 0) || base.UsageMb < 0 {
|
||||
return gerror.Newf("invalid traffic usage value: %v", base.UsageMb)
|
||||
}
|
||||
if index > 0 {
|
||||
builder.WriteByte(' ')
|
||||
}
|
||||
builder.WriteString(fmt.Sprintf("%s.%s USING %s.%s TAGS (%s) VALUES (%s, %s, %s)",
|
||||
dbName,
|
||||
childTableName(stable, base.Iccid),
|
||||
dbName,
|
||||
stable,
|
||||
sqlString(base.Iccid),
|
||||
sqlTime(timestamp(item)),
|
||||
strconv.FormatFloat(base.UsageMb, 'f', -1, 64),
|
||||
sqlString(base.PackageCode),
|
||||
))
|
||||
}
|
||||
if _, err := tdengine.ExecForDb(ctx, dbName, builder.String()); err != nil {
|
||||
return gerror.Wrap(err, "write traffic usage failed")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func usageBase[T usageItem](item T) model.TrafficUsageBase {
|
||||
switch value := any(item).(type) {
|
||||
case model.TrafficUsageDailyItem:
|
||||
return value.TrafficUsageBase
|
||||
case model.TrafficUsageMonthlyItem:
|
||||
return value.TrafficUsageBase
|
||||
default:
|
||||
return model.TrafficUsageBase{}
|
||||
}
|
||||
}
|
||||
|
||||
// childTableName builds the TDengine child-table name for one ICCID.
|
||||
//
|
||||
// The rule must stay byte-identical to Admin's childTableNameWithPrefix
|
||||
// (iotlcm-admin-svr internal/logic/trafficusage/traffic_usage.go): drop every
|
||||
// character that is not a letter, digit or underscore, and fall back to an FNV
|
||||
// hash when the result is empty or longer than 190 characters. Admin's QueryDaily
|
||||
// reads child tables by that same rule, so diverging here would silently split
|
||||
// one card across two child tables and hide its data from the diagnosis page.
|
||||
func childTableName(stable, iccid string) string {
|
||||
prefix := "sim_card_usage_m_"
|
||||
if stable == "t_sim_card_usage_daily" {
|
||||
prefix = "sim_card_usage_d_"
|
||||
}
|
||||
var builder strings.Builder
|
||||
builder.WriteString(prefix)
|
||||
for _, char := range strings.ToLower(iccid) {
|
||||
if unicode.IsLetter(char) || unicode.IsDigit(char) || char == '_' {
|
||||
builder.WriteRune(char)
|
||||
}
|
||||
}
|
||||
name := builder.String()
|
||||
if name == prefix || len(name) > 190 {
|
||||
return fmt.Sprintf("%s%x", prefix, hashString(iccid))
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
// hashString mirrors Admin's FNV-1a fallback so both sides derive the same
|
||||
// child-table name for identifiers that cannot be embedded verbatim.
|
||||
func hashString(value string) uint32 {
|
||||
hash := fnv.New32a()
|
||||
_, _ = hash.Write([]byte(value))
|
||||
return hash.Sum32()
|
||||
}
|
||||
|
||||
func snapshotDay(value time.Time) time.Time {
|
||||
if value.IsZero() {
|
||||
value = time.Now()
|
||||
}
|
||||
return time.Date(value.Year(), value.Month(), value.Day(), 23, 59, 59, 0, value.Location())
|
||||
}
|
||||
|
||||
func snapshotMonth(value time.Time) time.Time {
|
||||
if value.IsZero() {
|
||||
value = time.Now()
|
||||
}
|
||||
return time.Date(value.Year(), value.Month()+1, 0, 23, 59, 59, 0, value.Location())
|
||||
}
|
||||
|
||||
func sqlString(value string) string {
|
||||
return "'" + strings.ReplaceAll(value, "'", "''") + "'"
|
||||
}
|
||||
|
||||
func sqlTime(value time.Time) string {
|
||||
return sqlString(value.Format("2006-01-02 15:04:05.000"))
|
||||
}
|
||||
Reference in New Issue
Block a user