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

@@ -8,7 +8,7 @@ import (
"log/slog"
"os"
"runtime"
"sort"
"slices"
"strings"
"sync"
"time"
@@ -295,7 +295,7 @@ func (c *CronTask) aggregateDailySummaryGo(ctx context.Context, dayStart, dayEnd
for _, snap := range hourlySnapshots {
snapTimes = append(snapTimes, snap.SnapshotTime.Unix())
}
sort.Slice(snapTimes, func(i, j int) bool { return snapTimes[i] < snapTimes[j] })
slices.Sort(snapTimes)
}
lifecycleDeletions := c.applyLifecycleDeletions(ctx, aggMap, dayStart, dayEnd)
@@ -353,7 +353,7 @@ LIMIT 1
for t := range set {
times = append(times, t)
}
sort.Slice(times, func(i, j int) bool { return times[i] < times[j] })
slices.Sort(times)
vcenterSnapTimes[vcenter] = times
}
@@ -843,20 +843,12 @@ func (c *CronTask) applyInventoryCreations(ctx context.Context, agg map[dailyAgg
func (c *CronTask) scanHourlyTablesParallel(ctx context.Context, snapshots []report.SnapshotRecord) (map[dailyAggKey]*dailyAggVal, error) {
agg := make(map[dailyAggKey]*dailyAggVal, 1024)
mu := sync.Mutex{}
workers := runtime.NumCPU()
if workers < 2 {
workers = 2
}
if workers > len(snapshots) {
workers = len(snapshots)
}
workers := min(max(runtime.NumCPU(), 2), len(snapshots))
jobs := make(chan report.SnapshotRecord, len(snapshots))
wg := sync.WaitGroup{}
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
defer wg.Done()
wg.Go(func() {
for snap := range jobs {
rows, err := c.scanHourlyTable(ctx, snap)
if err != nil {
@@ -873,7 +865,7 @@ func (c *CronTask) scanHourlyTablesParallel(ctx context.Context, snapshots []rep
}
mu.Unlock()
}
}()
})
}
for _, snap := range snapshots {
jobs <- snap
@@ -1114,7 +1106,7 @@ WHERE "SnapshotTime" >= ? AND "SnapshotTime" < ?`
for t := range timeSet {
snapTimes = append(snapTimes, t)
}
sort.Slice(snapTimes, func(i, j int) bool { return snapTimes[i] < snapTimes[j] })
slices.Sort(snapTimes)
return agg, snapTimes, rows.Err()
}
@@ -1166,7 +1158,7 @@ INSERT INTO %s (
silverPct = float64(v.silverHits) * 100 / float64(v.samples)
goldPct = float64(v.goldHits) * 100 / float64(v.samples)
}
args := []interface{}{
args := []any{
v.key.Name,
v.key.Vcenter,
nullIfEmpty(v.key.VmId),
@@ -1214,7 +1206,7 @@ func int64OrZero(v sql.NullInt64) int64 {
return 0
}
func nullIfEmpty(s string) interface{} {
func nullIfEmpty(s string) any {
if strings.TrimSpace(s) == "" {
return nil
}
@@ -1224,13 +1216,13 @@ func nullIfEmpty(s string) interface{} {
func makePlaceholders(driver string, n int) string {
if driver == "sqlite" {
parts := make([]string, n)
for i := 0; i < n; i++ {
for i := range n {
parts[i] = "?"
}
return strings.Join(parts, ",")
}
parts := make([]string, n)
for i := 0; i < n; i++ {
for i := range n {
parts[i] = fmt.Sprintf("$%d", i+1)
}
return strings.Join(parts, ",")