Skip to content

Commit 56196a2

Browse files
committed
feat: delete incident if it was less than 1 second
1 parent 984cca5 commit 56196a2

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

store/bolt.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ func (b *Bolt) EndIncident(ctx context.Context, hostID string, eventID int, ts t
146146
return nil
147147
})
148148
}
149+
func (b *Bolt) DeleteIncident(ctx context.Context, hostID string, eventID int) error {
150+
return b.conn.Update(func(tx *bolt.Tx) error {
151+
bucket := tx.Bucket([]byte(fmt.Sprintf("e-%s", hostID)))
152+
if bucket == nil {
153+
return nil
154+
}
155+
156+
c := bucket.Cursor()
157+
for k, _ := c.First(); k != nil; k, _ = c.Next() {
158+
if int(binary.BigEndian.Uint64(k)) != eventID {
159+
continue
160+
}
161+
return bucket.Delete(k)
162+
}
163+
164+
return nil
165+
})
166+
}
149167
func (b *Bolt) FindIncidents(ctx context.Context, hostID string, skip, limit int) ([]*types.Incident, error) {
150168
res := []*types.Incident{}
151169
err := b.conn.View(func(tx *bolt.Tx) error {

store/memory.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,23 @@ func (m *Memory) EndIncident(ctx context.Context, hostID string, eventID int, ts
116116

117117
return nil
118118
}
119+
func (m *Memory) DeleteIncident(ctx context.Context, hostID string, eventID int) error {
120+
m.Lock()
121+
defer m.Unlock()
122+
123+
if _, ok := m.incidents[hostID]; !ok {
124+
return nil
125+
}
126+
127+
for i, e := range m.incidents[hostID] {
128+
if e.ID == eventID {
129+
m.incidents[hostID] = append(m.incidents[hostID][:i], m.incidents[hostID][i+1:]...)
130+
break
131+
}
132+
}
133+
134+
return nil
135+
}
119136
func (m *Memory) FindIncidents(ctx context.Context, hostID string, skip, limit int) ([]*types.Incident, error) {
120137
m.RLock()
121138
defer m.RUnlock()

store/store.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ type Interface interface {
2323

2424
// AddIncident puts a new incident to the store.
2525
// EndIncident marks the incident as finished by setting the end time.
26+
// DeleteIncident removes the incident from the store.
2627
// FindIncidents returns the list of incidents for the ID.
2728
AddIncident(ctx context.Context, hostID string, e *types.Incident) error
2829
EndIncident(ctx context.Context, hostID string, eventID int, ts time.Time) error
30+
DeleteIncident(ctx context.Context, hostID string, eventID int) error
2931
FindIncidents(ctx context.Context, hostID string, skip, limit int) ([]*types.Incident, error)
3032

3133
Close() error

0 commit comments

Comments
 (0)