The given code can be modified to process the data into the desired format as follows:

try:
    rewards = query.all()
    if not rewards:
        return result
    # Get the first point's time
    current_time = format_datetime_unicode(rewards[0].coming_time)
    # Generate a new interval based on the actual data range
    # interval = fw_device_healthy_moniter.get_format_suitable_interval(current_time,rewards[-1].coming_time,interval)
    result['interval'] = interval
    span = fw_device_healthy_moniter.TIME_SPAN[interval]
    prev_time = current_time
    for data in rewards:
        general_data = data.resource_general
        res_gnl = general_data['resource_general']
        if not general_data:
            continue
        usage_data = res_gnl['{0}_usage'.format(msg_type)]
        # Get the current point's time
        next_time = data.coming_time
        # Get the time difference
        timedelta_seconds = math.ceil((next_time - current_time).total_seconds())
        prev_timedelta_seconds = math.ceil((next_time - prev_time).total_seconds())
        # Include the point data if it satisfies the time difference condition
        if (prev_timedelta_seconds >= 120) or (timedelta_seconds >= span * 50 and timedelta_seconds < 120 * span):
            # Format the next_time
            next_time = format_datetime_unicode(next_time)
            data_dict = {
                msg_type: usage_data,
                'coming_time': '{0}'.format(next_time)
            }
            if msg_type == 'disk':
                data_dict['data_disk_usage'] = res_gnl.get('data_disk_usage', '')
            elif msg_type == 'cpu':
                data_dict['cpu_dp_avg_usage'] = res_gnl.get('cpu_dp_avg_usage', '')
                data_dict['cpu_manage_usage'] = res_gnl.get('cpu_manage_usage', '')
            elif msg_type == 'mem':
                data_dict['mem_sys_usage'] = res_gnl.get('mem_sys_usage', '')
                data_dict['mem_config_usage'] = res_gnl.get('mem_config_usage', '')
                data_dict['mem_app_usage'] = res_gnl.get('mem_app_usage', '')
            result['data'].append(data_dict)
            # Update current_time and prev_time
            current_time = next_time
            prev_time = next_time
except Exception as e:
    print(str(e))

This code will process the given data into a list of dictionaries where each dictionary represents a data point with the required fields 'cpu_manage_usage', 'cpu_usage', 'cpu_dp_avg_usage', and 'coming_time'. The 'coming_time' field is formatted as a string in the desired format.

Python Data Processing: Formatting Data Points with Time Intervals

原文地址: https://www.cveoy.top/t/topic/bl6h 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录