Skip to content

Commit

Permalink
Add back backfill_figures_metrics, pending deprecation
Browse files Browse the repository at this point in the history
Calls backfill_daily_metrics and backfill_monthly_metrics, w/options
  • Loading branch information
bryanlandia committed Jun 15, 2021
1 parent 459b181 commit 7ae08e3
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
2 changes: 2 additions & 0 deletions figures/management/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"""
"""
1 change: 1 addition & 0 deletions figures/management/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 1 addition & 2 deletions figures/management/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
"""Django management commands for Figures.
"""
Management commands for Figures.
"""
57 changes: 57 additions & 0 deletions figures/management/commands/backfill_figures_metrics.py
Original file line number Diff line number Diff line change
@@ -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')
67 changes: 67 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 7ae08e3

Please sign in to comment.