From 169593d1343ffef6f2ec6d8f494ac73a42554d97 Mon Sep 17 00:00:00 2001 From: Nicolas Takashi Date: Thu, 28 Nov 2024 22:01:52 +0000 Subject: [PATCH] [CHORE] enhancing dashboard usage Signed-off-by: Nicolas Takashi --- pkg/api/v1/metric_usage.go | 12 +++++++++--- source/grafana/grafana.go | 12 ++++++++++-- source/perses/perses.go | 12 ++++++++++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/pkg/api/v1/metric_usage.go b/pkg/api/v1/metric_usage.go index 48ad142..233d5f0 100644 --- a/pkg/api/v1/metric_usage.go +++ b/pkg/api/v1/metric_usage.go @@ -111,10 +111,16 @@ type RuleUsage struct { Expression string `json:"expression"` } +type DashboardUsage struct { + ID string `json:"uid"` + Name string `json:"title"` + URL string `json:"url"` +} + type MetricUsage struct { - Dashboards Set[string] `json:"dashboards,omitempty"` - RecordingRules Set[RuleUsage] `json:"recordingRules,omitempty"` - AlertRules Set[RuleUsage] `json:"alertRules,omitempty"` + Dashboards Set[DashboardUsage] `json:"dashboards,omitempty"` + RecordingRules Set[RuleUsage] `json:"recordingRules,omitempty"` + AlertRules Set[RuleUsage] `json:"alertRules,omitempty"` } func MergeUsage(old, new *MetricUsage) *MetricUsage { diff --git a/source/grafana/grafana.go b/source/grafana/grafana.go index ea2a1cd..4a18714 100644 --- a/source/grafana/grafana.go +++ b/source/grafana/grafana.go @@ -144,10 +144,18 @@ func (c *grafanaCollector) generateUsage(metricNames modelAPIV1.Set[string], cur dashboardURL := fmt.Sprintf("%s/d/%s", c.grafanaURL, currentDashboard.UID) for metricName := range metricNames { if usage, ok := metricUsage[metricName]; ok { - usage.Dashboards.Add(dashboardURL) + usage.Dashboards.Add(modelAPIV1.DashboardUsage{ + ID: currentDashboard.UID, + Name: currentDashboard.Title, + URL: dashboardURL, + }) } else { metricUsage[metricName] = &modelAPIV1.MetricUsage{ - Dashboards: modelAPIV1.NewSet(dashboardURL), + Dashboards: modelAPIV1.NewSet(modelAPIV1.DashboardUsage{ + ID: currentDashboard.UID, + Name: currentDashboard.Title, + URL: dashboardURL, + }), } } } diff --git a/source/perses/perses.go b/source/perses/perses.go index 98926b8..bb604a4 100644 --- a/source/perses/perses.go +++ b/source/perses/perses.go @@ -90,10 +90,18 @@ func (c *persesCollector) generateUsage(metricNames modelAPIV1.Set[string], curr dashboardURL := fmt.Sprintf("%s/api/v1/projects/%s/dashboards/%s", c.persesURL, currentDashboard.Metadata.Project, currentDashboard.Metadata.Name) for metricName := range metricNames { if usage, ok := metricUsage[metricName]; ok { - usage.Dashboards.Add(dashboardURL) + usage.Dashboards.Add(modelAPIV1.DashboardUsage{ + ID: fmt.Sprintf("%s/%s", currentDashboard.Metadata.Project, currentDashboard.Metadata.Name), + Name: currentDashboard.Metadata.Name, + URL: dashboardURL, + }) } else { metricUsage[metricName] = &modelAPIV1.MetricUsage{ - Dashboards: modelAPIV1.NewSet(dashboardURL), + Dashboards: modelAPIV1.NewSet(modelAPIV1.DashboardUsage{ + ID: fmt.Sprintf("%s/%s", currentDashboard.Metadata.Project, currentDashboard.Metadata.Name), + Name: currentDashboard.Metadata.Name, + URL: dashboardURL, + }), } } }