Skip to content

Commit

Permalink
feat: optimize speed to adding incident record
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 25, 2021
1 parent 88d41b6 commit 758cc98
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
2 changes: 1 addition & 1 deletion exporter/freeze.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func freezeStatus(s *store.Store) frozenStatus {
status.CurrentStatus = append(status.CurrentStatus, freezeProbeHistory(r))
}

for _, i := range s.CurrentIncidents {
for _, i := range s.CurrentIncidents() {
status.CurrentIncidents = append(status.CurrentIncidents, freezeIncident(i))
}
for _, i := range s.IncidentHistory {
Expand Down
14 changes: 14 additions & 0 deletions store/incident.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,17 @@ func (i *Incident) SameTarget(r Record) bool {
func (i *Incident) IsContinued(r Record) bool {
return i.ResolvedAt.IsZero() && i.Status == r.Status && i.Message == r.Message
}

type byIncidentCaused []*Incident

func (xs byIncidentCaused) Len() int {
return len(xs)
}

func (xs byIncidentCaused) Less(i, j int) bool {
return xs[i].CausedAt.Before(xs[j].CausedAt)
}

func (xs byIncidentCaused) Swap(i, j int) {
xs[i], xs[j] = xs[j], xs[i]
}
51 changes: 31 additions & 20 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Store struct {
Console io.Writer

ProbeHistory ProbeHistoryMap
CurrentIncidents []*Incident
currentIncidents map[string]*Incident
IncidentHistory []*Incident

OnIncident []IncidentHandler
Expand All @@ -77,9 +77,10 @@ type Store struct {

func New(path string) (*Store, error) {
store := &Store{
Path: path,
Console: os.Stdout,
ProbeHistory: make(ProbeHistoryMap),
Path: path,
Console: os.Stdout,
ProbeHistory: make(ProbeHistoryMap),
currentIncidents: make(map[string]*Incident),
}

var err error
Expand All @@ -95,29 +96,39 @@ func (s *Store) Close() error {
return s.file.Close()
}

func (s *Store) setIncidentIfNeed(r Record, needCallback bool) {
for i := 0; i < len(s.CurrentIncidents); i++ {
x := s.CurrentIncidents[i]
if x.SameTarget(r) {
if !x.IsContinued(r) {
x.ResolvedAt = r.CheckedAt
s.IncidentHistory = append(s.IncidentHistory, x)
s.CurrentIncidents = append(s.CurrentIncidents[:i], s.CurrentIncidents[i+1:]...)

if len(s.IncidentHistory) > INCIDENT_HISTORY_LEN {
s.IncidentHistory = s.IncidentHistory[1:]
}

break
}
func (s *Store) CurrentIncidents() []*Incident {
result := make([]*Incident, len(s.currentIncidents))

i := 0
for _, x := range s.currentIncidents {
result[i] = x
i++
}

sort.Sort(byIncidentCaused(result))

return result
}

func (s *Store) setIncidentIfNeed(r Record, needCallback bool) {
target := r.Target.String()
if cur, ok := s.currentIncidents[target]; ok {
if cur.IsContinued(r) {
return
}

cur.ResolvedAt = r.CheckedAt
s.IncidentHistory = append(s.IncidentHistory, cur)
delete(s.currentIncidents, target)

if len(s.IncidentHistory) > INCIDENT_HISTORY_LEN {
s.IncidentHistory = s.IncidentHistory[1:]
}
}

if r.Status != STATUS_HEALTHY {
incident := NewIncident(r)
s.CurrentIncidents = append(s.CurrentIncidents, incident)
s.currentIncidents[target] = incident

if needCallback {
s.IncidentCount++
Expand Down
23 changes: 12 additions & 11 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,49 +202,50 @@ func TestStore_incident(t *testing.T) {
t.Helper()

s.Append(store.Record{
Target: &url.URL{Scheme: "dummy", Opaque: opaque},
Message: message,
Status: status,
CheckedAt: time.Now(),
Target: &url.URL{Scheme: "dummy", Opaque: opaque},
Message: message,
Status: status,
})
}

appendRecord("incident-test-1", "1-1", store.STATUS_HEALTHY)
assertIncidents(s.CurrentIncidents)
assertIncidents(s.CurrentIncidents())
assertIncidents(s.IncidentHistory)
assertLastIncident("")

appendRecord("incident-test-1", "1-2", store.STATUS_FAILURE)
assertIncidents(s.CurrentIncidents, "dummy:incident-test-1")
assertIncidents(s.CurrentIncidents(), "dummy:incident-test-1")
assertIncidents(s.IncidentHistory)
assertLastIncident("1-2")

appendRecord("incident-test-1", "1-2", store.STATUS_FAILURE)
assertIncidents(s.CurrentIncidents, "dummy:incident-test-1")
assertIncidents(s.CurrentIncidents(), "dummy:incident-test-1")
assertIncidents(s.IncidentHistory)
assertLastIncident("1-2")

appendRecord("incident-test-2", "2-1", store.STATUS_FAILURE)
assertIncidents(s.CurrentIncidents, "dummy:incident-test-1", "dummy:incident-test-2")
assertIncidents(s.CurrentIncidents(), "dummy:incident-test-1", "dummy:incident-test-2")
assertIncidents(s.IncidentHistory)
assertLastIncident("2-1")

appendRecord("incident-test-1", "1-3", store.STATUS_FAILURE)
assertIncidents(s.CurrentIncidents, "dummy:incident-test-2", "dummy:incident-test-1")
assertIncidents(s.CurrentIncidents(), "dummy:incident-test-2", "dummy:incident-test-1")
assertIncidents(s.IncidentHistory, "dummy:incident-test-1")
assertLastIncident("1-3")

appendRecord("incident-test-2", "2-1", store.STATUS_FAILURE)
assertIncidents(s.CurrentIncidents, "dummy:incident-test-2", "dummy:incident-test-1")
assertIncidents(s.CurrentIncidents(), "dummy:incident-test-2", "dummy:incident-test-1")
assertIncidents(s.IncidentHistory, "dummy:incident-test-1")
assertLastIncident("1-3")

appendRecord("incident-test-1", "1-4", store.STATUS_HEALTHY)
assertIncidents(s.CurrentIncidents, "dummy:incident-test-2")
assertIncidents(s.CurrentIncidents(), "dummy:incident-test-2")
assertIncidents(s.IncidentHistory, "dummy:incident-test-1", "dummy:incident-test-1")
assertLastIncident("1-3")

appendRecord("incident-test-2", "2-2", store.STATUS_HEALTHY)
assertIncidents(s.CurrentIncidents)
assertIncidents(s.CurrentIncidents())
assertIncidents(s.IncidentHistory, "dummy:incident-test-1", "dummy:incident-test-1", "dummy:incident-test-2")
assertLastIncident("1-3")
}
Expand Down

0 comments on commit 758cc98

Please sign in to comment.