Files
karma/store/store.go
Łukasz Mierzwa 0a0e3b0d16 Refactor store code to be a little less ugly
This makes the code a little bit cleaner and safer, as it moves lots of locks into the store package, but it could still use some work.
2017-04-01 16:26:39 -07:00

45 lines
1.1 KiB
Go

package store
import (
"sync"
"github.com/cloudflare/unsee/models"
)
type dataStore struct {
Lock sync.RWMutex
Alerts []models.UnseeAlertGroup
Silences map[string]models.UnseeSilence
Colors models.UnseeColorMap
Autocomplete []models.UnseeAutocomplete
}
// Store will keep all Alertmanager data we collect
var Store = dataStore{}
// GetSilence returns silence data for specific silence id or nil if not found
func (ds *dataStore) GetSilence(s string) *models.UnseeSilence {
ds.Lock.RLock()
defer ds.Lock.RUnlock()
if silence, found := ds.Silences[s]; found {
return &silence
}
return nil
}
// SetSilences allows to update silence list stored internally
func (ds *dataStore) SetSilences(s map[string]models.UnseeSilence) {
ds.Lock.Lock()
defer ds.Lock.Unlock()
ds.Silences = s
}
// Update will lock the store and update internal data
func (ds *dataStore) Update(alerts []models.UnseeAlertGroup, colors models.UnseeColorMap, autocomplete []models.UnseeAutocomplete) {
ds.Lock.Lock()
defer ds.Lock.Unlock()
ds.Alerts = alerts
ds.Colors = colors
ds.Autocomplete = autocomplete
}