Skip to content

Commit

Permalink
Merge pull request #86 from K-Phoen/timeseries
Browse files Browse the repository at this point in the history
Timeseries
  • Loading branch information
K-Phoen committed Nov 21, 2021
2 parents 576edbc + 28d1435 commit e0166a7
Show file tree
Hide file tree
Showing 39 changed files with 3,472 additions and 1,872 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/K-Phoen/dark
go 1.17

require (
github.com/K-Phoen/grabana v0.18.1
github.com/K-Phoen/grabana v0.19.0
github.com/K-Phoen/sdk v0.0.0-20211119151408-adef1e5fdd11
github.com/spf13/cobra v1.2.1
github.com/stretchr/testify v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6L
github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/K-Phoen/grabana v0.18.1 h1:L9kFCCRyaH6EucYZ2sepgLUXicNEtrdPa0l84DbRb+M=
github.com/K-Phoen/grabana v0.18.1/go.mod h1:e6GwAnk6L0g+tba2yhKdzWvDah8cPPlwPptyrnkK8ng=
github.com/K-Phoen/grabana v0.19.0 h1:/rjWWZt3qkuze3EBXxlNjGxc9ZkBfPj4nZJCmu8RkOs=
github.com/K-Phoen/grabana v0.19.0/go.mod h1:e6GwAnk6L0g+tba2yhKdzWvDah8cPPlwPptyrnkK8ng=
github.com/K-Phoen/sdk v0.0.0-20211119151408-adef1e5fdd11 h1:bcJqfzSQge+pyJ1beMzUmf3bh9bRyIzEFu8PXUx8Hgk=
github.com/K-Phoen/sdk v0.0.0-20211119151408-adef1e5fdd11/go.mod h1:fnbOsbRksULSfcXjOI6W1/HISz5o/u1iEhF/fLedqTg=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
Expand Down
71 changes: 71 additions & 0 deletions internal/pkg/converter/alert.go
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
}
39 changes: 39 additions & 0 deletions internal/pkg/converter/annotation.go
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,
})
}
56 changes: 56 additions & 0 deletions internal/pkg/converter/annotation_test.go
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)
}
51 changes: 51 additions & 0 deletions internal/pkg/converter/externallink.go
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
}
45 changes: 45 additions & 0 deletions internal/pkg/converter/externallink_test.go
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)
}
Loading

0 comments on commit e0166a7

Please sign in to comment.