-
Notifications
You must be signed in to change notification settings - Fork 198
/
presenterStatusHandler.go
138 lines (113 loc) · 3.29 KB
/
presenterStatusHandler.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
package presenter
import (
"math/big"
"strings"
"sync"
)
//maxLogLines is used to specify how many lines of logs need to store in slice
var maxLogLines = 100
// PresenterStatusHandler is the AppStatusHandler impl that is able to process and store received data
type PresenterStatusHandler struct {
presenterMetrics *sync.Map
logLines []string
mutLogLineWrite sync.RWMutex
oldRound uint64
synchronizationSpeedHistory []uint64
totalRewardsOld *big.Float
}
// NewPresenterStatusHandler will return an instance of the struct
func NewPresenterStatusHandler() *PresenterStatusHandler {
psh := &PresenterStatusHandler{
presenterMetrics: &sync.Map{},
synchronizationSpeedHistory: make([]uint64, 0),
totalRewardsOld: big.NewFloat(0),
}
return psh
}
// IsInterfaceNil returns true if there is no value under the interface
func (psh *PresenterStatusHandler) IsInterfaceNil() bool {
return psh == nil
}
// SetInt64Value method - will update the value for a key
func (psh *PresenterStatusHandler) SetInt64Value(key string, value int64) {
psh.presenterMetrics.Store(key, value)
}
// SetUInt64Value method - will update the value for a key
func (psh *PresenterStatusHandler) SetUInt64Value(key string, value uint64) {
psh.presenterMetrics.Store(key, value)
}
// SetStringValue method - will update the value of a key
func (psh *PresenterStatusHandler) SetStringValue(key string, value string) {
psh.presenterMetrics.Store(key, value)
}
// Increment - will increment the value of a key
func (psh *PresenterStatusHandler) Increment(key string) {
keyValueI, ok := psh.presenterMetrics.Load(key)
if !ok {
return
}
keyValue, ok := keyValueI.(uint64)
if !ok {
return
}
keyValue++
psh.presenterMetrics.Store(key, keyValue)
}
// AddUint64 - will increase the value of a key with a value
func (psh *PresenterStatusHandler) AddUint64(key string, value uint64) {
keyValueI, ok := psh.presenterMetrics.Load(key)
if !ok {
return
}
keyValue, ok := keyValueI.(uint64)
if !ok {
return
}
keyValue += value
psh.presenterMetrics.Store(key, keyValue)
}
// Decrement - will decrement the value of a key
func (psh *PresenterStatusHandler) Decrement(key string) {
keyValueI, ok := psh.presenterMetrics.Load(key)
if !ok {
return
}
keyValue, ok := keyValueI.(uint64)
if !ok {
return
}
if keyValue == 0 {
return
}
keyValue--
psh.presenterMetrics.Store(key, keyValue)
}
// Close method - won't do anything
func (psh *PresenterStatusHandler) Close() {
}
func (psh *PresenterStatusHandler) Write(p []byte) (n int, err error) {
go func(p []byte) {
logLine := string(p)
stringSlice := strings.Split(logLine, "\n")
psh.mutLogLineWrite.Lock()
for _, line := range stringSlice {
line = strings.Replace(line, "\r", "", len(line))
if line != "" {
psh.logLines = append(psh.logLines, line)
}
}
startPos := len(psh.logLines) - maxLogLines
if startPos < 0 {
startPos = 0
}
psh.logLines = psh.logLines[startPos:len(psh.logLines)]
psh.mutLogLineWrite.Unlock()
}(p)
return len(p), nil
}
// GetLogLines will return log lines that's need to be displayed
func (psh *PresenterStatusHandler) GetLogLines() []string {
psh.mutLogLineWrite.RLock()
defer psh.mutLogLineWrite.RUnlock()
return psh.logLines
}