sync: 对齐小程序 v1.0.2 生产代码并脱敏

This commit is contained in:
MiFaZhan
2026-10-29 15:15:12 +08:00
parent a7abd476f0
commit 5fcf74deb0
423 changed files with 15001 additions and 6382 deletions
@@ -36,18 +36,18 @@ type appDiagnosisTrafficConfig struct {
MaxIdleConns int
}
// dbNameFor 返回指定超级表所在 database。
//
// 日流量与月流量分库存储(两库 duration 不同,而 duration 只能建库时确定);
// 未单独配置 dailyDbName/monthlyDbName 时回退到 DbName,与改造前行为一致。
func (c appDiagnosisTrafficConfig) dbNameFor(stable string) string {
if stable == appDiagnosisDailyStable && c.DailyDbName != "" {
return c.DailyDbName
func (cfg appDiagnosisTrafficConfig) databaseFor(stable string) string {
switch stable {
case appDiagnosisDailyStable:
if cfg.DailyDbName != "" {
return cfg.DailyDbName
}
case appDiagnosisMonthlyStable:
if cfg.MonthlyDbName != "" {
return cfg.MonthlyDbName
}
}
if stable == appDiagnosisMonthlyStable && c.MonthlyDbName != "" {
return c.MonthlyDbName
}
return c.DbName
return cfg.DbName
}
type appDiagnosisTrafficRecord struct {
@@ -67,24 +67,28 @@ func (s *sAppDiagnosis) queryDiagnosisDailyFlow(ctx context.Context, iccid strin
records, err := appDiagnosisQueryTrafficRows(ctx, iccid, appDiagnosisDailyStable, startTime, endTime)
if err != nil {
return nil, gerror.Wrap(err, "查询SIM卡使用流量失败,请稍后重试")
g.Log().Errorf(ctx, "app diagnosis daily traffic query failed, iccid:%s, days:%d, err:%+v", appDiagnosisMaskIdentifier(iccid), days, err)
return nil, gerror.New("流量趋势服务暂时不可用,请稍后重试")
}
usageMap := make(map[string]float64, len(records))
for _, item := range records {
usageMap[item.Ts.Format("2006-01-02")] = item.UsageMb
usageMap[item.Ts.Format("2006-01-02")] += item.UsageMb
}
res := &model.AppDiagnosisFlowModel{
Times: make([]string, 0, days),
Flow: make([]float64, 0, days),
Unit: "MB",
Granularity: "day",
HasData: len(records) > 0,
Times: make([]string, 0, days),
Flow: make([]float64, 0, days),
FlowAvailable: make([]bool, 0, days),
Unit: "MB",
Granularity: "day",
HasData: len(records) > 0,
}
for i := 0; i < days; i++ {
current := startTime.AddDate(0, 0, i)
res.Times = append(res.Times, current.Format("01-02"))
res.Flow = append(res.Flow, usageMap[current.Format("2006-01-02")])
usage, ok := usageMap[current.Format("2006-01-02")]
res.Flow = append(res.Flow, usage)
res.FlowAvailable = append(res.FlowAvailable, ok)
}
return res, nil
}
@@ -100,25 +104,29 @@ func (s *sAppDiagnosis) queryDiagnosisMonthlyFlow(ctx context.Context, iccid str
records, err := appDiagnosisQueryTrafficRows(ctx, iccid, appDiagnosisMonthlyStable, startMonth, endTime)
if err != nil {
return nil, gerror.Wrap(err, "查询SIM卡使用流量失败,请稍后重试")
g.Log().Errorf(ctx, "app diagnosis monthly traffic query failed, iccid:%s, months:%d, err:%+v", appDiagnosisMaskIdentifier(iccid), months, err)
return nil, gerror.New("流量趋势服务暂时不可用,请稍后重试")
}
usageMap := make(map[string]float64, len(records))
for _, item := range records {
usageMap[item.Ts.Format("2006-01")] = item.UsageMb
usageMap[item.Ts.Format("2006-01")] += item.UsageMb
}
res := &model.AppDiagnosisFlowModel{
Times: make([]string, 0, months),
Flow: make([]float64, 0, months),
Unit: "MB",
Granularity: "month",
HasData: len(records) > 0,
Times: make([]string, 0, months),
Flow: make([]float64, 0, months),
FlowAvailable: make([]bool, 0, months),
Unit: "MB",
Granularity: "month",
HasData: len(records) > 0,
}
for i := 0; i < months; i++ {
current := startMonth.AddDate(0, i, 0)
key := current.Format("2006-01")
res.Times = append(res.Times, key)
res.Flow = append(res.Flow, usageMap[key])
usage, ok := usageMap[key]
res.Flow = append(res.Flow, usage)
res.FlowAvailable = append(res.FlowAvailable, ok)
}
return res, nil
}
@@ -133,8 +141,8 @@ func appDiagnosisQueryTrafficRows(ctx context.Context, iccid string, stable stri
g.Log().Debug(ctx, "app diagnosis traffic query skipped, tdengine disabled")
return []appDiagnosisTrafficRecord{}, nil
}
dbName := cfg.dbNameFor(stable)
if err := appDiagnosisValidateTableConfig(dbName, stable); err != nil {
database := cfg.databaseFor(stable)
if err := appDiagnosisValidateTableConfig(database, stable); err != nil {
return nil, err
}
@@ -150,7 +158,7 @@ func appDiagnosisQueryTrafficRows(ctx context.Context, iccid string, stable stri
db.SetMaxOpenConns(cfg.MaxOpenConns)
db.SetMaxIdleConns(cfg.MaxIdleConns)
table := appDiagnosisQualify(dbName, appDiagnosisChildTrafficTableName(stable, iccid))
table := appDiagnosisQualify(database, appDiagnosisChildTrafficTableName(stable, iccid))
rows, err := db.QueryContext(ctx, fmt.Sprintf(
"SELECT ts, usage_mb, package_code, iccid FROM %s%s ORDER BY ts ASC",
table,
@@ -179,18 +187,23 @@ func appDiagnosisQueryTrafficRows(ctx context.Context, iccid string, stable stri
}
func appDiagnosisGetTrafficConfig(ctx context.Context) appDiagnosisTrafficConfig {
// dbName 只是兑底键名,不决定实际访问哪个库;默认取日库,避免兑底到不存在的库名。
dbName := strings.TrimSpace(g.Cfg().MustGet(ctx, "tsdb.tdengine.dbName", "iotlcm_daily").String())
return appDiagnosisTrafficConfig{
cfg := appDiagnosisTrafficConfig{
Enabled: strings.EqualFold(g.Cfg().MustGet(ctx, "tsdb.database", "TdEngine").String(), "TdEngine"),
DriverName: strings.TrimSpace(g.Cfg().MustGet(ctx, "tsdb.tdengine.type", "taosWS").String()),
Dsn: strings.TrimSpace(g.Cfg().MustGet(ctx, "tsdb.tdengine.dsn", "").String()),
DbName: dbName,
DailyDbName: strings.TrimSpace(g.Cfg().MustGet(ctx, "tsdb.tdengine.dailyDbName", dbName).String()),
MonthlyDbName: strings.TrimSpace(g.Cfg().MustGet(ctx, "tsdb.tdengine.monthlyDbName", dbName).String()),
DbName: strings.TrimSpace(g.Cfg().MustGet(ctx, "tsdb.tdengine.dbName", "iotlcm_daily").String()),
DailyDbName: strings.TrimSpace(g.Cfg().MustGet(ctx, "tsdb.tdengine.dailyDbName", "").String()),
MonthlyDbName: strings.TrimSpace(g.Cfg().MustGet(ctx, "tsdb.tdengine.monthlyDbName", "").String()),
MaxOpenConns: g.Cfg().MustGet(ctx, "tsdb.tdengine.maxOpenConns", 100).Int(),
MaxIdleConns: g.Cfg().MustGet(ctx, "tsdb.tdengine.maxIdleConns", 10).Int(),
}
if cfg.DailyDbName == "" {
cfg.DailyDbName = cfg.DbName
}
if cfg.MonthlyDbName == "" {
cfg.MonthlyDbName = cfg.DbName
}
return cfg
}
func appDiagnosisValidateTableConfig(database string, stable string) error {