This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -341,6 +342,7 @@ func ListSnapshotsByRange(ctx context.Context, database db.Database, snapshotTyp
|
||||
|
||||
startUnix := start.Unix()
|
||||
endUnix := end.Unix()
|
||||
loc := start.Location()
|
||||
|
||||
var rows *sqlx.Rows
|
||||
var err error
|
||||
@@ -386,7 +388,7 @@ ORDER BY snapshot_time ASC, table_name ASC
|
||||
}
|
||||
records = append(records, SnapshotRecord{
|
||||
TableName: tableName,
|
||||
SnapshotTime: time.Unix(snapshotTime, 0),
|
||||
SnapshotTime: time.Unix(snapshotTime, 0).In(loc),
|
||||
SnapshotType: recordType,
|
||||
SnapshotCount: snapshotCnt,
|
||||
})
|
||||
@@ -394,6 +396,65 @@ ORDER BY snapshot_time ASC, table_name ASC
|
||||
return records, rows.Err()
|
||||
}
|
||||
|
||||
func SnapshotRecordsWithFallback(ctx context.Context, database db.Database, snapshotType, prefix, layout string, start, end time.Time) ([]SnapshotRecord, error) {
|
||||
records, err := ListSnapshotsByRange(ctx, database, snapshotType, start, end)
|
||||
if err == nil && len(records) > 0 {
|
||||
return records, nil
|
||||
}
|
||||
|
||||
fallback, err2 := recordsFromTableNames(ctx, database, snapshotType, prefix, layout, start, end)
|
||||
if err2 != nil {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, err2
|
||||
}
|
||||
if len(fallback) > 0 {
|
||||
return fallback, nil
|
||||
}
|
||||
return records, err
|
||||
}
|
||||
|
||||
func recordsFromTableNames(ctx context.Context, database db.Database, snapshotType, prefix, layout string, start, end time.Time) ([]SnapshotRecord, error) {
|
||||
tables, err := ListTablesByPrefix(ctx, database, prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
records := make([]SnapshotRecord, 0, len(tables))
|
||||
for _, table := range tables {
|
||||
if !strings.HasPrefix(table, prefix) {
|
||||
continue
|
||||
}
|
||||
suffix := strings.TrimPrefix(table, prefix)
|
||||
var ts time.Time
|
||||
switch layout {
|
||||
case "epoch":
|
||||
val, err := strconv.ParseInt(suffix, 10, 64)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ts = time.Unix(val, 0)
|
||||
default:
|
||||
parsed, err := time.ParseInLocation(layout, suffix, time.Local)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
ts = parsed
|
||||
}
|
||||
if !ts.Before(start) && ts.Before(end) {
|
||||
records = append(records, SnapshotRecord{
|
||||
TableName: table,
|
||||
SnapshotTime: ts,
|
||||
SnapshotType: snapshotType,
|
||||
})
|
||||
}
|
||||
}
|
||||
sort.Slice(records, func(i, j int) bool {
|
||||
return records[i].SnapshotTime.Before(records[j].SnapshotTime)
|
||||
})
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func LatestSnapshotTime(ctx context.Context, database db.Database, snapshotType string) (time.Time, error) {
|
||||
dbConn := database.DB()
|
||||
driver := strings.ToLower(dbConn.DriverName())
|
||||
@@ -665,7 +726,7 @@ func SaveTableReport(logger *slog.Logger, Database db.Database, ctx context.Cont
|
||||
func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.Context, xlsx *excelize.File, tableName string) {
|
||||
if strings.HasPrefix(tableName, "inventory_daily_summary_") {
|
||||
suffix := strings.TrimPrefix(tableName, "inventory_daily_summary_")
|
||||
dayStart, err := time.Parse("20060102", suffix)
|
||||
dayStart, err := time.ParseInLocation("20060102", suffix, time.Local)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -673,7 +734,7 @@ func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.
|
||||
if err := EnsureSnapshotRegistry(ctx, database); err != nil {
|
||||
return
|
||||
}
|
||||
records, err := ListSnapshotsByRange(ctx, database, "hourly", dayStart, dayEnd)
|
||||
records, err := SnapshotRecordsWithFallback(ctx, database, "hourly", "inventory_hourly_", "epoch", dayStart, dayEnd)
|
||||
if err != nil || len(records) == 0 {
|
||||
return
|
||||
}
|
||||
@@ -687,7 +748,7 @@ func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.
|
||||
|
||||
if strings.HasPrefix(tableName, "inventory_monthly_summary_") {
|
||||
suffix := strings.TrimPrefix(tableName, "inventory_monthly_summary_")
|
||||
monthStart, err := time.Parse("200601", suffix)
|
||||
monthStart, err := time.ParseInLocation("200601", suffix, time.Local)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -695,7 +756,7 @@ func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.
|
||||
if err := EnsureSnapshotRegistry(ctx, database); err != nil {
|
||||
return
|
||||
}
|
||||
records, err := ListSnapshotsByRange(ctx, database, "daily", monthStart, monthEnd)
|
||||
records, err := SnapshotRecordsWithFallback(ctx, database, "daily", "inventory_daily_summary_", "20060102", monthStart, monthEnd)
|
||||
if err != nil || len(records) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user