Skip to content

Commit 02ef24d

Browse files
authored
Merge pull request #1722 from smartcontractkit/feat/CRE-1383_executive_dashboards
[CRE-1383] support grafana-polystat-plugin
2 parents 8fe3630 + dd90b9c commit 02ef24d

File tree

7 files changed

+638
-9
lines changed

7 files changed

+638
-9
lines changed

observability-lib/cmd/api/notification_policy/delete.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"strings"
66

77
"github.com/grafana/grafana-foundation-sdk/go/alerting"
8+
"github.com/spf13/cobra"
9+
810
"github.com/smartcontractkit/chainlink-common/observability-lib/api"
911
"github.com/smartcontractkit/chainlink-common/observability-lib/grafana"
10-
"github.com/spf13/cobra"
1112
)
1213

1314
var deleteCmd = &cobra.Command{
@@ -27,7 +28,7 @@ var deleteCmd = &cobra.Command{
2728
if err != nil {
2829
return err
2930
}
30-
if matchers != nil && len(matchers) > 0 {
31+
if len(matchers) > 0 {
3132
objectMatchers := alerting.ObjectMatchers{}
3233
notificationPolicy := alerting.NotificationPolicy{
3334
Receiver: grafana.Pointer(args[0]),

observability-lib/grafana/builder.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"github.com/grafana/grafana-foundation-sdk/go/cog/plugins"
99
"github.com/grafana/grafana-foundation-sdk/go/common"
1010
"github.com/grafana/grafana-foundation-sdk/go/dashboard"
11+
1112
"github.com/smartcontractkit/chainlink-common/observability-lib/grafana/businessvariable"
13+
"github.com/smartcontractkit/chainlink-common/observability-lib/grafana/polystat"
1214
)
1315

1416
type Builder struct {
@@ -35,6 +37,7 @@ type BuilderOptions struct {
3537
func NewBuilder(options *BuilderOptions) *Builder {
3638
plugins.RegisterDefaultPlugins()
3739
cog.NewRuntime().RegisterPanelcfgVariant(businessvariable.VariantConfig())
40+
cog.NewRuntime().RegisterPanelcfgVariant(polystat.VariantConfig())
3841

3942
builder := &Builder{}
4043

@@ -113,8 +116,11 @@ func (b *Builder) AddPanel(panel ...*Panel) {
113116
} else if item.businessVariablePanelBuilder != nil {
114117
item.businessVariablePanelBuilder.Id(panelID)
115118
b.dashboardBuilder.WithPanel(item.businessVariablePanelBuilder)
119+
} else if item.polystatPanelBuilder != nil {
120+
item.polystatPanelBuilder.Id(panelID)
121+
b.dashboardBuilder.WithPanel(item.polystatPanelBuilder)
116122
}
117-
if item.alertBuilders != nil && len(item.alertBuilders) > 0 {
123+
if len(item.alertBuilders) > 0 {
118124
b.AddAlert(item.alertBuilders...)
119125
}
120126
}
@@ -155,7 +161,7 @@ func (b *Builder) Build() (*Observability, error) {
155161
}
156162

157163
// Add common tags to alerts
158-
if b.alertsTags != nil && len(b.alertsTags) > 0 {
164+
if len(b.alertsTags) > 0 {
159165
tags := maps.Clone(b.alertsTags)
160166
maps.Copy(tags, alert.Labels)
161167

observability-lib/grafana/dashboard.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/grafana/grafana-foundation-sdk/go/alerting"
99
"github.com/grafana/grafana-foundation-sdk/go/dashboard"
10+
1011
"github.com/smartcontractkit/chainlink-common/observability-lib/api"
1112
)
1213

@@ -73,7 +74,7 @@ func getAlertRules(grafanaClient *api.Client, dashboardUID *string, folderUID st
7374
}
7475

7576
// check for alert rules by folder UID and group name
76-
if alertGroups != nil && len(alertGroups) > 0 {
77+
if len(alertGroups) > 0 {
7778
for _, alertGroup := range alertGroups {
7879
alertsRulePerGroup, errGetAlertRulesPerGroup := grafanaClient.GetAlertRulesByFolderUIDAndGroupName(folderUID, *alertGroup.Title)
7980
if errGetAlertRulesPerGroup != nil {
@@ -219,7 +220,7 @@ func (o *Observability) DeployToGrafana(options *DeployOptions) error {
219220
}
220221

221222
// Create contact points for the alerts
222-
if o.ContactPoints != nil && len(o.ContactPoints) > 0 {
223+
if len(o.ContactPoints) > 0 {
223224
for _, contactPoint := range o.ContactPoints {
224225
errCreateOrUpdateContactPoint := grafanaClient.CreateOrUpdateContactPoint(contactPoint)
225226
if errCreateOrUpdateContactPoint != nil {
@@ -229,7 +230,7 @@ func (o *Observability) DeployToGrafana(options *DeployOptions) error {
229230
}
230231

231232
// Create notification policies for the alerts
232-
if o.NotificationPolicies != nil && len(o.NotificationPolicies) > 0 {
233+
if len(o.NotificationPolicies) > 0 {
233234
for _, notificationPolicy := range o.NotificationPolicies {
234235
errAddNestedPolicy := grafanaClient.AddNestedPolicy(notificationPolicy)
235236
if errAddNestedPolicy != nil {

observability-lib/grafana/dashboard_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"testing"
55

66
"github.com/grafana/grafana-foundation-sdk/go/expr"
7-
"github.com/smartcontractkit/chainlink-common/observability-lib/grafana"
87
"github.com/stretchr/testify/require"
8+
9+
"github.com/smartcontractkit/chainlink-common/observability-lib/grafana"
910
)
1011

1112
func TestGenerateJSON(t *testing.T) {
@@ -83,6 +84,7 @@ func TestGenerateJSON(t *testing.T) {
8384
}
8485

8586
json, err := o.GenerateJSON()
87+
require.NoError(t, err)
8688
require.IsType(t, json, []byte{})
8789
})
8890
}

observability-lib/grafana/panels.go

Lines changed: 127 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
"github.com/grafana/grafana-foundation-sdk/go/table"
1616
"github.com/grafana/grafana-foundation-sdk/go/text"
1717
"github.com/grafana/grafana-foundation-sdk/go/timeseries"
18+
1819
"github.com/smartcontractkit/chainlink-common/observability-lib/grafana/businessvariable"
20+
"github.com/smartcontractkit/chainlink-common/observability-lib/grafana/polystat"
1921
)
2022

2123
type QueryType string
@@ -180,6 +182,7 @@ type Panel struct {
180182
textPanelBuilder *text.PanelBuilder
181183
histogramPanelBuilder *histogram.PanelBuilder
182184
businessVariablePanelBuilder *businessvariable.PanelBuilder
185+
polystatPanelBuilder *polystat.PanelBuilder
183186
alertBuilders []*alerting.RuleBuilder
184187
}
185188

@@ -437,7 +440,7 @@ func NewTimeSeriesPanel(options *TimeSeriesPanelOptions) *Panel {
437440
}
438441

439442
var alertBuilders []*alerting.RuleBuilder
440-
if options.AlertsOptions != nil && len(options.AlertsOptions) > 0 {
443+
if len(options.AlertsOptions) > 0 {
441444
for _, alert := range options.AlertsOptions {
442445
// this is used as an internal mechanism to set the panel title in the alert to associate panelId with alert
443446
alert.PanelTitle = *options.Title
@@ -1092,3 +1095,126 @@ func NewBusinessVariablePanel(options *BusinessVariablePanelOptions) *Panel {
10921095
businessVariablePanelBuilder: newPanel,
10931096
}
10941097
}
1098+
1099+
type PolystatPanelOptions struct {
1100+
*PanelOptions
1101+
Queries []Query
1102+
OperatorName polystat.OperatorName
1103+
PolygonGlobalFillColor string
1104+
PolygonSize polystat.PolygonSize
1105+
Columns *int
1106+
Rows *int
1107+
DisplayLimit *int
1108+
DefaultClickThrough string
1109+
DefaultClickThroughNewTab bool
1110+
DefaultClickThroughSanitize bool
1111+
AnimationSpeed *int
1112+
Radius string
1113+
TooltipDisplayMode string
1114+
TooltipPrimarySortBy polystat.SortByField
1115+
TooltipPrimarySortDirection polystat.SortByDirection
1116+
TooltipSecondarySortBy polystat.SortByField
1117+
TooltipSecondarySortDirection polystat.SortByDirection
1118+
GlobalUnitFormat string
1119+
GlobalDecimals *int
1120+
GlobalDisplayMode string
1121+
GlobalDisplayTextTriggeredEmpty string
1122+
GlobalThresholds []polystat.Threshold
1123+
}
1124+
1125+
func NewPolystatPanel(options *PolystatPanelOptions) *Panel {
1126+
setDefaults(options.PanelOptions)
1127+
1128+
newPanel := polystat.NewPanelBuilder().
1129+
Datasource(datasourceRef(options.Datasource)).
1130+
Title(*options.Title).
1131+
Description(options.Description).
1132+
Transparent(options.Transparent).
1133+
Span(options.Span).
1134+
Height(options.Height).
1135+
OperatorName(options.OperatorName)
1136+
1137+
if options.PolygonGlobalFillColor != "" {
1138+
newPanel.PolygonGlobalFillColor(options.PolygonGlobalFillColor)
1139+
}
1140+
1141+
if options.PolygonSize != 0 {
1142+
newPanel.PolygonSize(options.PolygonSize)
1143+
}
1144+
1145+
if options.Columns != nil {
1146+
newPanel.Columns(*options.Columns)
1147+
}
1148+
1149+
if options.Rows != nil {
1150+
newPanel.Rows(*options.Rows)
1151+
}
1152+
1153+
if options.DisplayLimit != nil {
1154+
newPanel.DisplayLimit(*options.DisplayLimit)
1155+
}
1156+
1157+
if options.DefaultClickThrough != "" {
1158+
newPanel.DefaultClickThrough(options.DefaultClickThrough)
1159+
}
1160+
1161+
newPanel.DefaultClickThroughNewTab(options.DefaultClickThroughNewTab)
1162+
1163+
newPanel.DefaultClickThroughSanitize(options.DefaultClickThroughSanitize)
1164+
1165+
if options.AnimationSpeed != nil {
1166+
newPanel.AnimationSpeed(*options.AnimationSpeed)
1167+
}
1168+
1169+
if options.Radius != "" {
1170+
newPanel.Radius(options.Radius)
1171+
}
1172+
1173+
if options.TooltipDisplayMode != "" {
1174+
newPanel.TooltipDisplayMode(options.TooltipDisplayMode)
1175+
}
1176+
1177+
if options.TooltipPrimarySortBy != "" {
1178+
newPanel.TooltipPrimarySortBy(options.TooltipPrimarySortBy)
1179+
}
1180+
1181+
if options.TooltipPrimarySortDirection != "" {
1182+
newPanel.TooltipPrimarySortDirection(options.TooltipPrimarySortDirection)
1183+
}
1184+
1185+
if options.TooltipSecondarySortBy != "" {
1186+
newPanel.TooltipSecondarySortBy(options.TooltipSecondarySortBy)
1187+
}
1188+
1189+
if options.TooltipSecondarySortDirection != "" {
1190+
newPanel.TooltipSecondarySortDirection(options.TooltipSecondarySortDirection)
1191+
}
1192+
1193+
if options.GlobalUnitFormat != "" {
1194+
newPanel.GlobalUnitFormat(options.GlobalUnitFormat)
1195+
}
1196+
1197+
if options.GlobalDecimals != nil {
1198+
newPanel.GlobalDecimals(*options.GlobalDecimals)
1199+
}
1200+
1201+
if options.GlobalDisplayMode != "" {
1202+
newPanel.GlobalDisplayMode(options.GlobalDisplayMode)
1203+
}
1204+
1205+
if options.GlobalDisplayTextTriggeredEmpty != "" {
1206+
newPanel.GlobalDisplayTextTriggeredEmpty(options.GlobalDisplayTextTriggeredEmpty)
1207+
}
1208+
1209+
if len(options.GlobalThresholds) > 0 {
1210+
newPanel.GlobalThresholds(options.GlobalThresholds)
1211+
}
1212+
1213+
for _, query := range options.Queries {
1214+
newPanel.WithTarget(newQuery(query))
1215+
}
1216+
1217+
return &Panel{
1218+
polystatPanelBuilder: newPanel,
1219+
}
1220+
}

0 commit comments

Comments
 (0)