-
Notifications
You must be signed in to change notification settings - Fork 0
/
event_history.go
146 lines (135 loc) · 4.37 KB
/
event_history.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cyclecmd
import "fmt"
// EventHistory records events and offers behavior to manipulate the history and to
// read events from history.
type EventHistory struct {
// entries is a sequence of events and tokens that triggered those events
entries []EventHistoryEntry
}
// NewEventHistory initialises an event history instance that can be used to record past events.
//
// Returns:
// - `*EventHistory` : Returns an instance of EventHistory
func NewEventHistory() *EventHistory {
return &EventHistory{}
}
// Len returns the number of events that has been recorded.
//
// Returns:
// - `int` : Number of events
func (eh *EventHistory) Len() int {
return len(eh.entries)
}
// AddEvent will add an event to the history.
//
// Parameters:
// - `eventEntry` : Entry that will be recorded and contains all information related to an event.
func (eh *EventHistory) AddEvent(eventEntry EventHistoryEntry) {
eh.entries = append(eh.entries, eventEntry)
}
// RetrieveEventEntryByIndex will return the event entry at index position
//
// Parameters:
// - `index` : Position in the event history that you want to access
//
// Returns:
// - `EventHistoryEntry` : The event history entry at index position
// - `error` : Returns an error when there is no event history entry at position index
func (eh *EventHistory) RetrieveEventEntryByIndex(index int) (EventHistoryEntry, error) {
if index < 0 || index >= eh.Len() {
return EventHistoryEntry{}, fmt.Errorf("index %v error, index is either smaller than 0 or larger than the length of the event history", index)
}
return eh.entries[index], nil
}
// RemoveNthEventFromHistory removes the nth event from the history. If the nth element does not exist,
// nothing will happen.
//
// Parameters:
// - `n` : nth event that should be removed from history
func (eh *EventHistory) RemoveNthEventFromHistory(n int) {
eventHistoryLength := eh.Len()
if n > eventHistoryLength || n < 0 {
return
}
eh.entries = append(eh.entries[:n], eh.entries[n+1:]...)
}
// PrintLastEventHistoryEntries will print information related to the last n events that
// were recorded.
//
// Parameters:
// - `n` : Number of events
func (eh *EventHistory) PrintLastEventHistoryEntries(n int) {
count := 0
for i := eh.Len() - 1; i >= 0; i-- {
if count == n {
break
}
fmt.Printf("Event Name: %s, Token: %s\r\n", eh.entries[i].EventName, eh.entries[i].Token)
count += 1
}
}
// GetLastEventsFromHistoryToEventReference will return all event names that followed after a specific event happened.
//
// Parameters:
// - `eventName` : The name of the event that serves as a reference
//
// Returns:
// - `[]string` : Returns a series of event names that happened after the reference event
func (eh *EventHistory) GetLastEventsFromHistoryToEventReference(eventName string) []string {
var eventNames []string
for i := eh.Len() - 1; i >= 0; i-- {
eventNameFromHistory := eh.entries[i].EventName
if eventNameFromHistory == eventName {
break
}
eventNames = append(eventNames, eventNameFromHistory)
}
// We need to reverse the array since we appended the elements from the back to the front
return reverseArray(eventNames)
}
// MostRecentSpliceEventsOfHistory will return a range of events that occured between some event that
// is specified by eventName.
//
// Parameters:
// - `eventName` : Name of the reference event
//
// Returns:
// - `[]EventHistoryEntry` : Sequence of event history entries
func (eh *EventHistory) MostRecentSpliceEventsOfHistory(eventName string) []EventHistoryEntry {
var splicedEvents []EventHistoryEntry
lastIndex := eh.Len() - 1
foundStart := false
foundEnd := false
for i := lastIndex; i >= 0; i-- {
if foundStart {
splicedEvents = append(splicedEvents, eh.entries[i])
}
if eventName == eh.entries[i].EventName && !foundStart {
foundStart = true
continue
}
if eventName == eh.entries[i].EventName && foundStart {
foundEnd = true
splicedEvents = splicedEvents[:len(splicedEvents)-1]
}
if foundEnd {
break
}
}
// We need to reverse the array since we appended the elements from the back to the front
return reverseArray(splicedEvents)
}
// Generic implementation to reverse a slice.
//
// Parameters:
// - `arr` : Slice that should be reversed
//
// Returns:
// - `[]T` : Reversed slice
func reverseArray[T any](arr []T) []T {
n := len(arr)
for i := 0; i < n/2; i++ {
arr[i], arr[n-i-1] = arr[n-i-1], arr[i]
}
return arr
}