feat: enhance summary pivot specifications to include column fields
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-02-27 14:11:02 +11:00
parent 504621f80d
commit 3b0dfda992
5 changed files with 172 additions and 45 deletions

View File

@@ -918,29 +918,31 @@ func addTotalsChartSheet(logger *slog.Logger, database db.Database, ctx context.
}
type summaryPivotSpec struct {
Metric string
Title string
TitleCell string
PivotName string
PivotRange string
RowFields []string
DataField string
DataName string
DataSummary string
Metric string
Title string
TitleCell string
PivotName string
PivotRange string
RowFields []string
ColumnFields []string
DataField string
DataName string
DataSummary string
}
func defaultSummaryPivotSpecs(summarySheet string) []summaryPivotSpec {
return []summaryPivotSpec{
{
Metric: "avg_vcpu",
Title: "Sum of Avg vCPUs",
TitleCell: "A1",
PivotName: "PivotAvgVcpu",
PivotRange: fmt.Sprintf("%s!A3:H40", summarySheet),
RowFields: []string{"Datacenter", "ResourcePool"},
DataField: "AvgVcpuCount",
DataName: "Sum of Avg vCPUs",
DataSummary: "Sum",
Metric: "avg_vcpu",
Title: "Sum of Avg vCPUs",
TitleCell: "A1",
PivotName: "PivotAvgVcpu",
PivotRange: fmt.Sprintf("%s!A3:H40", summarySheet),
RowFields: []string{"Datacenter"},
ColumnFields: []string{"ResourcePool"},
DataField: "AvgVcpuCount",
DataName: "Sum of Avg vCPUs",
DataSummary: "Sum",
},
{
Metric: "avg_ram",
@@ -1117,6 +1119,19 @@ func addSummaryPivotSheet(logger *slog.Logger, xlsx *excelize.File, dataSheet st
if missingField {
continue
}
columns := make([]excelize.PivotTableField, 0, len(spec.ColumnFields))
for _, columnField := range spec.ColumnFields {
resolved, ok := resolveField(columnField)
if !ok {
logger.Warn("summary pivot skipped: missing column field", "table", tableName, "pivot", spec.PivotName, "field", columnField)
missingField = true
break
}
columns = append(columns, excelize.PivotTableField{Data: resolved})
}
if missingField {
continue
}
dataField, ok := resolveField(spec.DataField)
if !ok {
logger.Warn("summary pivot skipped: missing data field", "table", tableName, "pivot", spec.PivotName, "field", spec.DataField)
@@ -1128,6 +1143,7 @@ func addSummaryPivotSheet(logger *slog.Logger, xlsx *excelize.File, dataSheet st
DataRange: dataRange,
PivotTableRange: spec.PivotRange,
Rows: rows,
Columns: columns,
Data: []excelize.PivotTableField{{Data: dataField, Name: spec.DataName, Subtotal: spec.DataSummary}},
RowGrandTotals: true,
ColGrandTotals: true,

View File

@@ -43,10 +43,16 @@ func TestAddSummaryPivotSheetCreatesPivotTables(t *testing.T) {
"PivotProratedVmCount": false,
"PivotVmNameCount": false,
}
var avgVcpuPivot excelize.PivotTableOptions
avgVcpuFound := false
for _, pivot := range pivots {
if _, ok := expectedNames[pivot.Name]; ok {
expectedNames[pivot.Name] = true
}
if pivot.Name == "PivotAvgVcpu" {
avgVcpuPivot = pivot
avgVcpuFound = true
}
if strings.Contains(pivot.DataRange, "'") {
t.Fatalf("pivot %q has quoted DataRange %q; expected unquoted sheet reference", pivot.Name, pivot.DataRange)
}
@@ -59,4 +65,13 @@ func TestAddSummaryPivotSheetCreatesPivotTables(t *testing.T) {
t.Fatalf("missing expected pivot table %q", name)
}
}
if !avgVcpuFound {
t.Fatal("missing PivotAvgVcpu definition")
}
if len(avgVcpuPivot.Rows) != 1 || avgVcpuPivot.Rows[0].Data != "Datacenter" {
t.Fatalf("PivotAvgVcpu rows = %#v; expected Datacenter only", avgVcpuPivot.Rows)
}
if len(avgVcpuPivot.Columns) != 1 || avgVcpuPivot.Columns[0].Data != "ResourcePool" {
t.Fatalf("PivotAvgVcpu columns = %#v; expected ResourcePool only", avgVcpuPivot.Columns)
}
}