MCPcopy Create free account
hub / github.com/0xUnixIO/pulse / Build

Function Build

internal/usage/summary.go:134–303  ·  view source on GitHub ↗

Build 构建仪表盘统计摘要。

(nodeStore nodes.Store, userStore users.Store, days int)

Source from the content-addressed store, hash-verified

132
133// Build 构建仪表盘统计摘要。
134func Build(nodeStore nodes.Store, userStore users.Store, days int) (Summary, error) {
135 allNodes, err := nodeStore.List()
136 if err != nil {
137 return Summary{}, err
138 }
139 // 统计摘要只包含启用的节点
140 nodesList := make([]nodes.Node, 0, len(allNodes))
141 for _, n := range allNodes {
142 if !n.Disabled {
143 nodesList = append(nodesList, n)
144 }
145 }
146
147 usersList, err := userStore.ListUsers()
148 if err != nil {
149 return Summary{}, err
150 }
151
152 nodeStats := make([]NodeStat, 0, len(nodesList))
153 for _, n := range nodesList {
154 nodeStats = append(nodeStats, NodeStat{
155 ID: n.ID,
156 Name: n.Name,
157 UploadBytes: n.UploadBytes,
158 DownloadBytes: n.DownloadBytes,
159 })
160 }
161
162 if days <= 0 {
163 days = 14
164 }
165
166 s := Summary{
167 NodesCount: len(nodesList),
168 NodeStats: nodeStats,
169 UsersCount: len(usersList),
170 Days: days,
171 DaysOptions: []int{7, 14, 30},
172 }
173
174 // 流量总览从节点累计值汇总,不受用户流量重置影响
175 for _, n := range nodesList {
176 s.TotalUploadBytes += n.UploadBytes
177 s.TotalDownloadBytes += n.DownloadBytes
178 }
179 s.TotalUsedBytes = s.TotalUploadBytes + s.TotalDownloadBytes
180
181 now := time.Now()
182 expiringDeadline := now.Add(7 * 24 * time.Hour)
183 for _, u := range usersList {
184 if u.OnlineAt != nil && now.Sub(*u.OnlineAt) <= OnlineThreshold {
185 s.OnlineUsersCount++
186 }
187 s.TotalConnections += u.Connections
188 s.TotalDevices += u.Devices
189 s.TotalBilledUploadBytes += u.UploadBytes
190 s.TotalBilledDownloadBytes += u.DownloadBytes
191 switch u.EffectiveStatus() {

Callers 2

RunFunction · 0.92
TestBuildFunction · 0.70

Calls 8

aggregateDailyTrafficFunction · 0.85
aggregateNodePeriodStatsFunction · 0.85
AddMethod · 0.80
EffectiveStatusMethod · 0.80
ListMethod · 0.65
ListUsersMethod · 0.65
ListNodeDailyUsageMethod · 0.65
ListTodayUserStatsMethod · 0.65

Tested by 1

TestBuildFunction · 0.56