raidcheck/filter_changed_states.go

33 lines
631 B
Go
Raw Normal View History

2021-12-21 20:51:12 +01:00
package main
import (
"reflect"
2021-12-22 22:17:51 +01:00
"git.tordarus.net/Tordarus/dockerhealth"
2021-12-21 20:51:12 +01:00
)
func filterChanges(in <-chan RaidState) <-chan RaidState {
out := make(chan RaidState, 4)
go func(in <-chan RaidState, out chan<- RaidState) {
defer close(out)
currentStates := map[string]RaidState{}
2021-12-22 22:17:51 +01:00
first := true
2021-12-21 20:51:12 +01:00
for state := range in {
if oldState, ok := currentStates[state.Name]; !ok || !reflect.DeepEqual(oldState.DevicesUp, state.DevicesUp) {
2021-12-22 22:17:51 +01:00
dockerhealth.Healthy = first
2021-12-21 20:51:12 +01:00
currentStates[state.Name] = state
out <- state
2021-12-22 22:17:51 +01:00
} else {
dockerhealth.Healthy = true
2021-12-21 20:51:12 +01:00
}
2021-12-22 22:17:51 +01:00
first = false
2021-12-21 20:51:12 +01:00
}
}(in, out)
return out
}