Commit 0c6d8398 by bergquist

alerting: remove zero units from duration

parent 93124f38
......@@ -2,7 +2,6 @@ package api
import (
"testing"
"time"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
......@@ -12,24 +11,6 @@ import (
. "github.com/smartystreets/goconvey/convey"
)
func TestRemoveZeroUnitsFromInterval(t *testing.T) {
tcs := []struct {
interval time.Duration
expected string
}{
{interval: time.Duration(time.Hour), expected: "1h"},
{interval: time.Duration(time.Hour + time.Minute), expected: "1h1m"},
{interval: time.Duration((time.Hour * 10) + time.Minute), expected: "10h1m"},
}
for _, tc := range tcs {
got := removeZeroesFromDuration(tc.interval)
if got != tc.expected {
t.Errorf("expected %s got %s internval: %v", tc.expected, got, tc.interval)
}
}
}
func TestAlertingApiEndpoint(t *testing.T) {
Convey("Given an alert in a dashboard with an acl", t, func() {
......
package dtos
import (
"strings"
"fmt"
"time"
"github.com/grafana/grafana/pkg/components/null"
......@@ -24,14 +24,27 @@ type AlertRule struct {
CanEdit bool `json:"canEdit"`
}
func removeZeroesFromDuration(interval time.Duration) string {
frequency := interval.String()
func formatShort(interval time.Duration) string {
var result string
frequency = strings.Replace(frequency, "0h", "", 1)
frequency = strings.Replace(frequency, "0m", "", 1)
frequency = strings.Replace(frequency, "0s", "", 1)
hours := interval / time.Hour
if hours > 0 {
result += fmt.Sprintf("%dh", hours)
}
remaining := interval - (hours * time.Hour)
mins := remaining / time.Minute
if mins > 0 {
result += fmt.Sprintf("%dm", mins)
}
remaining = remaining - (mins * time.Minute)
seconds := remaining / time.Second
if seconds > 0 {
result += fmt.Sprintf("%ds", seconds)
}
return frequency
return result
}
func NewAlertNotification(notification *models.AlertNotification) *AlertNotification {
......@@ -42,7 +55,7 @@ func NewAlertNotification(notification *models.AlertNotification) *AlertNotifica
IsDefault: notification.IsDefault,
Created: notification.Created,
Updated: notification.Updated,
Frequency: removeZeroesFromDuration(notification.Frequency),
Frequency: formatShort(notification.Frequency),
NotifyOnce: notification.NotifyOnce,
Settings: notification.Settings,
}
......
package dtos
import (
"testing"
"time"
)
func TestFormatShort(t *testing.T) {
tcs := []struct {
interval time.Duration
expected string
}{
{interval: time.Duration(time.Hour), expected: "1h"},
{interval: time.Duration(time.Hour + time.Minute), expected: "1h1m"},
{interval: time.Duration((time.Hour * 10) + time.Minute), expected: "10h1m"},
{interval: time.Duration((time.Hour * 10) + (time.Minute * 10) + time.Second), expected: "10h10m1s"},
}
for _, tc := range tcs {
got := formatShort(tc.interval)
if got != tc.expected {
t.Errorf("expected %s got %s interval: %v", tc.expected, got, tc.interval)
}
parsed, err := time.ParseDuration(tc.expected)
if err != nil {
t.Fatalf("could not parse expected duration")
}
if parsed != tc.interval {
t.Errorf("expectes the parsed duration to equal the interval. Got %v expected: %v", parsed, tc.interval)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment