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
488b4237
Commit
488b4237
authored
Jun 23, 2016
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(alerting): update state if not been updated for 15min
parent
925806df
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
13 deletions
+51
-13
pkg/api/alerting.go
+1
-0
pkg/models/alert_state.go
+8
-0
pkg/services/alerting/engine.go
+21
-4
pkg/services/sqlstore/alert_state.go
+21
-9
No files found.
pkg/api/alerting.go
View file @
488b4237
...
...
@@ -140,6 +140,7 @@ func GetAlertStates(c *middleware.Context) Response {
// PUT /api/alerts/events/:id
func
PutAlertState
(
c
*
middleware
.
Context
,
cmd
models
.
UpdateAlertStateCommand
)
Response
{
cmd
.
AlertId
=
c
.
ParamsInt64
(
":alertId"
)
cmd
.
OrgId
=
c
.
OrgId
query
:=
models
.
GetAlertByIdQuery
{
Id
:
cmd
.
AlertId
}
if
err
:=
bus
.
Dispatch
(
&
query
);
err
!=
nil
{
...
...
pkg/models/alert_state.go
View file @
488b4237
...
...
@@ -28,6 +28,7 @@ func (this *UpdateAlertStateCommand) IsValidState() bool {
type
UpdateAlertStateCommand
struct
{
AlertId
int64
`json:"alertId" binding:"Required"`
OrgId
int64
`json:"orgId" binding:"Required"`
NewState
string
`json:"newState" binding:"Required"`
Info
string
`json:"info"`
...
...
@@ -42,3 +43,10 @@ type GetAlertsStateQuery struct {
Result
*
[]
AlertState
}
type
GetLastAlertStateQuery
struct
{
AlertId
int64
OrgId
int64
Result
*
AlertState
}
pkg/services/alerting/engine.go
View file @
488b4237
...
...
@@ -143,14 +143,12 @@ func (e *Engine) resultHandler() {
}
func
(
e
*
Engine
)
reactToState
(
result
*
AlertResult
)
{
query
:=
&
m
.
GetAlertByIdQuery
{
Id
:
result
.
AlertJob
.
Rule
.
Id
}
bus
.
Dispatch
(
query
)
if
query
.
Result
.
ShouldUpdateState
(
result
.
State
)
{
if
shouldUpdateState
(
result
)
{
cmd
:=
&
m
.
UpdateAlertStateCommand
{
AlertId
:
result
.
AlertJob
.
Rule
.
Id
,
NewState
:
result
.
State
,
Info
:
result
.
Description
,
OrgId
:
result
.
AlertJob
.
Rule
.
OrgId
,
}
if
err
:=
bus
.
Dispatch
(
cmd
);
err
!=
nil
{
...
...
@@ -161,3 +159,22 @@ func (e *Engine) reactToState(result *AlertResult) {
e
.
notifier
.
Notify
(
result
)
}
}
func
shouldUpdateState
(
result
*
AlertResult
)
bool
{
query
:=
&
m
.
GetLastAlertStateQuery
{
AlertId
:
result
.
AlertJob
.
Rule
.
Id
,
OrgId
:
result
.
AlertJob
.
Rule
.
OrgId
,
}
if
err
:=
bus
.
Dispatch
(
query
);
err
!=
nil
{
log
.
Error2
(
"Failed to read last alert state"
,
"error"
,
err
)
return
false
}
now
:=
time
.
Now
()
noEarlierState
:=
query
.
Result
==
nil
olderThen15Min
:=
query
.
Result
.
Created
.
Before
(
now
.
Add
(
time
.
Minute
*
-
15
))
changedState
:=
query
.
Result
.
NewState
!=
result
.
State
return
noEarlierState
||
changedState
||
olderThen15Min
}
pkg/services/sqlstore/alert_state.go
View file @
488b4237
...
...
@@ -12,6 +12,23 @@ import (
func
init
()
{
bus
.
AddHandler
(
"sql"
,
SetNewAlertState
)
bus
.
AddHandler
(
"sql"
,
GetAlertStateLogByAlertId
)
bus
.
AddHandler
(
"sql"
,
GetLastAlertStateQuery
)
}
func
GetLastAlertStateQuery
(
cmd
*
m
.
GetLastAlertStateQuery
)
error
{
states
:=
make
([]
m
.
AlertState
,
0
)
if
err
:=
x
.
Where
(
"alert_id = ? and org_id = ? "
,
cmd
.
AlertId
,
cmd
.
OrgId
)
.
Desc
(
"created"
)
.
Find
(
&
states
);
err
!=
nil
{
return
err
}
if
len
(
states
)
==
0
{
cmd
.
Result
=
nil
return
fmt
.
Errorf
(
"invalid amount of alertstates. Expected 1 got %v"
,
len
(
states
))
}
cmd
.
Result
=
&
states
[
0
]
return
nil
}
func
SetNewAlertState
(
cmd
*
m
.
UpdateAlertStateCommand
)
error
{
...
...
@@ -30,17 +47,12 @@ func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
return
fmt
.
Errorf
(
"Could not find alert"
)
}
if
alert
.
State
==
cmd
.
NewState
{
cmd
.
Result
=
&
m
.
Alert
{}
return
nil
}
alert
.
State
=
cmd
.
NewState
sess
.
Id
(
alert
.
Id
)
.
Update
(
&
alert
)
alertState
:=
m
.
AlertState
{
AlertId
:
cmd
.
AlertId
,
OrgId
:
cmd
.
Alert
Id
,
OrgId
:
cmd
.
Org
Id
,
NewState
:
cmd
.
NewState
,
Info
:
cmd
.
Info
,
Created
:
time
.
Now
(),
...
...
@@ -54,12 +66,12 @@ func SetNewAlertState(cmd *m.UpdateAlertStateCommand) error {
}
func
GetAlertStateLogByAlertId
(
cmd
*
m
.
GetAlertsStateQuery
)
error
{
alertLog
s
:=
make
([]
m
.
AlertState
,
0
)
state
s
:=
make
([]
m
.
AlertState
,
0
)
if
err
:=
x
.
Where
(
"alert_id = ?"
,
cmd
.
AlertId
)
.
Desc
(
"created"
)
.
Find
(
&
alertLog
s
);
err
!=
nil
{
if
err
:=
x
.
Where
(
"alert_id = ?"
,
cmd
.
AlertId
)
.
Desc
(
"created"
)
.
Find
(
&
state
s
);
err
!=
nil
{
return
err
}
cmd
.
Result
=
&
alertLog
s
cmd
.
Result
=
&
state
s
return
nil
}
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