-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analytics): Command to populate arbitrary periods of analytics d…
…ata (#4155)
- Loading branch information
Showing
4 changed files
with
189 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import argparse | ||
from typing import Any | ||
|
||
from app_analytics.constants import ANALYTICS_READ_BUCKET_SIZE | ||
from app_analytics.tasks import ( | ||
populate_api_usage_bucket, | ||
populate_feature_evaluation_bucket, | ||
) | ||
from django.conf import settings | ||
from django.core.management import BaseCommand | ||
|
||
MINUTES_IN_DAY: int = 1440 | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser: argparse.ArgumentParser) -> None: | ||
parser.add_argument( | ||
"--days-to-populate", | ||
type=int, | ||
dest="days_to_populate", | ||
help="Last n days to populate", | ||
default=30, | ||
) | ||
|
||
def handle(self, *args: Any, days_to_populate: int, **options: Any) -> None: | ||
if settings.USE_POSTGRES_FOR_ANALYTICS: | ||
minutes_to_populate = MINUTES_IN_DAY * days_to_populate | ||
populate_api_usage_bucket( | ||
ANALYTICS_READ_BUCKET_SIZE, | ||
minutes_to_populate, | ||
) | ||
populate_feature_evaluation_bucket( | ||
ANALYTICS_READ_BUCKET_SIZE, | ||
minutes_to_populate, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
from django.core.management import call_command | ||
from pytest_django.fixtures import SettingsWrapper | ||
from pytest_mock import MockerFixture | ||
|
||
|
||
def test_populate_buckets__postgres_analytics_disabled__noop( | ||
settings: SettingsWrapper, | ||
mocker: MockerFixture, | ||
) -> None: | ||
# Given | ||
settings.USE_POSTGRES_FOR_ANALYTICS = False | ||
populate_api_usage_bucket_mock = mocker.patch( | ||
"app_analytics.management.commands.populate_buckets.populate_api_usage_bucket" | ||
) | ||
populate_feature_evaluation_bucket = mocker.patch( | ||
"app_analytics.management.commands.populate_buckets.populate_feature_evaluation_bucket" | ||
) | ||
|
||
# When | ||
call_command("populate_buckets") | ||
|
||
# Then | ||
populate_api_usage_bucket_mock.assert_not_called() | ||
populate_feature_evaluation_bucket.assert_not_called() | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"options, expected_call_every", | ||
[ | ||
({}, 43200), | ||
( | ||
{"days_to_populate": 10}, | ||
14400, | ||
), | ||
], | ||
) | ||
def test_populate_buckets__postgres_analytics_enabled__calls_expected( | ||
settings: SettingsWrapper, | ||
mocker: MockerFixture, | ||
options: dict[str, Any], | ||
expected_call_every: int, | ||
) -> None: | ||
# Given | ||
settings.USE_POSTGRES_FOR_ANALYTICS = True | ||
populate_api_usage_bucket_mock = mocker.patch( | ||
"app_analytics.management.commands.populate_buckets.populate_api_usage_bucket" | ||
) | ||
populate_feature_evaluation_bucket = mocker.patch( | ||
"app_analytics.management.commands.populate_buckets.populate_feature_evaluation_bucket" | ||
) | ||
expected_bucket_size = 15 | ||
mocker.patch( | ||
"app_analytics.management.commands.populate_buckets.ANALYTICS_READ_BUCKET_SIZE", | ||
new=expected_bucket_size, | ||
) | ||
|
||
# When | ||
call_command("populate_buckets", **options) | ||
|
||
# Then | ||
populate_api_usage_bucket_mock.assert_called_once_with( | ||
expected_bucket_size, | ||
expected_call_every, | ||
) | ||
populate_feature_evaluation_bucket.assert_called_once_with( | ||
expected_bucket_size, | ||
expected_call_every, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters