Skip to content

Commit 7e3ad2e

Browse files
committed
wip: cleanup state/status
1 parent 7eb8102 commit 7e3ad2e

File tree

2 files changed

+16
-61
lines changed

2 files changed

+16
-61
lines changed

internal/state.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,22 @@ type StateModel struct {
1313
ParticipationKeys *[]api.ParticipationKey
1414
}
1515

16+
func getAverage(data []float64) float64 {
17+
sum := 0.0
18+
for _, element := range data {
19+
sum += element
20+
}
21+
return sum / (float64(len(data)))
22+
}
23+
func getAverageDuration(timings []time.Duration) time.Duration {
24+
sum := 0.0
25+
for _, element := range timings {
26+
sum += element.Seconds()
27+
}
28+
avg := sum / (float64(len(timings)))
29+
return time.Duration(avg * float64(time.Second))
30+
}
31+
1632
func (s *StateModel) Watch(cb func(model *StateModel, err error), ctx context.Context, client *api.ClientWithResponses) {
1733
err := s.Status.Fetch(ctx, client)
1834
if err != nil {

internal/status.go

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"github.com/algorandfoundation/hack-tui/api"
7-
"time"
87
)
98

109
// StatusModel represents a status response from algod.Status
@@ -53,63 +52,3 @@ func (m *StatusModel) Fetch(ctx context.Context, client *api.ClientWithResponses
5352
}
5453
return nil
5554
}
56-
57-
func getAverage(data []float64) float64 {
58-
sum := 0.0
59-
for _, element := range data {
60-
sum += element
61-
}
62-
return sum / (float64(len(data)))
63-
}
64-
func getAverageDuration(timings []time.Duration) time.Duration {
65-
sum := 0.0
66-
for _, element := range timings {
67-
sum += element.Seconds()
68-
}
69-
avg := sum / (float64(len(timings)))
70-
return time.Duration(avg * float64(time.Second))
71-
}
72-
73-
// Watch uses WaitForBlockWithResponse to wait for changes and emits to the HeartBeat channel
74-
func (m *StatusModel) Watch(cb func(model *StatusModel, err error), ctx context.Context, client *api.ClientWithResponses) {
75-
lastRound := m.LastRound
76-
timings := make([]time.Duration, 0)
77-
txns := make([]float64, 0)
78-
for {
79-
startTime := time.Now()
80-
status, err := client.WaitForBlockWithResponse(ctx, int(lastRound))
81-
endTime := time.Now()
82-
if err != nil {
83-
cb(nil, err)
84-
}
85-
var format api.GetBlockParamsFormat = "json"
86-
block, err := client.GetBlockWithResponse(ctx, int(lastRound), &api.GetBlockParams{
87-
Format: &format,
88-
})
89-
if err != nil {
90-
cb(nil, err)
91-
}
92-
m.LastRound = uint64(status.JSON200.LastRound)
93-
94-
dur := endTime.Sub(startTime)
95-
timings = append(timings, dur)
96-
if block.JSON200.Block["txns"] != nil {
97-
txns = append(txns, float64(len(block.JSON200.Block["txns"].([]any)))/getAverageDuration(timings).Seconds())
98-
} else {
99-
txns = append(txns, 0)
100-
}
101-
102-
m.Metrics.RoundTime = getAverageDuration(timings)
103-
m.Metrics.Window = len(timings)
104-
m.Metrics.TPS = getAverage(txns)
105-
106-
// Trim data
107-
if len(timings) >= 100 {
108-
timings = timings[1:]
109-
txns = txns[1:]
110-
}
111-
112-
lastRound = m.LastRound
113-
cb(m, nil)
114-
}
115-
}

0 commit comments

Comments
 (0)