def test_functions(self): get_switch_list() get_hangupcause_name(self.hangupcause.pk) get_hangupcause_name(2) get_hangupcause_id(self.hangupcause.code) # Template tags hangupcause_name_with_title(self.hangupcause.pk) get_country_id_prefix(['44', '442'])
def test_functions(self): get_switch_list() get_hangupcause_name(self.hangupcause.pk) get_hangupcause_name(2) get_hangupcause_id(self.hangupcause.code) # Template tags hangupcause_name_with_title(self.hangupcause.pk) get_country_id_prefix(['44', '442'])
def test_functions(self): get_switch_list() get_hangupcause_name(self.hangupcause.pk) get_hangupcause_name(2) get_hangupcause_id(self.hangupcause.code) # Template tags hangupcause_name_with_title(self.hangupcause.pk) value = {'_id': {'val': 1}} mongo_id(value, 'val') get_hc_list() get_country_id(['44', '442'])
def test_functions(self): get_switch_list() get_hangupcause_name(self.hangupcause.pk) get_hangupcause_name(2) get_hangupcause_id(self.hangupcause.code) # Template tags hangupcause_name_with_title(self.hangupcause.pk) value = {'_id': {'val': 1}} mongo_id(value, 'val') get_hc_list() get_country_id(['44', '442'])
def custom_sql_aggr_top_hangup(user, switch_id, hangup_cause_id, limit, start_date, end_date): """ perform query to retrieve and aggregate calls data by country """ result = [] total_calls = total_duration = total_billsec = total_buy_cost = total_sell_cost = 0 with connection.cursor() as cursor: sqlquery = sqlquery_aggr_hangup_cause sqlquery = sqlquery.replace("#USER_CONDITION#", condition_user(user)) sqlquery = sqlquery.replace("#SWITCH_CONDITION#", condition_switch_id(switch_id)) sqlquery = sqlquery.replace("#HANGUP_CONDITION#", condition_hangup_cause_id(hangup_cause_id)) params = { 'start_date': start_date, 'end_date': end_date, 'limit': limit, } cursor.execute(sqlquery, params) rows = cursor.fetchall() for row in rows: result.append({ "hangup_cause_id": row[0], "duration": row[1], "billsec": row[2], "nbcalls": row[3], "buy_cost": row[4], "sell_cost": row[5], "hangup_cause_name": get_hangupcause_name(row[0]), }) total_calls += row[1] total_duration += row[2] total_billsec += row[3] total_buy_cost += row[4] total_sell_cost += row[5] return (result, total_calls, total_duration, total_billsec, total_buy_cost, total_sell_cost)
def test_functions(self): get_switch_list() get_hangupcause_name(self.hangupcause.pk) get_hangupcause_name(2) get_hangupcause_id(self.hangupcause.code) # Template tags hangupcause_name_with_title(self.hangupcause.pk) value = {'_id': {'val': 1}} mongo_id(value, 'val') seen_unseen(value) seen_unseen('') seen_unseen_word(value) seen_unseen_word('')
def cdr_export_to_csv(request): """ **Logic Description**: Retrieve calls records from Postgresql according to search parameters, then export the result into CSV/XLS/JSON file """ format_type = request.GET['format'] # get the response object, this can be used as a stream response = HttpResponse(content_type='text/%s' % format_type) # force download response[ 'Content-Disposition'] = 'attachment;filename=export.%s' % format_type # get query_var from request.session start_date = request.session.get('session_start_date') end_date = request.session.get('session_end_date') start_date = ceil_strdate(start_date, 'start', True) end_date = ceil_strdate(end_date, 'end', True) if request.session.get('session_export_kwargs'): kwargs = request.session.get('session_export_kwargs') if start_date: kwargs['starting_date__gte'] = start_date if end_date: kwargs['starting_date__lte'] = end_date cdrs = CDR.objects.filter(**kwargs) headers = ('Call-date', 'CLID', 'Destination', 'Duration', 'Bill sec', 'Hangup cause', 'AccountCode', 'Direction') list_val = [] for cdr in cdrs: starting_date = str(cdr.starting_date) list_val.append(( starting_date, cdr.caller_id_number + '-' + cdr.caller_id_name, cdr.destination_number, cdr.duration, cdr.billsec, get_hangupcause_name(cdr.hangup_cause_id), cdr.accountcode, cdr.direction, )) data = tablib.Dataset(*list_val, headers=headers) if format_type == Export_choice.XLS: response.write(data.xls) elif format_type == Export_choice.CSV: response.write(data.csv) elif format_type == Export_choice.JSON: response.write(data.json) return response
def cdr_export_to_csv(request): """ **Logic Description**: Retrieve calls records from Postgresql according to search parameters, then export the result into CSV/XLS/JSON file """ format_type = request.GET['format'] # get the response object, this can be used as a stream response = HttpResponse(content_type='text/%s' % format_type) # force download response['Content-Disposition'] = 'attachment;filename=export.%s' % format_type # get query_var from request.session start_date = request.session.get('session_start_date') end_date = request.session.get('session_end_date') start_date = ceil_strdate(start_date, 'start', True) end_date = ceil_strdate(end_date, 'end', True) if request.session.get('session_export_kwargs'): kwargs = request.session.get('session_export_kwargs') if start_date: kwargs['starting_date__gte'] = start_date if end_date: kwargs['starting_date__lte'] = end_date cdrs = CDR.objects.filter(**kwargs) headers = ('Call-date', 'CLID', 'Destination', 'Duration', 'Bill sec', 'Hangup cause', 'AccountCode', 'Direction') list_val = [] for cdr in cdrs: starting_date = str(cdr.starting_date) list_val.append(( starting_date, cdr.caller_id_number + '-' + cdr.caller_id_name, cdr.destination_number, cdr.duration, cdr.billsec, get_hangupcause_name(cdr.hangup_cause_id), cdr.accountcode, cdr.direction, )) data = tablib.Dataset(*list_val, headers=headers) if format_type == Export_choice.XLS: response.write(data.xls) elif format_type == Export_choice.CSV: response.write(data.csv) elif format_type == Export_choice.JSON: response.write(data.json) return response
def hangupcause_name_with_title(id): """Tag is used to get hangupcause name with lowercase""" try: val = get_hangupcause_name(id) t = re.sub("([a-z])'([A-Z])", lambda m: m.group(0).lower(), val.title()) return re.sub("\d([A-Z])", lambda m: m.group(0).lower(), t) except: return ''
def custom_sql_aggr_top_hangup(user, switch_id, hangup_cause_id, limit, start_date, end_date): """ perform query to retrieve and aggregate calls data by country """ result = [] total_calls = total_duration = total_billsec = total_buy_cost = total_sell_cost = 0 with connection.cursor() as cursor: sqlquery = sqlquery_aggr_hangup_cause sqlquery = sqlquery.replace("#USER_CONDITION#", condition_user(user)) sqlquery = sqlquery.replace("#SWITCH_CONDITION#", condition_switch_id(switch_id)) sqlquery = sqlquery.replace("#HANGUP_CONDITION#", condition_hangup_cause_id(hangup_cause_id)) params = { 'start_date': start_date, 'end_date': end_date, 'limit': limit, } cursor.execute(sqlquery, params) rows = cursor.fetchall() for row in rows: result.append({ "hangup_cause_id": row[0], "duration": row[1], "billsec": row[2], "nbcalls": row[3], "buy_cost": row[4], "sell_cost": row[5], "hangup_cause_name": get_hangupcause_name(row[0]), }) total_calls += row[1] total_duration += row[2] total_billsec += row[3] total_buy_cost += row[4] total_sell_cost += row[5] return (result, total_calls, total_duration, total_billsec, total_buy_cost, total_sell_cost)
def cdr_dashboard(request): """CDR dashboard on the last 24 hours **Attributes**: * ``template`` - cdr/dashboard.html * ``form`` - SwitchForm **Logic Description**: Display calls aggregated information for the last 24hours, several report will be created and displayed such as hourly call report and hangup cause/country analytics. """ logging.debug('CDR dashboard view start') form = SwitchForm(request.POST or None) if form.is_valid(): logging.debug('CDR dashboard view with search option') switch_id = int(getvar(request, 'switch_id')) else: switch_id = 0 # Get list of calls/duration for each of the last 24 hours (calls_hour_aggr, total_calls, total_duration, total_billsec, total_buy_cost, total_sell_cost) = custom_sql_matv_voip_cdr_aggr_last24hours(request.user, switch_id) # Build chart data for last 24h calls (xdata, ydata, ydata2, ydata3, ydata4, ydata5) = ([], [], [], [], [], []) for i in calls_hour_aggr: start_time = (time.mktime(calls_hour_aggr[i]['calltime'].timetuple()) * 1000) xdata.append(start_time) ydata.append(int(calls_hour_aggr[i]['nbcalls'])) ydata2.append(int(calls_hour_aggr[i]['duration']/60)) ydata3.append(int(calls_hour_aggr[i]['billsec']/60)) ydata4.append(int(calls_hour_aggr[i]['buy_cost'])) ydata5.append(int(calls_hour_aggr[i]['sell_cost'])) tooltip_date = "%d %b %y %H:%M %p" extra_serie1 = {"tooltip": {"y_start": "", "y_end": " calls"}, "date_format": tooltip_date} extra_serie2 = {"tooltip": {"y_start": "", "y_end": " min"}, "date_format": tooltip_date} extra_serie3 = {"tooltip": {"y_start": "", "y_end": " min"}, "date_format": tooltip_date} extra_serie4 = {"tooltip": {"y_start": "", "y_end": ""}, "date_format": tooltip_date} extra_serie5 = {"tooltip": {"y_start": "", "y_end": ""}, "date_format": tooltip_date} kwargs1 = {} kwargs1['bar'] = True final_chartdata = { 'x': xdata, 'name1': 'Calls', 'y1': ydata, 'extra1': extra_serie1, 'kwargs1': kwargs1, 'name2': 'Duration', 'y2': ydata2, 'extra2': extra_serie2, 'name3': 'Billsec', 'y3': ydata3, 'extra3': extra_serie3, 'name4': 'Buy cost', 'y4': ydata4, 'extra4': extra_serie4, 'name5': 'Sell cost', 'y5': ydata5, 'extra5': extra_serie5, } final_charttype = "linePlusBarChart" # Get top 5 of country calls for last 24 hours country_data = custom_sql_aggr_top_country_last24hours(request.user, switch_id, limit=5) # Build pie chart data for last 24h calls per country (xdata, ydata) = ([], []) for country in country_data: xdata.append(get_country_name(country["country_id"])) ydata.append(percentage(country["nbcalls"], total_calls)) color_list = ['#FFC36C', '#FFFF9D', '#BEEB9F', '#79BD8F', '#FFB391'] extra_serie = {"tooltip": {"y_start": "", "y_end": " %"}, "color_list": color_list} country_analytic_chartdata = {'x': xdata, 'y1': ydata, 'extra1': extra_serie} country_analytic_charttype = "pieChart" country_extra = { 'x_is_date': False, 'x_axis_format': '', 'tag_script_js': True, 'jquery_on_ready': True, } # Get top 10 of hangup cause calls for last 24 hours hangup_cause_data = custom_sql_aggr_top_hangup_last24hours(request.user, switch_id) # hangup analytic pie chart data (xdata, ydata) = ([], []) for hangup_cause in hangup_cause_data: xdata.append(str(get_hangupcause_name(hangup_cause["hangup_cause_id"]))) ydata.append(str(percentage(hangup_cause["nbcalls"], total_calls))) color_list = ['#2A343F', '#7E8282', '#EA9664', '#30998F', '#449935'] extra_serie = {"tooltip": {"y_start": "", "y_end": " %"}, "color_list": color_list} hangup_analytic_chartdata = {'x': xdata, 'y1': ydata, 'extra1': extra_serie} hangup_analytic_charttype = "pieChart" hangup_extra = country_extra logging.debug("Result calls_hour_aggr %d" % len(calls_hour_aggr)) logging.debug("Result hangup_cause_data %d" % len(hangup_cause_data)) logging.debug("Result country_data %d" % len(country_data)) # Calculate the Average Time of Call metric_aggr = calculate_act_acd(total_calls, total_duration) final_extra = { 'x_is_date': True, 'x_axis_format': '%H:%M', # 'x_axis_format': '%d %b %Y', 'tag_script_js': True, 'jquery_on_ready': True, 'focus_enable': True, } logging.debug('CDR dashboard view end') variables = { 'total_calls': total_calls, 'total_duration': int_convert_to_minute(total_duration), 'total_buy_cost': total_buy_cost, 'total_sell_cost': total_sell_cost, 'metric_aggr': metric_aggr, 'country_data': country_data, 'hangup_analytic': hangup_cause_data, 'form': form, 'final_chartdata': final_chartdata, 'final_charttype': final_charttype, 'final_chartcontainer': 'final_container', 'final_extra': final_extra, 'hangup_analytic_charttype': hangup_analytic_charttype, 'hangup_analytic_chartdata': hangup_analytic_chartdata, 'hangup_chartcontainer': 'hangup_piechart_container', 'hangup_extra': hangup_extra, 'country_analytic_charttype': country_analytic_charttype, 'country_analytic_chartdata': country_analytic_chartdata, 'country_chartcontainer': 'country_piechart_container', 'country_extra': country_extra, } return render_to_response('cdr/dashboard.html', variables, context_instance=RequestContext(request))
def hangupcause_name(id): """Tag is used to get hangupcause name""" return get_hangupcause_name(id)
def cdr_dashboard(request): """CDR dashboard on the last 24 hours **Attributes**: * ``template`` - cdr/dashboard.html * ``form`` - SwitchForm **Logic Description**: Display calls aggregated information for the last 24hours, several report will be created and displayed such as hourly call report and hangup cause/country analytics. """ logging.debug('CDR dashboard view start') form = SwitchForm(request.POST or None) if form.is_valid(): logging.debug('CDR dashboard view with search option') switch_id = int(getvar(request, 'switch_id')) else: switch_id = 0 # Get list of calls/duration for each of the last 24 hours (calls_hour_aggr, total_calls, total_duration, total_billsec, total_buy_cost, total_sell_cost) = custom_sql_matv_voip_cdr_aggr_last24hours(request.user, switch_id) # Build chart data for last 24h calls (xdata, ydata, ydata2, ydata3, ydata4, ydata5) = ([], [], [], [], [], []) for i in calls_hour_aggr: start_time = (time.mktime(calls_hour_aggr[i]['calltime'].timetuple()) * 1000) xdata.append(start_time) ydata.append(int(calls_hour_aggr[i]['nbcalls'])) ydata2.append(int(calls_hour_aggr[i]['duration']/60)) ydata3.append(int(calls_hour_aggr[i]['billsec']/60)) ydata4.append(int(calls_hour_aggr[i]['buy_cost'])) ydata5.append(int(calls_hour_aggr[i]['sell_cost'])) tooltip_date = "%d %b %y %H:%M %p" extra_serie1 = {"tooltip": {"y_start": "", "y_end": " calls"}, "date_format": tooltip_date} extra_serie2 = {"tooltip": {"y_start": "", "y_end": " min"}, "date_format": tooltip_date} extra_serie3 = {"tooltip": {"y_start": "", "y_end": " min"}, "date_format": tooltip_date} extra_serie4 = {"tooltip": {"y_start": "", "y_end": ""}, "date_format": tooltip_date} extra_serie5 = {"tooltip": {"y_start": "", "y_end": ""}, "date_format": tooltip_date} kwargs1 = {} kwargs1['bar'] = True final_chartdata = { 'x': xdata, 'name1': 'Calls', 'y1': ydata, 'extra1': extra_serie1, 'kwargs1': kwargs1, 'name2': 'Duration', 'y2': ydata2, 'extra2': extra_serie2, 'name3': 'Billsec', 'y3': ydata3, 'extra3': extra_serie3, 'name4': 'Buy cost', 'y4': ydata4, 'extra4': extra_serie4, 'name5': 'Sell cost', 'y5': ydata5, 'extra5': extra_serie5, } final_charttype = "linePlusBarChart" # Get top 5 of country calls for last 24 hours country_data = custom_sql_aggr_top_country_last24hours(request.user, switch_id, limit=5) # Build pie chart data for last 24h calls per country (xdata, ydata) = ([], []) for country in country_data: xdata.append(get_country_name(country["country_id"])) ydata.append(percentage(country["nbcalls"], total_calls)) color_list = ['#FFC36C', '#FFFF9D', '#BEEB9F', '#79BD8F', '#FFB391'] extra_serie = {"tooltip": {"y_start": "", "y_end": " %"}, "color_list": color_list} country_analytic_chartdata = {'x': xdata, 'y1': ydata, 'extra1': extra_serie} country_analytic_charttype = "pieChart" country_extra = { 'x_is_date': False, 'x_axis_format': '', 'tag_script_js': True, 'jquery_on_ready': True, } # Get top 10 of hangup cause calls for last 24 hours hangup_cause_data = custom_sql_aggr_top_hangup_last24hours(request.user, switch_id) # hangup analytic pie chart data (xdata, ydata) = ([], []) for hangup_cause in hangup_cause_data: xdata.append(str(get_hangupcause_name(hangup_cause["hangup_cause_id"]))) ydata.append(str(percentage(hangup_cause["nbcalls"], total_calls))) color_list = ['#2A343F', '#7E8282', '#EA9664', '#30998F', '#449935'] extra_serie = {"tooltip": {"y_start": "", "y_end": " %"}, "color_list": color_list} hangup_analytic_chartdata = {'x': xdata, 'y1': ydata, 'extra1': extra_serie} hangup_analytic_charttype = "pieChart" hangup_extra = country_extra logging.debug("Result calls_hour_aggr %d" % len(calls_hour_aggr)) logging.debug("Result hangup_cause_data %d" % len(hangup_cause_data)) logging.debug("Result country_data %d" % len(country_data)) # Calculate the Average Time of Call metric_aggr = calculate_act_acd(total_calls, total_duration) final_extra = { 'x_is_date': True, 'x_axis_format': '%H:%M', # 'x_axis_format': '%d %b %Y', 'tag_script_js': True, 'jquery_on_ready': True, 'focus_enable': True, } logging.debug('CDR dashboard view end') variables = { 'total_calls': total_calls, 'total_duration': int_convert_to_minute(total_duration), 'total_buy_cost': total_buy_cost, 'total_sell_cost': total_sell_cost, 'metric_aggr': metric_aggr, 'country_data': country_data, 'hangup_analytic': hangup_cause_data, 'form': form, 'final_chartdata': final_chartdata, 'final_charttype': final_charttype, 'final_chartcontainer': 'final_container', 'final_extra': final_extra, 'hangup_analytic_charttype': hangup_analytic_charttype, 'hangup_analytic_chartdata': hangup_analytic_chartdata, 'hangup_chartcontainer': 'hangup_piechart_container', 'hangup_extra': hangup_extra, 'country_analytic_charttype': country_analytic_charttype, 'country_analytic_chartdata': country_analytic_chartdata, 'country_chartcontainer': 'country_piechart_container', 'country_extra': country_extra, } return render_to_response('cdr/dashboard.html', variables, context_instance=RequestContext(request))