Skip to content

DB: do not fetch data and others when deleting rows #10446

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

Merged
merged 4 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 15 additions & 5 deletions readthedocs/analytics/tasks.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Tasks for Read the Docs' analytics."""

from django.conf import settings
from django.db import connection
from django.utils import timezone

import readthedocs
from readthedocs.worker import app

from .models import PageView
from .utils import send_to_analytics

DEFAULT_PARAMETERS = {
Expand Down Expand Up @@ -80,7 +80,17 @@ def delete_old_page_counts():
"""
retention_days = settings.RTD_ANALYTICS_DEFAULT_RETENTION_DAYS
days_ago = timezone.now().date() - timezone.timedelta(days=retention_days)
return PageView.objects.filter(
date__lt=days_ago,
date__gt=days_ago - timezone.timedelta(days=90),
).delete()

# NOTE: we are using raw SQL here to avoid Django doing a SELECT first to
# send `pre_` and `post_` delete signals
# See https://docs.djangoproject.com/en/4.2/ref/models/querysets/#delete
with connection.cursor() as cursor:
cursor.execute(
# "SELECT COUNT(*) FROM analytics_pageview WHERE date BETWEEN %s AND %s",
"DELETE FROM analytics_pageview WHERE date BETWEEN %s AND %s",
[
days_ago - timezone.timedelta(days=90),
days_ago,
],
)
return cursor.fetchall()
18 changes: 14 additions & 4 deletions readthedocs/telemetry/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Tasks related to telemetry."""

from django.conf import settings
from django.db import connections
from django.utils import timezone

from readthedocs.builds.models import Build
Expand Down Expand Up @@ -33,7 +34,16 @@ def delete_old_build_data():
"""
retention_days = settings.RTD_TELEMETRY_DATA_RETENTION_DAYS
days_ago = timezone.now().date() - timezone.timedelta(days=retention_days)
return BuildData.objects.filter(
created__lt=days_ago,
created__gt=days_ago - timezone.timedelta(days=90),
).delete()
# NOTE: we are using raw SQL here to avoid Django doing a SELECT first to
# send `pre_` and `post_` delete signals
# See https://docs.djangoproject.com/en/4.2/ref/models/querysets/#delete
with connections["telemetry"].cursor() as cursor:
cursor.execute(
# "SELECT COUNT(*) FROM telemetry_builddata WHERE created BETWEEN %s AND %s",
"DELETE FROM telemetry_builddata WHERE created BETWEEN %s AND %s",
[
days_ago - timezone.timedelta(days=90),
days_ago,
],
)
return cursor.fetchall()