mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 14:06:36 +08:00
58624db397
- Replace retention ALTER DELETE with MATERIALIZE TTL; use TRUNCATE for delete-all - Remove GORM ClickHouse pool; migrate user access log reads to ChConn - Drop query-side trim(remote_addr); enable wait_for_async_insert=1 - Add of_node_traffic_hourly MV and dashboard traffic trend fallback - Add GET /admin/status/clickhouse operational metrics endpoint
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package analytics
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// DeleteAllNodeAccessLogs deletes all node access logs.
|
|
func DeleteAllNodeAccessLogs(ctx context.Context) (int64, error) {
|
|
conn, err := nodeAccessLogConn()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
outcome, err := truncateClickHouseTable(ctx, conn, nodeAccessLogTableName())
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return outcome.EligibleCount, nil
|
|
}
|
|
|
|
// DeleteNodeAccessLogsBefore expires logs older than cutoff via table TTL.
|
|
func DeleteNodeAccessLogsBefore(ctx context.Context, cutoff time.Time) (int64, error) {
|
|
conn, err := nodeAccessLogConn()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
tableName := nodeAccessLogTableName()
|
|
cutoff = cutoff.UTC()
|
|
outcome, err := expireRowsViaTTL(
|
|
ctx,
|
|
conn,
|
|
tableName,
|
|
fmt.Sprintf("SELECT count() FROM %s WHERE logged_at < ?", tableName),
|
|
[]any{cutoff},
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return outcome.EligibleCount, nil
|
|
}
|
|
|
|
// DeleteNodeAccessLogsByNodeBefore expires logs for a node older than cutoff via table TTL.
|
|
func DeleteNodeAccessLogsByNodeBefore(ctx context.Context, nodeID string, before time.Time) (int64, error) {
|
|
conn, err := nodeAccessLogConn()
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
tableName := nodeAccessLogTableName()
|
|
before = before.UTC()
|
|
outcome, err := expireRowsViaTTL(
|
|
ctx,
|
|
conn,
|
|
tableName,
|
|
fmt.Sprintf("SELECT count() FROM %s WHERE node_id = ? AND logged_at < ?", tableName),
|
|
[]any{nodeID, before},
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return outcome.EligibleCount, nil
|
|
} |