Skip to content

Commit

Permalink
[CHORE] enhancing dashboard usage
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas Takashi <[email protected]>
  • Loading branch information
nicolastakashi committed Nov 27, 2024
1 parent f708fbe commit 7609a52
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 9 additions & 3 deletions pkg/api/v1/metric_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 10 additions & 2 deletions source/grafana/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions source/perses/perses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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,
}),
}
}
}
Expand Down

0 comments on commit 7609a52

Please sign in to comment.