<commit_before>from __future__ import absolute_import
from __future__ import print_function

from django.core.management.base import BaseCommand
from django.utils.timezone import now as timezone_now
from typing import Any, Dict, List

from zerver.models import UserPresence, UserActivity
from zerver.lib.utils import statsd, statsd_key

from datetime import datetime, timedelta
from collections import defaultdict

class Command(BaseCommand):
    help = """Sends active user statistics to statsd.

    Run as a cron job that runs every 10 minutes."""

    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        # Get list of all active users in the last 1 week
        cutoff = timezone_now() - timedelta(minutes=30, hours=168)

        users = UserPresence.objects.select_related().filter(timestamp__gt=cutoff)

        # Calculate 10min, 2hrs, 12hrs, 1day, 2 business days (TODO business days), 1 week bucket of stats
        hour_buckets = [0.16, 2, 12, 24, 48, 168]
        user_info = defaultdict(dict) # type: Dict[str, Dict[float, List[str]]]

        for last_presence in users:
            if last_presence.status == UserPresence.IDLE:
                known_active = last_presence.timestamp - timedelta(minutes=30)
            else:
                known_active = last_presence.timestamp<commit_msg>pep8: Add compliance with rule E261 to active_user_stats.py.<commit_after>from __future__ import absolute_import
from __future__ import print_function

from django.core.management.base import BaseCommand
from django.utils.timezone import now as timezone_now
from typing import Any, Dict, List

from zerver.models import UserPresence, UserActivity
from zerver.lib.utils import statsd, statsd_key

from datetime import datetime, timedelta
from collections import defaultdict

class Command(BaseCommand):
    help = """Sends active user statistics to statsd.

    Run as a cron job that runs every 10 minutes."""

    def handle(self, *args, **options):
        # type: (*Any, **Any) -> None
        # Get list of all active users in the last 1 week
        cutoff = timezone_now() - timedelta(minutes=30, hours=168)

        users = UserPresence.objects.select_related().filter(timestamp__gt=cutoff)

        # Calculate 10min, 2hrs, 12hrs, 1day, 2 business days (TODO business days), 1 week bucket of stats
        hour_buckets = [0.16, 2, 12, 24, 48, 168]
        user_info = defaultdict(dict)  # type: Dict[str, Dict[float, List[str]]]

        for last_presence in users:
            if last_presence.status == UserPresence.IDLE:
                known_active = last_presence.timestamp - timedelta(minutes=30)
            else:
                known_active = last_presence.timestamp
{'fraction_non_alphanumeric': 0.08204562178072111, 'fraction_numerical': 0.024282560706401765, 'mean_word_length': 3.3784219001610305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5319139', 'n_tokens_mistral': 855, 'n_tokens_neox': 766, 'n_words': 264}