Skip to content

Commit

Permalink
fix(dashboard): Updated the Dashboard JSON to HCL Converter Utility w…
Browse files Browse the repository at this point in the history
…ith Latest Fields in the Resource
  • Loading branch information
vinay-newrelic committed Nov 22, 2024
1 parent 69b8299 commit 809b0f1
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions internal/utils/terraform/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package terraform

import (
"encoding/json"
"fmt"
"strings"

log "github.com/sirupsen/logrus"
Expand All @@ -27,26 +28,35 @@ var (
"viz.stacked-bar": "widget_stacked_bar",
"logger.log-table-widget": "widget_log_table",
}
ThresholdSeverityValues = map[string]string{
"CRITICAL": "critical",
"WARNING": "warning",
}
)

type DashboardWidgetRawConfiguration struct {
DataFormatters []DataFormatter `json:"dataFormatters"`
NRQLQueries []DashboardWidgetNRQLQuery `json:"nrqlQueries"`
LinkedEntityGUIDs []string `json:"linkedEntityGuids"`
Text string `json:"text"`
Limit float64 `json:"limit,omitempty"`
Facet DashboardWidgetFacet `json:"facet,omitempty"`
Legend DashboardWidgetLegend `json:"legend,omitempty"`
Threshold json.RawMessage `json:"thresholds,omitempty"`
YAxisLeft DashboardWidgetYAxisLeft `json:"yAxisLeft,omitempty"`
NullValues DashboardWidgetNullValues `json:"nullValues,omitempty"`
Units DashboardWidgetUnits `json:"units,omitempty"`
Colors DashboardWidgetColors `json:"colors,omitempty"`
PlatformOptions DashboardWidgetPlatformOptions `json:"platformOptions,omitempty"`
RefreshRate DashboardWidgetRefreshRate `json:"refreshRate,omitempty"`
InitialSorting DashboardWidgetInitialSorting `json:"initialSorting,omitempty"`
}

type DataFormatter struct {
Name string `json:"name"`
Precision interface{} `json:"precision"`
Type string `json:"type"`
Format string `json:"format"`
}

