代码提交

This commit is contained in:
2026-11-14 11:24:19 +08:00
parent e1674ec96b
commit 37757d6aa5
5 changed files with 176 additions and 21 deletions
+99 -1
View File
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"stat/common/statutils"
"time"
)
func LineCount(iTemp string, iValue int64, permilValue int64) string {
@@ -442,7 +443,7 @@ func (s *BotHourStat) GenerateTodayTable(title string, statName string, filepath
}
}
func (s *BotHourStat) DisplayDayHourTextWithTitle(appName string, compare *BotHourStat) (text string, title string, descript string) {
func (s *BotHourStat) DisplayDayHourTextWithTitle(appName string, compare, lastWeek *BotHourStat) (text string, title string, descript string) {
calc := func(curr, hist *statutils.Stat) (*statutils.Stat, *statutils.Stat) {
diff := curr.Sub(hist)
rate := diff.Permil(hist)
@@ -587,6 +588,7 @@ func (s *BotHourStat) DisplayDayHourTextWithTitle(appName string, compare *BotHo
text += fmt.Sprintf("┗游戏下分: %d\n", s.TodayGame.DecAmount)
}
}()
text += s.DisplayCompareText("9️⃣", compare, lastWeek)
text += fmt.Sprintf("🆚%s同比数据:当日的昨天%d点\n", appName, compare.SumDate.Hour())
} else {
func() {
@@ -693,7 +695,103 @@ func (s *BotHourStat) DisplayDayHourTextWithTitle(appName string, compare *BotHo
text += fmt.Sprintf(" ┗游戏下分: %d\n", s.TodayGame.DecAmount)
}
}()
text += s.DisplayCompareText("6️⃣", compare, lastWeek)
text += fmt.Sprintf("🆚【%s】同比数据未找到\n", appName)
}
return text, title, descript
}
// compareMetric 核心对比指标。数据机器人汇报末尾的"核心对比"块用,只挑最常看的几项;
// 全量指标在上面各段落里已经逐行带了同比涨跌,这里不重复。
type compareMetric struct {
Name string
IsMoney bool // true 表示底层值的单位是"分",展示前要除以 100
Pick func(*BotHourStat) int64
}
var compareMetrics = []compareMetric{
{Name: "当日充值", IsMoney: true, Pick: func(s *BotHourStat) int64 { return s.TodayStat.Recharge.Value() }},
{Name: "当日新增", Pick: func(s *BotHourStat) int64 { return s.TodayStat.Register.Count() }},
{Name: "当日日活", Pick: func(s *BotHourStat) int64 { return s.TodayStat.Visit.Count() }},
{Name: "小时充值", IsMoney: true, Pick: func(s *BotHourStat) int64 { return s.HourStat.Recharge.Value() }},
{Name: "小时新增", Pick: func(s *BotHourStat) int64 { return s.HourStat.Register.Count() }},
}
// weekdayCN 星期的中文名,用来拼"周三"这种基准时间标签
var weekdayCN = [...]string{"日", "一", "二", "三", "四", "五", "六"}
// formatRate 把万分比涨跌渲染成带箭头的百分数,箭头用法和 LineCount 保持一致。
// 这里按万分比算而不是沿用 LineCount 的千分比: 千分比只能给出一位小数,
// 23.45% 会被截成 23.40%,而这一块就是专门拿来看涨跌的。
func formatRate(bp int64) string {
v := float64(bp) / 100
switch {
case v < 0:
return fmt.Sprintf("⬇%.2f%%", v)
case v > 0:
return fmt.Sprintf("⬆+%.2f%%", v)
default:
return fmt.Sprintf("%.2f%%", v)
}
}
// divisor 该指标展示时的除数
func (m compareMetric) divisor() int64 {
if m.IsMoney {
return 100
}
return 1
}
// compareCell 渲染一个指标相对某一期基准的涨跌幅。
// base 为 nil 表示那一期的记录没查到;基准为 0 时算不出涨跌幅,
// 写 -- 而不是一个看起来正常的 0.00%。
func compareCell(label string, m compareMetric, curr int64, base *BotHourStat) string {
if base == nil {
return fmt.Sprintf("%s:无数据", label)
}
baseValue := m.Pick(base)
if baseValue == 0 {
return fmt.Sprintf("%s:--", label)
}
bp := (curr - baseValue) * 10000 / baseValue
return fmt.Sprintf("%s:%s", label, formatRate(bp))
}
// hourDesc 把小时对齐的时点写成"11-14 周六 15点"
func hourDesc(t time.Time) string {
return fmt.Sprintf("%s 周%s %d点", t.Format("01-02"), weekdayCN[t.Weekday()], t.Hour())
}
// compareDesc 基准时间的可读描述。不重复写小时(两期基准都跟当日同一个小时),
// 但保留星期: 环比是整七天前,星期几必然和当日一致,写出来才能一眼看出没错位。
// 记录没查到时直接在标签上标出来,免得看到一排"无数据"还要猜是哪天的。
func compareDesc(want time.Time, got *BotHourStat) string {
desc := fmt.Sprintf("%s 周%s", want.Format("01-02"), weekdayCN[want.Weekday()])
if got == nil {
return desc + "(无数据)"
}
return desc
}
// DisplayCompareText 汇报末尾的"核心对比"块。
// 业务口径: 同比 = 对比昨天同一小时, 环比 = 对比上周当日同一小时。
// 这个叫法和财务上的习惯正好相反,是按需求方的要求定的,不要"纠正"过来。
// 两期基准都拿不到时返回空串,不往汇报里塞一块全是"无数据"的内容。
func (s *BotHourStat) DisplayCompareText(index string, yesterday, lastWeek *BotHourStat) string {
if s == nil || (yesterday == nil && lastWeek == nil) {
return ""
}
text := fmt.Sprintf("%s核心对比:\n", index)
text += fmt.Sprintf("┣当日:%s\n", hourDesc(s.SumDate))
text += fmt.Sprintf("┣基准:环比 %s / 同比 %s\n",
compareDesc(s.SumDate.AddDate(0, 0, -7), lastWeek),
compareDesc(s.SumDate.AddDate(0, 0, -1), yesterday))
for _, m := range compareMetrics {
curr := m.Pick(s)
text += fmt.Sprintf("┣%s:%d %s %s\n", m.Name, curr/m.divisor(),
compareCell("环比", m, curr, lastWeek),
compareCell("同比", m, curr, yesterday))
}
return text
}