From 7c43b18658363eeca7952e5b200e84d97c67fd04 Mon Sep 17 00:00:00 2001 From: Chris Grindstaff Date: Tue, 7 Jan 2025 13:59:59 -0500 Subject: [PATCH] feat: adding the bucket and policy rest template to the data protection dashboard --- .../clusterschedule/clusterschedule.go | 36 +++++++++---------- .../plugins/snapshotpolicy/snapshotpolicy.go | 12 ++++--- .../plugins/snapshotpolicy/snapshotpolicy.go | 11 ++++-- 3 files changed, 33 insertions(+), 26 deletions(-) diff --git a/cmd/collectors/rest/plugins/clusterschedule/clusterschedule.go b/cmd/collectors/rest/plugins/clusterschedule/clusterschedule.go index 6447ddd55..4736860ef 100644 --- a/cmd/collectors/rest/plugins/clusterschedule/clusterschedule.go +++ b/cmd/collectors/rest/plugins/clusterschedule/clusterschedule.go @@ -26,24 +26,10 @@ func (c *ClusterScheule) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matri cron := instance.GetLabel("cron") updateDetailsJSON := gjson.Result{Type: gjson.JSON, Raw: cron} var cronVal, minStr, hourStr, weekDayStr string - if minutes := updateDetailsJSON.Get("minutes"); minutes.Exists() { - for _, m := range minutes.Array() { - minStr = minStr + m.String() + ", " - } - minStr = strings.TrimSuffix(minStr, ", ") - } - if hours := updateDetailsJSON.Get("hours"); hours.Exists() { - for _, h := range hours.Array() { - hourStr = hourStr + h.String() + ", " - } - hourStr = strings.TrimSuffix(hourStr, ", ") - } - if weekdays := updateDetailsJSON.Get("weekdays"); weekdays.Exists() { - for _, w := range weekdays.Array() { - weekDayStr = weekDayStr + w.String() + ", " - } - weekDayStr = strings.TrimSuffix(weekDayStr, ", ") - } + + minStr = list(updateDetailsJSON.Get("minutes")) + hourStr = list(updateDetailsJSON.Get("hours")) + weekDayStr = list(updateDetailsJSON.Get("weekdays")) if minStr != "" { cronVal = cronVal + "minutes: " + "[" + minStr + "] " @@ -54,7 +40,19 @@ func (c *ClusterScheule) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matri if weekDayStr != "" { cronVal = cronVal + "weekdays: " + "[" + weekDayStr + "]" } - instance.SetLabel("cron", cronVal) + instance.SetLabel("cron", strings.TrimSpace(cronVal)) } return nil, nil, nil } + +func list(get gjson.Result) string { + if !get.IsArray() { + return "" + } + array := get.Array() + items := make([]string, 0, len(array)) + for _, e := range array { + items = append(items, e.ClonedString()) + } + return strings.Join(items, ", ") +} diff --git a/cmd/collectors/rest/plugins/snapshotpolicy/snapshotpolicy.go b/cmd/collectors/rest/plugins/snapshotpolicy/snapshotpolicy.go index ee8705a17..d44f83a0b 100644 --- a/cmd/collectors/rest/plugins/snapshotpolicy/snapshotpolicy.go +++ b/cmd/collectors/rest/plugins/snapshotpolicy/snapshotpolicy.go @@ -9,6 +9,7 @@ import ( "github.com/netapp/harvest/v2/pkg/matrix" "github.com/netapp/harvest/v2/pkg/util" "github.com/netapp/harvest/v2/third_party/tidwall/gjson" + "slices" "strconv" "strings" ) @@ -28,16 +29,19 @@ func (m *SnapshotPolicy) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matri for _, instance := range data.GetInstances() { copies := instance.GetLabel("copies") copiesJSON := gjson.Result{Type: gjson.JSON, Raw: "[" + copies + "]"} - var scheduleVal string var copiesValue int + var schedules []string for _, copiesData := range copiesJSON.Array() { - count := copiesData.Get("count").String() + count := copiesData.Get("count").ClonedString() countVal, _ := strconv.Atoi(count) schedule := copiesData.Get("schedule.name").ClonedString() - scheduleVal = scheduleVal + schedule + ":" + count + "," + schedules = append(schedules, schedule+":"+count) copiesValue += countVal } - instance.SetLabel("schedules", strings.TrimSuffix(scheduleVal, ",")) + + slices.Sort(schedules) + + instance.SetLabel("schedules", strings.Join(schedules, ",")) instance.SetLabel("copies", strconv.Itoa(copiesValue)) } diff --git a/cmd/collectors/zapi/plugins/snapshotpolicy/snapshotpolicy.go b/cmd/collectors/zapi/plugins/snapshotpolicy/snapshotpolicy.go index 4f028c29e..c4981b484 100644 --- a/cmd/collectors/zapi/plugins/snapshotpolicy/snapshotpolicy.go +++ b/cmd/collectors/zapi/plugins/snapshotpolicy/snapshotpolicy.go @@ -8,6 +8,7 @@ import ( "github.com/netapp/harvest/v2/cmd/poller/plugin" "github.com/netapp/harvest/v2/pkg/matrix" "github.com/netapp/harvest/v2/pkg/util" + "slices" "strconv" "strings" ) @@ -27,17 +28,21 @@ func (m *SnapshotPolicy) Run(dataMap map[string]*matrix.Matrix) ([]*matrix.Matri for _, instance := range data.GetInstances() { copies := strings.Split(instance.GetLabel("copies"), ",") schedules := strings.Split(instance.GetLabel("schedules"), ",") + var schedulesS []string - var scheduleVal string var copiesValue int if len(copies) > 1 { for index, copiesData := range copies { countVal, _ := strconv.Atoi(copiesData) schedule := schedules[index] - scheduleVal = scheduleVal + schedule + ":" + copiesData + "," + schedulesS = append(schedulesS, schedule+":"+copiesData) + copiesValue += countVal } - instance.SetLabel("schedules", strings.TrimSuffix(scheduleVal, ",")) + + slices.Sort(schedulesS) + + instance.SetLabel("schedules", strings.Join(schedulesS, ",")) instance.SetLabel("copies", strconv.Itoa(copiesValue)) } }