4
4
"context"
5
5
"errors"
6
6
"github.com/algorandfoundation/hack-tui/api"
7
- "time"
8
7
)
9
8
10
9
type StateModel struct {
@@ -17,25 +16,12 @@ type StateModel struct {
17
16
Watching bool
18
17
}
19
18
20
- func getAverage (data []float64 ) float64 {
21
- sum := 0.0
22
- for _ , element := range data {
23
- sum += element
24
- }
25
- return sum / (float64 (len (data )))
26
- }
27
- func getAverageDuration (timings []time.Duration ) time.Duration {
28
- sum := 0.0
29
- for _ , element := range timings {
30
- sum += element .Seconds ()
31
- }
32
- avg := sum / (float64 (len (timings )))
33
- return time .Duration (avg * float64 (time .Second ))
34
- }
35
-
36
19
// TODO: allow context to handle loop
37
20
func (s * StateModel ) Watch (cb func (model * StateModel , err error ), ctx context.Context , client * api.ClientWithResponses ) {
38
21
s .Watching = true
22
+ if s .Metrics .Window == 0 {
23
+ s .Metrics .Window = 100
24
+ }
39
25
40
26
err := s .Status .Fetch (ctx , client )
41
27
if err != nil {
@@ -44,61 +30,33 @@ func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Co
44
30
45
31
lastRound := s .Status .LastRound
46
32
47
- // Collection of Round Durations
48
- timings := make ([]time.Duration , 0 )
49
- // Collection of Transaction Counts
50
- txns := make ([]float64 , 0 )
51
-
52
33
for {
53
34
if ! s .Watching {
54
35
break
55
36
}
56
- // Collect Time of Round
57
- startTime := time .Now ()
58
37
status , err := client .WaitForBlockWithResponse (ctx , int (lastRound ))
59
38
if err != nil {
60
39
cb (nil , err )
61
40
}
62
41
if status .StatusCode () != 200 {
63
42
cb (nil , errors .New (status .Status ()))
64
43
}
65
- // Store round timing
66
- endTime := time .Now ()
67
- dur := endTime .Sub (startTime )
68
- timings = append (timings , dur )
69
44
70
45
// Update Status
71
46
s .Status .LastRound = uint64 (status .JSON200 .LastRound )
72
47
73
48
// Fetch Keys
74
49
s .UpdateKeys (ctx , client )
75
50
76
- // Fetch Block
77
- var format api.GetBlockParamsFormat = "json"
78
- block , err := client .GetBlockWithResponse (ctx , int (lastRound ), & api.GetBlockParams {
79
- Format : & format ,
80
- })
81
- if err != nil {
82
- cb (nil , err )
83
- }
84
-
85
- // Check for transactions
86
- if block .JSON200 .Block ["txns" ] != nil {
87
- // Get the average duration in seconds (TPS)
88
- txnCount := float64 (len (block .JSON200 .Block ["txns" ].([]any )))
89
- txns = append (txns , txnCount / getAverageDuration (timings ).Seconds ())
90
- } else {
91
- txns = append (txns , 0 )
92
- }
93
-
94
- // Fetch RX/TX every 5th round
51
+ // Run Round Averages and RX/TX every 5 rounds
95
52
if s .Status .LastRound % 5 == 0 {
96
- s .UpdateMetrics (ctx , client , timings , txns )
97
- }
98
- // Trim data
99
- if len (timings ) >= 100 {
100
- timings = timings [1 :]
101
- txns = txns [1 :]
53
+ bm , err := GetBlockMetrics (ctx , client , s .Status .LastRound , s .Metrics .Window )
54
+ if err != nil {
55
+ cb (nil , err )
56
+ }
57
+ s .Metrics .RoundTime = bm .AvgTime
58
+ s .Metrics .TPS = bm .TPS
59
+ s .UpdateMetricsFromRPC (ctx , client )
102
60
}
103
61
104
62
lastRound = s .Status .LastRound
@@ -110,20 +68,7 @@ func (s *StateModel) Stop() {
110
68
s .Watching = false
111
69
}
112
70
113
- func (s * StateModel ) UpdateMetrics (
114
- ctx context.Context ,
115
- client * api.ClientWithResponses ,
116
- timings []time.Duration ,
117
- txns []float64 ,
118
- ) {
119
- if s == nil {
120
- panic ("StateModel is nil while UpdateMetrics is called" )
121
- }
122
- // Set Metrics
123
- s .Metrics .RoundTime = getAverageDuration (timings )
124
- s .Metrics .Window = len (timings )
125
- s .Metrics .TPS = getAverage (txns )
126
-
71
+ func (s * StateModel ) UpdateMetricsFromRPC (ctx context.Context , client * api.ClientWithResponses ) {
127
72
// Fetch RX/TX
128
73
res , err := GetMetrics (ctx , client )
129
74
if err != nil {
@@ -135,7 +80,6 @@ func (s *StateModel) UpdateMetrics(
135
80
s .Metrics .RX = res ["algod_network_received_bytes_total" ]
136
81
}
137
82
}
138
-
139
83
func (s * StateModel ) UpdateAccounts () {
140
84
s .Accounts = AccountsFromState (s )
141
85
}
0 commit comments