fix(仪表盘): 增加对数据库驱动和仓储实例的有效性检查

This commit is contained in:
yangjianbo
2026-01-12 10:53:41 +08:00
parent 839ab37d40
commit 1073317a3e
2 changed files with 19 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"log"
"strings"
"time"
@@ -17,6 +18,13 @@ type dashboardAggregationRepository struct {
// NewDashboardAggregationRepository 创建仪表盘预聚合仓储。
func NewDashboardAggregationRepository(sqlDB *sql.DB) service.DashboardAggregationRepository {
if sqlDB == nil {
return nil
}
if !isPostgresDriver(sqlDB) {
log.Printf("[DashboardAggregation] 检测到非 PostgreSQL 驱动,已自动禁用预聚合")
return nil
}
return newDashboardAggregationRepositoryWithSQL(sqlDB)
}
@@ -24,6 +32,14 @@ func newDashboardAggregationRepositoryWithSQL(sqlq sqlExecutor) *dashboardAggreg
return &dashboardAggregationRepository{sql: sqlq}
}
func isPostgresDriver(db *sql.DB) bool {
if db == nil {
return false
}
_, ok := db.Driver().(*pq.Driver)
return ok
}
func (r *dashboardAggregationRepository) AggregateRange(ctx context.Context, start, end time.Time) error {
startUTC := start.UTC()
endUTC := end.UTC()

View File

@@ -85,6 +85,9 @@ func NewDashboardService(usageRepo UsageLogRepository, aggRepo DashboardAggregat
aggUsageDays = cfg.DashboardAgg.Retention.UsageLogsDays
}
}
if aggRepo == nil {
aggEnabled = false
}
return &DashboardService{
usageRepo: usageRepo,
aggRepo: aggRepo,