|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "github.com/algorandfoundation/hack-tui/api"
|
7 |
| - "time" |
8 | 7 | )
|
9 | 8 |
|
10 | 9 | // StatusModel represents a status response from algod.Status
|
@@ -53,63 +52,3 @@ func (m *StatusModel) Fetch(ctx context.Context, client *api.ClientWithResponses
|
53 | 52 | }
|
54 | 53 | return nil
|
55 | 54 | }
|
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