<commit_before>import logging

from datetime import timedelta
from django.conf import settings
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.utils import timezone

from goals.models import (
    BehaviorProgress,
    GoalProgress,
    CategoryProgress,
    UserBehavior
)

logger = logging.getLogger("loggly_logs")


class Command(BaseCommand):
    help = (
        'Update/creates BehaviorProgress, GoalProgress, & CategoryProgress. '
        'Run Nightly'
    )

    def handle(self, *args, **options):
        User = get_user_model()

        err_msg = "Failed to generate scores for {0}"
        bp_created = 0
        bp_count = 0
        gp_count = 0  # for aggregate action info

        gp_scores_count = 0  # for aggreage behavior info
        cp_scores_count = 0

        # BehaviorProgress was originally created _only_ when a user did the
        # daily checking. However, now that it's used to automatically aggregate
        # action completions, we need to create them automatically for the user
        # every day.
        today = timezone.now().date()
        for ub in UserBehavior.objects.all():
            try:
                bp = BehaviorProgress.objects.get(
                    user=ub.user,
                    user_behavior=ub,
                    reported_on__contains=today
                )
            except BehaviorProgress.DoesNotExist:
                # We need to create one.
                BehaviorProgress.objects.create(
                    user=ub.user,
                    user_behavior=ub,
                    status=BehaviorProgress.OFF_COURSE
                )
                bp_created += 1
            except (BehaviorProgress.MultipleObjectsReturned, Exception):
                pass

        try:
            # Update BehaviorProgress instances in some time period, so they
            # re-calculate stats for completed actions
            date = timezone.now() - timedelta(days=settings.PROGRESS_HISTORY_DAYS)
            for bp in BehaviorProgress.objects.filter(reported_on__gte=date):
                bp.save()  # Calls BehaviorProgress._calculate_action_progress
                bp_count += 1
        except Exception:
            logger.error(err_msg.format("BehaviorProgress"), exc_info=1)

        try:
            # Generate scores for goal progress for all users that have
            # selected one or more Goals.
            for user in User.objects.filter(usergoal__isnull=False).distinct():
                objs = GoalProgress.objects.generate_scores(user)
                gp_scores_count += objs.count()
            logger.info("Generated scores for GoalProgress")
        except Exception:
            logger.error(err_msg.format("GoalProgress"), exc_info=1)

        try:
            # Re-calculate action progress stats for GoalProgress objects over
            # the history range in which we're interested.
            dt = timezone.now() - timedelta(days=settings.PROGRESS_HISTORY_DAYS)<commit_msg>updating comments in the aggregate_progress command<commit_after>import logging

from datetime import timedelta
from django.conf import settings
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.utils import timezone

from goals.models import (
    BehaviorProgress,
    GoalProgress,
    CategoryProgress,
    UserBehavior
)

logger = logging.getLogger("loggly_logs")


class Command(BaseCommand):
    help = (
        'Update/creates BehaviorProgress, GoalProgress, & CategoryProgress. '
        'Run Nightly'
    )

    def handle(self, *args, **options):
        User = get_user_model()

        err_msg = "Failed to generate scores for {0}"
        bp_created = 0
        bp_count = 0
        gp_count = 0  # for aggregate action info

        gp_scores_count = 0  # for aggreage behavior info
        cp_scores_count = 0

        # BehaviorProgress was originally created _only_ when a user did the
        # daily checking. However, now that it's used to automatically aggregate
        # action completions, we need to create them automatically for the user
        # every day.
        today = timezone.now().date()
        for ub in UserBehavior.objects.all():
            try:
                bp = BehaviorProgress.objects.get(
                    user=ub.user,
                    user_behavior=ub,
                    reported_on__contains=today
                )
            except BehaviorProgress.DoesNotExist:
                # We need to create one.
                BehaviorProgress.objects.create(
                    user=ub.user,
                    user_behavior=ub,
                    status=BehaviorProgress.OFF_COURSE
                )
                bp_created += 1
            except (BehaviorProgress.MultipleObjectsReturned, Exception):
                pass

        try:
            # Update BehaviorProgress instances in some time period, so they
            # re-calculate stats for completed actions
            date = timezone.now() - timedelta(days=settings.PROGRESS_HISTORY_DAYS)
            for bp in BehaviorProgress.objects.filter(reported_on__gte=date):
                bp.save()  # Calls BehaviorProgress._calculate_action_progress
                bp_count += 1
        except Exception:
            logger.error(err_msg.format("BehaviorProgress"), exc_info=1)

        try:
            # Generate scores for goal progress for all users that have
            # selected one or more Goals.
            for user in User.objects.filter(usergoal__isnull=False).distinct():
                objs = GoalProgress.objects.generate_scores(user)
                gp_scores_count += objs.count()
            logger.info("Generated scores for GoalProgress")
        except Exception:
            logger.error(err_msg.format("GoalProgress"), exc_info=1)

        try:
            # Re-calculate action progress stats, and daily/weekly/monthly
            # checkin values for GoalProgress objects over the history range in
            # which we're interested.
            dt = timezone.now() - timedelta(days=settings.PROGRESS_HISTORY_DAYS)
{'fraction_non_alphanumeric': 0.06778011999351387, 'fraction_numerical': 0.0032430679422733905, 'mean_word_length': 2.0519544779811976, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2403217', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1520, 'n_words': 438}