Skip to content

Commit

Permalink
Move Moira states to separate file, make types for it
Browse files Browse the repository at this point in the history
  • Loading branch information
borovskyav committed Feb 16, 2019
1 parent e476540 commit 8b27e38
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
58 changes: 58 additions & 0 deletions state.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package moira

type State string

type TtlState string

// Default moira triggers and metrics states
const (
StateOK State = "OK"
StateWARN State = "WARN"
StateERROR State = "ERROR"
StateNODATA State = "NODATA"
// Use this for trigger check unexpected errors
StateEXCEPTION State = "EXCEPTION"
// Use this only for test notifications
StateTEST State = "TEST"
)

// Moira ttl states
const (
TTLStateOK TtlState = "OK"
TTLStateWARN TtlState = "WARN"
TTLStateERROR TtlState = "ERROR"
TTLStateNODATA TtlState = "NODATA"
TTLStateDEL TtlState = "DEL"
)

var (
eventStatesPriority = [...]State{StateOK, StateWARN, StateERROR, StateNODATA, StateEXCEPTION, StateTEST}
stateScore = map[State]int64{
StateOK: 0,
StateWARN: 1,
StateERROR: 100,
StateNODATA: 1000,
StateEXCEPTION: 100000,
}
eventStateWeight1 = map[State]int{
StateOK: 0,
StateWARN: 1,
StateERROR: 100,
StateNODATA: 10000,
}
)

func (state TtlState) ToMetricState() State {
if state == TTLStateDEL {
return StateNODATA
}
return State(state)
}

// ToTriggerState is an auxiliary function to handle trigger state properly.
func (state TtlState) ToTriggerState() State {
if state == TTLStateDEL {
return StateOK
}
return State(state)
}
27 changes: 27 additions & 0 deletions state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package moira

import (
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestTtlState_ToMetricState(t *testing.T) {
Convey("ToMetricState test", t, func() {
So(TTLStateDEL.ToMetricState(), ShouldResemble, StateNODATA)
So(TTLStateOK.ToMetricState(), ShouldResemble, StateOK)
So(TTLStateWARN.ToMetricState(), ShouldResemble, StateWARN)
So(TTLStateERROR.ToMetricState(), ShouldResemble, StateERROR)
So(TTLStateNODATA.ToMetricState(), ShouldResemble, StateNODATA)
})
}

func TestTtlState_ToTriggerState(t *testing.T) {
Convey("ToTriggerState test", t, func() {
So(TTLStateDEL.ToTriggerState(), ShouldResemble, StateOK)
So(TTLStateOK.ToTriggerState(), ShouldResemble, StateOK)
So(TTLStateWARN.ToTriggerState(), ShouldResemble, StateWARN)
So(TTLStateERROR.ToTriggerState(), ShouldResemble, StateERROR)
So(TTLStateNODATA.ToTriggerState(), ShouldResemble, StateNODATA)
})
}

0 comments on commit 8b27e38

Please sign in to comment.