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
2ca7284a
Commit
2ca7284a
authored
Sep 06, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
tech(notifications): splitt into 3 queries
closes #5883
parent
c893e5d2
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
93 additions
and
25 deletions
+93
-25
pkg/api/alerting.go
+2
-2
pkg/models/alert_notifications.go
+12
-0
pkg/services/alerting/notifier.go
+1
-5
pkg/services/sqlstore/alert_notification.go
+62
-15
pkg/services/sqlstore/alert_notification_test.go
+16
-3
No files found.
pkg/api/alerting.go
View file @
2ca7284a
...
...
@@ -147,7 +147,7 @@ func DelAlert(c *middleware.Context) Response {
}
func
GetAlertNotifications
(
c
*
middleware
.
Context
)
Response
{
query
:=
&
models
.
GetAlertNotificationsQuery
{
OrgId
:
c
.
OrgId
}
query
:=
&
models
.
GetAl
lAl
ertNotificationsQuery
{
OrgId
:
c
.
OrgId
}
if
err
:=
bus
.
Dispatch
(
query
);
err
!=
nil
{
return
ApiError
(
500
,
"Failed to get alert notifications"
,
err
)
...
...
@@ -179,7 +179,7 @@ func GetAlertNotificationById(c *middleware.Context) Response {
return
ApiError
(
500
,
"Failed to get alert notifications"
,
err
)
}
return
Json
(
200
,
query
.
Result
[
0
]
)
return
Json
(
200
,
query
.
Result
)
}
func
CreateAlertNotification
(
c
*
middleware
.
Context
,
cmd
models
.
CreateAlertNotificationCommand
)
Response
{
...
...
pkg/models/alert_notifications.go
View file @
2ca7284a
...
...
@@ -46,8 +46,20 @@ type DeleteAlertNotificationCommand struct {
type
GetAlertNotificationsQuery
struct
{
Name
string
Id
int64
OrgId
int64
Result
*
AlertNotification
}
type
GetAlertNotificationsToSendQuery
struct
{
Ids
[]
int64
OrgId
int64
Result
[]
*
AlertNotification
}
type
GetAllAlertNotificationsQuery
struct
{
OrgId
int64
Result
[]
*
AlertNotification
}
pkg/services/alerting/notifier.go
View file @
2ca7284a
...
...
@@ -88,11 +88,7 @@ func (n *RootNotifier) uploadImage(context *EvalContext) error {
}
func
(
n
*
RootNotifier
)
getNotifiers
(
orgId
int64
,
notificationIds
[]
int64
)
([]
Notifier
,
error
)
{
query
:=
&
m
.
GetAlertNotificationsQuery
{
OrgId
:
orgId
}
if
len
(
notificationIds
)
==
0
{
query
.
Ids
=
[]
int64
{
0
}
}
query
:=
&
m
.
GetAlertNotificationsToSendQuery
{
OrgId
:
orgId
,
Ids
:
notificationIds
}
if
err
:=
bus
.
Dispatch
(
query
);
err
!=
nil
{
return
nil
,
err
...
...
pkg/services/sqlstore/alert_notification.go
View file @
2ca7284a
...
...
@@ -16,6 +16,8 @@ func init() {
bus
.
AddHandler
(
"sql"
,
CreateAlertNotificationCommand
)
bus
.
AddHandler
(
"sql"
,
UpdateAlertNotification
)
bus
.
AddHandler
(
"sql"
,
DeleteAlertNotification
)
bus
.
AddHandler
(
"sql"
,
GetAlertNotificationsToSend
)
bus
.
AddHandler
(
"sql"
,
GetAllAlertNotifications
)
}
func
DeleteAlertNotification
(
cmd
*
m
.
DeleteAlertNotificationCommand
)
error
{
...
...
@@ -32,10 +34,20 @@ func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
}
func
GetAlertNotifications
(
query
*
m
.
GetAlertNotificationsQuery
)
error
{
return
getAlertNotification
s
Internal
(
query
,
x
.
NewSession
())
return
getAlertNotificationInternal
(
query
,
x
.
NewSession
())
}
func
getAlertNotificationsInternal
(
query
*
m
.
GetAlertNotificationsQuery
,
sess
*
xorm
.
Session
)
error
{
func
GetAllAlertNotifications
(
query
*
m
.
GetAllAlertNotificationsQuery
)
error
{
results
:=
make
([]
*
m
.
AlertNotification
,
0
)
if
err
:=
x
.
Where
(
"org_id = ?"
,
query
.
OrgId
)
.
Find
(
&
results
);
err
!=
nil
{
return
err
}
query
.
Result
=
results
return
nil
}
func
GetAlertNotificationsToSend
(
query
*
m
.
GetAlertNotificationsToSendQuery
)
error
{
var
sql
bytes
.
Buffer
params
:=
make
([]
interface
{},
0
)
...
...
@@ -54,7 +66,44 @@ func getAlertNotificationsInternal(query *m.GetAlertNotificationsQuery, sess *xo
sql
.
WriteString
(
` WHERE alert_notification.org_id = ?`
)
params
=
append
(
params
,
query
.
OrgId
)
if
query
.
Name
!=
""
||
query
.
Id
!=
0
||
len
(
query
.
Ids
)
>
0
{
sql
.
WriteString
(
` AND ((alert_notification.is_default = 1)`
)
if
len
(
query
.
Ids
)
>
0
{
sql
.
WriteString
(
` OR alert_notification.id IN (?`
+
strings
.
Repeat
(
",?"
,
len
(
query
.
Ids
)
-
1
)
+
")"
)
for
_
,
v
:=
range
query
.
Ids
{
params
=
append
(
params
,
v
)
}
}
sql
.
WriteString
(
`)`
)
results
:=
make
([]
*
m
.
AlertNotification
,
0
)
if
err
:=
x
.
Sql
(
sql
.
String
(),
params
...
)
.
Find
(
&
results
);
err
!=
nil
{
return
err
}
query
.
Result
=
results
return
nil
}
func
getAlertNotificationInternal
(
query
*
m
.
GetAlertNotificationsQuery
,
sess
*
xorm
.
Session
)
error
{
var
sql
bytes
.
Buffer
params
:=
make
([]
interface
{},
0
)
sql
.
WriteString
(
`SELECT
alert_notification.id,
alert_notification.org_id,
alert_notification.name,
alert_notification.type,
alert_notification.created,
alert_notification.updated,
alert_notification.settings,
alert_notification.is_default
FROM alert_notification
`
)
sql
.
WriteString
(
` WHERE alert_notification.org_id = ?`
)
params
=
append
(
params
,
query
.
OrgId
)
if
query
.
Name
!=
""
||
query
.
Id
!=
0
{
if
query
.
Name
!=
""
{
sql
.
WriteString
(
` AND alert_notification.name = ?`
)
params
=
append
(
params
,
query
.
Name
)
...
...
@@ -64,13 +113,6 @@ func getAlertNotificationsInternal(query *m.GetAlertNotificationsQuery, sess *xo
sql
.
WriteString
(
` AND alert_notification.id = ?`
)
params
=
append
(
params
,
query
.
Id
)
}
if
len
(
query
.
Ids
)
>
0
{
sql
.
WriteString
(
` AND ((alert_notification.is_default = 1) OR alert_notification.id IN (?`
+
strings
.
Repeat
(
",?"
,
len
(
query
.
Ids
)
-
1
)
+
"))"
)
for
_
,
v
:=
range
query
.
Ids
{
params
=
append
(
params
,
v
)
}
}
}
results
:=
make
([]
*
m
.
AlertNotification
,
0
)
...
...
@@ -78,20 +120,25 @@ func getAlertNotificationsInternal(query *m.GetAlertNotificationsQuery, sess *xo
return
err
}
query
.
Result
=
results
if
len
(
results
)
==
0
{
query
.
Result
=
nil
}
else
{
query
.
Result
=
results
[
0
]
}
return
nil
}
func
CreateAlertNotificationCommand
(
cmd
*
m
.
CreateAlertNotificationCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
existingQuery
:=
&
m
.
GetAlertNotificationsQuery
{
OrgId
:
cmd
.
OrgId
,
Name
:
cmd
.
Name
}
err
:=
getAlertNotification
s
Internal
(
existingQuery
,
sess
)
err
:=
getAlertNotificationInternal
(
existingQuery
,
sess
)
if
err
!=
nil
{
return
err
}
if
len
(
existingQuery
.
Result
)
>
0
{
if
existingQuery
.
Result
!=
nil
{
return
fmt
.
Errorf
(
"Alert notification name %s already exists"
,
cmd
.
Name
)
}
...
...
@@ -124,11 +171,11 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
// check if name exists
sameNameQuery
:=
&
m
.
GetAlertNotificationsQuery
{
OrgId
:
cmd
.
OrgId
,
Name
:
cmd
.
Name
}
if
err
:=
getAlertNotification
s
Internal
(
sameNameQuery
,
sess
);
err
!=
nil
{
if
err
:=
getAlertNotificationInternal
(
sameNameQuery
,
sess
);
err
!=
nil
{
return
err
}
if
len
(
sameNameQuery
.
Result
)
>
0
&&
sameNameQuery
.
Result
[
0
]
.
Id
!=
current
.
Id
{
if
sameNameQuery
.
Result
!=
nil
&&
sameNameQuery
.
Result
.
Id
!=
current
.
Id
{
return
fmt
.
Errorf
(
"Alert notification name %s already exists"
,
cmd
.
Name
)
}
...
...
pkg/services/sqlstore/alert_notification_test.go
View file @
2ca7284a
...
...
@@ -23,7 +23,7 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
err
:=
GetAlertNotifications
(
cmd
)
fmt
.
Printf
(
"errror %v"
,
err
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
cmd
.
Result
),
ShouldEqual
,
0
)
So
(
cmd
.
Result
,
ShouldBeNil
)
})
Convey
(
"Can save Alert Notification"
,
func
()
{
...
...
@@ -65,21 +65,34 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
cmd3
:=
m
.
CreateAlertNotificationCommand
{
Name
:
"ops2"
,
Type
:
"email"
,
OrgId
:
1
,
Settings
:
simplejson
.
New
()}
cmd4
:=
m
.
CreateAlertNotificationCommand
{
IsDefault
:
true
,
Name
:
"default"
,
Type
:
"email"
,
OrgId
:
1
,
Settings
:
simplejson
.
New
()}
otherOrg
:=
m
.
CreateAlertNotificationCommand
{
Name
:
"default"
,
Type
:
"email"
,
OrgId
:
2
,
Settings
:
simplejson
.
New
()}
So
(
CreateAlertNotificationCommand
(
&
cmd1
),
ShouldBeNil
)
So
(
CreateAlertNotificationCommand
(
&
cmd2
),
ShouldBeNil
)
So
(
CreateAlertNotificationCommand
(
&
cmd3
),
ShouldBeNil
)
So
(
CreateAlertNotificationCommand
(
&
cmd4
),
ShouldBeNil
)
So
(
CreateAlertNotificationCommand
(
&
otherOrg
),
ShouldBeNil
)
Convey
(
"search"
,
func
()
{
query
:=
&
m
.
GetAlertNotificationsQuery
{
query
:=
&
m
.
GetAlertNotifications
ToSend
Query
{
Ids
:
[]
int64
{
cmd1
.
Result
.
Id
,
cmd2
.
Result
.
Id
,
112341231
},
OrgId
:
1
,
}
err
:=
GetAlertNotifications
(
query
)
err
:=
GetAlertNotifications
ToSend
(
query
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
3
)
})
Convey
(
"all"
,
func
()
{
query
:=
&
m
.
GetAllAlertNotificationsQuery
{
OrgId
:
1
,
}
err
:=
GetAllAlertNotifications
(
query
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
4
)
})
})
})
}
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