type DashboardWidgetFacet struct {
Expand All @@ -62,6 +72,23 @@ type DashboardWidgetLegend struct {
Enabled bool `json:"enabled,omitempty"`
}

type DashboardWidgetThreshold struct {
From float64 `json:"from,omitempty"`
Name string `json:"name,omitempty"`
Severity string `json:"severity,omitempty"`
To float64 `json:"to,omitempty"`
}

type DashboardWidgetLineThreshold struct {
IsLabelVisible bool `json:"isLabelVisible,omitempty"`
Threshold []DashboardWidgetThreshold `json:"thresholds,omitempty"`
}

type DashboardWidgetBillBoardThreshold struct {
AlertSeverity string `json:"alertSeverity,omitempty"`
Value float64 `json:"value,omitempty"`
}

type DashboardWidgetYAxisLeft struct {
Max float64 `json:"max,omitempty"`
Min float64 `json:"min,omitempty"`
Expand Down Expand Up @@ -96,10 +123,20 @@ type DashboardWidgetColorOverrides struct {
Color string `json:"color,omitempty"`
SeriesName string `json:"seriesName,omitempty"`
}

type DashboardWidgetPlatformOptions struct {
IgnoreTimeRange bool `json:"ignoreTimeRange,omitempty"`
}

type DashboardWidgetRefreshRate struct {
Frequency interface{} `json:"frequency,omitempty"`
}

type DashboardWidgetInitialSorting struct {
Direction string `json:"direction"`
Name string `json:"name"`
}

func GenerateDashboardHCL(resourceLabel string, shiftWidth int, input []byte) (string, error) {

Check failure on line 140 in internal/utils/terraform/dashboard.go

View workflow job for this annotation

GitHub Actions / lint

cyclomatic complexity 25 of func `GenerateDashboardHCL` is high (> 20) (gocyclo)
var d dashboards.DashboardInput
if err := json.Unmarshal(input, &d); err != nil {
Expand Down Expand Up @@ -143,9 +180,64 @@ func GenerateDashboardHCL(resourceLabel string, shiftWidth int, input []byte) (s
h.WriteBooleanAttribute("ignore_time_range", config.PlatformOptions.IgnoreTimeRange)
h.WriteFloatAttribute("y_axis_left_min", config.YAxisLeft.Min)
h.WriteFloatAttribute("y_axis_left_max", config.YAxisLeft.Max)
writeInterfaceValues(h, "refresh_rate", config.RefreshRate.Frequency) // function to handle different types of refresh rates which cant be handled through struct

if w.Visualization.ID == "viz.line" {
h.WriteBooleanAttribute("y_axis_left_zero", config.YAxisLeft.Zero)

var widgetLineThreshold DashboardWidgetLineThreshold
if err := json.Unmarshal(config.Threshold, &widgetLineThreshold); err != nil {
log.Fatal("Error unmarshalling widgetLineThreshold:", err)
}

h.WriteBooleanAttribute("is_label_visible", widgetLineThreshold.IsLabelVisible)

for _, q := range widgetLineThreshold.Threshold {
h.WriteBlock("threshold", []string{}, func() {
h.WriteStringAttribute("name", q.Name)
h.WriteStringAttribute("severity", q.Severity)
h.WriteFloatAttribute("from", q.From)
h.WriteFloatAttribute("to", q.To)
})
}
}
if w.Visualization.ID == "viz.billboard" {
var billboardThreshold []DashboardWidgetBillBoardThreshold
if err := json.Unmarshal(config.Threshold, &billboardThreshold); err != nil {
log.Fatal("Error unmarshalling billboardThreshold:", err)
}
for _, q := range billboardThreshold {
h.WriteFloatAttribute(ThresholdSeverityValues[q.AlertSeverity], q.Value)
}

for _, q := range config.DataFormatters {
h.WriteBlock("data_format", []string{}, func() {
h.WriteStringAttribute("name", q.Name)
h.WriteStringAttribute("type", q.Type)
h.WriteStringAttribute("format", q.Format)
writeInterfaceValues(h, "precision", q.Precision) // function to handle different types of precision
})
}
}
if w.Visualization.ID == "viz.table" {
h.WriteBlock("initial_sorting", []string{}, func() {
h.WriteStringAttribute("name", config.InitialSorting.Name)
h.WriteStringAttribute("direction", config.InitialSorting.Direction)
})
for _, q := range config.DataFormatters {
h.WriteBlock("data_format", []string{}, func() {
h.WriteStringAttribute("name", q.Name)
h.WriteStringAttribute("type", q.Type)
h.WriteStringAttribute("format", q.Format)
writeInterfaceValues(h, "precision", q.Precision) // function to handle different types of precision
})
}
}

if w.Visualization.ID == "viz.bullet" {
h.WriteFloatAttribute("limit", config.Limit)
}

h.WriteBlock("null_values", []string{}, func() {
h.WriteStringAttribute("null_value", config.NullValues.NullValue)
for _, so := range config.NullValues.SeriesOverrides {
Expand Down Expand Up @@ -238,3 +330,14 @@ func requireValidVisualizationID(id string) {
log.Fatalf("unrecognized widget type \"%s\"", id)
}
}

func writeInterfaceValues(h *HCLGen, title string, titleValue interface{}) {
switch titleValue.(type) {

Check failure on line 335 in internal/utils/terraform/dashboard.go

View workflow job for this annotation

GitHub Actions / lint

S1034: assigning the result of this type assertion to a variable (switch titleValue := titleValue.(type)) could eliminate type assertions in switch cases (gosimple)
case string:
h.WriteStringAttribute(title, fmt.Sprintf("%s", titleValue)) // string without quotes
case float64:
h.WriteFloatAttribute(title, titleValue.(float64)) // integer without quotes

Check failure on line 339 in internal/utils/terraform/dashboard.go

View workflow job for this annotation

GitHub Actions / lint

S1034(related information): could eliminate this type assertion (gosimple)
default:
h.WriteStringAttribute(title, fmt.Sprintf("%s", ""))

Check failure on line 341 in internal/utils/terraform/dashboard.go

View workflow job for this annotation

GitHub Actions / lint

S1025: the argument is already a string, there's no need to use fmt.Sprintf (gosimple)
}
}

0 comments on commit 809b0f1

Please sign in to comment.