Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
0c6d8398
Commit
0c6d8398
authored
Jun 05, 2018
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
alerting: remove zero units from duration
parent
93124f38
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
27 deletions
+55
-27
pkg/api/alerting_test.go
+0
-19
pkg/api/dtos/alerting.go
+21
-8
pkg/api/dtos/alerting_test.go
+34
-0
No files found.
pkg/api/alerting_test.go
View file @
0c6d8398
...
...
@@ -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
()
{
...
...
pkg/api/dtos/alerting.go
View file @
0c6d8398
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
,
}
...
...
pkg/api/dtos/alerting_test.go
0 → 100644
View file @
0c6d8398
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
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment