analytics: Add management command to clear single stat.

This commit is contained in:
Rishi Gupta
2017-10-04 15:55:43 -07:00
committed by Tim Abbott
parent d6e21b5ca9
commit 0f31cddf49
3 changed files with 70 additions and 1 deletions

View File

@@ -232,6 +232,14 @@ def do_drop_all_analytics_tables():
FillState.objects.all().delete()
Anomaly.objects.all().delete()
def do_drop_single_stat(property):
# type: (str) -> None
UserCount.objects.filter(property=property).delete()
StreamCount.objects.filter(property=property).delete()
RealmCount.objects.filter(property=property).delete()
InstallationCount.objects.filter(property=property).delete()
FillState.objects.filter(property=property).delete()
## DataCollector-level operations ##
def do_pull_by_sql_query(property, start_time, end_time, query, group_by):

View File

@@ -0,0 +1,33 @@
import sys
from argparse import ArgumentParser
from django.db import connection
from django.core.management.base import BaseCommand
from analytics.lib.counts import do_drop_single_stat, COUNT_STATS
from typing import Any
class Command(BaseCommand):
help = """Clear analytics tables."""
def add_arguments(self, parser):
# type: (ArgumentParser) -> None
parser.add_argument('--force',
action='store_true',
help="Actually do it.")
parser.add_argument('--property',
type=str,
help="The property of the stat to be cleared.")
def handle(self, *args, **options):
# type: (*Any, **Any) -> None
property = options['property']
if property not in COUNT_STATS:
print("Invalid property: %s" % (property,))
sys.exit(1)
if not options['force']:
print("No action taken. Use --force.")
sys.exit(1)
do_drop_single_stat(property)

View File

@@ -9,7 +9,7 @@ from django.utils.timezone import utc as timezone_utc
from analytics.lib.counts import CountStat, COUNT_STATS, process_count_stat, \
do_fill_count_stat_at_hour, do_increment_logging_stat, DataCollector, \
sql_data_collector, LoggingCountStat, do_aggregate_to_summary_table, \
do_drop_all_analytics_tables, DependentCountStat
do_drop_all_analytics_tables, do_drop_single_stat, DependentCountStat
from analytics.models import BaseCount, InstallationCount, RealmCount, \
UserCount, StreamCount, FillState, Anomaly, installation_epoch, \
last_successful_fill
@@ -782,6 +782,34 @@ class TestDeleteStats(AnalyticsTestCase):
for table in list(analytics.models.values()):
self.assertFalse(table.objects.exists())
def test_do_drop_single_stat(self):
# type: () -> None
user = self.create_user()
stream = self.create_stream_with_recipient()[0]
count_args_to_delete = {'property': 'to_delete', 'end_time': self.TIME_ZERO, 'value': 10}
count_args_to_save = {'property': 'to_save', 'end_time': self.TIME_ZERO, 'value': 10}
for count_args in [count_args_to_delete, count_args_to_save]:
UserCount.objects.create(user=user, realm=user.realm, **count_args)
StreamCount.objects.create(stream=stream, realm=stream.realm, **count_args)
RealmCount.objects.create(realm=user.realm, **count_args)
InstallationCount.objects.create(**count_args)
FillState.objects.create(property='to_delete', end_time=self.TIME_ZERO, state=FillState.DONE)
FillState.objects.create(property='to_save', end_time=self.TIME_ZERO, state=FillState.DONE)
Anomaly.objects.create(info='test anomaly')
analytics = apps.get_app_config('analytics')
for table in list(analytics.models.values()):
self.assertTrue(table.objects.exists())
do_drop_single_stat('to_delete')
for table in list(analytics.models.values()):
if table._meta.db_table == 'analytics_anomaly':
self.assertTrue(table.objects.exists())
else:
self.assertFalse(table.objects.filter(property='to_delete').exists())
self.assertTrue(table.objects.filter(property='to_save').exists())
class TestActiveUsersAudit(AnalyticsTestCase):
def setUp(self):
# type: () -> None