Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[noissue]: Update django-import-export requirement from <3.3.0,>=2.9 to >=2.9,<3.4.0 #211

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/2079.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added field ``disk_size`` to Repository and RepositoryVersion that shows current total size in bytes of files stored on disk.
20 changes: 20 additions & 0 deletions pulpcore/app/models/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ class Meta:
unique_together = ("name", "pulp_domain")
verbose_name_plural = "repositories"

@property
def disk_size(self):
"""Returns the approximate size on disk for all artifacts stored across all versions."""
all_content = (
RepositoryContent.objects.filter(repository=self)
.distinct("content")
.values_list("content")
)
return (
Artifact.objects.filter(content__pk__in=all_content)
.distinct()
.aggregate(size=models.Sum("size"))["size"]
or 0
)

def on_new_version(self, version):
"""Called after a new repository version has been created.

Expand Down Expand Up @@ -795,6 +810,11 @@ def artifacts(self):
"""
return self.repository.cast().artifacts_for_version(self)

@property
def disk_size(self):
"""Returns the size on disk of all the artifacts in this repository version."""
return self.artifacts.distinct().aggregate(size=models.Sum("size"))["size"] or 0

def added(self, base_version=None):
"""
Args:
Expand Down
27 changes: 27 additions & 0 deletions pulpcore/app/serializers/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from rest_framework import fields, serializers
from rest_framework_nested.serializers import NestedHyperlinkedModelSerializer
from drf_spectacular.utils import extend_schema_field

from pulpcore.app import models, settings
from pulpcore.app.serializers import (
Expand Down Expand Up @@ -49,6 +50,18 @@ class RepositorySerializer(ModelSerializer):
required=False,
allow_null=True,
)
disk_size = serializers.SerializerMethodField(
help_text=_(
"Approximate size on disk of all artifacts in the repository across all its versions."
),
)

@extend_schema_field(serializers.IntegerField(read_only=True, allow_null=True))
def get_disk_size(self, obj):
if view := self.context.get("view", None):
if getattr(view, "action", "") == "retrieve":
return obj.disk_size
return None

def validate_remote(self, value):
if value and type(value) not in self.Meta.model.REMOTE_TYPES:
Expand All @@ -66,6 +79,7 @@ class Meta:
"latest_version_href",
"name",
"description",
"disk_size",
"retain_repo_versions",
"remote",
)
Expand Down Expand Up @@ -420,12 +434,25 @@ class RepositoryVersionSerializer(ModelSerializer, NestedHyperlinkedModelSeriali
source="*",
read_only=True,
)
disk_size = serializers.SerializerMethodField(
help_text=_(
"Approximate size on disk of all artifacts in the repository version."
),
)

@extend_schema_field(serializers.IntegerField(read_only=True, allow_null=True))
def get_disk_size(self, obj):
if view := self.context.get("view", None):
if getattr(view, "action", "") == "retrieve":
return obj.disk_size
return None

class Meta:
model = models.RepositoryVersion
fields = ModelSerializer.Meta.fields + (
"pulp_href",
"number",
"disk_size",
"repository",
"base_version",
"content_summary",
Expand Down
55 changes: 55 additions & 0 deletions pulpcore/tests/functional/api/test_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,58 @@ def test_repository_content_filters(
# but not in its latest version anymore
results = file_repository_api_client.list(latest_with_content=content.pulp_href).results
assert results == []


@pytest.mark.parallel
def test_repo_size(
file_repo,
file_repository_api_client,
file_repository_version_api_client,
file_remote_factory,
basic_manifest_path,
random_artifact_factory,
file_content_api_client,
monitor_task
):
# Check that 'disk_size' is only added on retrieve operations
file_repo = file_repository_api_client.read(file_repo.pulp_href)
assert file_repo.disk_size == 0
repo_ver0 = file_repository_version_api_client.read(file_repo.latest_version_href)
assert repo_ver0.disk_size == 0

listed_repo = file_repository_api_client.list().results[0]
assert listed_repo.disk_size is None
listed_ver = file_repository_version_api_client.list(file_repo.pulp_href).results[0]
assert listed_ver.disk_size is None

# Sync repository with on_demand
remote = file_remote_factory(manifest_path=basic_manifest_path, policy="on_demand")
body = {"remote": remote.pulp_href}
monitor_task(file_repository_api_client.sync(file_repo.pulp_href, body).task)
file_repo = file_repository_api_client.read(file_repo.pulp_href)

# disk_size should still be 0
assert file_repo.disk_size == 0
repo_ver1 = file_repository_version_api_client.read(file_repo.latest_version_href)
assert repo_ver1.disk_size == 0

# Resync with immediate
remote = file_remote_factory(manifest_path=basic_manifest_path, policy="immediate")
body = {"remote": remote.pulp_href}
monitor_task(file_repository_api_client.sync(file_repo.pulp_href, body).task)
file_repo = file_repository_api_client.read(file_repo.pulp_href)

assert file_repo.disk_size == 3072 # 3 * 1024
repo_ver1 = file_repository_version_api_client.read(file_repo.latest_version_href)
assert repo_ver1.disk_size == 3072

# Add content unit w/ same name, but different artifact
art1 = random_artifact_factory()
body = {"repository": file_repo.pulp_href, "artifact": art1.pulp_href, "relative_path": "1.iso"}
monitor_task(file_content_api_client.create(**body).task)
file_repo = file_repository_api_client.read(file_repo.pulp_href)

assert file_repo.disk_size == 3072 + art1.size # All 4 artifacts in repo
repo_ver2 = file_repository_version_api_client.read(file_repo.latest_version_href)
assert repo_ver2.content_summary.present["file.file"]["count"] == 3
assert repo_ver2.disk_size == 2048 + art1.size # New size of 3 artifacts in version
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cryptography>=38.0.1,<41.0.4
Django~=4.2.0 # LTS version, switch only if we have a compelling reason to
django-filter>=23.1,<=23.2
django-guid>=3.3,<=3.3.1
django-import-export>=2.9,<3.3.0
django-import-export>=2.9,<3.4.0
django-lifecycle>=1.0,<=1.0.2
djangorestframework>=3.14.0,<=3.14.0
djangorestframework-queryfields>=1.0,<=1.0.0
Expand Down
Loading