Refactor code to use 'any' type and improve context handling
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -180,7 +180,7 @@ WHERE job_name = ?
|
||||
return err
|
||||
}
|
||||
|
||||
func nullableString(s string) interface{} {
|
||||
func nullableString(s string) any {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -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, ",")
|
||||
|
||||
@@ -61,7 +61,7 @@ func insertHourlyCache(ctx context.Context, dbConn *sqlx.DB, rows []InventorySna
|
||||
defer stmt.Close()
|
||||
|
||||
for _, r := range rows {
|
||||
args := []interface{}{
|
||||
args := []any{
|
||||
r.SnapshotTime, r.Vcenter, r.VmId, r.VmUuid, r.Name, r.CreationTime, r.DeletionTime, r.ResourcePool,
|
||||
r.Datacenter, r.Cluster, r.Folder, r.ProvisionedDisk, r.VcpuCount, r.RamGB, r.IsTemplate, r.PoweredOn, r.SrmPlaceholder,
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func insertHourlyBatch(ctx context.Context, dbConn *sqlx.DB, tableName string, r
|
||||
}
|
||||
defer stmt.Close()
|
||||
for _, row := range rows {
|
||||
args := []interface{}{
|
||||
args := []any{
|
||||
row.InventoryId,
|
||||
row.Name,
|
||||
row.Vcenter,
|
||||
@@ -138,7 +138,7 @@ func insertHourlyBatch(ctx context.Context, dbConn *sqlx.DB, tableName string, r
|
||||
defer stmt.Close()
|
||||
|
||||
for _, row := range rows {
|
||||
args := []interface{}{
|
||||
args := []any{
|
||||
row.InventoryId,
|
||||
row.Name,
|
||||
row.Vcenter,
|
||||
|
||||
@@ -27,7 +27,7 @@ func acquireSnapshotProbe(ctx context.Context) (func(), error) {
|
||||
}
|
||||
}
|
||||
|
||||
func boolStringFromInterface(value interface{}) string {
|
||||
func boolStringFromInterface(value any) string {
|
||||
switch v := value.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
@@ -164,7 +164,7 @@ func SnapshotTooSoon(prevUnix, currUnix int64, expectedSeconds int64) bool {
|
||||
}
|
||||
|
||||
// querySnapshotRows builds a SELECT with proper rebind for the given table/columns/where.
|
||||
func querySnapshotRows(ctx context.Context, dbConn *sqlx.DB, table string, columns []string, where string, args ...interface{}) (*sqlx.Rows, error) {
|
||||
func querySnapshotRows(ctx context.Context, dbConn *sqlx.DB, table string, columns []string, where string, args ...any) (*sqlx.Rows, error) {
|
||||
if err := db.ValidateTableName(table); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -24,8 +24,6 @@ import (
|
||||
"github.com/vmware/govmomi/vim25/types"
|
||||
)
|
||||
|
||||
type ctxLoggerKey struct{}
|
||||
|
||||
type deletionCandidate struct {
|
||||
vmID string
|
||||
vmUUID string
|
||||
@@ -42,10 +40,7 @@ type vcenterResources struct {
|
||||
}
|
||||
|
||||
func loggerFromCtx(ctx context.Context, fallback *slog.Logger) *slog.Logger {
|
||||
if ctx == nil {
|
||||
return fallback
|
||||
}
|
||||
if l, ok := ctx.Value(ctxLoggerKey{}).(*slog.Logger); ok && l != nil {
|
||||
if l := db.LoggerFromContext(ctx); l != nil {
|
||||
return l
|
||||
}
|
||||
return fallback
|
||||
@@ -132,10 +127,7 @@ func (c *CronTask) RunVcenterSnapshotHourly(ctx context.Context, logger *slog.Lo
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
minIntervalSeconds := intWithDefault(c.Settings.Values.Settings.VcenterInventorySnapshotSeconds, 3600) / 3
|
||||
if minIntervalSeconds < 1 {
|
||||
minIntervalSeconds = 1
|
||||
}
|
||||
minIntervalSeconds := max(intWithDefault(c.Settings.Values.Settings.VcenterInventorySnapshotSeconds, 3600)/3, 1)
|
||||
if !lastSnapshot.IsZero() && startTime.Sub(lastSnapshot) < time.Duration(minIntervalSeconds)*time.Second {
|
||||
c.Logger.Info("Skipping hourly snapshot, last snapshot too recent",
|
||||
"last_snapshot", lastSnapshot,
|
||||
@@ -217,7 +209,7 @@ func (c *CronTask) RunVcenterSnapshotHourly(ctx context.Context, logger *slog.Lo
|
||||
|
||||
metrics.RecordHourlySnapshot(startTime, rowCount, err)
|
||||
var deferredTables []string
|
||||
deferredReportTables.Range(func(key, _ interface{}) bool {
|
||||
deferredReportTables.Range(func(key, _ any) bool {
|
||||
name, ok := key.(string)
|
||||
if ok && strings.TrimSpace(name) != "" && name != tableName {
|
||||
deferredTables = append(deferredTables, name)
|
||||
@@ -488,10 +480,7 @@ func buildUnionQuery(tables []string, columns []string, whereClause string) (str
|
||||
batches := make([]string, 0, (len(tables)/maxCompoundTerms)+1)
|
||||
batchIndex := 0
|
||||
for start := 0; start < len(tables); start += maxCompoundTerms {
|
||||
end := start + maxCompoundTerms
|
||||
if end > len(tables) {
|
||||
end = len(tables)
|
||||
}
|
||||
end := min(start+maxCompoundTerms, len(tables))
|
||||
queries := make([]string, 0, end-start)
|
||||
for _, table := range tables[start:end] {
|
||||
safeName, err := db.SafeTableName(table)
|
||||
@@ -1337,7 +1326,7 @@ func (c *CronTask) initVcenterResources(ctx context.Context, log *slog.Logger, u
|
||||
|
||||
func (c *CronTask) captureHourlySnapshotForVcenter(ctx context.Context, startTime time.Time, tableName string, url string, deferredReportTables *sync.Map) error {
|
||||
log := c.Logger.With("vcenter", url)
|
||||
ctx = context.WithValue(ctx, ctxLoggerKey{}, log)
|
||||
ctx = db.WithLoggerContext(ctx, log)
|
||||
started := time.Now()
|
||||
log.Debug("connecting to vcenter for hourly snapshot", "url", url)
|
||||
vc, resources, cleanup, err := c.initVcenterResources(ctx, log, url, startTime, started)
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"runtime"
|
||||
"sort"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -246,7 +246,7 @@ func (c *CronTask) aggregateMonthlySummaryGoHourly(ctx context.Context, monthSta
|
||||
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, monthStart, monthEnd)
|
||||
@@ -394,20 +394,12 @@ func (c *CronTask) aggregateMonthlySummaryGo(ctx context.Context, monthStart, mo
|
||||
func (c *CronTask) scanDailyTablesParallel(ctx context.Context, snapshots []report.SnapshotRecord) (map[monthlyAggKey]*monthlyAggVal, error) {
|
||||
agg := make(map[monthlyAggKey]*monthlyAggVal, 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.scanDailyTable(ctx, snap)
|
||||
if err != nil {
|
||||
@@ -424,7 +416,7 @@ func (c *CronTask) scanDailyTablesParallel(ctx context.Context, snapshots []repo
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
for _, snap := range snapshots {
|
||||
jobs <- snap
|
||||
|
||||
Reference in New Issue
Block a user