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

Method ListUserAnalysis

internal/store/postgres/accesslogs.go:133–181  ·  view source on GitHub ↗
(since, until time.Time)

Source from the content-addressed store, hash-verified

131}
132
133func (s *AccessLogStore) ListUserAnalysis(since, until time.Time) ([]accesslogs.UserAnalysis, error) {
134 ctx := context.Background()
135 // access_logs 按用户聚合连接数和独立 IP,JOIN user_node_daily_usage 汇总流量
136 const query = `
137WITH access_stats AS (
138 SELECT
139 SPLIT_PART(username, '@', 1) AS username,
140 COUNT(*) AS connections,
141 COUNT(DISTINCT source_ip) AS distinct_ips,
142 MAX(created_at) AS last_seen
143 FROM access_logs
144 WHERE created_at >= $1 AND created_at <= $2
145 GROUP BY 1
146),
147traffic_stats AS (
148 SELECT
149 u.username,
150 COALESCE(SUM(unu.upload_bytes + unu.download_bytes), 0) AS total_bytes
151 FROM user_node_daily_usage unu
152 JOIN users u ON u.id = unu.user_id
153 WHERE unu.date >= TO_CHAR($1::timestamptz AT TIME ZONE 'UTC', 'YYYY-MM-DD')
154 AND unu.date <= TO_CHAR($2::timestamptz AT TIME ZONE 'UTC', 'YYYY-MM-DD')
155 GROUP BY u.username
156)
157SELECT
158 a.username,
159 a.connections,
160 a.distinct_ips,
161 COALESCE(t.total_bytes, 0) AS total_bytes,
162 a.last_seen
163FROM access_stats a
164LEFT JOIN traffic_stats t ON t.username = a.username
165ORDER BY a.distinct_ips DESC, total_bytes DESC`
166
167 rows, err := s.db.Query(ctx, query, since, until)
168 if err != nil {
169 return nil, fmt.Errorf("list user analysis: %w", err)
170 }
171 defer rows.Close()
172 var out []accesslogs.UserAnalysis
173 for rows.Next() {
174 var a accesslogs.UserAnalysis
175 if err := rows.Scan(&a.Username, &a.Connections, &a.DistinctIPs, &a.TotalBytes, &a.LastSeen); err != nil {
176 return nil, fmt.Errorf("scan user analysis: %w", err)
177 }
178 out = append(out, a)
179 }
180 return out, rows.Err()
181}
182
183func (s *AccessLogStore) Count() (int64, error) {
184 ctx := context.Background()

Callers

nothing calls this directly

Calls 4

NextMethod · 0.80
QueryMethod · 0.65
CloseMethod · 0.65
ErrMethod · 0.65

Tested by

no test coverage detected