def get_syrx_data(config):
    """
    Gets the data for the syrx number in config and returns a kendo-formatted object of the data
    :param config: EquipmentReport
    :return:
    """
    report_series_dict = {}
    benchmark_series_dict = {}

    if len(config.syrx_nums) > 1:
        target_unit = "mmbtu"
    else:
        target_unit = None

    for syrx_num in config.syrx_nums:
        # get the equipment point
        ep = g.uow.equipment.get_equipment_point_by_syrx_num(syrx_num)

        if not target_unit:
            target_unit = ep['units'].lower()

        conversion_factor = SharedReporting.get_component_point_conversion(ep['units'].lower(), 'mmbtu')

        syrx_series = get_consumption_data_for_year(syrx_num, config.report_year, config.start_month, config.end_month,
                                                    conversion_factor)
        # add to the existing series
        add_to_new_series_data(report_series_dict, syrx_series[0]['data'])

        syrx_series_benchmark = get_consumption_data_for_year(syrx_num, config.comparison_year, config.start_month,
                                                              config.end_month, conversion_factor)

        # add to the existing series
        add_to_new_series_data(benchmark_series_dict, syrx_series_benchmark[0]['data'])

    report_difference_list, benchmark_difference_list = calculate_difference_equipment_report(report_series_dict,
                                                                                              benchmark_series_dict)

    consumption_series_list = convert_dict_to_list(report_series_dict)
    consumption_series_list[0]['name'] = 'Reported Consumption'
    benchmark_series_list = convert_dict_to_list(benchmark_series_dict)
    consumption_series_list += benchmark_series_list
    consumption_series_list[1]['name'] = 'Benchmark Consumption'

    difference_series_list = convert_dict_to_list(report_difference_list)
    difference_series_list[0]['name'] = 'Reported Consumption'
    difference_series_list += convert_dict_to_list(benchmark_difference_list)
    difference_series_list[1]['name'] = 'Benchmark Consumption'

    chart_info = ComponentDifferenceDataContainer()
    chart_info.component_description = ""
    chart_info.title = "Intensity"
    chart_info.consumption_data = consumption_series_list
    chart_info.difference_data = difference_series_list
    chart_info.x_axis_label = "Temperature (F)"
    chart_info.y_axis_label = "Energy Consumption (" + SharedReporting.get_y_unit_label(target_unit.lower()) + ")"
    return chart_info
def get_comparison_chart_data(config):
    """
    Gets a list of the series used for the comparison report charts
    :param config: ComponentReportingComparisonConfiguration
    :return: TotalEnergyDataContainer that holds all information for the chart
    """
    series_list = []
    for entity in config.component_ids:
        # get label from the entity
        label = get_label_for_entity(entity)

        # get actual component_id from the list
        syrx_nums = get_syrx_nums_from_entity(entity)

        # combine the comparison year with the historical mode years
        all_years = list(set([config.comparison_year] + config.historical_years))
        for year in all_years:
            new_series_data = {}
            for syrx_num in syrx_nums:
                # get the equipment point by syrx num
                ep = g.uow.equipment.get_equipment_point_by_syrx_num(syrx_num)

                # get the series data for each point
                syrx_series = g.uow.compiled_point_records.get_data_for_syrx_nums([syrx_num], year, 1, 12)

                if len(syrx_series) > 0:
                    # convert the data to the new values
                    conversion_factor = SharedReporting.get_component_point_conversion(ep['units'].lower(), config.unit)

                    convert_data_to_btu_unit(syrx_series[0]['data'], conversion_factor)

                    # add to the new_series_data
                    add_to_new_series_data(new_series_data, syrx_series[0]['data'])

            if len(new_series_data) > 0:
                new_series_data_list = []
                # change the data from a dictionary to list
                for key in sorted(new_series_data.keys()):
                    new_series_data_list.append([key, new_series_data[key]])

                # overwrite the point name with the component name
                new_series = [{"name": None,
                               "data": new_series_data_list}]
                new_series[0]["name"] = label + " - " + str(year)

                series_list += new_series

    chart_info = TotalEnergyDataContainer()
    chart_info.title = "Intensity"
    chart_info.data = series_list
    chart_info.x_axis_label = "Temperature (F)"
    chart_info.y_axis_label = "Energy Consumption (" + SharedReporting.get_y_unit_label(config.unit) + ")"

    return chart_info
