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
83a12afc
Commit
83a12afc
authored
Jun 16, 2018
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adds tests for journaling sql operations
parent
c21938d4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
8 deletions
+60
-8
pkg/models/alert_notifications.go
+1
-0
pkg/services/alerting/notifiers/base.go
+6
-5
pkg/services/sqlstore/alert_notification.go
+10
-3
pkg/services/sqlstore/alert_notification_test.go
+43
-0
No files found.
pkg/models/alert_notifications.go
View file @
83a12afc
...
@@ -9,6 +9,7 @@ import (
...
@@ -9,6 +9,7 @@ import (
var
(
var
(
ErrNotificationFrequencyNotFound
=
errors
.
New
(
"Notification frequency not specified"
)
ErrNotificationFrequencyNotFound
=
errors
.
New
(
"Notification frequency not specified"
)
ErrJournalingNotFound
=
errors
.
New
(
"alert notification journaling not found"
)
)
)
type
AlertNotification
struct
{
type
AlertNotification
struct
{
...
...
pkg/services/alerting/notifiers/base.go
View file @
83a12afc
...
@@ -73,15 +73,16 @@ func (n *NotifierBase) ShouldNotify(c *alerting.EvalContext) bool {
...
@@ -73,15 +73,16 @@ func (n *NotifierBase) ShouldNotify(c *alerting.EvalContext) bool {
NotifierId
:
n
.
Id
,
NotifierId
:
n
.
Id
,
}
}
if
err
:=
bus
.
DispatchCtx
(
c
.
Ctx
,
cmd
);
err
!=
nil
{
err
:=
bus
.
DispatchCtx
(
c
.
Ctx
,
cmd
)
if
err
!=
nil
{
n
.
log
.
Error
(
"Could not determine last time alert notifier fired"
,
"Alert name"
,
c
.
Rule
.
Name
,
"Error"
,
err
)
n
.
log
.
Error
(
"Could not determine last time alert notifier fired"
,
"Alert name"
,
c
.
Rule
.
Name
,
"Error"
,
err
)
return
false
return
false
}
}
// this currently serves two purposes.
if
err
==
models
.
ErrJournalingNotFound
{
// 1. make sure failed notifications try again
return
true
// 2. make sure we send notifications if no previous exist
}
// this should be refactored //Carl Bergquist
if
!
cmd
.
Result
.
Success
{
if
!
cmd
.
Result
.
Success
{
return
true
return
true
}
}
...
...
pkg/services/sqlstore/alert_notification.go
View file @
83a12afc
...
@@ -249,13 +249,20 @@ func RecordNotificationJournal(ctx context.Context, cmd *m.RecordNotificationJou
...
@@ -249,13 +249,20 @@ func RecordNotificationJournal(ctx context.Context, cmd *m.RecordNotificationJou
func
GetLatestNotification
(
ctx
context
.
Context
,
cmd
*
m
.
GetLatestNotificationQuery
)
error
{
func
GetLatestNotification
(
ctx
context
.
Context
,
cmd
*
m
.
GetLatestNotificationQuery
)
error
{
return
inTransactionCtx
(
ctx
,
func
(
sess
*
DBSession
)
error
{
return
inTransactionCtx
(
ctx
,
func
(
sess
*
DBSession
)
error
{
notificationJournal
:=
&
m
.
AlertNotificationJournal
{}
nj
:=
&
m
.
AlertNotificationJournal
{}
_
,
err
:=
sess
.
Desc
(
"alert_notification_journal.sent_at"
)
.
Limit
(
1
)
.
Where
(
"alert_notification_journal.org_id = ? AND alert_notification_journal.alert_id = ? AND alert_notification_journal.notifier_id = ?"
,
cmd
.
OrgId
,
cmd
.
AlertId
,
cmd
.
NotifierId
)
.
Get
(
notificationJournal
)
_
,
err
:=
sess
.
Desc
(
"alert_notification_journal.sent_at"
)
.
Limit
(
1
)
.
Where
(
"alert_notification_journal.org_id = ? AND alert_notification_journal.alert_id = ? AND alert_notification_journal.notifier_id = ?"
,
cmd
.
OrgId
,
cmd
.
AlertId
,
cmd
.
NotifierId
)
.
Get
(
nj
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
cmd
.
Result
=
notificationJournal
if
nj
.
AlertId
==
0
&&
nj
.
Id
==
0
&&
nj
.
NotifierId
==
0
&&
nj
.
OrgId
==
0
{
return
m
.
ErrJournalingNotFound
}
cmd
.
Result
=
nj
return
nil
return
nil
})
})
}
}
...
...
pkg/services/sqlstore/alert_notification_test.go
View file @
83a12afc
package
sqlstore
package
sqlstore
import
(
import
(
"context"
"testing"
"testing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/components/simplejson"
...
@@ -12,6 +13,48 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
...
@@ -12,6 +13,48 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
Convey
(
"Testing Alert notification sql access"
,
t
,
func
()
{
Convey
(
"Testing Alert notification sql access"
,
t
,
func
()
{
InitTestDB
(
t
)
InitTestDB
(
t
)
Convey
(
"Alert notification journal"
,
func
()
{
var
alertId
int64
=
5
var
orgId
int64
=
5
var
notifierId
int64
=
5
Convey
(
"Getting last journal should raise error if no one exists"
,
func
()
{
query
:=
&
m
.
GetLatestNotificationQuery
{
AlertId
:
alertId
,
OrgId
:
orgId
,
NotifierId
:
notifierId
}
err
:=
GetLatestNotification
(
context
.
Background
(),
query
)
So
(
err
,
ShouldEqual
,
m
.
ErrJournalingNotFound
)
Convey
(
"shoulbe be able to record two journaling events"
,
func
()
{
createCmd
:=
&
m
.
RecordNotificationJournalCommand
{
AlertId
:
alertId
,
NotifierId
:
notifierId
,
OrgId
:
orgId
,
Success
:
true
,
SentAt
:
1
}
err
:=
RecordNotificationJournal
(
context
.
Background
(),
createCmd
)
So
(
err
,
ShouldBeNil
)
createCmd
.
SentAt
+=
1000
//increase epoch
err
=
RecordNotificationJournal
(
context
.
Background
(),
createCmd
)
So
(
err
,
ShouldBeNil
)
Convey
(
"get last journaling event"
,
func
()
{
err
:=
GetLatestNotification
(
context
.
Background
(),
query
)
So
(
err
,
ShouldBeNil
)
So
(
query
.
Result
.
SentAt
,
ShouldEqual
,
1001
)
Convey
(
"be able to clear all journaling for an notifier"
,
func
()
{
cmd
:=
&
m
.
CleanNotificationJournalCommand
{
AlertId
:
alertId
,
NotifierId
:
notifierId
,
OrgId
:
orgId
}
err
:=
CleanNotificationJournal
(
context
.
Background
(),
cmd
)
So
(
err
,
ShouldBeNil
)
Convey
(
"querying for last junaling should raise error"
,
func
()
{
query
:=
&
m
.
GetLatestNotificationQuery
{
AlertId
:
alertId
,
OrgId
:
orgId
,
NotifierId
:
notifierId
}
err
:=
GetLatestNotification
(
context
.
Background
(),
query
)
So
(
err
,
ShouldEqual
,
m
.
ErrJournalingNotFound
)
})
})
})
})
})
})
Convey
(
"Alert notifications should be empty"
,
func
()
{
Convey
(
"Alert notifications should be empty"
,
func
()
{
cmd
:=
&
m
.
GetAlertNotificationsQuery
{
cmd
:=
&
m
.
GetAlertNotificationsQuery
{
OrgId
:
2
,
OrgId
:
2
,
...
...
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