This commit is contained in:
ryan
2026-06-19 15:13:24 +08:00
parent 0b34792709
commit 32861c5db9
376 changed files with 3648 additions and 19957 deletions
@@ -95,10 +95,10 @@ func CountNodeAccessLogs(ctx context.Context, filter NodeAccessLogFilter) (int64
ipSQL := fmt.Sprintf(`
SELECT count() FROM (
SELECT trim(remote_addr) AS remote_addr
SELECT trim(remote_addr) AS trimmed_remote_addr
FROM %s
WHERE %s AND remote_addr != ''
GROUP BY trim(remote_addr)
WHERE %s AND trim(remote_addr) != ''
GROUP BY trimmed_remote_addr
)`, tableName, clause)
var totalIPs int64
if err := conn.QueryRow(ctx, ipSQL, args...).Scan(&totalIPs); err != nil {
@@ -117,11 +117,11 @@ func RegionCountsNodeAccessLogs(ctx context.Context, nodeID string, since time.T
clause, args := buildNodeAccessLogFilterClause(filter)
tableName := nodeAccessLogTableName()
sql := fmt.Sprintf(`
SELECT trim(region) AS region, count() AS count
SELECT trim(region) AS trimmed_region, count() AS count
FROM %s
WHERE %s AND trim(region) != ''
GROUP BY trim(region)
ORDER BY count DESC, region ASC`, tableName, clause)
GROUP BY trimmed_region
ORDER BY count DESC, trimmed_region ASC`, tableName, clause)
if limit > 0 {
sql += " LIMIT ?"
args = append(args, limit)
@@ -141,4 +141,4 @@ ORDER BY count DESC, region ASC`, tableName, clause)
result = append(result, item)
}
return result, nil
}
}
@@ -9,6 +9,8 @@ import (
"time"
)
const nodeAccessLogFilterClauseCapacity = 6
// NodeAccessLogFilter scopes ClickHouse node access log queries.
type NodeAccessLogFilter struct {
NodeID string
@@ -24,8 +26,8 @@ type NodeAccessLogFilter struct {
}
func buildNodeAccessLogFilterClause(filter NodeAccessLogFilter) (string, []any) {
parts := make([]string, 0, 6)
args := make([]any, 0, 6)
parts := make([]string, 0, nodeAccessLogFilterClauseCapacity)
args := make([]any, 0, nodeAccessLogFilterClauseCapacity)
if trimmed := strings.TrimSpace(filter.NodeID); trimmed != "" {
parts = append(parts, "node_id = ?")
args = append(args, trimmed)
@@ -102,4 +104,4 @@ func nodeAccessLogEpochExpr() string {
func nodeAccessLogTableName() string {
return "of_node_access_logs"
}
}
@@ -139,7 +139,7 @@ func IPAggregatesNodeAccessLogs(ctx context.Context, filter NodeAccessLogFilter,
tableName := nodeAccessLogTableName()
sql := fmt.Sprintf(`
SELECT
trim(remote_addr) AS remote_addr,
trim(remote_addr) AS trimmed_remote_addr,
count() AS request_count,
countIf(status_code < 400) AS success_count,
countIf(status_code >= 400 AND status_code < 500) AS client_error_count,
@@ -147,7 +147,7 @@ SELECT
max(%s) AS last_seen_epoch
FROM %s
WHERE %s AND trim(remote_addr) != ''
GROUP BY trim(remote_addr)`, lastSeenExpr, tableName, queryClause)
GROUP BY trimmed_remote_addr`, lastSeenExpr, tableName, queryClause)
rows, err := conn.Query(ctx, sql, queryArgs...)
if err != nil {
return nil, fmt.Errorf("ip aggregates node access logs: %w", err)
@@ -183,13 +183,13 @@ func IPSummariesNodeAccessLogs(ctx context.Context, filter NodeAccessLogFilter,
tableName := nodeAccessLogTableName()
sql := fmt.Sprintf(`
SELECT
trim(remote_addr) AS remote_addr,
trim(remote_addr) AS trimmed_remote_addr,
count() AS total_requests,
sum(%s) AS recent_requests,
max(%s) AS last_seen_epoch
FROM %s
WHERE %s AND trim(remote_addr) != ''
GROUP BY trim(remote_addr)`, recentClause, lastSeenExpr, tableName, clause)
GROUP BY trimmed_remote_addr`, recentClause, lastSeenExpr, tableName, clause)
rows, err := conn.Query(ctx, sql, queryArgs...)
if err != nil {
return nil, fmt.Errorf("ip summaries node access logs: %w", err)
@@ -239,4 +239,4 @@ ORDER BY bucket_epoch ASC`, bucketExpr, tableName, clause)
result = append(result, item)
}
return result, nil
}
}