Return silence state on /silences
silenceState = "expired" | "active" | "pending" ``` "status": { "state": "expired" } ```
This commit is contained in:
parent
d463f1c298
commit
401e440db4
|
@ -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{
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue