mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-03 07:06:36 +08:00
528240026d
- make format 现在与 code-check 使用同一格式化器(golangci-lint fmt),消除 goimports -local 与 gofumpt 的格式拉锯 - .golangci.yml 关闭默认 50/3 截断,完整上报所有问题(只增强不弱化) - 全库 gofumpt 规范化(203 files, 纯格式无行为变更)
77 lines
2.4 KiB
Go
77 lines
2.4 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package task
|
|
|
|
import (
|
|
"Wavelet/core/contracts"
|
|
"Wavelet/pkg/logger"
|
|
"Wavelet/plugins/domain/upload/models"
|
|
"Wavelet/plugins/domain/upload/shared"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
uploadstats "Wavelet/plugins/domain/upload/stats"
|
|
)
|
|
|
|
const (
|
|
// RebuildUploadStatsTask is the Asynq task name for rebuilding upload stats.
|
|
RebuildUploadStatsTask = "upload:rebuild_stats"
|
|
// TaskTypeRebuildUploadStats is the admin-dispatchable task type.
|
|
TaskTypeRebuildUploadStats = "rebuild_upload_stats"
|
|
)
|
|
|
|
// RebuildUploadStatsMeta describes the upload stats rebuild task.
|
|
var RebuildUploadStatsMeta = contracts.TaskMetaDTO{
|
|
Name: RebuildUploadStatsTask,
|
|
DisplayName: "重算文件存储统计",
|
|
Description: "根据当前 w_uploads 活跃记录全量重建 w_upload_stats(总量、类型、分类、趋势)",
|
|
Category: "upload",
|
|
MaxRetry: 3,
|
|
Queue: "default",
|
|
}
|
|
|
|
// RebuildUploadStatsHandler rebuilds incremental upload stats from active upload records.
|
|
type RebuildUploadStatsHandler struct{}
|
|
|
|
// Execute scans active uploads and rebuilds all upload stat dimensions.
|
|
func (h *RebuildUploadStatsHandler) Execute(ctx context.Context, _ []byte) (*contracts.TaskResultDTO, error) {
|
|
db := shared.GetDB(ctx)
|
|
if db == nil {
|
|
return nil, errors.New("database service not available")
|
|
}
|
|
|
|
var activeCount int64
|
|
if err := db.
|
|
Model(&models.Upload{}).
|
|
Where("status != ?", models.UploadStatusDeleted).
|
|
Count(&activeCount).Error; err != nil {
|
|
logger.ErrorF(ctx, "统计活跃上传记录失败: %v", err)
|
|
return nil, fmt.Errorf("count active uploads: %w", err)
|
|
}
|
|
|
|
logger.InfoF(ctx, "开始重算文件存储统计,活跃记录数: %d", activeCount)
|
|
|
|
if err := uploadstats.RebuildUploadStats(ctx); err != nil {
|
|
logger.ErrorF(ctx, "重算文件存储统计失败: %v", err)
|
|
return nil, fmt.Errorf("rebuild upload stats: %w", err)
|
|
}
|
|
|
|
var totalStat models.UploadStat
|
|
if err := db.
|
|
Where("dimension = ? AND stat_key = ?", models.UploadStatDimensionTotal, "").
|
|
First(&totalStat).Error; err != nil {
|
|
logger.ErrorF(ctx, "读取总量统计失败: %v", err)
|
|
return nil, fmt.Errorf("read total upload stat: %w", err)
|
|
}
|
|
|
|
msg := fmt.Sprintf(
|
|
"文件存储统计重算完成,活跃文件: %d 个,总大小: %d 字节",
|
|
totalStat.FileCount,
|
|
totalStat.FileSize,
|
|
)
|
|
logger.InfoF(ctx, "%s", msg)
|
|
return &contracts.TaskResultDTO{Message: msg}, nil
|
|
}
|