Return silence state on /silences

silenceState = "expired" | "active" | "pending"
```
"status": {
  "state": "expired"
}
```
This commit is contained in:
Max Leonard Inden 2017-05-10 11:55:28 +02:00
parent d463f1c298
commit 401e440db4
No known key found for this signature in database
GPG Key ID: 5403C5464810BC26
2 changed files with 28 additions and 0 deletions

View File

@ -574,6 +574,9 @@ func silenceFromProto(s *silencepb.Silence) (*types.Silence, error) {
StartsAt: s.StartsAt,
EndsAt: s.EndsAt,
UpdatedAt: s.UpdatedAt,
Status: types.SilenceStatus{
State: types.CalcSilenceState(s.StartsAt, s.EndsAt),
},
}
for _, m := range s.Matchers {
matcher := &types.Matcher{

View File

@ -333,6 +333,31 @@ type Silence struct {
// timeFunc provides the time against which to evaluate
// the silence. Used for test injection.
now func() time.Time
Status SilenceStatus `json:"status"`
}
type SilenceStatus struct {
State SilenceState `json:"state"`
}
type SilenceState string
const (
SilenceStateExpired SilenceState = "expired"
SilenceStateActive SilenceState = "active"
SilenceStatePending SilenceState = "pending"
)
func CalcSilenceState(start, end time.Time) SilenceState {
current := time.Now()
if current.Before(start) {
return SilenceStatePending
}
if current.Before(end) {
return SilenceStateActive
}
return SilenceStateExpired
}
// Validate returns true iff all fields of the silence have valid values.