Update aggregate API to use binary megabytes and enhance documentation
Publish telemt-api gateway Docker image / test (push) Successful in 27s
Publish telemt-api gateway Docker image / build-and-push (push) Successful in 1m26s

- Changed API responses and internal calculations to use binary megabytes (MiB) instead of octets for traffic metrics.
- Updated relevant endpoints in AGGREGATE.md to reflect the new metric units.
- Modified handler and merge logic to accommodate the new data structure and ensure accurate traffic reporting.
- Enhanced tests to validate the changes in traffic calculations and summary data.
- Deprecated the use of total_octets in favor of total_megabytes for consistency across the API.
This commit is contained in:
Denozordec
2026-03-30 01:04:00 +07:00
parent fb35107b82
commit 4a4f15bd0b
7 changed files with 89 additions and 35 deletions
+28 -5
View File
@@ -18,7 +18,7 @@ func BuildTraffic(results []ServerFetchResult) []TrafficRow {
perUser[u.Username] = map[string]TrafficServerStats{}
}
perUser[u.Username][fr.Alias] = TrafficServerStats{
TotalOctets: u.TotalOctets,
TotalMegabytes: octetsToMegabytes(u.TotalOctets),
CurrentConnections: u.CurrentConnections,
Revision: fr.Revision,
}
@@ -162,7 +162,7 @@ func BuildUsers(results []ServerFetchResult, includeLinks bool, minTotalOctets u
m[u.Username] = a
}
a.byServer[fr.Alias] = TrafficServerStats{
TotalOctets: u.TotalOctets,
TotalMegabytes: octetsToMegabytes(u.TotalOctets),
CurrentConnections: u.CurrentConnections,
Revision: fr.Revision,
}
@@ -192,7 +192,7 @@ func BuildUsers(results []ServerFetchResult, includeLinks bool, minTotalOctets u
a := m[name]
rows = append(rows, UsersRow{
Username: name,
TotalOctets: a.total,
TotalMegabytes: octetsToMegabytes(a.total),
ByServer: a.byServer,
Links: a.links,
ActiveUniqueIPs: a.act,
@@ -212,6 +212,7 @@ func BuildSummary(results []ServerFetchResult, topN int) SummaryData {
}
sumByUser := map[string]uint64{}
maxUniqueIPByUser := map[string]uint64{}
var fleetOctets, fleetConn uint64
ok, fail := 0, 0
serverRows := make([]ServerFetchResult, 0, len(results))
@@ -235,6 +236,9 @@ func BuildSummary(results []ServerFetchResult, topN int) SummaryData {
fleetOctets += u.TotalOctets
fleetConn += u.CurrentConnections
sumByUser[u.Username] += u.TotalOctets
if u.ActiveUniqueIPs > maxUniqueIPByUser[u.Username] {
maxUniqueIPByUser[u.Username] = u.ActiveUniqueIPs
}
}
}
@@ -257,7 +261,25 @@ func BuildSummary(results []ServerFetchResult, topN int) SummaryData {
}
top := make([]TopUser, 0, len(pairs))
for _, p := range pairs {
top = append(top, TopUser{Username: p.name, TotalOctets: p.n})
top = append(top, TopUser{Username: p.name, TotalMegabytes: octetsToMegabytes(p.n)})
}
ipPairs := make([]pair, 0, len(maxUniqueIPByUser))
for n, v := range maxUniqueIPByUser {
ipPairs = append(ipPairs, pair{name: n, n: v})
}
sort.Slice(ipPairs, func(i, j int) bool {
if ipPairs[i].n != ipPairs[j].n {
return ipPairs[i].n > ipPairs[j].n
}
return ipPairs[i].name < ipPairs[j].name
})
if len(ipPairs) > topN {
ipPairs = ipPairs[:topN]
}
topIP := make([]TopUserByUniqueIPs, 0, len(ipPairs))
for _, p := range ipPairs {
topIP = append(topIP, TopUserByUniqueIPs{Username: p.name, UniqueIPs: p.n})
}
return SummaryData{
@@ -265,8 +287,9 @@ func BuildSummary(results []ServerFetchResult, topN int) SummaryData {
ServersTotal: len(results),
ServersOK: ok,
ServersFailed: fail,
FleetTotalOctets: fleetOctets,
FleetTotalMegabytes: octetsToMegabytes(fleetOctets),
FleetTotalConnections: fleetConn,
TopUsers: top,
TopUsersByUniqueIPs: topIP,
}
}