mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 14:06:36 +08:00
63cd906cfc
- Merge all files inside openflare-server to the repository root directory. - Relocate agent, relay, and flared subprojects from internal/ to internal/apps/. - Combine docker-compose files and update build context paths to root. - Update GitHub workflows and Dockerfiles to refer to new directories and package names. - Rewrite Go package imports across all files. - Resolve database renew test race condition and clean up docs.
73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package task
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
uploadstats "github.com/Rain-kl/Wavelet/internal/apps/upload/stats"
|
|
"github.com/Rain-kl/Wavelet/internal/db"
|
|
"github.com/Rain-kl/Wavelet/internal/model"
|
|
"github.com/Rain-kl/Wavelet/internal/task"
|
|
)
|
|
|
|
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 = task.TaskMeta{
|
|
Type: TaskTypeRebuildUploadStats,
|
|
AsynqTask: RebuildUploadStatsTask,
|
|
Name: "重算文件存储统计",
|
|
Description: "根据当前 w_uploads 活跃记录全量重建 w_upload_stats(总量、类型、分类、趋势)",
|
|
SupportsTime: false,
|
|
MaxRetry: task.DefaultMaxRetry,
|
|
Queue: task.QueueDefault,
|
|
Retryable: true,
|
|
}
|
|
|
|
// 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) (*task.TaskResult, error) {
|
|
var activeCount int64
|
|
if err := db.DB(ctx).
|
|
Model(&model.Upload{}).
|
|
Where("status != ?", model.UploadStatusDeleted).
|
|
Count(&activeCount).Error; err != nil {
|
|
task.AppendLog(ctx, "统计活跃上传记录失败: %v", err)
|
|
return nil, fmt.Errorf("count active uploads: %w", err)
|
|
}
|
|
|
|
task.AppendLog(ctx, "开始重算文件存储统计,活跃记录数: %d", activeCount)
|
|
|
|
if err := uploadstats.RebuildUploadStats(ctx); err != nil {
|
|
task.AppendLog(ctx, "重算文件存储统计失败: %v", err)
|
|
return nil, fmt.Errorf("rebuild upload stats: %w", err)
|
|
}
|
|
|
|
var totalStat model.UploadStat
|
|
if err := db.DB(ctx).
|
|
Where("dimension = ? AND stat_key = ?", model.UploadStatDimensionTotal, "").
|
|
First(&totalStat).Error; err != nil {
|
|
task.AppendLog(ctx, "读取总量统计失败: %v", err)
|
|
return nil, fmt.Errorf("load total upload stats: %w", err)
|
|
}
|
|
|
|
msg := fmt.Sprintf(
|
|
"文件存储统计重算完成,活跃记录 %d 条,统计文件数 %d,总大小 %d 字节",
|
|
activeCount,
|
|
totalStat.FileCount,
|
|
totalStat.FileSize,
|
|
)
|
|
task.AppendLog(ctx, "%s", msg)
|
|
return &task.TaskResult{Message: msg}, nil
|
|
}
|