-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
1231 lines (1142 loc) · 29.9 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2023 Blink Labs Software
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/mikioh/tcp"
"github.com/mikioh/tcpinfo"
"github.com/rivo/tview"
netutil "github.com/shirou/gopsutil/v3/net"
"github.com/shirou/gopsutil/v3/process"
terminal "golang.org/x/term"
"github.com/blinklabs-io/nview/internal/config"
"github.com/blinklabs-io/nview/internal/version"
)
// Global command line flags
var cmdlineFlags struct {
configFile string
}
// Global tview application and pages
var app = tview.NewApplication()
var pages = tview.NewPages()
// Main viewport - flexible box
var flex = tview.NewFlex()
// Our text views
var blockTextView = tview.NewTextView().
SetDynamicColors(true)
var chainTextView = tview.NewTextView().
SetDynamicColors(true).
SetTextColor(tcell.ColorGreen).
SetChangedFunc(func() {
// Redraw the screen on a change
app.Draw()
})
var connectionTextView = tview.NewTextView().
SetDynamicColors(true).
SetTextColor(tcell.ColorGreen).
SetChangedFunc(func() {
app.Draw()
})
var coreTextView = tview.NewTextView().
SetDynamicColors(true).
SetTextColor(tcell.ColorGreen).
SetChangedFunc(func() {
app.Draw()
})
var footerTextView = tview.NewTextView().
SetDynamicColors(true).
SetTextColor(tcell.ColorGreen)
var headerTextView = tview.NewTextView().
SetTextColor(tcell.ColorGreen)
var nodeTextView = tview.NewTextView().
SetDynamicColors(true).
SetTextColor(tcell.ColorGreen).
SetChangedFunc(func() {
app.Draw()
})
var peerTextView = tview.NewTextView().
SetDynamicColors(true).
SetChangedFunc(func() {
app.Draw()
})
var resourceTextView = tview.NewTextView().
SetDynamicColors(true).
SetTextColor(tcell.ColorGreen).
SetChangedFunc(func() {
app.Draw()
})
// Text strings
var blockText, chainText, coreText, connectionText, nodeText, peerText, resourceText string
// Metrics variables
var processMetrics *process.Process
// Track our failures
var failCount uint32 = 0
func main() {
// Check if any command line flags are given
flag.StringVar(
&cmdlineFlags.configFile,
"config",
"",
"path to config file to load",
)
flag.Parse()
// Load config
cfg, err := config.LoadConfig(cmdlineFlags.configFile)
if err != nil {
fmt.Printf("Failed to load config: %s", err)
os.Exit(1)
}
// Create a background context
ctx := context.Background()
// Exit if NODE_NAME is > 19 characters
if len([]rune(cfg.App.NodeName)) > 19 {
fmt.Println(
"Please keep node name at or below 19 characters in length!",
)
os.Exit(1)
}
// Determine if we're P2P
p2p = getP2P(ctx, processMetrics)
// Set role
setRole()
// Get public IP
ip, err := getPublicIP(ctx)
if err == nil {
publicIP = &ip
}
checkPeers = true
// Fetch data from Prometheus
go func() {
for {
prom, err := getPromMetrics(ctx)
if err != nil && prom != nil {
failCount++
time.Sleep(time.Second * time.Duration(cfg.Prometheus.Refresh))
continue
}
promMetrics = prom
time.Sleep(time.Second * time.Duration(cfg.Prometheus.Refresh))
}
}()
// Set Epoch
go func() {
for {
setCurrentEpoch()
if currentEpoch != 0 {
time.Sleep(time.Second * 20)
}
}
}()
// Update Process metrics
go func() {
for {
proc, err := getProcessMetrics(ctx)
if err != nil {
failCount++
time.Sleep(time.Second * 1)
continue
}
processMetrics = proc
time.Sleep(time.Second * 1)
}
}()
// Set uptimes
go func() {
for {
uptime := getUptimes(ctx, processMetrics)
if uptime != 0 {
uptimes = uptime
}
time.Sleep(time.Second * 1)
}
}()
// Filter peers
go func() {
for {
err := filterPeers(ctx)
if err != nil {
failCount++
time.Sleep(time.Second * 1)
continue
}
time.Sleep(time.Second * 1)
}
}()
// Ping peers
go func() {
for {
err := pingPeers(ctx)
if err != nil {
failCount++
time.Sleep(time.Second * 10)
continue
}
time.Sleep(time.Second * 10)
}
}()
// Populate initial text from metrics
nodeText = getNodeText(ctx)
nodeTextView.SetText(nodeText).SetTitle("Node").SetBorder(true)
resourceText = getResourceText(ctx)
resourceTextView.SetText(resourceText).SetTitle("Resources").SetBorder(true)
connectionText = getConnectionText(ctx)
connectionTextView.SetText(connectionText).
SetTitle("Connections").
SetBorder(true)
coreText = getCoreText(ctx)
coreTextView.SetText(coreText).SetTitle("Core").SetBorder(true)
chainText = fmt.Sprintf("%s%s", getEpochText(ctx), getChainText(ctx))
chainTextView.SetText(chainText).SetTitle("Chain").SetBorder(true)
blockText = getBlockText(ctx)
blockTextView.SetText(blockText).
SetTitle("Block Propagation").
SetBorder(true)
peerText = getPeerText(ctx)
peerTextView.SetText(peerText).SetTitle("Peers").SetBorder(true)
// Set our footer
defaultFooterText := " [yellow](esc/q)[white] Quit | [yellow](p)[white] Peer Analysis"
footerTextView.SetText(defaultFooterText)
// Add content to our flex box
layout := tview.NewFlex()
leftSide := tview.NewFlex()
middleSide := tview.NewFlex()
flex.SetDirection(tview.FlexRow).
// Row 1 is our application header
AddItem(headerTextView.SetText(fmt.Sprintln(" > nview -", version.GetVersionString())),
1,
1,
false).
// Row 2 is our main text section, and its own flex
AddItem(layout.
AddItem(leftSide.SetDirection(tview.FlexRow).
// Node
AddItem(nodeTextView,
8,
0,
false).
// Resources
AddItem(resourceTextView,
8,
0,
false).
// Connections
AddItem(connectionTextView,
11,
0,
false),
37,
1,
false).
AddItem(middleSide.SetDirection(tview.FlexRow).
// Chain
AddItem(chainTextView,
8,
1,
false).
// Block
AddItem(blockTextView,
4,
0,
false).
// Peers
AddItem(peerTextView,
0,
3,
true),
74,
2,
true),
0,
6,
true).
// Row 3 is our footer
AddItem(footerTextView, 2, 0, false)
// Core
if role == "Core" {
leftSide.AddItem(coreTextView, 0, 1, false)
} else {
leftSide.AddItem(nil, 0, 1, false)
}
// TODO: another section + data
// layout.AddItem(tview.NewBox().SetBorder(true).SetTitle("Coming Soon"), 22, 1, false)
peerStats.RTTresultsMap = make(map[string]*Peer)
peerStats.RTTresultsSlice = []*Peer{}
// capture inputs
flex.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Rune() == 104 || event.Rune() == 114 { // h or r
setRole()
resetPeers()
checkPeers = true
footerTextView.Clear()
footerTextView.SetText(defaultFooterText)
var tmpText string
tmpText = getNodeText(ctx)
if tmpText != "" && tmpText != nodeText {
nodeText = tmpText
nodeTextView.Clear()
nodeTextView.SetText(nodeText)
}
tmpText = getResourceText(ctx)
if tmpText != "" && tmpText != resourceText {
resourceText = tmpText
resourceTextView.Clear()
resourceTextView.SetText(resourceText)
}
tmpText = getConnectionText(ctx)
if tmpText != "" && tmpText != connectionText {
connectionText = tmpText
connectionTextView.Clear()
connectionTextView.SetText(connectionText)
}
tmpText = getCoreText(ctx)
if tmpText != "" && tmpText != coreText {
coreText = tmpText
coreTextView.Clear()
coreTextView.SetText(coreText)
}
tmpText = fmt.Sprintf(
"%s\n%s",
getEpochText(ctx),
getChainText(ctx),
)
if tmpText != "" && tmpText != chainText {
chainText = tmpText
chainTextView.Clear()
chainTextView.SetText(chainText)
}
tmpText = getBlockText(ctx)
if tmpText != "" && tmpText != blockText {
blockText = tmpText
blockTextView.Clear()
blockTextView.SetText(blockText)
}
// Peers are last since they take time to process
tmpText = getPeerText(ctx)
if tmpText != "" && tmpText != peerText {
peerText = tmpText
peerTextView.Clear()
peerTextView.SetText(peerText)
// Scroll to the top only once
if scrollPeers {
scrollPeers = false
peerTextView.ScrollToBeginning()
}
}
}
if event.Rune() == 112 { // p
resetPeers()
checkPeers = true
scrollPeers = false
}
if event.Rune() == 113 || event.Key() == tcell.KeyEscape { // q
app.Stop()
}
return event
})
// Pages
pages.AddPage("Main", flex, true, true)
// Start our background refresh timer
go func() {
for {
if failCount >= cfg.App.Retries {
panic(
fmt.Errorf(
"COULD NOT CONNECT TO A RUNNING INSTANCE, %d FAILED ATTEMPTS IN A ROW!",
failCount,
),
)
}
// Refresh all the things
setRole()
var tmpText string
tmpText = getNodeText(ctx)
if tmpText != "" && tmpText != nodeText {
nodeText = tmpText
nodeTextView.Clear()
nodeTextView.SetText(nodeText)
}
tmpText = getResourceText(ctx)
if tmpText != "" && tmpText != resourceText {
resourceText = tmpText
resourceTextView.Clear()
resourceTextView.SetText(resourceText)
}
tmpText = getConnectionText(ctx)
if tmpText != "" && tmpText != connectionText {
connectionText = tmpText
connectionTextView.Clear()
connectionTextView.SetText(connectionText)
}
tmpText = getCoreText(ctx)
if tmpText != "" && tmpText != coreText {
coreText = tmpText
coreTextView.Clear()
coreTextView.SetText(coreText)
}
tmpText = fmt.Sprintf(
"%s\n%s",
getEpochText(ctx),
getChainText(ctx),
)
if tmpText != "" && tmpText != chainText {
chainText = tmpText
chainTextView.Clear()
chainTextView.SetText(chainText)
}
tmpText = getBlockText(ctx)
if tmpText != "" && tmpText != blockText {
blockText = tmpText
blockTextView.Clear()
blockTextView.SetText(blockText)
}
tmpText = getPeerText(ctx)
if tmpText != "" && tmpText != peerText {
peerText = tmpText
peerTextView.Clear()
peerTextView.SetText(peerText)
// Scroll to the top only once
if scrollPeers {
scrollPeers = false
peerTextView.ScrollToBeginning()
}
}
time.Sleep(time.Second * time.Duration(cfg.App.Refresh))
}
}()
if err := app.SetRoot(pages, true).EnableMouse(false).Run(); err != nil {
panic(err)
}
}
var uptimes uint64
func getUptimes(ctx context.Context, processMetrics *process.Process) uint64 {
if processMetrics == nil {
return uptimes
}
// Calculate uptime
createTime, err := processMetrics.CreateTimeWithContext(ctx)
if err != nil {
return uptimes
}
// createTime is milliseconds since UNIX epoch, convert to seconds
uptimes = uint64(time.Now().Unix() - (createTime / 1000))
return uptimes
}
// Track size of epoch items
var epochItemsLast = 0
func getEpochProgress() float32 {
cfg := config.GetConfig()
var epochProgress float32
if promMetrics == nil {
epochProgress = float32(0.0)
} else if promMetrics.EpochNum >= uint64(cfg.Node.ShelleyTransEpoch) {
epochProgress = float32(
(float32(promMetrics.SlotInEpoch) / float32(cfg.Node.ShelleyGenesis.EpochLength)) * 100,
)
} else {
epochProgress = float32(
(float32(promMetrics.SlotInEpoch) / float32(cfg.Node.ByronGenesis.EpochLength)) * 100,
)
}
return epochProgress
}
func getEpochText(ctx context.Context) string {
var sb strings.Builder
epochProgress := getEpochProgress()
epochProgress1dec := fmt.Sprintf("%.1f", epochProgress)
sb.WriteString(
fmt.Sprintf(
// `" Epoch [blue]%d[white] [[blue]%s%%[white]], [blue]%s[white] %-12s\n",
" [green]Epoch: [white]%d[blue] [[white]%s%%[blue]]\n",
currentEpoch,
epochProgress1dec,
// epochTimeLeft,
// "remaining",
),
)
// Epoch progress bar
var epochBar string
var granularity int = 68
var charMarked string
var charUnmarked string
// TODO: legacy mode vs new
if false {
charMarked = string('#')
charUnmarked = string('.')
} else {
charMarked = string('▌')
charUnmarked = string('▖')
}
epochItems := int(epochProgress) * granularity / 100
if epochBar == "" || epochItems != epochItemsLast {
epochBar = ""
epochItemsLast = epochItems
for i := 0; i <= granularity-1; i++ {
if i < epochItems {
epochBar += fmt.Sprintf("[blue]%s", charMarked)
} else {
epochBar += fmt.Sprintf("[white]%s", charUnmarked)
}
}
}
sb.WriteString(fmt.Sprintf(" [blue]%s[green]\n", epochBar))
return fmt.Sprint(sb.String())
}
func getChainText(ctx context.Context) string {
if promMetrics == nil {
return chainText
}
var sb strings.Builder
// Blocks / Slots / Tx
mempoolTxKBytes := promMetrics.MempoolBytes / 1024
kWidth := strconv.Itoa(10 -
len(strconv.FormatUint(promMetrics.MempoolTx, 10)) -
len(strconv.FormatUint(mempoolTxKBytes, 10)))
tipRef := getSlotTipRef()
tipDiff := (tipRef - promMetrics.SlotNum)
// Row 1
sb.WriteString(fmt.Sprintf(
" Block : [white]%-"+strconv.Itoa(10)+"s[green]",
strconv.FormatUint(promMetrics.BlockNum, 10),
))
sb.WriteString(fmt.Sprintf(
" Tip (ref) : [white]%-"+strconv.Itoa(10)+"s[green]",
strconv.FormatUint(tipRef, 10),
))
sb.WriteString(fmt.Sprintf(
" Forks : [white]%-"+strconv.Itoa(10)+"s[green]\n",
strconv.FormatUint(promMetrics.Forks, 10),
))
// Row 2
sb.WriteString(fmt.Sprintf(
" Slot : [white]%-"+strconv.Itoa(10)+"s[green]",
strconv.FormatUint(promMetrics.SlotNum, 10),
))
if promMetrics.SlotNum == 0 {
sb.WriteString(fmt.Sprintf(
" Status : [white]%-"+strconv.Itoa(
10,
)+"s[green]",
"starting",
))
} else if tipDiff <= 20 {
sb.WriteString(fmt.Sprintf(
" Tip (diff) : [white]%-"+strconv.Itoa(9)+"s[green]",
fmt.Sprintf("%s 😀", strconv.FormatUint(tipDiff, 10)),
))
} else if tipDiff <= 600 {
sb.WriteString(fmt.Sprintf(
" Tip (diff) : [yellow]%-"+strconv.Itoa(9)+"s[green]",
fmt.Sprintf("%s 😐", strconv.FormatUint(tipDiff, 10)),
))
} else {
syncProgress := float32((float32(promMetrics.SlotNum) / float32(tipRef)) * 100)
sb.WriteString(fmt.Sprintf(
" Syncing : [yellow]%-"+strconv.Itoa(10)+"s[green]",
fmt.Sprintf("%2.1f", syncProgress),
))
}
sb.WriteString(fmt.Sprintf(
" Total Tx : [white]%-"+strconv.Itoa(10)+"s[green]\n",
strconv.FormatUint(promMetrics.TxProcessed, 10),
))
// Row 3
sb.WriteString(fmt.Sprintf(
" Slot epoch : [white]%-"+strconv.Itoa(10)+"s[green]",
strconv.FormatUint(promMetrics.SlotInEpoch, 10),
))
sb.WriteString(fmt.Sprintf(
" Density : [white]%-"+strconv.Itoa(10)+"s[green]",
fmt.Sprintf("%3.5f", promMetrics.Density*100/1),
))
sb.WriteString(fmt.Sprintf(
" Pending Tx : [white]%d[blue]/[white]%d[blue]%-"+kWidth+"s\n",
promMetrics.MempoolTx,
mempoolTxKBytes,
"K",
))
return fmt.Sprint(sb.String())
}
func getConnectionText(ctx context.Context) string {
cfg := config.GetConfig()
var sb strings.Builder
if p2p {
if promMetrics == nil {
return connectionText
}
sb.WriteString(fmt.Sprintf(" [green]P2P : %s\n",
"enabled",
))
sb.WriteString(fmt.Sprintf(" [green]Incoming : [white]%s\n",
strconv.FormatUint(promMetrics.ConnIncoming, 10),
))
sb.WriteString(fmt.Sprintf(" [green]Outgoing : [white]%s\n",
strconv.FormatUint(promMetrics.ConnOutgoing, 10),
))
sb.WriteString(fmt.Sprintf(" [green]Cold Peers : [white]%s\n",
strconv.FormatUint(promMetrics.PeersCold, 10),
))
sb.WriteString(fmt.Sprintf(" [green]Warm Peers : [white]%s\n",
strconv.FormatUint(promMetrics.PeersWarm, 10),
))
sb.WriteString(fmt.Sprintf(" [green]Hot Peers : [white]%s\n",
strconv.FormatUint(promMetrics.PeersHot, 10),
))
sb.WriteString(fmt.Sprintf(" [green]Uni-Dir : [white]%s\n",
strconv.FormatUint(promMetrics.ConnUniDir, 10),
))
sb.WriteString(fmt.Sprintf(" [green]Bi-Dir : [white]%s\n",
strconv.FormatUint(promMetrics.ConnBiDir, 10),
))
sb.WriteString(fmt.Sprintf(" [green]Duplex : [white]%s\n",
strconv.FormatUint(promMetrics.ConnDuplex, 10),
))
} else {
if processMetrics == nil {
return connectionText
}
// Get process in/out connections
connections, err := netutil.ConnectionsPidWithContext(ctx, "tcp", processMetrics.Pid)
if err != nil {
sb.WriteString(fmt.Sprintf("Failed to get processes: %v", err))
}
var peersIn []string
var peersOut []string
// Loops each connection, looking for ESTABLISHED
for _, c := range connections {
if c.Status == "ESTABLISHED" {
// If local port == node port, it's incoming
if c.Laddr.Port == cfg.Node.Port {
peersIn = append(peersIn, fmt.Sprintf("%s:%d", c.Raddr.IP, c.Raddr.Port))
}
// If local port != node port, ekg port, or prometheus port, it's outgoing
if c.Laddr.Port != cfg.Node.Port && c.Laddr.Port != uint32(12788) && c.Laddr.Port != cfg.Prometheus.Port {
peersOut = append(peersOut, fmt.Sprintf("%s:%d", c.Raddr.IP, c.Raddr.Port))
}
}
}
sb.WriteString(fmt.Sprintf(" [green]P2P : [yellow]%s\n",
"disabled",
))
sb.WriteString(fmt.Sprintf(" [green]Incoming : [white]%s\n",
strconv.Itoa(len(peersIn)),
))
sb.WriteString(fmt.Sprintf(" [green]Outgoing : [white]%s\n",
strconv.Itoa(len(peersOut)),
))
}
return fmt.Sprint(sb.String())
}
func getCoreText(ctx context.Context) string {
if promMetrics == nil {
return coreText
}
var sb strings.Builder
// Core section
if role == "Core" {
// TODO: block log functionality
var adoptedFmt string = "white"
var invalidFmt string = "white"
if promMetrics.IsLeader != promMetrics.Adopted {
adoptedFmt = "yellow"
}
if promMetrics.DidntAdopt != 0 {
invalidFmt = "red"
}
leader := strconv.FormatUint(promMetrics.IsLeader, 10)
sb.WriteString(fmt.Sprintf(" [green]Leader : [white]%s\n",
leader,
))
adopted := strconv.FormatUint(promMetrics.Adopted, 10)
sb.WriteString(fmt.Sprintf(" [green]Adopted : ["+adoptedFmt+"]%s\n",
adopted,
))
invalid := strconv.FormatUint(promMetrics.DidntAdopt, 10)
sb.WriteString(fmt.Sprintf(" [green]Invalid : ["+invalidFmt+"]%s\n",
invalid,
))
sb.WriteString(" [green]Missed : ")
var missedSlotsPct float32
if promMetrics.AboutToLead > 0 {
missedSlotsPct = float32(
promMetrics.MissedSlots,
) / (float32(promMetrics.AboutToLead + promMetrics.MissedSlots)) * 100
}
sb.WriteString(fmt.Sprintf("[white]%s [blue]([white]%s %%[blue])\n",
strconv.FormatUint(promMetrics.MissedSlots, 10),
fmt.Sprintf("%.2f", missedSlotsPct),
))
sb.WriteString("\n")
// KES
sb.WriteString(fmt.Sprintf(" [green]KES period : [white]%d\n",
promMetrics.KesPeriod,
))
sb.WriteString(fmt.Sprintf(" [green]KES remain : [white]%d\n",
promMetrics.RemainingKesPeriods,
))
} else {
sb.WriteString(fmt.Sprintf("%18s\n",
"N/A",
))
}
failCount = 0
return fmt.Sprint(sb.String())
}
func getBlockText(ctx context.Context) string {
if promMetrics == nil {
return blockText
}
// Style / UI
var width = 71
// Get our terminal size
tcols, tlines, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil {
failCount++
return fmt.Sprintf("ERROR: %v", err)
}
// Validate size
if width >= tcols {
footerTextView.Clear()
footerTextView.SetText(" [yellow](esc/q) Quit\n")
return fmt.Sprintf(
"\n [red]Terminal width too small![white]\n Please increase by [yellow]%d[white] columns\n",
width-tcols+1,
)
}
// TODO: populate lines
line := 10
if line >= (tlines - 1) {
footerTextView.Clear()
footerTextView.SetText(" [yellow](esc/q) Quit\n")
return fmt.Sprintf(
"\n [red]Terminal height too small![white]\n Please increase by [yellow]%d[white] lines\n",
line-tlines+2,
)
}
var sb strings.Builder
blk1s := fmt.Sprintf("%.2f", promMetrics.BlocksW1s*100)
blk3s := fmt.Sprintf("%.2f", promMetrics.BlocksW3s*100)
blk5s := fmt.Sprintf("%.2f", promMetrics.BlocksW5s*100)
delay := fmt.Sprintf("%.2f", promMetrics.BlockDelay)
// Row 1
sb.WriteString(
fmt.Sprintf(
" [green]Last Delay : [white]%s[blue]%-"+strconv.Itoa(
10-len(delay),
)+"s",
delay,
"s",
),
)
sb.WriteString(
fmt.Sprintf(" [green]Served : [white]%-"+strconv.Itoa(10)+"s",
strconv.FormatUint(promMetrics.BlocksServed, 10),
),
)
sb.WriteString(
fmt.Sprintf(" [green]Late (>5s) : [white]%-"+strconv.Itoa(10)+"s\n",
strconv.FormatUint(promMetrics.BlocksLate, 10),
),
)
// Row 2
sb.WriteString(
fmt.Sprintf(
" [green]Within 1s : [white]%s%-"+strconv.Itoa(10-len(blk1s))+"s",
blk1s,
"%",
),
)
sb.WriteString(
fmt.Sprintf(
" [green]Within 3s : [white]%s%-"+strconv.Itoa(10-len(blk3s))+"s",
blk3s,
"%",
),
)
sb.WriteString(
fmt.Sprintf(
" [green]Within 5s : [white]%s%-"+strconv.Itoa(
10-len(blk5s),
)+"s\n",
blk5s,
"%",
),
)
failCount = 0
return fmt.Sprint(sb.String())
}
func getNodeText(ctx context.Context) string {
cfg := config.GetConfig()
var network string
if cfg.App.Network != "" {
network = strings.ToUpper(cfg.App.Network[:1]) + cfg.App.Network[1:]
} else {
network = strings.ToUpper(cfg.Node.Network[:1]) + cfg.Node.Network[1:]
}
nodeVersion, nodeRevision, _ := getNodeVersion()
var sb strings.Builder
sb.WriteString(
fmt.Sprintf(" [green]Name : [white]%s\n", cfg.App.NodeName),
)
sb.WriteString(fmt.Sprintf(" [green]Role : [white]%s\n", role))
sb.WriteString(fmt.Sprintf(" [green]Network : [white]%s\n", network))
sb.WriteString(fmt.Sprintf(
" [green]Version : [white]%s\n",
fmt.Sprintf(
"[white]%s[blue] [[white]%s[blue]]",
nodeVersion,
nodeRevision,
),
))
if publicIP != nil {
sb.WriteString(
fmt.Sprintf(" [green]Public IP : [white]%s\n", publicIP),
)
} else {
sb.WriteString(fmt.Sprintln())
}
sb.WriteString(fmt.Sprintf(" [green]Uptime : [white]%s\n",
timeFromSeconds(uptimes),
))
return fmt.Sprint(sb.String())
}
func getPeerText(ctx context.Context) string {
if processMetrics == nil {
return peerText
}
var sb strings.Builder
// Style / UI
var width = 71
var charMarked string
var charUnmarked string
// TODO: legacy mode vs new
if false {
charMarked = string('#')
charUnmarked = string('.')
} else {
charMarked = string('▌')
charUnmarked = string('▖')
}
var granularity int = 68
granularitySmall := granularity / 2
if checkPeers {
peerCount := len(peersFiltered)
sb.WriteString(
fmt.Sprintf(" [yellow]%s [blue]%d[white]/[green]%d[white]\n",
"Peer analysis started... please wait!",
len(peerStats.RTTresultsSlice),
peerCount,
),
)
scrollPeers = false
return sb.String()
}
peerCount := len(peersFiltered)
sb.WriteString(" [green]RTT : Peers / Percent\n")
sb.WriteString(fmt.Sprintf(
" [green]0-50ms : [white]%5s %.f%%",
strconv.Itoa(peerStats.CNT1),
peerStats.PCT1,
))
sb.WriteString(fmt.Sprintf(
"%"+strconv.Itoa(10-len(fmt.Sprintf("%.f", peerStats.PCT1)))+"s",
" ",
))
for i := 0; i < granularitySmall; i++ {
if i < int(peerStats.PCT1) {
sb.WriteString(fmt.Sprintf("[green]%s", charMarked))
} else {
sb.WriteString(fmt.Sprintf("[white]%s", charUnmarked))
}
}
sb.WriteString("[white]\n") // closeRow
sb.WriteString(fmt.Sprintf(
" [green]50-100ms : [white]%5s %.f%%",
strconv.Itoa(peerStats.CNT2),
peerStats.PCT2,
))
sb.WriteString(fmt.Sprintf(
"%"+strconv.Itoa(10-len(fmt.Sprintf("%.f", peerStats.PCT2)))+"s",
"",
))
for i := 0; i < granularitySmall; i++ {
if i < int(peerStats.PCT2) {
sb.WriteString(fmt.Sprintf("[yellow]%s", charMarked))
} else {
sb.WriteString(fmt.Sprintf("[white]%s", charUnmarked))
}
}
sb.WriteString("[white]\n") // closeRow
sb.WriteString(fmt.Sprintf(
" [green]100-200ms : [white]%5s %.f%%",
strconv.Itoa(peerStats.CNT3),
peerStats.PCT3,
))
sb.WriteString(fmt.Sprintf(
"%"+strconv.Itoa(10-len(fmt.Sprintf("%.f", peerStats.PCT3)))+"s",
"",
))
for i := 0; i < granularitySmall; i++ {
if i < int(peerStats.PCT3) {
sb.WriteString(fmt.Sprintf("[red]%s", charMarked))
} else {
sb.WriteString(fmt.Sprintf("[white]%s", charUnmarked))
}
}
sb.WriteString("[white]\n") // closeRow
sb.WriteString(fmt.Sprintf(
" [green]200ms < : [white]%5s %.f%%",
strconv.Itoa(peerStats.CNT4),
peerStats.PCT4,
))
sb.WriteString(fmt.Sprintf(
"%"+strconv.Itoa(10-len(fmt.Sprintf("%.f", peerStats.PCT4)))+"s",
"",
))
for i := 0; i < granularitySmall; i++ {
if i < int(peerStats.PCT4) {
sb.WriteString(fmt.Sprintf("[fuchsia]%s", charMarked))
} else {
sb.WriteString(fmt.Sprintf("[white]%s", charUnmarked))
}
}
sb.WriteString("[white]\n") // closeRow
// Divider
sb.WriteString(fmt.Sprintf("%s\n", strings.Repeat("-", width-1)))