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

Retain daily submission counters for one year #889

Merged
merged 6 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,19 @@ def add_arguments(self, parser):
help='Recalculate counters for every user. Default is False',
)

parser.add_argument(
'--skip_monthly',
action='store_true',
default=False,
help='Skip updating monthly counters. Default is False',
)

def handle(self, *args, **kwargs):
days = kwargs['days']
self._chunks = kwargs['chunks']
self._force = kwargs['force']
self._verbosity = kwargs['verbosity']
self._skip_monthly = kwargs['skip_monthly']
today = timezone.now().date()
delta = timedelta(days=days)
date_threshold = today - delta
Expand Down Expand Up @@ -98,7 +106,8 @@ def handle(self, *args, **kwargs):

daily_counters, total_submissions = self.build_counters(xf)
self.add_daily_counters(daily_counters)
self.add_monthly_counters(total_submissions, xf, user)
if not self._skip_monthly:
self.add_monthly_counters(total_submissions, xf, user)

self.update_user_profile(user)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from django.conf import settings
from django.core.management import call_command
from django.db import migrations


def populate_daily_counts_for_year(apps, schema_editor):
if settings.SKIP_HEAVY_MIGRATIONS:
print(
"""
!!! ATTENTION !!!
If you have existing projects you need to run this management command:

> python manage.py populate_submission_counters -f --skip_monthly

Until you do, total usage counts from /api/v2/service_usage and /api/v2/asset_usage will be incorrect
"""
)
else:
print(
"""
This might take a while. If it is too slow, you may want to re-run the
migration with SKIP_HEAVY_MIGRATIONS=True and run the following management command:

> python manage.py populate_submission_counters -f --skip_monthly

Until you do, total usage counts from /api/v2/service_usage and /api/v2/asset_usage will be incorrect
"""
)
call_command('populate_submission_counters', force=True, skip_monthly=True)


class Migration(migrations.Migration):

dependencies = [
('logger', '0027_on_delete_cascade_monthlyxformsubmissioncounter'),
('main', '0010_userprofile_metadata_jsonfield'),
]

# We don't do anything when migrating in reverse
# Just set DAILY_COUNTER_MAX_DAYS back to 31 and counters will be auto-deleted
operations = [
migrations.RunPython(
populate_daily_counts_for_year,
migrations.RunPython.noop,
),
]
2 changes: 1 addition & 1 deletion onadata/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ def skip_suspicious_operations(record):
]

# Set the maximum number of days daily counters can be kept for
DAILY_COUNTERS_MAX_DAYS = env.int('DAILY_COUNTERS_MAX_DAYS', 31)
DAILY_COUNTERS_MAX_DAYS = env.int('DAILY_COUNTERS_MAX_DAYS', 366)

SERVICE_ACCOUNT = {
'BACKEND': env.cache_url(
Expand Down