<commit_before>import os

from django.conf import settings
from django.core.management.base import BaseCommand
import datetime
from registration.models import RegistrationProfile

from stats.models import ValueStore


class Command(BaseCommand):
    help = 'creates bulk shifts from existing data'
    args = ""

    option_list = BaseCommand.option_list

    def handle(self, *args, **options):
        shifts = RegistrationProfile.objects.all()\
            .filter(needs__time_period_from__date_time__lte=datetime.datetime.now())
        seconds = 0.0
        for shift in shifts:
            theshift = shift.needs.all()
            for single_shift in theshift:
                deelta = single_shift.time_period_to.date_time - single_shift.time_period_from.date_time
                seconds += deelta.total_seconds()
        total_hours = int(((seconds/60)/60))
        try:
            key = ValueStore.objects.get(name="total-volunteer-hours")
            key.value = total_hours
            key.save()
        except ValueStore.DoesNotExist:
            ValueStore.objects.create(name="total-volunteer-hours", value=total_hours)




<commit_msg>Optimize worked hour calculation<commit_after># coding: utf-8

import datetime

from django.core.management.base import BaseCommand

from registration.models import RegistrationProfile
from stats.models import ValueStore


class Command(BaseCommand):
    help = 'creates bulk shifts from existing data'
    args = ""

    option_list = BaseCommand.option_list

    def handle(self, *args, **options):
        shifts = RegistrationProfile.objects.all()\
            .filter(needs__time_period_from__date_time__lte=datetime.datetime.now())\
            .prefetch_related('needs', 'needs__time_period_to', 'needs__time_period_from')\
            .only('needs__time_period_to', 'needs__time_period_from')

        total_seconds = 0.0
        for shift in shifts:
            needs_in_shift = shift.needs.all()
            for single_shift in needs_in_shift:
                delta = single_shift.time_period_to.date_time - single_shift.time_period_from.date_time
                total_seconds += delta.total_seconds()
        total_hours = int(total_seconds)/3600

        value_object, created = ValueStore.objects.get_or_create(name="total-volunteer-hours", defaults=dict(value=total_hours))
        if not created:
            value_object.value = total_hours
            value_object.save()
{'fraction_non_alphanumeric': 0.08640458640458641, 'fraction_numerical': 0.005323505323505323, 'mean_word_length': 2.978827361563518, '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': '6200003', 'n_tokens_mistral': 709, 'n_tokens_neox': 665, 'n_words': 141}