def insight_builder(insights): # search only needed keys # make copy of each Insight and create each insight m_keys = ( "metric_name", "api", "report_name", "objective", "unit", "currency", "id", "validator_insight_type", ) all_keys = {} cop = [] if isinstance(insights, dict): if "metric_name" in insights.keys(): for key, value in insights.items(): if key in m_keys: all_keys[key] = value cop = copy.deepcopy(all_keys) return print(cop) elif isinstance(insights, list): for insight in insights: insight_builder(insight)
def first_check(insights): if isinstance(insights, dict): for key, value in insights.items(): if key == METR_SUMS: for i in map(dict, value): return print(f"The sums M_S is {i['sum'] + i['sum_level'] + i['sum_general']}, " f"and average M_S is ") else: first_check(value) elif isinstance(insights, list): for insight in insights: first_check(insight)
def all_in(insights): if isinstance(insights, dict): for key, value in insights.items(): if key == "entities_affected": in_ent_aff(value) else: all_in(value) if isinstance(insights, list): for insight in insights: all_in(insight) return insights
def all_ins_o_in_dict(insights): result = [] mix = {} if isinstance(insights, dict): for key, value in insights.items(): if key == OBJ_DICT: mix[key] = value result.append(mix) return print(result) elif isinstance(insights, list): for insight in insights: all_ins_o_in_dict(insight)
def replace_it(insights): if isinstance(insights, dict): copy_insights = insights.copy() for key, value in insights.items(): if key == "report_name" and value == "device": change = {"report_name": "DEVICE"} copy_insights.update(change) else: replace_it(value) return print(copy_insights) elif isinstance(insights, list): for insight in insights: replace_it(insight)
def calculate_summary(insights): if isinstance(insights, dict): copy_insights = insights.copy() for key, value in insights.items(): if key == "page_id" and value == "(not set)": change = {"page_id": "None"} copy_insights.update(change) else: calculate_summary(value) return print(copy_insights) elif isinstance(insights, list): for insight in insights: calculate_summary(insight)
def get_all_insobj(insights): result_bj = [] if isinstance(insights, dict): for key, value in insights.items(): if key == OBJ_KEY: result_bj.append(key) return print(f"{result_bj}") # HOW can do _1, _2 ... >?????? else: get_all_insobj(value) elif isinstance(insights, list): for insight in insights: get_all_insobj(insight)
def all_uniq(insights): result = [] result1 = {} if isinstance(insights, dict): for key, value in insights.items(): if key == CAM_ID: result1[key] = value result.append(result1) return print(result1) else: all_uniq(value) elif isinstance(insights, list): for insight in insights: all_uniq(insight)
def remove_keys(insights): if isinstance(insights, dict): copy_insights = insights.copy() for key, value in insights.items(): if key in UNIT_KEYS: if value != "EUR": del copy_insights[key] else: remove_keys(value) return print(copy_insights) elif isinstance(insights, list): for insight in insights: remove_keys(insight)
def summary(insights): if isinstance(insights, dict): for key, value in insights.items(): try: if key == 'period' and value > 4: insights[key] = 7 elif key == 'period' and value is None: insights[key] = 7 if key == 'metric_sums': for val in value: if val['sum_general'] != 0: if insights['api'] == 1: val[ 'summary'] = (val['sum'] * val['sum_level'] / val['sum_general']) / insights['period'] elif insights['api'] == 2: val[ 'summary'] = (val['sum'] * val['sum_level'] ** 2 / val['sum_general']) / insights['period'] elif insights['api'] == 3: val[ 'summary'] = (val['sum_level'] / val['sum_general']) / insights['period'] elif insights['api'] == 4: val['summary'] = f"{(val['sum_level'] * 100) / insights['period']}" if key == "metric_sums": for values in value: if values["sum_general"] != 0: if insights["api"] == 1: values[ "summary" ] = (values['sum'] * values['sum_level'] / values['sum_general']) / insights['period'] elif insights["api"] == 2: values[ "summary" ] = (values['sum'] * values['sum_level'] ** 2 / values['sum_general']) / insights['period'] elif insights["api"] == 3: values[ "summary" ] = f"{(values['sum_level'] / values['sum_general']) / insights['period']}" elif insights["api"] == 4: values[ "summary" ] = f"{(values['sum_level'] * 100) / insights['period']}" if insights['api'] == 1: values['summary'] = \ (values['sum'] * values['sum_level'] / values['sum_general']) / insights['period'] elif insights['api'] == 2: values[ 'summary'] = (values['sum'] * values['sum_level'] ** 2 / values['sum_general']) / insights['period'] elif insights['api'] == 3: values[ 'summary'] = (values['sum_level'] / values['sum_general']) / insights['period'] elif insights['api'] == 4: values['summary'] = f"{(values['sum_level'] * 100) / insights['period']}" else: values["summary"] = "Zero division problem!" return print(values) except Exception as error: print(f'Houston we have {error}') elif isinstance(insights, list): for insight in insights: summary(insight)