mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 05:56:38 +08:00
7d71f1e4e1
- New internal/repository/logstore abstraction: exported domain interfaces (AccessLogStore/ObservabilityStore/UserAccessLogStore/StatusStore), config-driven provider (Active/Build/Migrating/SetConfigReader), GORM implementation for PostgreSQL/SQLite (incl. hourly rollups computed in real time, migration listers, PG partition maintenance), and a ClickHouse wrapper preserving the native batch path; repository facade delegates to logstore; import-lint test enforces apps never import analyticsrepo. - ClickHouse is now optional: the log DB is either the main DB (postgres when database.enabled, else sqlite) or clickhouse; boot validation + first-run seed; log_database / log_db_migration are protected keys. - New user task 切换日志数据库 (of_log_db_switch): freeze log writes, drain batch writers, copy all 6 raw log tables by id (preserving IDs) with target-partition pre-creation for PG, flip log_database on success, clear the freeze flag on failure. - Per-store retention (log_retention_days_*) with expiry cleanup folded into the daily system_cleanup task; legacy database_auto_cleanup_* and of_database_auto_cleanup decommissioned. - goose migrations: 6 log tables in PG (2 monthly-partitioned) + SQLite, retention config seeds, schedule cleanup; GET /api/v1/admin/status/log-database endpoint; frontend retention settings, switch-task UI and status badge; changelog and docs updated. docs(plan): log database decoupling implementation plan docs(design): log database decoupling design (ClickHouse optional)
66 lines
2.1 KiB
Go
66 lines
2.1 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package analytics
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/Rain-kl/Wavelet/internal/infra/config"
|
|
db "github.com/Rain-kl/Wavelet/internal/infra/persistence"
|
|
analyticsmodel "github.com/Rain-kl/Wavelet/internal/model/analytics"
|
|
)
|
|
|
|
// ClickHouseOperationalStats summarizes ClickHouse merge/mutation pressure
|
|
// and in-process batch writer queue health.
|
|
type ClickHouseOperationalStats = analyticsmodel.ClickHouseOperationalStats
|
|
|
|
// GetClickHouseOperationalStats returns operational metrics for the configured database.
|
|
func GetClickHouseOperationalStats(ctx context.Context) (*ClickHouseOperationalStats, error) {
|
|
if db.ChConn == nil {
|
|
return nil, fmt.Errorf("clickhouse native connection is not initialized")
|
|
}
|
|
database := config.Config.ClickHouse.Database
|
|
stats := &ClickHouseOperationalStats{Database: database}
|
|
|
|
partsSQL := `
|
|
SELECT
|
|
count() AS active_parts,
|
|
ifNull(sum(rows), 0) AS total_rows
|
|
FROM system.parts
|
|
WHERE active AND database = ?`
|
|
var activeParts, totalRows uint64
|
|
if err := db.ChConn.QueryRow(ctx, partsSQL, database).Scan(&activeParts, &totalRows); err != nil {
|
|
return nil, fmt.Errorf("query system.parts: %w", err)
|
|
}
|
|
stats.ActiveParts = safeInt64Count(activeParts)
|
|
stats.TotalRows = safeInt64Count(totalRows)
|
|
|
|
mutationsSQL := `
|
|
SELECT count()
|
|
FROM system.mutations
|
|
WHERE is_done = 0 AND database = ?`
|
|
if err := db.ChConn.QueryRow(ctx, mutationsSQL, database).Scan(&stats.PendingMutations); err != nil {
|
|
return nil, fmt.Errorf("query system.mutations: %w", err)
|
|
}
|
|
|
|
asyncSQL := `
|
|
SELECT
|
|
count() AS queue_entries,
|
|
ifNull(sum(bytes), 0) AS queue_bytes
|
|
FROM system.asynchronous_inserts
|
|
WHERE database = ?`
|
|
var queueEntries, queueBytes uint64
|
|
if err := db.ChConn.QueryRow(ctx, asyncSQL, database).Scan(&queueEntries, &queueBytes); err != nil {
|
|
// Older ClickHouse versions may not expose asynchronous_inserts; treat as optional.
|
|
stats.AsyncInsertQueue = 0
|
|
stats.AsyncInsertBytes = 0
|
|
} else {
|
|
stats.AsyncInsertQueue = safeInt64Count(queueEntries)
|
|
stats.AsyncInsertBytes = safeInt64Count(queueBytes)
|
|
}
|
|
|
|
return stats, nil
|
|
}
|