def get_intensity_data(cls, group_ids, account_type, report_year, comparison_type, demand_type): if demand_type != 'all': report_index = "has_peak" else: report_index = "no_peak" y_unit_map = "sum_btu" final_data = [] for group_id in group_ids: descendants = GroupRepository.get_descendants(group_id) accounts = [] for g in descendants: accounts += cls.get_accounts_for_group(g['id'], account_type, report_year, comparison_type, demand_type) if len(accounts) >= 1: data = Db.r.db("pathian").table("compiled_energy_records").get_all(*accounts, index=report_index).group( lambda record: {"year": record['year'], "value": record["comparison_value"]}).map( lambda record: {y_unit_map: record[y_unit_map], "sum_size_normalization": record["sum_size_normalization"]}).reduce( lambda a, b: {y_unit_map: a[y_unit_map] + b[y_unit_map], "sum_size_normalization": a["sum_size_normalization"] + b[ "sum_size_normalization"]}).ungroup().run() data_insert = [] for entry in data: data_insert.append( [entry['group']['value'], entry['reduction'][y_unit_map] / entry['reduction']['sum_size_normalization']]) final_data.append({'name': GroupRepository.get_group_by_id(group_id).name, 'data': data_insert}) return final_data
def get_total_energy_data_with_y(cls, group_id, report_year, benchmark_year, account_type, comparison_type, demand_type, y_units, y_unit_map): if demand_type != 'all': report_index = "has_peak" else: report_index = "no_peak" same_year = int(report_year) == int(benchmark_year) unit_factor = get_factor(y_units, y_unit_map) data = TotalEnergyData descendants = GroupRepository.get_descendants(group_id) accounts = [] # Get all accounts for each group for the desired year for g in descendants: accounts += cls.get_accounts_for_group(g['id'], account_type, report_year, comparison_type, demand_type) if not same_year: accounts += cls.get_accounts_for_group(g['id'], account_type, benchmark_year, comparison_type,demand_type) if len(accounts) < 1: benchmark_data = [] else: benchmark_data = Db.r.db("pathian").table("compiled_energy_records").get_all(*accounts, index=report_index).group( lambda record: {"year": record['year'], "value": record["comparison_value"]}).map( lambda record: {y_unit_map: record[y_unit_map], "sum_hours_in_record": record["sum_hours_in_record"]}).reduce( lambda a, b: {y_unit_map: a[y_unit_map] + b[y_unit_map], "sum_hours_in_record": a["sum_hours_in_record"] + b["sum_hours_in_record"]}).ungroup().run() grouped = defaultdict(list) for d in benchmark_data: grouped[d['group']['value']].append(d) reported_consumption = [] diff = [] benchmark_diff = [] benchmark_consumption = [] grouped_keys = list(grouped.keys()) grouped_keys.sort() if same_year: for value in grouped_keys: entry = grouped[value] reported_consumption.append( [value, entry[0]['reduction'][y_unit_map] * unit_factor / entry[0]['reduction']['sum_hours_in_record']] ) diff.append([value, 0]) benchmark_consumption.append( [value, entry[0]['reduction'][y_unit_map] * unit_factor / entry[0]['reduction']['sum_hours_in_record']]) benchmark_diff.append([value, 0]) else: for value in grouped_keys: entry = grouped[value] if len(entry) > 1: report_record = 0 benchmark_record = 0 for record in entry: if record['group']['year'] == int(report_year): report_record = record['reduction'][y_unit_map] * unit_factor / record['reduction'][ 'sum_hours_in_record'] reported_consumption.append([value, report_record]) elif record['group']['year'] == int(benchmark_year): benchmark_record = record['reduction'][y_unit_map] * unit_factor / record['reduction'][ 'sum_hours_in_record'] benchmark_consumption.append([value, benchmark_record]) benchmark_diff.append([value, 0]) diff.append([value, benchmark_record - report_record]) data.reported_consumption = reported_consumption data.benchmark_consumption = benchmark_consumption data.diff = diff data.benchmark_diff = benchmark_diff return data