Skip to content

Commit

Permalink
Merge pull request #100 from gdashboard/compact-mode
Browse files Browse the repository at this point in the history
Implement compact mode
  • Loading branch information
iRevive authored Jan 30, 2024
2 parents 304efbf + 32ce9c1 commit 818c9ed
Show file tree
Hide file tree
Showing 18 changed files with 198 additions and 52 deletions.
1 change: 1 addition & 0 deletions docs/data-sources/bar_gauge.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ data "gdashboard_bar_gauge" "mails_sent" {

### Optional

- `compact_json` (Boolean) Whether to use compat JSON encoding or not.
- `description` (String) The description of this panel.
- `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field))
- `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph))
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/dashboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ The "HTTP" section has two panel blocks, both with dimensions `8x10` and data so
### Optional

- `annotations` (Block List) The annotations to add to the dashboard. (see [below for nested schema](#nestedblock--annotations))
- `compact_json` (Boolean) Whether to use compat JSON encoding or not.
- `description` (String) The description of the dashboard.
- `editable` (Boolean) Whether to make the dashboard editable or not.
- `graph_tooltip` (String) Controls tooltip and hover highlight behavior across different panels: `default`, `shared-crosshair`, `shared-tooltip`.
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/gauge.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ data "gdashboard_gauge" "native_memory" {

### Optional

- `compact_json` (Boolean) Whether to use compat JSON encoding or not.
- `description` (String) The description of this panel.
- `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field))
- `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph))
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/stat.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ data "gdashboard_stat" "status_2" {

### Optional

- `compact_json` (Boolean) Whether to use compat JSON encoding or not.
- `description` (String) The description of this panel.
- `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field))
- `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph))
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ data "gdashboard_table" "test" {

### Optional

- `compact_json` (Boolean) Whether to use compat JSON encoding or not.
- `description` (String) The description of this panel.
- `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field))
- `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph))
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/text.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ data "gdashboard_text" "code" {

### Optional

- `compact_json` (Boolean) Whether to use compat JSON encoding or not.
- `description` (String) The description of this panel.
- `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph))

Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/timeseries.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ data "gdashboard_timeseries" "native_memory" {
### Optional

- `axis` (Block List) Axis display options. (see [below for nested schema](#nestedblock--axis))
- `compact_json` (Boolean) Whether to use compat JSON encoding or not.
- `description` (String) The description of this panel.
- `field` (Block List) The customization of field options. (see [below for nested schema](#nestedblock--field))
- `graph` (Block List) The visualization options. (see [below for nested schema](#nestedblock--graph))
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ data "gdashboard_timeseries" "override_defaults" {

### Optional

- `compact_json` (Boolean) Whether to use compat JSON encoding or not.
- `defaults` (Block List) The default values to use with when an attribute is missing in the data source definition. (see [below for nested schema](#nestedblock--defaults))

<a id="nestedblock--defaults"></a>
Expand Down
24 changes: 18 additions & 6 deletions internal/provider/bar_gauge_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func NewBarGaugeDataSource() datasource.DataSource {

// BarGaugeDataSource defines the data source implementation.
type BarGaugeDataSource struct {
Defaults BarGaugeDefaults
CompactJson bool
Defaults BarGaugeDefaults
}

type BarGaugeDefaults struct {
Expand All @@ -44,6 +45,7 @@ type BarGaugeGraphDefault struct {
type BarGaugeDataSourceModel struct {
Id types.String `tfsdk:"id"`
Json types.String `tfsdk:"json"`
CompactJson types.Bool `tfsdk:"compact_json"`
Title types.String `tfsdk:"title"`
Description types.String `tfsdk:"description"`
Queries []Query `tfsdk:"queries"`
Expand Down Expand Up @@ -121,10 +123,11 @@ func (d *BarGaugeDataSource) Schema(ctx context.Context, req datasource.SchemaRe
},

Attributes: map[string]schema.Attribute{
"id": idAttribute(),
"json": jsonAttribute(),
"title": titleAttribute(),
"description": descriptionAttribute(),
"id": idAttribute(),
"json": jsonAttribute(),
"compact_json": compactJsonAttribute(),
"title": titleAttribute(),
"description": descriptionAttribute(),
},
}
}
Expand All @@ -144,6 +147,7 @@ func (d *BarGaugeDataSource) Configure(_ context.Context, req datasource.Configu
)
}

d.CompactJson = defaults.CompactJson
d.Defaults = defaults.BarGauge
}

Expand Down Expand Up @@ -218,7 +222,15 @@ func (d *BarGaugeDataSource) Read(ctx context.Context, req datasource.ReadReques
panel.CommonPanel.Description = &description
}

jsonData, err := json.MarshalIndent(panel, "", " ")
var jsonData []byte
var err error

if data.CompactJson.ValueBool() || d.CompactJson {
jsonData, err = json.Marshal(panel)
} else {
jsonData, err = json.MarshalIndent(panel, "", " ")
}

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err))
return
Expand Down
21 changes: 15 additions & 6 deletions internal/provider/dashboard_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func NewDashboardDataSource() datasource.DataSource {

// DashboardDataSource defines the data source implementation.
type DashboardDataSource struct {
Defaults DashboardDefaults
CompactJson bool
Defaults DashboardDefaults
}

type DashboardDefaults struct {
Expand All @@ -47,6 +48,7 @@ type Time struct {
type DashboardDataSourceModel struct {
Id types.String `tfsdk:"id"`
Json types.String `tfsdk:"json"`
CompactJson types.Bool `tfsdk:"compact_json"`
Title types.String `tfsdk:"title"`
Description types.String `tfsdk:"description"`
Version types.Int64 `tfsdk:"version"`
Expand Down Expand Up @@ -1171,10 +1173,8 @@ func (d *DashboardDataSource) Schema(ctx context.Context, req datasource.SchemaR
"id": schema.StringAttribute{
Computed: true,
},
"json": schema.StringAttribute{
Computed: true,
Description: "The Grafana-API-compatible JSON of this panel.",
},
"json": jsonAttribute(),
"compact_json": compactJsonAttribute(),
"title": schema.StringAttribute{
Required: true,
Description: "The title of the dashboard.",
Expand Down Expand Up @@ -1221,6 +1221,7 @@ func (d *DashboardDataSource) Configure(_ context.Context, req datasource.Config
)
}

d.CompactJson = defaults.CompactJson
d.Defaults = defaults.Dashboard
}

Expand Down Expand Up @@ -2009,7 +2010,15 @@ func (d *DashboardDataSource) Read(ctx context.Context, req datasource.ReadReque
dashboard.GraphTooltip = 2
}

jsonData, err := json.MarshalIndent(dashboard, "", " ")
var jsonData []byte
var err error

if data.CompactJson.ValueBool() || d.CompactJson {
jsonData, err = json.Marshal(dashboard)
} else {
jsonData, err = json.MarshalIndent(dashboard, "", " ")
}

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err))
return
Expand Down
35 changes: 35 additions & 0 deletions internal/provider/dashboard_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func TestAccDashboardDataSource(t *testing.T) {
Config: testAccDashboardDataSourceProvider_Links_Datasource_Valid,
Check: resource.TestCheckResourceAttr("data.gdashboard_dashboard.test", "json", testAccDashboardDataSourceProvider_Links_Datasource_Valid_ExpectedJson),
},
{
Config: testAccDashboardDataSourceProvider_Compact_Json,
Check: resource.TestCheckResourceAttr("data.gdashboard_dashboard.test", "json", testAccDashboardDataSourceProvider_Compact_Json_ExpectedJson),
},
},
})
}
Expand Down Expand Up @@ -2064,3 +2068,34 @@ const testAccDashboardDataSourceProvider_Links_Datasource_Valid_ExpectedJson = `
}`

////

const testAccDashboardDataSourceProvider_Compact_Json = `
data "gdashboard_dashboard" "test" {
title = "Test"
compact_json = true
links {
dashboards {
title = "My dashboards"
tags = ["tag 1", "tag-2"]
as_dropdown = true
include_time_range = true
include_template_variables = true
new_tab = true
}
external {
title = "My dashboards"
url = "https://grafana.com"
tooltip = "Some tooltip"
icon = "cloud"
include_time_range = true
include_template_variables = true
new_tab = true
}
}
layout { }
}`

const testAccDashboardDataSourceProvider_Compact_Json_ExpectedJson = `{"title":"Test","style":"dark","timezone":"","liveNow":false,"editable":true,"panels":[],"templating":{"list":[]},"annotations":{"list":null},"schemaVersion":0,"version":1,"links":[{"title":"My dashboards","type":"dashboards","asDropdown":true,"includeVars":true,"keepTime":true,"tags":["tag 1","tag-2"],"targetBlank":true},{"title":"My dashboards","type":"link","icon":"cloud","includeVars":true,"keepTime":true,"targetBlank":true,"tooltip":"Some tooltip","url":"https://grafana.com"}],"time":{"from":"now-6h","to":"now"},"timepicker":{"refresh_intervals":null,"time_options":null}}`
24 changes: 18 additions & 6 deletions internal/provider/gauge_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func NewGaugeDataSource() datasource.DataSource {

// GaugeDataSource defines the data source implementation.
type GaugeDataSource struct {
Defaults GaugeDefaults
CompactJson bool
Defaults GaugeDefaults
}

type GaugeDefaults struct {
Expand All @@ -44,6 +45,7 @@ type GaugeGraphDefault struct {
type GaugeDataSourceModel struct {
Id types.String `tfsdk:"id"`
Json types.String `tfsdk:"json"`
CompactJson types.Bool `tfsdk:"compact_json"`
Title types.String `tfsdk:"title"`
Description types.String `tfsdk:"description"`
Queries []Query `tfsdk:"queries"`
Expand Down Expand Up @@ -113,10 +115,11 @@ func (d *GaugeDataSource) Schema(ctx context.Context, req datasource.SchemaReque
},

Attributes: map[string]schema.Attribute{
"id": idAttribute(),
"json": jsonAttribute(),
"title": titleAttribute(),
"description": descriptionAttribute(),
"id": idAttribute(),
"json": jsonAttribute(),
"compact_json": compactJsonAttribute(),
"title": titleAttribute(),
"description": descriptionAttribute(),
},
}
}
Expand All @@ -136,6 +139,7 @@ func (d *GaugeDataSource) Configure(_ context.Context, req datasource.ConfigureR
)
}

d.CompactJson = defaults.CompactJson
d.Defaults = defaults.Gauge
}

Expand Down Expand Up @@ -210,7 +214,15 @@ func (d *GaugeDataSource) Read(ctx context.Context, req datasource.ReadRequest,
panel.CommonPanel.Description = &description
}

jsonData, err := json.MarshalIndent(panel, "", " ")
var jsonData []byte
var err error

if data.CompactJson.ValueBool() || d.CompactJson {
jsonData, err = json.Marshal(panel)
} else {
jsonData, err = json.MarshalIndent(panel, "", " ")
}

if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Could not marshal json: %s", err))
return
Expand Down
26 changes: 16 additions & 10 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@ type GrafanaDashboardBuilderProvider struct {
}

type Defaults struct {
Dashboard DashboardDefaults
Timeseries TimeseriesDefaults
BarGauge BarGaugeDefaults
Stat StatDefaults
Gauge GaugeDefaults
Table TableDefaults
CompactJson bool
Dashboard DashboardDefaults
Timeseries TimeseriesDefaults
BarGauge BarGaugeDefaults
Stat StatDefaults
Gauge GaugeDefaults
Table TableDefaults
}

// GrafanaDashboardBuilderProviderModel describes the provider data model.
type GrafanaDashboardBuilderProviderModel struct {
Defaults []DefaultsModel `tfsdk:"defaults"`
CompactJson types.Bool `tfsdk:"compact_json"`
Defaults []DefaultsModel `tfsdk:"defaults"`
}

type DefaultsModel struct {
Dashboard []DashboardDefaultsModel `tfsdk:"dashboard"`
Timeseries []TimeseriesDefaultsModel `tfsdk:"timeseries"`
BarGuage []BarGaugeDefaultsModel `tfsdk:"bar_gauge"`
BarGauge []BarGaugeDefaultsModel `tfsdk:"bar_gauge"`
Stat []StatDefaultsModel `tfsdk:"stat"`
Gauge []GaugeDefaultsModel `tfsdk:"gauge"`
Table []TableDefaultsModel `tfsdk:"table"`
Expand Down Expand Up @@ -92,6 +94,9 @@ func (p *GrafanaDashboardBuilderProvider) Metadata(_ context.Context, req provid
func (p *GrafanaDashboardBuilderProvider) Schema(ctx context.Context, req provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "The provider offers a handy syntax to define Grafana dashboards: time series, gauge, bar gauge, stat, etc.",
Attributes: map[string]schema.Attribute{
"compact_json": compactJsonAttribute(),
},
Blocks: map[string]schema.Block{
"defaults": schema.ListNestedBlock{
Description: "The default values to use with when an attribute is missing in the data source definition.",
Expand Down Expand Up @@ -195,6 +200,7 @@ func (p *GrafanaDashboardBuilderProvider) Configure(ctx context.Context, req pro
}

defaults := Defaults{
CompactJson: data.CompactJson.ValueBool(),
Dashboard: DashboardDefaults{
Editable: true,
Style: "dark",
Expand Down Expand Up @@ -390,8 +396,8 @@ func (p *GrafanaDashboardBuilderProvider) Configure(ctx context.Context, req pro
}
}

if len(data.Defaults) > 0 && len(data.Defaults[0].BarGuage) > 0 {
opts := data.Defaults[0].BarGuage[0]
if len(data.Defaults) > 0 && len(data.Defaults[0].BarGauge) > 0 {
opts := data.Defaults[0].BarGauge[0]

updateFieldDefaults(&defaults.BarGauge.Field, opts.Field)

Expand Down
Loading

0 comments on commit 818c9ed

Please sign in to comment.