From 7ae08e331cce19de23fd79a08253e6013e9bfab6 Mon Sep 17 00:00:00 2001 From: Bryan Wilson Date: Mon, 14 Jun 2021 13:48:06 -0700 Subject: [PATCH] Add back backfill_figures_metrics, pending deprecation Calls backfill_daily_metrics and backfill_monthly_metrics, w/options --- figures/management/__init__.py | 2 + figures/management/base.py | 1 + figures/management/commands/__init__.py | 3 +- .../commands/backfill_figures_metrics.py | 57 ++++++++++++++++ tests/test_commands.py | 67 +++++++++++++++++++ 5 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 figures/management/commands/backfill_figures_metrics.py diff --git a/figures/management/__init__.py b/figures/management/__init__.py index e69de29b..c0f6f287 100644 --- a/figures/management/__init__.py +++ b/figures/management/__init__.py @@ -0,0 +1,2 @@ +""" +""" diff --git a/figures/management/base.py b/figures/management/base.py index 34d2af4d..418e9839 100644 --- a/figures/management/base.py +++ b/figures/management/base.py @@ -41,6 +41,7 @@ def get_date(self, date_str=None): def add_arguments(self, parser): ''' ''' + # TODO: allow passing the queue to use. Warn if no_delay specified. parser.add_argument( '--site', help='backfill a specific site. provide numeric id or domain name', diff --git a/figures/management/commands/__init__.py b/figures/management/commands/__init__.py index 7b3ca0e5..3b3b17a0 100644 --- a/figures/management/commands/__init__.py +++ b/figures/management/commands/__init__.py @@ -1,3 +1,2 @@ +"""Django management commands for Figures. """ -Management commands for Figures. -""" \ No newline at end of file diff --git a/figures/management/commands/backfill_figures_metrics.py b/figures/management/commands/backfill_figures_metrics.py new file mode 100644 index 00000000..484a5b63 --- /dev/null +++ b/figures/management/commands/backfill_figures_metrics.py @@ -0,0 +1,57 @@ +"""Deprecated: +Please call instead one of: +backfill_figures_daily_metrics, backfill_figures_monthly_metrics, or +backfill_figures_enrollment_data + +Backfills Figures historical metrics + +""" + +from __future__ import print_function + +from __future__ import absolute_import +from textwrap import dedent +import warnings + +from django.core.management import call_command +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + """Pending Deprecation: Populate Figures metrics models + """ + help = dedent(__doc__).strip() + + def add_arguments(self, parser): + parser.add_argument('--overwrite', + action='store_true', + default=False, + help='overwrite existing data in SiteMonthlyMetrics') + parser.add_argument('--site', + help='backfill a specific site. provide id or domain name') + + def handle(self, *args, **options): + ''' + Pending deprecation. Passes handling off to new commands. + ''' + warnings.warn( + "backfill_figures_metrics is pending deprecation and will be removed in " + "Figures 1.0. Please use one of backfill_figures_daily_metrics, " + "backfill_figures_monthly_metrics, or backfill_figures_enrollment_data, " + "instead.", + PendingDeprecationWarning + ) + print('BEGIN: Backfill Figures Metrics') + + call_command( + 'backfill_figures_monthly_metrics', + overwrite=options['overwrite'], + site=options['site'] + ) + call_command( + 'backfill_figures_daily_metrics', + overwrite=options['overwrite'], + site=options['site'] + ) + + print('DONE: Backfill Figures Metrics') diff --git a/tests/test_commands.py b/tests/test_commands.py index bafd7c8e..3387e319 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -98,3 +98,70 @@ def test_backfill_daily_for_site(self): with mock.patch(self.PLAIN_PATH) as mock_populate: call_command('backfill_figures_daily_metrics', no_delay=True) assert mock_populate.called_with(site_id=1) + + +class TestPopulateFiguresMetricsCommand(object): + """Test that command gives a pending deprecation warning and that it calls the correct + substitute management commands based on passed options. + """ + + def test_pending_deprecation(self): + mock_call_path = 'figures.management.commands.populate_figures_metrics.call_command' + with mock.patch(mock_call_path): + with pytest.warns(PendingDeprecationWarning): + call_command('populate_figures_metrics') + + @pytest.mark.parametrize('options, subst_command, subst_call_options', [ + ( + {'mau': True, 'no_delay': None, 'date': '2021-06-14', 'experimental': None}, + 'run_figures_mau_metrics', + {'no_delay': None} + ), + ( + { + 'mau': False, 'no_delay': True, 'date': '2021-06-14', + 'experimental': True, 'force_update': True + }, + 'backfill_figures_daily_metrics', + { + 'no_delay': True, 'experimental': True, 'overwrite': True, + 'date_start': '2021-06-14', 'date_end': '2021-06-14' + } + ) + ]) + def test_correct_subtitute_commands_called(self, options, subst_command, subst_call_options): + old_pop_cmd = 'figures.management.commands.populate_figures_metrics.call_command' + with mock.patch(old_pop_cmd) as mock_call_cmd: + call_command('populate_figures_metrics', **options) # this isn't the patched version of call_command + mock_call_cmd.assert_called_with(subst_command, **subst_call_options) + + +class TestBackfillFiguresMetricsCommand(object): + """Test that command gives a pending deprecation warning and that it calls the correct + substitute management commands based on passed options. + """ + + def test_pending_deprecation(self): + mock_call_path = 'figures.management.commands.backfill_figures_metrics.call_command' + with mock.patch(mock_call_path): + with pytest.warns(PendingDeprecationWarning): + call_command('backfill_figures_metrics') + + @pytest.mark.parametrize('options, subst_call_options', [ + ( + {'site': 1, 'overwrite': None}, + {'site': 1, 'overwrite': None} + ), + ( + {'overwrite': True}, + {'site': None, 'overwrite': True} + ), + ]) + def test_correct_subtitute_commands_called(self, options, subst_call_options): + old_pop_cmd = 'figures.management.commands.backfill_figures_metrics.call_command' + with mock.patch(old_pop_cmd) as mock_call_cmd: + call_command('backfill_figures_metrics', **options) # this isn't the patched version of call_command + dailycmd = 'backfill_figures_daily_metrics' + monthlycmd = 'backfill_figures_monthly_metrics' + mock_call_cmd.assert_any_call(dailycmd, **subst_call_options) + mock_call_cmd.assert_any_call(monthlycmd, **subst_call_options)