• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stillya / wg-relay / 17277209982

27 Aug 2025 07:52PM UTC coverage: 13.246% (+8.4%) from 4.874%
17277209982

Pull #3

github

stillya
added prom integration
Pull Request #3: Added metrics support

78 of 293 new or added lines in 11 files covered. (26.62%)

5 existing lines in 4 files now uncovered.

111 of 838 relevant lines covered (13.25%)

0.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.77
/pkg/monitor/tableprinter.go
1
package monitor
2

3
import (
4
        "fmt"
5
        "strings"
6
        "time"
7

8
        "github.com/stillya/wg-relay/pkg/maps/metricsmap"
9
)
10

11
type TablePrinter struct{}
12

13
func newTablePrinter() *TablePrinter {
1✔
14
        return &TablePrinter{}
1✔
15
}
1✔
16

17
func (tp *TablePrinter) PrintTrafficTable(metricsData []metricsmap.MetricData, elapsed time.Duration) {
1✔
18
        tp.clearScreen()
1✔
19

1✔
20
        var rxBytes, txBytes uint64
1✔
21
        for _, metric := range metricsData {
2✔
22
                if metric.Key.Dir == metricsmap.MetricFromWg && metric.Key.Reason == metricsmap.MetricForwarded {
2✔
23
                        rxBytes += metric.Value.Bytes
1✔
24
                } else if metric.Key.Dir == metricsmap.MetricToWg && metric.Key.Reason == metricsmap.MetricForwarded {
3✔
25
                        txBytes += metric.Value.Bytes
1✔
26
                }
1✔
27
        }
28

29
        totalBytes := rxBytes + txBytes
1✔
30
        avgRate := float64(totalBytes) / elapsed.Seconds()
1✔
31

1✔
32
        // Calculate estimated daily usage based on current rate
1✔
33
        secondsInDay := float64(24 * 60 * 60)
1✔
34
        estimatedRxDaily := uint64(float64(rxBytes) / elapsed.Seconds() * secondsInDay)
1✔
35
        estimatedTxDaily := uint64(float64(txBytes) / elapsed.Seconds() * secondsInDay)
1✔
36
        estimatedTotalDaily := estimatedRxDaily + estimatedTxDaily
1✔
37

1✔
38
        fmt.Printf("\n")
1✔
39
        fmt.Printf("                         wg-relay traffic statistics\n")
1✔
40
        fmt.Printf("\n")
1✔
41
        fmt.Printf(" %-18s | %12s | %12s | %12s | %12s\n", "", "from_wg", "to_wg", "total", "avg. rate")
1✔
42
        fmt.Printf(" %s+%s+%s+%s+%s\n",
1✔
43
                strings.Repeat("-", 18),
1✔
44
                strings.Repeat("-", 14),
1✔
45
                strings.Repeat("-", 14),
1✔
46
                strings.Repeat("-", 14),
1✔
47
                strings.Repeat("-", 14))
1✔
48
        fmt.Printf(" %-18s | %12s | %12s | %12s | %9s/s\n", "traffic",
1✔
49
                formatBytes(rxBytes),
1✔
50
                formatBytes(txBytes),
1✔
51
                formatBytes(totalBytes),
1✔
52
                formatBytes(uint64(avgRate)))
1✔
53
        fmt.Printf(" %s+%s+%s+%s+%s\n",
1✔
54
                strings.Repeat("-", 18),
1✔
55
                strings.Repeat("-", 14),
1✔
56
                strings.Repeat("-", 14),
1✔
57
                strings.Repeat("-", 14),
1✔
58
                strings.Repeat("-", 14))
1✔
59
        fmt.Printf(" %-18s | %12s | %12s | %12s |\n", "estimated",
1✔
60
                formatBytes(estimatedRxDaily),
1✔
61
                formatBytes(estimatedTxDaily),
1✔
62
                formatBytes(estimatedTotalDaily))
1✔
63
        fmt.Printf("\n")
1✔
64
}
65

66
func (tp *TablePrinter) clearScreen() {
1✔
67
        fmt.Print("\033c")
1✔
68
}
1✔
69

70
func formatBytes(bytes uint64) string {
1✔
71
        const unit = 1024
1✔
72
        if bytes < unit {
1✔
NEW
73
                return fmt.Sprintf("%d B", bytes)
×
NEW
74
        }
×
75
        div, exp := uint64(unit), 0
1✔
76
        for n := bytes / unit; n >= unit; n /= unit {
2✔
77
                div *= unit
1✔
78
                exp++
1✔
79
        }
1✔
80
        return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
1✔
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc