libs/file_processing/data_qty_stats.py
def calculate_data_quantity_stats(
        participant: Participant,
        earliest_time_bin_number: Optional[int] = None,
        latest_time_bin_number: Optional[int] = None,
):
    """ Update the SummaryStatisticDaily  stats for a participant, using ChunkRegistry data
    earliest_time_bin_number -- expressed in hours since 1/1/1970
    latest_time_bin_number -- expressed in hours since 1/1/1970 """
    study_timezone = participant.study.timezone
    query = ChunkRegistry.objects.filter(participant=participant)

    # Filter by date range
    if earliest_time_bin_number is not None:
        start_datetime = datetime.utcfromtimestamp(earliest_time_bin_number * CHUNK_TIMESLICE_QUANTUM)
        # Round down to the beginning of the included day, in the study's timezone
        start_date = start_datetime.astimezone(study_timezone).date()
        query = query.filter(time_bin__gte=_utc_datetime_of_local_midnight_date(start_date, study_timezone))
    if latest_time_bin_number is not None:
        end_datetime = datetime.utcfromtimestamp(latest_time_bin_number * CHUNK_TIMESLICE_QUANTUM)
        # Round up to the beginning of the next day, in the study's timezone
        end_date = end_datetime.astimezone(study_timezone).date() + timedelta(days=1)
        query = query.filter(time_bin__lt=_utc_datetime_of_local_midnight_date(end_date, study_timezone))

    daily_data_quantities = defaultdict(lambda: defaultdict(int))
    # Construct a dict formatted like this: dict[date][data_type] = total_bytes
    for chunkregistry in query.values_list('time_bin', 'data_type', 'file_size'):
        day = chunkregistry[0].astimezone(study_timezone).date()
        filesize = chunkregistry[2]
        if filesize is None:
            filesize = 0
        daily_data_quantities[day][chunkregistry[1]] += filesize
    # For each date, create a DataQuantity object
    for day, day_data in daily_data_quantities.items():
        data_quantity = {
            "participant": participant,
            "date": day,
            "defaults": {}
        }
        for data_type, total_bytes in day_data.items():
            if data_type in ALL_DATA_STREAMS:
                data_quantity["defaults"][f"{data_type}_bytes"] = total_bytes
        SummaryStatisticDaily.objects.update_or_create(**data_quantity)
 Update the SummaryStatisticDaily  stats for a participant, using ChunkRegistry data
    earliest_time_bin_number -- expressed in hours since 1/1/1970
    latest_time_bin_number -- expressed in hours since 1/1/1970 
Update the SummaryStatisticDaily  stats for a participant, using ChunkRegistry data
earliest_time_bin_number -- expressed in hours since 1/1/1970
latest_time_bin_number -- expressed in hours since 1/1/1970
Update the SummaryStatisticDaily stats for a participant using ChunkRegistry data earliest_time_bin_number -- expressed in hours since 1 / 1 / 1970 latest_time_bin_number -- expressed in hours since 1 / 1 / 1970
def calculate_data_quantity_stats(
        participant: Participant,
        earliest_time_bin_number: Optional[int] = None,
        latest_time_bin_number: Optional[int] = None,
):
    study_timezone = participant.study.timezone
    query = ChunkRegistry.objects.filter(participant=participant)
    if earliest_time_bin_number is not None:
        start_datetime = datetime.utcfromtimestamp(earliest_time_bin_number * CHUNK_TIMESLICE_QUANTUM)
        start_date = start_datetime.astimezone(study_timezone).date()
        query = query.filter(time_bin__gte=_utc_datetime_of_local_midnight_date(start_date, study_timezone))
    if latest_time_bin_number is not None:
        end_datetime = datetime.utcfromtimestamp(latest_time_bin_number * CHUNK_TIMESLICE_QUANTUM)
        end_date = end_datetime.astimezone(study_timezone).date() + timedelta(days=1)
        query = query.filter(time_bin__lt=_utc_datetime_of_local_midnight_date(end_date, study_timezone))
    daily_data_quantities = defaultdict(lambda: defaultdict(int))
    for chunkregistry in query.values_list('time_bin', 'data_type', 'file_size'):
        day = chunkregistry[0].astimezone(study_timezone).date()
        filesize = chunkregistry[2]
        if filesize is None:
            filesize = 0
        daily_data_quantities[day][chunkregistry[1]] += filesize
    for day, day_data in daily_data_quantities.items():
        data_quantity = {
            "participant": participant,
            "date": day,
            "defaults": {}
        }
        for data_type, total_bytes in day_data.items():
            if data_type in ALL_DATA_STREAMS:
                data_quantity["defaults"][f"{data_type}_bytes"] = total_bytes
        SummaryStatisticDaily.objects.update_or_create(**data_quantity)
def calculate_data_quantity_stats ( participant : Participant , earliest_time_bin_number : Optional [ int ] = None , latest_time_bin_number : Optional [ int ] = None , ) : study_timezone = participant . study . timezone query = ChunkRegistry . objects . filter ( participant = participant ) if earliest_time_bin_number is not None : start_datetime = datetime . utcfromtimestamp ( earliest_time_bin_number * CHUNK_TIMESLICE_QUANTUM ) start_date = start_datetime . astimezone ( study_timezone ) . date ( ) query = query . filter ( time_bin__gte = _utc_datetime_of_local_midnight_date ( start_date , study_timezone ) ) if latest_time_bin_number is not None : end_datetime = datetime . utcfromtimestamp ( latest_time_bin_number * CHUNK_TIMESLICE_QUANTUM ) end_date = end_datetime . astimezone ( study_timezone ) . date ( ) + timedelta ( days = 1 ) query = query . filter ( time_bin__lt = _utc_datetime_of_local_midnight_date ( end_date , study_timezone ) ) daily_data_quantities = defaultdict ( lambda : defaultdict ( int ) ) for chunkregistry in query . values_list ( 'time_bin' , 'data_type' , 'file_size' ) : day = chunkregistry [ 0 ] . astimezone ( study_timezone ) . date ( ) filesize = chunkregistry [ 2 ] if filesize is None : filesize = 0 daily_data_quantities [ day ] [ chunkregistry [ 1 ] ] += filesize for day , day_data in daily_data_quantities . items ( ) : data_quantity = { "participant" : participant , "date" : day , "defaults" : { } } for data_type , total_bytes in day_data . items ( ) : if data_type in ALL_DATA_STREAMS : data_quantity [ "defaults" ] [ f"{data_type}_bytes" ] = total_bytes SummaryStatisticDaily . objects . update_or_create ( ** data_quantity )
Update the SummaryStatisticDaily  stats for a participant, using ChunkRegistry data
earliest_time_bin_number -- expressed in hours since 1/1/1970
latest_time_bin_number -- expressed in hours since 1/1/1970
Update the SummaryStatisticDaily stats for a participant using ChunkRegistry data earliest_time_bin_number -- expressed in hours since 1 / 1 / 1970 latest_time_bin_number -- expressed in hours since 1 / 1 / 1970
""" Update the SummaryStatisticDaily  stats for a participant, using ChunkRegistry data
    earliest_time_bin_number -- expressed in hours since 1/1/1970
    latest_time_bin_number -- expressed in hours since 1/1/1970 """ # Filter by date range # Round down to the beginning of the included day, in the study's timezone # Round up to the beginning of the next day, in the study's timezone # Construct a dict formatted like this: dict[date][data_type] = total_bytes # For each date, create a DataQuantity object
participant
Participant
earliest_time_bin_number
Optional[int]
latest_time_bin_number
Optional[int]