2016-10-05 03:38:20 +02:00
|
|
|
import os
|
2017-04-28 23:28:48 +02:00
|
|
|
import time
|
2017-11-16 00:55:49 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from typing import Any, Dict
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2017-11-16 00:55:49 +01:00
|
|
|
from django.conf import settings
|
2016-07-29 21:52:45 +02:00
|
|
|
from django.core.management.base import BaseCommand
|
2017-11-16 00:55:49 +01:00
|
|
|
from django.utils.dateparse import parse_datetime
|
2017-04-15 04:03:56 +02:00
|
|
|
from django.utils.timezone import now as timezone_now
|
2017-04-15 03:29:56 +02:00
|
|
|
from django.utils.timezone import utc as timezone_utc
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2017-02-02 01:53:45 +01:00
|
|
|
from analytics.lib.counts import COUNT_STATS, logger, process_count_stat
|
2017-11-16 00:55:49 +01:00
|
|
|
from scripts.lib.zulip_tools import ENDC, WARNING
|
2017-04-28 01:26:50 +02:00
|
|
|
from zerver.lib.timestamp import floor_to_hour
|
2017-11-02 13:06:44 +01:00
|
|
|
from zerver.models import Realm
|
2016-07-29 21:52:45 +02:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Fills Analytics tables.
|
|
|
|
|
|
|
|
Run as a cron job that runs every hour."""
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def add_arguments(self, parser: 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,
|
2017-11-04 12:38:25 +01:00
|
|
|
help='Update stat tables from current state to'
|
|
|
|
'--time. Defaults to the current time.',
|
2017-04-15 04:03:56 +02:00
|
|
|
default=timezone_now().isoformat())
|
2016-07-29 21:52:45 +02:00
|
|
|
parser.add_argument('--utc',
|
2017-04-28 23:50:21 +02:00
|
|
|
action='store_true',
|
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-04-28 23:28:48 +02:00
|
|
|
parser.add_argument('--verbose',
|
|
|
|
action='store_true',
|
|
|
|
help="Print timing information to stdout.",
|
|
|
|
default=False)
|
2016-07-29 21:52:45 +02:00
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def handle(self, *args: Any, **options: 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)
|
|
|
|
|
2017-11-05 06:54:00 +01:00
|
|
|
def run_update_analytics_counts(self, options: Dict[str, Any]) -> None:
|
2017-10-05 03:47:50 +02:00
|
|
|
# installation_epoch relies on there being at least one realm; we
|
|
|
|
# shouldn't run the analytics code if that condition isn't satisfied
|
|
|
|
if not Realm.objects.exists():
|
|
|
|
logger.info("No realms, stopping update_analytics_counts")
|
|
|
|
return
|
2017-04-28 01:26:50 +02:00
|
|
|
|
2017-10-05 03:47:50 +02:00
|
|
|
fill_to_time = parse_datetime(options['time'])
|
2016-10-05 05:43:19 +02:00
|
|
|
if options['utc']:
|
2017-04-15 03:29:56 +02:00
|
|
|
fill_to_time = fill_to_time.replace(tzinfo=timezone_utc)
|
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-04-28 01:26:50 +02:00
|
|
|
fill_to_time = floor_to_hour(fill_to_time.astimezone(timezone_utc))
|
|
|
|
|
2016-10-05 05:43:19 +02:00
|
|
|
if options['stat'] is not None:
|
2017-04-28 23:28:48 +02:00
|
|
|
stats = [COUNT_STATS[options['stat']]]
|
2016-07-29 21:52:45 +02:00
|
|
|
else:
|
2017-04-28 23:28:48 +02:00
|
|
|
stats = list(COUNT_STATS.values())
|
2017-02-02 01:53:45 +01:00
|
|
|
|
2017-04-28 23:28:48 +02:00
|
|
|
logger.info("Starting updating analytics counts through %s" % (fill_to_time,))
|
|
|
|
if options['verbose']:
|
|
|
|
start = time.time()
|
|
|
|
last = start
|
|
|
|
|
|
|
|
for stat in stats:
|
|
|
|
process_count_stat(stat, fill_to_time)
|
|
|
|
if options['verbose']:
|
|
|
|
print("Updated %s in %.3fs" % (stat.property, time.time() - last))
|
|
|
|
last = time.time()
|
|
|
|
|
|
|
|
if options['verbose']:
|
|
|
|
print("Finished updating analytics counts through %s in %.3fs" %
|
|
|
|
(fill_to_time, time.time() - start))
|
2017-02-02 01:53:45 +01:00
|
|
|
logger.info("Finished updating analytics counts through %s" % (fill_to_time,))
|