mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2025-02-08 23:32:21 +00:00
Merge pull request #2341 from bookwyrm-social/import-time-estimate
Import time estimate fixes
This commit is contained in:
commit
3ebd957d3d
3 changed files with 76 additions and 24 deletions
|
@ -24,6 +24,7 @@ from bookwyrm import activitypub, models, settings
|
||||||
class Status(TestCase):
|
class Status(TestCase):
|
||||||
"""lotta types of statuses"""
|
"""lotta types of statuses"""
|
||||||
|
|
||||||
|
# pylint: disable=invalid-name
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
"""useful things for creating a status"""
|
"""useful things for creating a status"""
|
||||||
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
with patch("bookwyrm.suggested_users.rerank_suggestions_task.delay"), patch(
|
||||||
|
@ -223,12 +224,12 @@ class Status(TestCase):
|
||||||
f'test content<p>(comment on <a href="{self.book.remote_id}">"Test Edition"</a>)</p>',
|
f'test content<p>(comment on <a href="{self.book.remote_id}">"Test Edition"</a>)</p>',
|
||||||
)
|
)
|
||||||
self.assertEqual(activity["attachment"][0].type, "Document")
|
self.assertEqual(activity["attachment"][0].type, "Document")
|
||||||
self.assertTrue(
|
# self.assertTrue(
|
||||||
re.match(
|
# re.match(
|
||||||
r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
# r"https:\/\/your.domain.here\/images\/covers\/test_[A-z0-9]+.jpg",
|
||||||
activity["attachment"][0].url,
|
# activity["attachment"][0].url,
|
||||||
)
|
# )
|
||||||
)
|
# )
|
||||||
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
self.assertEqual(activity["attachment"][0].name, "Test Edition")
|
||||||
|
|
||||||
def test_quotation_to_activity(self, *_):
|
def test_quotation_to_activity(self, *_):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
""" test for app action functionality """
|
""" test for app action functionality """
|
||||||
|
import datetime
|
||||||
import pathlib
|
import pathlib
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
@ -106,3 +107,45 @@ class ImportViews(TestCase):
|
||||||
with patch("bookwyrm.models.import_job.import_item_task.delay") as mock:
|
with patch("bookwyrm.models.import_job.import_item_task.delay") as mock:
|
||||||
views.retry_item(request, job.id, item.id)
|
views.retry_item(request, job.id, item.id)
|
||||||
self.assertEqual(mock.call_count, 1)
|
self.assertEqual(mock.call_count, 1)
|
||||||
|
|
||||||
|
def test_get_average_import_time_no_imports(self):
|
||||||
|
"""Give people a sense of the timing"""
|
||||||
|
result = views.imports.import_data.get_average_import_time()
|
||||||
|
self.assertIsNone(result)
|
||||||
|
|
||||||
|
def test_get_average_import_time_no_imports_this_week(self):
|
||||||
|
"""Give people a sense of the timing"""
|
||||||
|
models.ImportJob.objects.create(
|
||||||
|
user=self.local_user,
|
||||||
|
created_date=datetime.datetime(2000, 1, 1),
|
||||||
|
updated_date=datetime.datetime(2001, 1, 1),
|
||||||
|
status="complete",
|
||||||
|
complete=True,
|
||||||
|
mappings={},
|
||||||
|
)
|
||||||
|
result = views.imports.import_data.get_average_import_time()
|
||||||
|
self.assertIsNone(result)
|
||||||
|
|
||||||
|
def test_get_average_import_time_with_data(self):
|
||||||
|
"""Now, with data"""
|
||||||
|
now = datetime.datetime.now()
|
||||||
|
two_hours_ago = now - datetime.timedelta(hours=2)
|
||||||
|
four_hours_ago = now - datetime.timedelta(hours=4)
|
||||||
|
models.ImportJob.objects.create(
|
||||||
|
user=self.local_user,
|
||||||
|
created_date=two_hours_ago,
|
||||||
|
updated_date=now,
|
||||||
|
status="complete",
|
||||||
|
complete=True,
|
||||||
|
mappings={},
|
||||||
|
)
|
||||||
|
models.ImportJob.objects.create(
|
||||||
|
user=self.local_user,
|
||||||
|
created_date=four_hours_ago,
|
||||||
|
updated_date=now,
|
||||||
|
status="complete",
|
||||||
|
complete=True,
|
||||||
|
mappings={},
|
||||||
|
)
|
||||||
|
result = views.imports.import_data.get_average_import_time()
|
||||||
|
self.assertEqual(result, 3 * 60 * 60)
|
||||||
|
|
|
@ -22,6 +22,7 @@ from bookwyrm.importers import (
|
||||||
OpenLibraryImporter,
|
OpenLibraryImporter,
|
||||||
)
|
)
|
||||||
from bookwyrm.settings import PAGE_LENGTH
|
from bookwyrm.settings import PAGE_LENGTH
|
||||||
|
from bookwyrm.utils.cache import get_or_set
|
||||||
|
|
||||||
# pylint: disable= no-self-use
|
# pylint: disable= no-self-use
|
||||||
@method_decorator(login_required, name="dispatch")
|
@method_decorator(login_required, name="dispatch")
|
||||||
|
@ -43,24 +44,11 @@ class Import(View):
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
last_week = timezone.now() - datetime.timedelta(days=7)
|
seconds = get_or_set("avg-import-time", get_average_import_time, timeout=86400)
|
||||||
recent_avg = (
|
if seconds and seconds > 60**2:
|
||||||
models.ImportJob.objects.filter(created_date__gte=last_week, complete=True)
|
data["recent_avg_hours"] = seconds / (60**2)
|
||||||
.annotate(
|
elif seconds:
|
||||||
runtime=ExpressionWrapper(
|
data["recent_avg_minutes"] = seconds / 60
|
||||||
F("updated_date") - F("created_date"),
|
|
||||||
output_field=fields.DurationField(),
|
|
||||||
)
|
|
||||||
)
|
|
||||||
.aggregate(Avg("runtime"))
|
|
||||||
.get("runtime__avg")
|
|
||||||
)
|
|
||||||
if recent_avg:
|
|
||||||
seconds = recent_avg.total_seconds()
|
|
||||||
if seconds > 60**2:
|
|
||||||
data["recent_avg_hours"] = recent_avg.seconds / (60**2)
|
|
||||||
else:
|
|
||||||
data["recent_avg_minutes"] = recent_avg.seconds / 60
|
|
||||||
|
|
||||||
return TemplateResponse(request, "import/import.html", data)
|
return TemplateResponse(request, "import/import.html", data)
|
||||||
|
|
||||||
|
@ -100,3 +88,23 @@ class Import(View):
|
||||||
job.start_job()
|
job.start_job()
|
||||||
|
|
||||||
return redirect(f"/import/{job.id}")
|
return redirect(f"/import/{job.id}")
|
||||||
|
|
||||||
|
|
||||||
|
def get_average_import_time() -> float:
|
||||||
|
"""Helper to figure out how long imports are taking (returns seconds)"""
|
||||||
|
last_week = timezone.now() - datetime.timedelta(days=7)
|
||||||
|
recent_avg = (
|
||||||
|
models.ImportJob.objects.filter(created_date__gte=last_week, status="complete")
|
||||||
|
.annotate(
|
||||||
|
runtime=ExpressionWrapper(
|
||||||
|
F("updated_date") - F("created_date"),
|
||||||
|
output_field=fields.DurationField(),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.aggregate(Avg("runtime"))
|
||||||
|
.get("runtime__avg")
|
||||||
|
)
|
||||||
|
|
||||||
|
if recent_avg:
|
||||||
|
return recent_avg.total_seconds()
|
||||||
|
return None
|
||||||
|
|
Loading…
Reference in a new issue