From f59ce777728cf84ccdf711491772104a0fc0dd6e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Wed, 18 Jan 2023 23:52:04 +0800 Subject: Display unreferenced packages total size in package admin panel (#22498) --- models/packages/package_blob.go | 13 +++++++++++-- models/packages/package_file.go | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'models/packages') diff --git a/models/packages/package_blob.go b/models/packages/package_blob.go index 3b4a1ecf18..a55109af96 100644 --- a/models/packages/package_blob.go +++ b/models/packages/package_blob.go @@ -85,7 +85,16 @@ func DeleteBlobByID(ctx context.Context, blobID int64) error { } // GetTotalBlobSize returns the total blobs size in bytes -func GetTotalBlobSize() (int64, error) { - return db.GetEngine(db.DefaultContext). +func GetTotalBlobSize(ctx context.Context) (int64, error) { + return db.GetEngine(ctx). + SumInt(&PackageBlob{}, "size") +} + +// GetTotalUnreferencedBlobSize returns the total size of all unreferenced blobs in bytes +func GetTotalUnreferencedBlobSize(ctx context.Context) (int64, error) { + return db.GetEngine(ctx). + Table("package_blob"). + Join("LEFT", "package_file", "package_file.blob_id = package_blob.id"). + Where("package_file.id IS NULL"). SumInt(&PackageBlob{}, "size") } diff --git a/models/packages/package_file.go b/models/packages/package_file.go index 7f794836dc..97e7a0d407 100644 --- a/models/packages/package_file.go +++ b/models/packages/package_file.go @@ -199,9 +199,9 @@ func SearchFiles(ctx context.Context, opts *PackageFileSearchOptions) ([]*Packag return pfs, count, err } -// CalculateBlobSize sums up all blob sizes matching the search options. +// CalculateFileSize sums up all blob sizes matching the search options. // It does NOT respect the deduplication of blobs. -func CalculateBlobSize(ctx context.Context, opts *PackageFileSearchOptions) (int64, error) { +func CalculateFileSize(ctx context.Context, opts *PackageFileSearchOptions) (int64, error) { return db.GetEngine(ctx). Table("package_file"). Where(opts.toConds()). -- cgit v1.2.3