-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from K-Phoen/timeseries
Timeseries
- Loading branch information
Showing
39 changed files
with
3,472 additions
and
1,872 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package converter | ||
|
||
import ( | ||
grabana "github.com/K-Phoen/grabana/decoder" | ||
"github.com/K-Phoen/sdk" | ||
) | ||
|
||
func (converter *JSON) convertAlert(panel sdk.Panel) *grabana.Alert { | ||
if panel.Alert == nil { | ||
return nil | ||
} | ||
|
||
sdkAlert := panel.Alert | ||
|
||
notifications := make([]string, 0, len(sdkAlert.Notifications)) | ||
for _, notification := range sdkAlert.Notifications { | ||
notifications = append(notifications, notification.UID) | ||
} | ||
|
||
alert := &grabana.Alert{ | ||
Title: sdkAlert.Name, | ||
Message: sdkAlert.Message, | ||
EvaluateEvery: sdkAlert.Frequency, | ||
For: sdkAlert.For, | ||
Tags: sdkAlert.AlertRuleTags, | ||
OnNoData: sdkAlert.NoDataState, | ||
OnExecutionError: sdkAlert.ExecutionErrorState, | ||
Notifications: notifications, | ||
If: converter.convertAlertConditions(sdkAlert), | ||
} | ||
|
||
return alert | ||
} | ||
|
||
func (converter *JSON) convertAlertConditions(sdkAlert *sdk.Alert) []grabana.AlertCondition { | ||
conditions := make([]grabana.AlertCondition, 0, len(sdkAlert.Conditions)) | ||
|
||
for _, condition := range sdkAlert.Conditions { | ||
conditions = append(conditions, grabana.AlertCondition{ | ||
Operand: condition.Operator.Type, | ||
Value: grabana.AlertValue{ | ||
Func: condition.Reducer.Type, | ||
QueryRef: condition.Query.Params[0], | ||
From: condition.Query.Params[1], | ||
To: condition.Query.Params[2], | ||
}, | ||
Threshold: converter.convertAlertThreshold(condition), | ||
}) | ||
} | ||
|
||
return conditions | ||
} | ||
|
||
func (converter *JSON) convertAlertThreshold(sdkCondition sdk.AlertCondition) grabana.AlertThreshold { | ||
threshold := grabana.AlertThreshold{} | ||
|
||
switch sdkCondition.Evaluator.Type { | ||
case "no_value": | ||
threshold.HasNoValue = true | ||
case "lt": | ||
threshold.Below = &sdkCondition.Evaluator.Params[0] | ||
case "gt": | ||
threshold.Above = &sdkCondition.Evaluator.Params[0] | ||
case "outside_range": | ||
threshold.OutsideRange = [2]float64{sdkCondition.Evaluator.Params[0], sdkCondition.Evaluator.Params[1]} | ||
case "within_range": | ||
threshold.WithinRange = [2]float64{sdkCondition.Evaluator.Params[0], sdkCondition.Evaluator.Params[1]} | ||
} | ||
|
||
return threshold | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package converter | ||
|
||
import ( | ||
grabanaDashboard "github.com/K-Phoen/grabana/dashboard" | ||
grabana "github.com/K-Phoen/grabana/decoder" | ||
"github.com/K-Phoen/sdk" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (converter *JSON) convertAnnotations(annotations []sdk.Annotation, dashboard *grabana.DashboardModel) { | ||
for _, annotation := range annotations { | ||
// grafana-sdk doesn't expose the "builtIn" field, so we work around that by skipping | ||
// the annotation we know to be built-in by its name | ||
if annotation.Name == "Annotations & Alerts" { | ||
continue | ||
} | ||
|
||
if annotation.Type != "tags" { | ||
converter.logger.Warn("unhandled annotation type: skipped", zap.String("type", annotation.Type), zap.String("name", annotation.Name)) | ||
continue | ||
} | ||
|
||
converter.convertTagAnnotation(annotation, dashboard) | ||
} | ||
} | ||
|
||
func (converter *JSON) convertTagAnnotation(annotation sdk.Annotation, dashboard *grabana.DashboardModel) { | ||
datasource := "" | ||
if annotation.Datasource != nil { | ||
datasource = *annotation.Datasource | ||
} | ||
|
||
dashboard.TagsAnnotation = append(dashboard.TagsAnnotation, grabanaDashboard.TagAnnotation{ | ||
Name: annotation.Name, | ||
Datasource: datasource, | ||
IconColor: annotation.IconColor, | ||
Tags: annotation.Tags, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package converter | ||
|
||
import ( | ||
"testing" | ||
|
||
grabana "github.com/K-Phoen/grabana/decoder" | ||
"github.com/K-Phoen/sdk" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestConvertTagAnnotationIgnoresBuiltIn(t *testing.T) { | ||
req := require.New(t) | ||
|
||
annotation := sdk.Annotation{Name: "Annotations & Alerts"} | ||
dashboard := &grabana.DashboardModel{} | ||
|
||
NewJSON(zap.NewNop()).convertAnnotations([]sdk.Annotation{annotation}, dashboard) | ||
|
||
req.Len(dashboard.TagsAnnotation, 0) | ||
} | ||
|
||
func TestConvertTagAnnotationIgnoresUnknownTypes(t *testing.T) { | ||
req := require.New(t) | ||
|
||
annotation := sdk.Annotation{Name: "Will be ignored", Type: "dashboard"} | ||
dashboard := &grabana.DashboardModel{} | ||
|
||
NewJSON(zap.NewNop()).convertAnnotations([]sdk.Annotation{annotation}, dashboard) | ||
|
||
req.Len(dashboard.TagsAnnotation, 0) | ||
} | ||
|
||
func TestConvertTagAnnotation(t *testing.T) { | ||
req := require.New(t) | ||
|
||
converter := NewJSON(zap.NewNop()) | ||
|
||
datasource := "-- Grafana --" | ||
annotation := sdk.Annotation{ | ||
Type: "tags", | ||
Datasource: &datasource, | ||
IconColor: "#5794F2", | ||
Name: "Deployments", | ||
Tags: []string{"deploy"}, | ||
} | ||
dashboard := &grabana.DashboardModel{} | ||
|
||
converter.convertAnnotations([]sdk.Annotation{annotation}, dashboard) | ||
|
||
req.Len(dashboard.TagsAnnotation, 1) | ||
req.Equal("Deployments", dashboard.TagsAnnotation[0].Name) | ||
req.ElementsMatch([]string{"deploy"}, dashboard.TagsAnnotation[0].Tags) | ||
req.Equal("#5794F2", dashboard.TagsAnnotation[0].IconColor) | ||
req.Equal(datasource, dashboard.TagsAnnotation[0].Datasource) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package converter | ||
|
||
import ( | ||
grabana "github.com/K-Phoen/grabana/decoder" | ||
"github.com/K-Phoen/sdk" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func (converter *JSON) convertExternalLinks(links []sdk.Link, dashboard *grabana.DashboardModel) { | ||
for _, link := range links { | ||
extLink := converter.convertExternalLink(link) | ||
if extLink == nil { | ||
continue | ||
} | ||
|
||
dashboard.ExternalLinks = append(dashboard.ExternalLinks, *extLink) | ||
} | ||
} | ||
|
||
func (converter *JSON) convertExternalLink(link sdk.Link) *grabana.DashboardExternalLink { | ||
if link.Type != "link" { | ||
converter.logger.Warn("unhandled link type: skipped", zap.String("type", link.Type), zap.String("title", link.Title)) | ||
return nil | ||
} | ||
|
||
if link.URL == nil || *link.URL == "" { | ||
converter.logger.Warn("link URL empty: skipped", zap.String("title", link.Title)) | ||
return nil | ||
} | ||
|
||
extLink := &grabana.DashboardExternalLink{ | ||
Title: link.Title, | ||
URL: *link.URL, | ||
IncludeVariableValues: link.IncludeVars, | ||
} | ||
|
||
if link.Tooltip != nil && *link.Tooltip != "" { | ||
extLink.Description = *link.Tooltip | ||
} | ||
if link.Icon != nil && *link.Icon != "" { | ||
extLink.Icon = *link.Icon | ||
} | ||
if link.TargetBlank != nil { | ||
extLink.OpenInNewTab = *link.TargetBlank | ||
} | ||
if link.KeepTime != nil { | ||
extLink.IncludeTimeRange = *link.KeepTime | ||
} | ||
|
||
return extLink | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package converter | ||
|
||
import ( | ||
"testing" | ||
|
||
grabana "github.com/K-Phoen/grabana/decoder" | ||
"github.com/K-Phoen/sdk" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestConvertExternalLink(t *testing.T) { | ||
req := require.New(t) | ||
|
||
externalLink := sdk.Link{ | ||
Title: "joe", | ||
Type: "link", | ||
Icon: strPtr("cloud"), | ||
IncludeVars: true, | ||
KeepTime: boolPtr(true), | ||
TargetBlank: boolPtr(true), | ||
Tooltip: strPtr("description"), | ||
URL: strPtr("http://lala"), | ||
} | ||
dashLink := sdk.Link{ | ||
Title: "not link", | ||
} | ||
|
||
converter := NewJSON(zap.NewNop()) | ||
|
||
dashboard := &grabana.DashboardModel{} | ||
converter.convertExternalLinks([]sdk.Link{externalLink, dashLink}, dashboard) | ||
|
||
req.Len(dashboard.ExternalLinks, 1) | ||
|
||
link := dashboard.ExternalLinks[0] | ||
|
||
req.Equal("joe", link.Title) | ||
req.Equal("description", link.Description) | ||
req.Equal("cloud", link.Icon) | ||
req.Equal("http://lala", link.URL) | ||
req.True(link.OpenInNewTab) | ||
req.True(link.IncludeTimeRange) | ||
req.True(link.IncludeVariableValues) | ||
} |
Oops, something went wrong.