@@ -12,6 +12,9 @@ type StateModel struct {
12
12
Metrics MetricsModel
13
13
Accounts map [string ]Account
14
14
ParticipationKeys * []api.ParticipationKey
15
+ // TODO: handle contexts instead of adding it to state
16
+ Admin bool
17
+ Watching bool
15
18
}
16
19
17
20
func getAverage (data []float64 ) float64 {
@@ -30,7 +33,10 @@ func getAverageDuration(timings []time.Duration) time.Duration {
30
33
return time .Duration (avg * float64 (time .Second ))
31
34
}
32
35
36
+ // TODO: allow context to handle loop
33
37
func (s * StateModel ) Watch (cb func (model * StateModel , err error ), ctx context.Context , client * api.ClientWithResponses ) {
38
+ s .Watching = true
39
+
34
40
err := s .Status .Fetch (ctx , client )
35
41
if err != nil {
36
42
cb (nil , err )
@@ -44,6 +50,9 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
44
50
txns := make ([]float64 , 0 )
45
51
46
52
for {
53
+ if ! s .Watching {
54
+ break
55
+ }
47
56
// Collect Time of Round
48
57
startTime := time .Now ()
49
58
status , err := client .WaitForBlockWithResponse (ctx , int (lastRound ))
@@ -62,13 +71,7 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
62
71
s .Status .LastRound = uint64 (status .JSON200 .LastRound )
63
72
64
73
// Fetch Keys
65
- s .ParticipationKeys , err = GetPartKeys (ctx , client )
66
- if err != nil {
67
- cb (nil , err )
68
- }
69
-
70
- // Get Accounts
71
- s .Accounts = AccountsFromState (s )
74
+ s .UpdateKeys (ctx , client )
72
75
73
76
// Fetch Block
74
77
var format api.GetBlockParamsFormat = "json"
@@ -93,6 +96,9 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
93
96
s .Metrics .Window = len (timings )
94
97
s .Metrics .TPS = getAverage (txns )
95
98
99
+ // Fetch RX/TX
100
+ s .UpdateMetrics (ctx , client , timings , txns )
101
+
96
102
// Trim data
97
103
if len (timings ) >= 100 {
98
104
timings = timings [1 :]
@@ -103,3 +109,49 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
103
109
cb (s , nil )
104
110
}
105
111
}
112
+
113
+ func (s * StateModel ) Stop () {
114
+ s .Watching = false
115
+ }
116
+
117
+ func (s * StateModel ) UpdateMetrics (
118
+ ctx context.Context ,
119
+ client * api.ClientWithResponses ,
120
+ timings []time.Duration ,
121
+ txns []float64 ,
122
+ ) {
123
+ if s == nil {
124
+ panic ("StateModel is nil while UpdateMetrics is called" )
125
+ }
126
+ // Set Metrics
127
+ s .Metrics .RoundTime = getAverageDuration (timings )
128
+ s .Metrics .Window = len (timings )
129
+ s .Metrics .TPS = getAverage (txns )
130
+
131
+ // Fetch RX/TX
132
+ res , err := GetMetrics (ctx , client )
133
+ if err != nil {
134
+ s .Metrics .Enabled = false
135
+ }
136
+ if err == nil {
137
+ s .Metrics .Enabled = true
138
+ s .Metrics .TX = res ["algod_network_sent_bytes_total" ]
139
+ s .Metrics .RX = res ["algod_network_received_bytes_total" ]
140
+ }
141
+ }
142
+
143
+ func (s * StateModel ) UpdateAccounts () {
144
+ s .Accounts = AccountsFromState (s )
145
+ }
146
+
147
+ func (s * StateModel ) UpdateKeys (ctx context.Context , client * api.ClientWithResponses ) {
148
+ var err error
149
+ s .ParticipationKeys , err = GetPartKeys (ctx , client )
150
+ if err != nil {
151
+ s .Admin = false
152
+ }
153
+ if err == nil {
154
+ s .Admin = true
155
+ s .UpdateAccounts ()
156
+ }
157
+ }
0 commit comments