2016-10-06 02:13:46 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-07-29 21:52:45 +02:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.utils.dateparse import parse_datetime
|
|
|
|
|
|
|
|
from analytics.models import RealmCount, UserCount
|
|
|
|
from analytics.lib.counts import COUNT_STATS, CountStat, process_count_stat
|
|
|
|
from zerver.lib.timestamp import datetime_to_string, is_timezone_aware
|
|
|
|
from zerver.models import UserProfile, Message
|
|
|
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Fills Analytics tables.
|
|
|
|
|
|
|
|
Run as a cron job that runs every hour."""
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (ArgumentParser) -> None
|
2016-10-12 23:40:48 +02:00
|
|
|
parser.add_argument('--time', '-t',
|
2016-07-29 21:52:45 +02:00
|
|
|
type=str,
|
2016-10-12 23:40:48 +02:00
|
|
|
help='Update stat tables from current state to --time. Defaults to the current time.',
|
2016-07-29 21:52:45 +02:00
|
|
|
default=datetime_to_string(timezone.now()))
|
|
|
|
parser.add_argument('--utc',
|
|
|
|
type=bool,
|
2016-10-12 23:40:48 +02:00
|
|
|
help="Interpret --time in UTC.",
|
2016-07-29 21:52:45 +02:00
|
|
|
default=False)
|
2016-10-12 23:40:48 +02:00
|
|
|
parser.add_argument('--stat', '-s',
|
2016-07-29 21:52:45 +02:00
|
|
|
type=str,
|
2016-10-12 23:40:48 +02:00
|
|
|
help="CountStat to process. If omitted, all stats are processed.")
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
# type: (*Any, **Any) -> None
|
2016-10-12 23:40:48 +02:00
|
|
|
fill_to_time = parse_datetime(options['time'])
|
2016-10-05 05:43:19 +02:00
|
|
|
if options['utc']:
|
2016-10-12 23:40:48 +02:00
|
|
|
fill_to_time = fill_to_time.replace(tzinfo=timezone.utc)
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2016-10-12 23:40:48 +02:00
|
|
|
if not (is_timezone_aware(fill_to_time)):
|
|
|
|
raise ValueError("--time must be timezone aware. Maybe you meant to use the --utc option?")
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2016-10-05 05:43:19 +02:00
|
|
|
if options['stat'] is not None:
|
2016-10-12 23:40:48 +02:00
|
|
|
process_count_stat(COUNT_STATS[options['stat']], fill_to_time)
|
2016-07-29 21:52:45 +02:00
|
|
|
else:
|
|
|
|
for stat in COUNT_STATS.values():
|
2016-10-12 23:40:48 +02:00
|
|
|
process_count_stat(stat, fill_to_time)
|