2016-10-06 02:13:46 +02:00
|
|
|
from __future__ import absolute_import
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-10-05 03:38:20 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from scripts.lib.zulip_tools import ENDC, WARNING
|
|
|
|
|
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
|
2016-10-05 03:38:20 +02:00
|
|
|
from django.conf import settings
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
from analytics.models import RealmCount, UserCount
|
2017-02-02 01:53:45 +01:00
|
|
|
from analytics.lib.counts import COUNT_STATS, logger, process_count_stat
|
2016-07-29 21:52:45 +02:00
|
|
|
from zerver.models import UserProfile, Message
|
|
|
|
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
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.',
|
2017-02-28 18:39:36 +01:00
|
|
|
default=timezone.now().isoformat())
|
2016-07-29 21:52:45 +02:00
|
|
|
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.")
|
2017-01-07 09:19:37 +01:00
|
|
|
parser.add_argument('--quiet', '-q',
|
|
|
|
type=str,
|
|
|
|
help="Suppress output to stdout.")
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
# type: (*Any, **Any) -> None
|
2016-10-05 03:38:20 +02:00
|
|
|
try:
|
|
|
|
os.mkdir(settings.ANALYTICS_LOCK_DIR)
|
|
|
|
except OSError:
|
|
|
|
print(WARNING + "Analytics lock %s is unavailable; exiting... " + ENDC)
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.run_update_analytics_counts(options)
|
|
|
|
finally:
|
|
|
|
os.rmdir(settings.ANALYTICS_LOCK_DIR)
|
|
|
|
|
|
|
|
def run_update_analytics_counts(self, options):
|
|
|
|
# type: (Dict[str, 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
|
|
|
|
2017-02-28 18:39:36 +01:00
|
|
|
if fill_to_time.tzinfo is None:
|
2016-10-12 23:40:48 +02:00
|
|
|
raise ValueError("--time must be timezone aware. Maybe you meant to use the --utc option?")
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2017-02-02 01:53:45 +01:00
|
|
|
logger.info("Starting updating analytics counts through %s" % (fill_to_time,))
|
|
|
|
|
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)
|
2017-02-02 01:53:45 +01:00
|
|
|
|
|
|
|
logger.info("Finished updating analytics counts through %s" % (fill_to_time,))
|