Display unreferenced packages total size in package admin panel (#22498)

This commit is contained in:
Lunny Xiao 2023-01-18 23:52:04 +08:00 committed by GitHub
parent 7ddc11def7
commit f59ce77772
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 29 additions and 10 deletions

View file

@ -85,7 +85,16 @@ func DeleteBlobByID(ctx context.Context, blobID int64) error {
} }
// GetTotalBlobSize returns the total blobs size in bytes // GetTotalBlobSize returns the total blobs size in bytes
func GetTotalBlobSize() (int64, error) { func GetTotalBlobSize(ctx context.Context) (int64, error) {
return db.GetEngine(db.DefaultContext). 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") SumInt(&PackageBlob{}, "size")
} }

View file

@ -199,9 +199,9 @@ func SearchFiles(ctx context.Context, opts *PackageFileSearchOptions) ([]*Packag
return pfs, count, err 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. // 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). return db.GetEngine(ctx).
Table("package_file"). Table("package_file").
Where(opts.toConds()). Where(opts.toConds()).

View file

@ -2645,6 +2645,7 @@ repos.size = Size
packages.package_manage_panel = Package Management packages.package_manage_panel = Package Management
packages.total_size = Total Size: %s packages.total_size = Total Size: %s
packages.unreferenced_size = Unreferenced Size: %s
packages.owner = Owner packages.owner = Owner
packages.creator = Creator packages.creator = Creator
packages.name = Name packages.name = Name

View file

@ -51,12 +51,18 @@ func Packages(ctx *context.Context) {
return return
} }
totalBlobSize, err := packages_model.GetTotalBlobSize() totalBlobSize, err := packages_model.GetTotalBlobSize(ctx)
if err != nil { if err != nil {
ctx.ServerError("GetTotalBlobSize", err) ctx.ServerError("GetTotalBlobSize", err)
return return
} }
totalUnreferencedBlobSize, err := packages_model.GetTotalUnreferencedBlobSize(ctx)
if err != nil {
ctx.ServerError("CalculateBlobSize", err)
return
}
ctx.Data["Title"] = ctx.Tr("packages.title") ctx.Data["Title"] = ctx.Tr("packages.title")
ctx.Data["PageIsAdmin"] = true ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminPackages"] = true ctx.Data["PageIsAdminPackages"] = true
@ -65,8 +71,9 @@ func Packages(ctx *context.Context) {
ctx.Data["AvailableTypes"] = packages_model.TypeList ctx.Data["AvailableTypes"] = packages_model.TypeList
ctx.Data["SortType"] = sort ctx.Data["SortType"] = sort
ctx.Data["PackageDescriptors"] = pds ctx.Data["PackageDescriptors"] = pds
ctx.Data["Total"] = total ctx.Data["TotalCount"] = total
ctx.Data["TotalBlobSize"] = totalBlobSize ctx.Data["TotalBlobSize"] = totalBlobSize - totalUnreferencedBlobSize
ctx.Data["TotalUnreferencedBlobSize"] = totalUnreferencedBlobSize
pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5) pager := context.NewPagination(int(total), setting.UI.PackagesPagingNum, page, 5)
pager.AddParamString("q", query) pager.AddParamString("q", query)

View file

@ -361,11 +361,11 @@ func checkSizeQuotaExceeded(ctx context.Context, doer, owner *user_model.User, p
} }
if setting.Packages.LimitTotalOwnerSize > -1 { if setting.Packages.LimitTotalOwnerSize > -1 {
totalSize, err := packages_model.CalculateBlobSize(ctx, &packages_model.PackageFileSearchOptions{ totalSize, err := packages_model.CalculateFileSize(ctx, &packages_model.PackageFileSearchOptions{
OwnerID: owner.ID, OwnerID: owner.ID,
}) })
if err != nil { if err != nil {
log.Error("CalculateBlobSize failed: %v", err) log.Error("CalculateFileSize failed: %v", err)
return err return err
} }
if totalSize+uploadSize > setting.Packages.LimitTotalOwnerSize { if totalSize+uploadSize > setting.Packages.LimitTotalOwnerSize {

View file

@ -4,7 +4,9 @@
<div class="ui container"> <div class="ui container">
{{template "base/alert" .}} {{template "base/alert" .}}
<h4 class="ui top attached header"> <h4 class="ui top attached header">
{{.locale.Tr "admin.packages.package_manage_panel"}} ({{.locale.Tr "admin.total" .Total}}, {{.locale.Tr "admin.packages.total_size" (FileSize .TotalBlobSize)}}) {{.locale.Tr "admin.packages.package_manage_panel"}} ({{.locale.Tr "admin.total" .TotalCount}},
{{.locale.Tr "admin.packages.total_size" (FileSize .TotalBlobSize)}},
{{.locale.Tr "admin.packages.unreferenced_size" (FileSize .TotalUnreferencedBlobSize)}})
</h4> </h4>
<div class="ui attached segment"> <div class="ui attached segment">
<form class="ui form ignore-dirty"> <form class="ui form ignore-dirty">