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
4fc16c36
Commit
4fc16c36
authored
Jun 28, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(alerting): saves new state when alert updates
parent
5a65dc5d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
72 additions
and
25 deletions
+72
-25
pkg/models/alert.go
+15
-5
pkg/services/sqlstore/alert.go
+35
-10
pkg/services/sqlstore/alert_rule_changes.go
+10
-6
pkg/services/sqlstore/alert_rule_changes_test.go
+10
-4
pkg/services/sqlstore/migrations/alert_mig.go
+2
-0
No files found.
pkg/models/alert.go
View file @
4fc16c36
...
...
@@ -71,14 +71,24 @@ type HeartBeatCommand struct {
}
type
AlertChange
struct
{
Id
int64
`json:"id"`
OrgId
int64
`json:"-"`
AlertId
int64
`json:"alertId"`
Type
string
`json:"type"`
Created
time
.
Time
`json:"created"`
Id
int64
`json:"id"`
OrgId
int64
`json:"-"`
AlertId
int64
`json:"alertId"`
UpdatedBy
int64
`json:"updatedBy"`
NewAlertSettings
*
simplejson
.
Json
`json:"newAlertSettings"`
Type
string
`json:"type"`
Created
time
.
Time
`json:"created"`
}
// Commands
type
CreateAlertChangeCommand
struct
{
OrgId
int64
AlertId
int64
UpdatedBy
int64
NewAlertSettings
*
simplejson
.
Json
Type
string
}
type
SaveAlertsCommand
struct
{
DashboardId
int64
UserId
int64
...
...
pkg/services/sqlstore/alert.go
View file @
4fc16c36
...
...
@@ -152,7 +152,14 @@ func DeleteAlertDefinition(dashboardId int64, sess *xorm.Session) error {
sqlog
.
Debug
(
"Alert deleted (due to dashboard deletion)"
,
"name"
,
alert
.
Name
,
"id"
,
alert
.
Id
)
if
err
:=
SaveAlertChange
(
"DELETED"
,
alert
,
sess
);
err
!=
nil
{
cmd
:=
&
m
.
CreateAlertChangeCommand
{
Type
:
"DELETED"
,
UpdatedBy
:
1
,
AlertId
:
alert
.
Id
,
OrgId
:
alert
.
OrgId
,
NewAlertSettings
:
alert
.
Settings
,
}
if
err
:=
SaveAlertChange
(
cmd
,
sess
);
err
!=
nil
{
return
err
}
}
...
...
@@ -167,15 +174,15 @@ func SaveAlerts(cmd *m.SaveAlertsCommand) error {
return
err
}
upsertAlerts
(
alerts
,
cmd
.
Alerts
,
sess
)
deleteMissingAlerts
(
alerts
,
cmd
.
Alerts
,
sess
)
upsertAlerts
(
alerts
,
cmd
,
sess
)
deleteMissingAlerts
(
alerts
,
cmd
,
sess
)
return
nil
})
}
func
upsertAlerts
(
alerts
[]
*
m
.
Alert
,
posted
[]
*
m
.
Alert
,
sess
*
xorm
.
Session
)
error
{
for
_
,
alert
:=
range
posted
{
func
upsertAlerts
(
alerts
[]
*
m
.
Alert
,
cmd
*
m
.
SaveAlertsCommand
,
sess
*
xorm
.
Session
)
error
{
for
_
,
alert
:=
range
cmd
.
Alerts
{
update
:=
false
var
alertToUpdate
*
m
.
Alert
...
...
@@ -198,7 +205,13 @@ func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) erro
}
sqlog
.
Debug
(
"Alert updated"
,
"name"
,
alert
.
Name
,
"id"
,
alert
.
Id
)
SaveAlertChange
(
"UPDATED"
,
alert
,
sess
)
SaveAlertChange
(
&
m
.
CreateAlertChangeCommand
{
OrgId
:
alert
.
OrgId
,
AlertId
:
alert
.
Id
,
NewAlertSettings
:
alert
.
Settings
,
UpdatedBy
:
cmd
.
UserId
,
Type
:
"UPDATED"
,
},
sess
)
}
}
else
{
...
...
@@ -211,18 +224,24 @@ func upsertAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Session) erro
}
sqlog
.
Debug
(
"Alert inserted"
,
"name"
,
alert
.
Name
,
"id"
,
alert
.
Id
)
SaveAlertChange
(
"CREATED"
,
alert
,
sess
)
SaveAlertChange
(
&
m
.
CreateAlertChangeCommand
{
OrgId
:
alert
.
OrgId
,
AlertId
:
alert
.
Id
,
NewAlertSettings
:
alert
.
Settings
,
UpdatedBy
:
cmd
.
UserId
,
Type
:
"CREATED"
,
},
sess
)
}
}
return
nil
}
func
deleteMissingAlerts
(
alerts
[]
*
m
.
Alert
,
posted
[]
*
m
.
Alert
,
sess
*
xorm
.
Session
)
error
{
func
deleteMissingAlerts
(
alerts
[]
*
m
.
Alert
,
cmd
*
m
.
SaveAlertsCommand
,
sess
*
xorm
.
Session
)
error
{
for
_
,
missingAlert
:=
range
alerts
{
missing
:=
true
for
_
,
k
:=
range
posted
{
for
_
,
k
:=
range
cmd
.
Alerts
{
if
missingAlert
.
PanelId
==
k
.
PanelId
{
missing
=
false
break
...
...
@@ -237,7 +256,13 @@ func deleteMissingAlerts(alerts []*m.Alert, posted []*m.Alert, sess *xorm.Sessio
sqlog
.
Debug
(
"Alert deleted"
,
"name"
,
missingAlert
.
Name
,
"id"
,
missingAlert
.
Id
)
err
=
SaveAlertChange
(
"DELETED"
,
missingAlert
,
sess
)
SaveAlertChange
(
&
m
.
CreateAlertChangeCommand
{
OrgId
:
missingAlert
.
OrgId
,
AlertId
:
missingAlert
.
Id
,
NewAlertSettings
:
missingAlert
.
Settings
,
UpdatedBy
:
cmd
.
UserId
,
Type
:
"DELETED"
,
},
sess
)
if
err
!=
nil
{
return
err
}
...
...
pkg/services/sqlstore/alert_rule_changes.go
View file @
4fc16c36
...
...
@@ -22,7 +22,9 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
alert_change.org_id,
alert_change.alert_id,
alert_change.type,
alert_change.created
alert_change.created,
alert_change.new_alert_settings,
alert_change.updated_by
FROM alert_change
`
)
...
...
@@ -48,12 +50,14 @@ func GetAlertRuleChanges(query *m.GetAlertChangesQuery) error {
return
nil
}
func
SaveAlertChange
(
c
hange
string
,
alert
*
m
.
Alert
,
sess
*
xorm
.
Session
)
error
{
func
SaveAlertChange
(
c
md
*
m
.
CreateAlertChangeCommand
,
sess
*
xorm
.
Session
)
error
{
_
,
err
:=
sess
.
Insert
(
&
m
.
AlertChange
{
OrgId
:
alert
.
OrgId
,
Type
:
change
,
Created
:
time
.
Now
(),
AlertId
:
alert
.
Id
,
OrgId
:
cmd
.
OrgId
,
Type
:
cmd
.
Type
,
Created
:
time
.
Now
(),
AlertId
:
cmd
.
AlertId
,
NewAlertSettings
:
cmd
.
NewAlertSettings
,
UpdatedBy
:
cmd
.
UpdatedBy
,
})
if
err
!=
nil
{
...
...
pkg/services/sqlstore/alert_rule_changes_test.go
View file @
4fc16c36
...
...
@@ -69,10 +69,16 @@ func TestAlertRuleChangesDataAccess(t *testing.T) {
Convey
(
"add 4 updates"
,
func
()
{
sess
:=
x
.
NewSession
()
SaveAlertChange
(
"UPDATED"
,
items
[
0
],
sess
)
SaveAlertChange
(
"UPDATED"
,
items
[
0
],
sess
)
SaveAlertChange
(
"UPDATED"
,
items
[
0
],
sess
)
SaveAlertChange
(
"UPDATED"
,
items
[
0
],
sess
)
updateCmd
:=
m
.
CreateAlertChangeCommand
{
AlertId
:
items
[
0
]
.
Id
,
OrgId
:
items
[
0
]
.
OrgId
,
UpdatedBy
:
1
,
}
SaveAlertChange
(
&
updateCmd
,
sess
)
SaveAlertChange
(
&
updateCmd
,
sess
)
SaveAlertChange
(
&
updateCmd
,
sess
)
SaveAlertChange
(
&
updateCmd
,
sess
)
sess
.
Commit
()
Convey
(
"query for max one change"
,
func
()
{
...
...
pkg/services/sqlstore/migrations/alert_mig.go
View file @
4fc16c36
...
...
@@ -36,6 +36,8 @@ func addAlertMigrations(mg *Migrator) {
{
Name
:
"org_id"
,
Type
:
DB_BigInt
,
Nullable
:
false
},
{
Name
:
"type"
,
Type
:
DB_NVarchar
,
Length
:
50
,
Nullable
:
false
},
{
Name
:
"created"
,
Type
:
DB_DateTime
,
Nullable
:
false
},
{
Name
:
"updated_by"
,
Type
:
DB_BigInt
,
Nullable
:
false
},
{
Name
:
"new_alert_settings"
,
Type
:
DB_Text
,
Nullable
:
false
},
},
}
...
...
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