fix daily aggregation report
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-01-15 12:38:11 +11:00
parent 3f985dcd4d
commit bba308ad28
2 changed files with 25 additions and 15 deletions

View File

@@ -507,6 +507,7 @@ WITH daily AS (
), totals AS ( ), totals AS (
SELECT COALESCE(SUM(total_samples_day), 0) AS total_samples FROM enriched SELECT COALESCE(SUM(total_samples_day), 0) AS total_samples FROM enriched
) )
-- monthly averages are weighted by the implied sample counts per day (SamplesPresent / AvgIsPresent)
INSERT INTO %s ( INSERT INTO %s (
"InventoryId", "Name", "Vcenter", "VmId", "EventKey", "CloudId", "CreationTime", "DeletionTime", "InventoryId", "Name", "Vcenter", "VmId", "EventKey", "CloudId", "CreationTime", "DeletionTime",
"ResourcePool", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "VcpuCount", "ResourcePool", "Datacenter", "Cluster", "Folder", "ProvisionedDisk", "VcpuCount",

View File

@@ -760,7 +760,7 @@ func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.
if err != nil || len(records) == 0 { if err != nil || len(records) == 0 {
return return
} }
points, err := buildDailyTotals(ctx, database.DB(), records) points, err := buildDailyTotals(ctx, database.DB(), records, true)
if err != nil || len(points) == 0 { if err != nil || len(points) == 0 {
return return
} }
@@ -1046,7 +1046,8 @@ WHERE %s
VmCount: row.VmCount, VmCount: row.VmCount,
VcpuTotal: float64(row.VcpuTotal), VcpuTotal: float64(row.VcpuTotal),
RamTotal: float64(row.RamTotal), RamTotal: float64(row.RamTotal),
PresenceRatio: row.PresenceRatio, // For hourly snapshots, prorated VM count equals VM count (no finer granularity).
PresenceRatio: float64(row.VmCount),
TinTotal: row.TinTotal, TinTotal: row.TinTotal,
BronzeTotal: row.BronzeTotal, BronzeTotal: row.BronzeTotal,
SilverTotal: row.SilverTotal, SilverTotal: row.SilverTotal,
@@ -1056,7 +1057,7 @@ WHERE %s
return points, nil return points, nil
} }
func buildDailyTotals(ctx context.Context, dbConn *sqlx.DB, records []SnapshotRecord) ([]totalsPoint, error) { func buildDailyTotals(ctx context.Context, dbConn *sqlx.DB, records []SnapshotRecord, prorateByAvg bool) ([]totalsPoint, error) {
points := make([]totalsPoint, 0, len(records)) points := make([]totalsPoint, 0, len(records))
for _, record := range records { for _, record := range records {
if err := db.ValidateTableName(record.TableName); err != nil { if err := db.ValidateTableName(record.TableName); err != nil {
@@ -1096,7 +1097,7 @@ WHERE %s
VmCount: row.VmCount, VmCount: row.VmCount,
VcpuTotal: row.VcpuTotal, VcpuTotal: row.VcpuTotal,
RamTotal: row.RamTotal, RamTotal: row.RamTotal,
PresenceRatio: row.PresenceRatio, PresenceRatio: computeProratedVmCount(row.PresenceRatio, row.VmCount, prorateByAvg),
TinTotal: row.TinTotal, TinTotal: row.TinTotal,
BronzeTotal: row.BronzeTotal, BronzeTotal: row.BronzeTotal,
SilverTotal: row.SilverTotal, SilverTotal: row.SilverTotal,
@@ -1106,6 +1107,13 @@ WHERE %s
return points, nil return points, nil
} }
func computeProratedVmCount(presenceRatio float64, vmCount int64, prorate bool) float64 {
if !prorate {
return float64(vmCount)
}
return presenceRatio * float64(vmCount)
}
func writeTotalsChart(logger *slog.Logger, xlsx *excelize.File, sheetName string, points []totalsPoint) { func writeTotalsChart(logger *slog.Logger, xlsx *excelize.File, sheetName string, points []totalsPoint) {
if len(points) == 0 { if len(points) == 0 {
return return
@@ -1167,6 +1175,7 @@ func writeTotalsChart(logger *slog.Logger, xlsx *excelize.File, sheetName string
Legend: excelize.ChartLegend{Position: "bottom"}, Legend: excelize.ChartLegend{Position: "bottom"},
XAxis: excelize.ChartAxis{MajorGridLines: true}, XAxis: excelize.ChartAxis{MajorGridLines: true},
YAxis: excelize.ChartAxis{MajorGridLines: true}, YAxis: excelize.ChartAxis{MajorGridLines: true},
Dimension: excelize.ChartDimension{Width: 960, Height: 480},
} }
if err := xlsx.AddChart(sheetName, anchor, &chart); err != nil { if err := xlsx.AddChart(sheetName, anchor, &chart); err != nil {
logger.Error("Error adding totals chart", "error", err, "anchor", anchor) logger.Error("Error adding totals chart", "error", err, "anchor", anchor)
@@ -1174,8 +1183,8 @@ func writeTotalsChart(logger *slog.Logger, xlsx *excelize.File, sheetName string
} }
makeChart("K2", "B", "E") makeChart("K2", "B", "E")
makeChart("K18", "C", "D") makeChart("K27", "C", "D")
makeChart("K34", "F", "G", "H", "I") makeChart("K52", "F", "G", "H", "I")
} }
func formatEpochHuman(value interface{}) string { func formatEpochHuman(value interface{}) string {