This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhistory_record.go
110 lines (83 loc) · 2.3 KB
/
history_record.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
package contentpubsub
import (
"time"
)
type HistoryRecord struct {
receivedEvents []*EventRecord
timeToSub []int
operationHistory map[string]int
}
type EventRecord struct {
eventSource string
eventData string
timeOfTravel time.Duration
}
func NewHistoryRecord() *HistoryRecord {
record := &HistoryRecord{operationHistory: make(map[string]int)}
record.operationHistory["Notify"] = 0
return record
}
// SaveReceivedEvent register the time a event took until it reached the subscriber
func (r *HistoryRecord) SaveReceivedEvent(eScource string, eBirth string, eData string) {
past, err1 := time.Parse(time.StampMilli, eBirth)
if err1 != nil {
return
}
present, err2 := time.Parse(time.StampMilli, time.Now().Format(time.StampMilli))
if err2 != nil {
return
}
eventRecord := &EventRecord{
eventSource: eScource,
timeOfTravel: present.Sub(past),
eventData: eData,
}
r.receivedEvents = append(r.receivedEvents, eventRecord)
}
// SaveTimeToSub register the time it took to confirm a subscription
func (r *HistoryRecord) SaveTimeToSub(start string) {
past, err1 := time.Parse(time.StampMilli, start)
if err1 != nil {
return
}
present, err2 := time.Parse(time.StampMilli, time.Now().Format(time.StampMilli))
if err2 != nil {
return
}
r.timeToSub = append(r.timeToSub, int(present.Sub(past).Milliseconds()))
}
// EventStats returns all events time of travel
func (r *HistoryRecord) EventStats() []int {
var events []int
for _, e := range r.receivedEvents {
events = append(events, int(e.timeOfTravel.Milliseconds()))
}
return events
}
// SubStats returns all subscriptions time to completion and deletes the saved values
func (r *HistoryRecord) SubStats() []int {
res := r.timeToSub
r.timeToSub = nil
return res
}
// CompileCorrectnessResults returns the number of events missing or received
// more than once, by comparing with a array of supposed received events
func (r *HistoryRecord) CorrectnessStats(expected []string) (int, int) {
missed := 0
duplicated := 0
for _, exp := range expected {
received := false
for _, e := range r.receivedEvents {
if e.eventData == exp && !received {
received = true
} else if e.eventData == exp {
duplicated++
}
}
if !received {
missed++
}
}
r.receivedEvents = nil
return missed, duplicated
}