def get_comparison_table_data(config, year):
    """
    Gets the rows for the data table after the chart
    :param config: ComponentReportingComparisonConfiguration
    :param year: year for the report
    :return: list of rows
    """
    rows = []
    row_dict_list = []
    bic_syrx_num = None
    bic_value = None
    bic_hours = None
    bic_ppsn = None

    syrx_nums_used = set()

    # get price for reporting group
    price = get_comparison_price('btu', config.submitted_to)

    for entity in config.component_ids:
        # since the application allows for groups, equipment, and equipment points to be selected, we need to get
        # the syrx nums for whatever the current reporting entity is.

        # only get the syrx nums which haven't been used yet
        syrx_nums = get_syrx_nums_from_entity(entity)
        syrx_nums = list(set(syrx_nums) - syrx_nums_used)

        # update list of used syrx nums
        syrx_nums_used |= set(syrx_nums)

        for syrx_num in syrx_nums:
            # get PPSN for the entity
            component_values = g.uow.compiled_point_records.get_ppsn([syrx_num], year)

            # convert the values
            units = g.uow.equipment.get_equipment_point_by_syrx_num(syrx_num)['units']
            conversion_factor = SharedReporting.get_component_point_conversion(units, config.unit)

            component_values['sum_value'] *= conversion_factor
            component_values['ppsn'] *= conversion_factor

            # add a dictionary of row data to the row_dict
            row_dict = construct_row(syrx_num, component_values, config.unit)

            row_dict['conversion_factor'] = conversion_factor

            # set the bic value if it has not yet been set
            if not bic_value:
                bic_syrx_num = syrx_num
                bic_ppsn = row_dict['ppsn']
                bic_value = row_dict['consumption']
                bic_hours = row_dict['hours']
            elif row_dict['ppsn'] < bic_ppsn:
                bic_syrx_num = syrx_num
                bic_ppsn = row_dict['ppsn']
                bic_value = row_dict['consumption']
                bic_hours = row_dict['hours']

            row_dict_list.append(row_dict)

    # using the bic_syrx_num, find the difference for each other component
    for row in row_dict_list:
        new_row = calculate_final_row_values(bic_syrx_num, bic_value, bic_hours, row, price)
        # update final rows value
        rows.append(new_row)

    return rows
def get_equipment_report_table(config):
    # get the reporting points (that can be converted to btu)
    equipment_points = g.uow.equipment.get_equipment_reporting_points(config.equipment_id)

    ret_obj = {
        'report_consumption_month': 0,
        'benchmark_consumption_month': 0,
        'report_consumption_year': 0,
        'benchmark_consumption_year': 0,
        'difference_month': 0,
        'difference_year': 0,
        'percent_month': 0,
        'percent_year': 0,
        'cost_month': 0,
        'cost_year': 0
    }

    # get the price for calculating cost savings
    price = find_price_for_equipment_points('mmbtu', config.equipment_id)

    # if they are all the same, find the price/unit
    for equipment_point in equipment_points:
        ep = g.uow.equipment.get_equipment_point_by_syrx_num(equipment_point['id'])
        conversion_factor = SharedReporting.get_component_point_conversion(ep['units'].lower(), 'mmbtu')

        temp_obj = calculate_totals_for_report(config, ep['units'], price, ep, conversion_factor)

        # update the ret_obj with the values
        ret_obj['report_consumption_month'] += temp_obj['report_consumption_month']
        ret_obj['benchmark_consumption_month'] += temp_obj['benchmark_consumption_month']
        ret_obj['report_consumption_year'] += temp_obj['report_consumption_year']
        ret_obj['benchmark_consumption_year'] += temp_obj['benchmark_consumption_year']
        ret_obj['cost_month'] += temp_obj['cost_month']
        ret_obj['cost_year'] += temp_obj['cost_year']

    # calculate final difference and percent difference
    ret_obj['difference_month'] = ret_obj['benchmark_consumption_month'] - ret_obj['report_consumption_month']
    ret_obj['difference_year'] = ret_obj['benchmark_consumption_year'] - ret_obj['report_consumption_year']

    if ret_obj['benchmark_consumption_month'] == 0:
        ret_obj['percent_month'] = "{:.2f}".format(0.0)
    else:
        ret_obj['percent_month'] = "{:.2f}".format((ret_obj['difference_month'] * 1.0 /
                                                    ret_obj['benchmark_consumption_month']) * 100.0)

    if ret_obj['benchmark_consumption_year'] == 0:
        ret_obj['percent_year'] = "{:.2f}".format(0.0)
    else:
        ret_obj['percent_year'] = "{:.2f}".format((ret_obj['difference_year'] * 1.0 /
                                                   ret_obj['benchmark_consumption_year']) * 100.0)

    # format the cost savings
    ret_obj['cost_month'] = "${:,.2f}".format(ret_obj['cost_month'])
    ret_obj['cost_year'] = "${:,.2f}".format(ret_obj['cost_year'])

    # format consumption numbers
    ret_obj['report_consumption_month'] = "{:,.2f}".format(ret_obj['report_consumption_month'])
    ret_obj['report_consumption_year'] = "{:,.2f}".format(ret_obj['report_consumption_year'])
    ret_obj['benchmark_consumption_month'] = "{:,.2f}".format(ret_obj['benchmark_consumption_month'])
    ret_obj['benchmark_consumption_year'] = "{:,.2f}".format(ret_obj['benchmark_consumption_year'])
    ret_obj['difference_month'] = "{:,.2f}".format(ret_obj['difference_month'])
    ret_obj['difference_year'] = "{:,.2f}".format(ret_obj['difference_year'])

    return json.dumps(ret_obj)