Refactor code to use 'any' type and improve context handling
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-02-18 16:16:27 +11:00
parent 6517a30fa2
commit f2d6b3158b
36 changed files with 197 additions and 175 deletions

View File

@@ -9,6 +9,7 @@ import (
"math"
"os"
"path/filepath"
"slices"
"sort"
"strconv"
"strings"
@@ -844,8 +845,8 @@ func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.
if logger == nil {
logger = slog.Default()
}
if strings.HasPrefix(tableName, "inventory_daily_summary_") {
suffix := strings.TrimPrefix(tableName, "inventory_daily_summary_")
if after, ok := strings.CutPrefix(tableName, "inventory_daily_summary_"); ok {
suffix := after
dayStart, err := time.ParseInLocation("20060102", suffix, time.Local)
if err != nil {
logger.Debug("hourly totals skip: invalid daily summary suffix", "table", tableName, "suffix", suffix, "error", err)
@@ -878,8 +879,8 @@ func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.
return
}
if strings.HasPrefix(tableName, "inventory_monthly_summary_") {
suffix := strings.TrimPrefix(tableName, "inventory_monthly_summary_")
if after, ok := strings.CutPrefix(tableName, "inventory_monthly_summary_"); ok {
suffix := after
monthStart, err := time.ParseInLocation("200601", suffix, time.Local)
if err != nil {
logger.Debug("daily totals skip: invalid monthly summary suffix", "table", tableName, "suffix", suffix, "error", err)
@@ -1001,10 +1002,7 @@ func titleCellFromPivotRange(pivotRange, fallback string) string {
if err != nil {
return fallback
}
titleRow := row - 2
if titleRow < 1 {
titleRow = 1
}
titleRow := max(row-2, 1)
cell, err := excelize.CoordinatesToCellName(col, titleRow)
if err != nil {
return fallback
@@ -1356,16 +1354,16 @@ func reportTypeFromTable(tableName string) string {
}
func reportWindowFromTable(tableName string) (time.Time, time.Time, bool) {
if strings.HasPrefix(tableName, "inventory_daily_summary_") {
suffix := strings.TrimPrefix(tableName, "inventory_daily_summary_")
if after, ok := strings.CutPrefix(tableName, "inventory_daily_summary_"); ok {
suffix := after
dayStart, err := time.ParseInLocation("20060102", suffix, time.Local)
if err != nil {
return time.Time{}, time.Time{}, false
}
return dayStart, dayStart.AddDate(0, 0, 1), true
}
if strings.HasPrefix(tableName, "inventory_monthly_summary_") {
suffix := strings.TrimPrefix(tableName, "inventory_monthly_summary_")
if after, ok := strings.CutPrefix(tableName, "inventory_monthly_summary_"); ok {
suffix := after
monthStart, err := time.ParseInLocation("200601", suffix, time.Local)
if err != nil {
return time.Time{}, time.Time{}, false
@@ -1383,7 +1381,7 @@ func addReportMetadataSheet(logger *slog.Logger, xlsx *excelize.File, meta repor
}
rows := []struct {
key string
value interface{}
value any
}{
{"ReportTable", meta.TableName},
{"ReportType", meta.ReportType},
@@ -1398,28 +1396,28 @@ func addReportMetadataSheet(logger *slog.Logger, xlsx *excelize.File, meta repor
rows = append(rows,
struct {
key string
value interface{}
value any
}{"DataWindowStart", meta.WindowStart.Format(time.RFC3339)},
struct {
key string
value interface{}
value any
}{"DataWindowEnd", meta.WindowEnd.Format(time.RFC3339)},
struct {
key string
value interface{}
value any
}{"DataWindowTimezone", time.Local.String()},
)
}
if meta.DBDriver != "" {
rows = append(rows, struct {
key string
value interface{}
value any
}{"DatabaseDriver", meta.DBDriver})
}
if meta.Duration > 0 && meta.RowCount > 0 {
rows = append(rows, struct {
key string
value interface{}
value any
}{"RowsPerSecond", math.Round((float64(meta.RowCount)/meta.Duration.Seconds())*1000) / 1000})
}
for i, row := range rows {
@@ -1433,9 +1431,9 @@ func addReportMetadataSheet(logger *slog.Logger, xlsx *excelize.File, meta repor
}
}
func scanRowValues(rows *sqlx.Rows, columnCount int) ([]interface{}, error) {
rawValues := make([]interface{}, columnCount)
scanArgs := make([]interface{}, columnCount)
func scanRowValues(rows *sqlx.Rows, columnCount int) ([]any, error) {
rawValues := make([]any, columnCount)
scanArgs := make([]any, columnCount)
for i := range rawValues {
scanArgs[i] = &rawValues[i]
}
@@ -1445,7 +1443,7 @@ func scanRowValues(rows *sqlx.Rows, columnCount int) ([]interface{}, error) {
return rawValues, nil
}
func normalizeCellValue(value interface{}) interface{} {
func normalizeCellValue(value any) any {
switch v := value.(type) {
case nil:
return ""
@@ -1749,14 +1747,14 @@ FROM diag, agg_diag
DeletedInInterval int64 `db:"deleted_in_interval"`
PartialPresence int64 `db:"partial_presence"`
}
overlapArgs := []interface{}{
overlapArgs := []any{
hourEndUnix, hourEndUnix,
hourStartUnix, hourStartUnix,
hourEndUnix, hourEndUnix,
hourStartUnix, hourStartUnix,
durationSeconds,
}
args := make([]interface{}, 0, len(overlapArgs)*3+6)
args := make([]any, 0, len(overlapArgs)*3+6)
args = append(args, overlapArgs...)
args = append(args, overlapArgs...)
args = append(args, hourStartUnix, hourEndUnix)
@@ -1847,7 +1845,7 @@ func estimateSnapshotInterval(records []SnapshotRecord) time.Duration {
if len(diffs) == 0 {
return time.Hour
}
sort.Slice(diffs, func(i, j int) bool { return diffs[i] < diffs[j] })
slices.Sort(diffs)
median := diffs[len(diffs)/2]
if median <= 0 {
return time.Hour
@@ -2032,7 +2030,7 @@ func writeTotalsChart(logger *slog.Logger, xlsx *excelize.File, sheetName string
makeChart("K52", "F", "G", "H", "I")
}
func formatEpochHuman(value interface{}) string {
func formatEpochHuman(value any) string {
var epoch int64
switch v := value.(type) {
case nil: