def get_cdr_mail_report(user): """ General function to get previous day CDR report """ # Get yesterday's CDR-Stats Mail Report yesterday = date.today() - timedelta(1) start_date = trunc_date_start(yesterday) end_date = trunc_date_end(yesterday) # Build filter for CDR.object kwargs = {} if start_date: kwargs['starting_date__gte'] = start_date if end_date: kwargs['starting_date__lte'] = end_date # user are restricted to their own CDRs if not user.is_superuser: kwargs['user_id'] = user.id cdrs = CDR.objects.filter(**kwargs)[:10] # 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(user, switch_id=0) # Get top 5 of country calls for last 24 hours country_data = custom_sql_aggr_top_country_last24hours(user, switch_id=0, limit=5) # Get top 10 of hangup cause calls for last 24 hours hangup_cause_data = custom_sql_aggr_top_hangup_last24hours(user, switch_id=0) # Calculate the Average Time of Call metric_aggr = calculate_act_acd(total_calls, total_duration) mail_data = { 'yesterday_date': start_date, 'rows': cdrs, 'total_duration': total_duration, 'total_calls': total_calls, 'total_buy_cost': total_buy_cost, 'total_sell_cost': total_sell_cost, 'metric_aggr': metric_aggr, 'country_data': country_data, 'hangup_cause_data': hangup_cause_data, } return mail_data
def cdr_country_report(request): """CDR country report **Attributes**: * ``template`` - cdr/country_report.html * ``form`` - CountryReportForm **Logic Description**: Retrieve call records from Postgresql for all countries and create reporting information for those countries """ metric = 'nbcalls' tday = datetime.today() switch_id = 0 hourly_charttype = "lineWithFocusChart" hourly_chartdata = {'x': []} country_id_list = [] total_metric = 0 # assign initial value in form fields form = CountryReportForm(request.POST or None, initial={'from_date': tday.strftime('%Y-%m-%d 00:00'), 'to_date': tday.strftime('%Y-%m-%d 23:55'), 'switch_id': switch_id}) start_date = trunc_date_start(tday) end_date = trunc_date_end(tday) if form.is_valid(): from_date = getvar(request, 'from_date') to_date = getvar(request, 'to_date') start_date = trunc_date_start(from_date) end_date = trunc_date_end(to_date) switch_id = getvar(request, 'switch_id') metric = getvar(request, 'metric') country_id = form.cleaned_data['country_id'] # convert list value in int country_id_list = [int(row) for row in country_id] # handle 0 (All) selection if 0 in country_id_list: country_id_list = [] # check metric is valid if metric not in ['nbcalls', 'duration', 'billsec', 'buy_cost', 'sell_cost']: metric = 'nbcalls' hourly_data = get_report_cdr_per_country(request.user, 'hour', start_date, end_date, switch_id, country_id_list) extra_serie = { "tooltip": {"y_start": "", "y_end": " " + metric}, "date_format": "%d %b %y %H:%M%p" } for country in hourly_data[metric]["columns"]: hourly_chartdata['x'] = hourly_data[metric]["x_timestamp"] country_name = get_country_name(int(country)).encode('utf-8') hourly_chartdata['name' + str(country)] = country_name.decode('ascii', 'ignore').replace("'", " ") hourly_chartdata['y' + str(country)] = hourly_data[metric]["values"][str(country)] hourly_chartdata['extra' + str(country)] = extra_serie total_calls = hourly_data["nbcalls"]["total"] total_duration = hourly_data["duration"]["total"] total_billsec = hourly_data["billsec"]["total"] total_buy_cost = hourly_data["buy_cost"]["total"] total_sell_cost = hourly_data["sell_cost"]["total"] # Calculate the Average Time of Call metric_aggr = calculate_act_acd(total_calls, total_duration) # Get top 10 of country calls top_country = 10 country_data = custom_sql_aggr_top_country(request.user, switch_id, top_country, start_date, end_date) # 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', '#58A6A6', '#86BF30', '#F2D022', '#D9AA1E', '#D98236'] 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, } data = { 'action': 'tabs-1', 'total_metric': total_metric, 'start_date': start_date, 'end_date': end_date, 'metric': metric, 'form': form, 'NUM_COUNTRY': settings.NUM_COUNTRY, 'hourly_charttype': hourly_charttype, 'hourly_chartdata': hourly_chartdata, 'hourly_chartcontainer': 'hourly_container', 'hourly_extra': { 'x_is_date': True, 'x_axis_format': '%d %b %Y', 'tag_script_js': True, 'jquery_on_ready': False, }, 'total_calls': total_calls, 'total_duration': total_duration, 'total_billsec': total_billsec, 'total_buy_cost': total_buy_cost, 'total_sell_cost': total_sell_cost, 'metric_aggr': metric_aggr, 'country_data': country_data, 'country_analytic_charttype': country_analytic_charttype, 'country_analytic_chartdata': country_analytic_chartdata, 'country_chartcontainer': 'country_piechart_container', 'country_extra': country_extra, 'top_country': top_country, } return render_to_response('cdr/country_report.html', data, context_instance=RequestContext(request))
def cdr_overview(request): """CDR graph by hourly/daily/monthly basis **Attributes**: * ``template`` - cdr/overview.html * ``form`` - CdrOverviewForm **Logic Description**: Get Call records from Postgresql table and build all monthly, daily, hourly analytics """ # initialize variables hourly_charttype = "lineWithFocusChart" daily_charttype = "lineWithFocusChart" hourly_chartdata = {'x': []} daily_chartdata = {'x': []} metric = 'nbcalls' # Default metric action = 'tabs-1' tday = datetime.today() switch_id = 0 # assign initial value in form fields form = CdrOverviewForm(request.POST or None, initial={'from_date': tday.strftime('%Y-%m-%d 00:00'), 'to_date': tday.strftime('%Y-%m-%d 23:55'), 'switch_id': switch_id}) start_date = trunc_date_start(tday) end_date = trunc_date_end(tday) if form.is_valid(): from_date = getvar(request, 'from_date') to_date = getvar(request, 'to_date') start_date = trunc_date_start(from_date) end_date = trunc_date_end(to_date) switch_id = getvar(request, 'switch_id') metric = getvar(request, 'metric') # get the number of hour that diff the date delta = end_date - start_date hour_diff = abs(divmod(delta.days * 86400 + delta.seconds, 60)[0]) / 60 if hour_diff <= 72: display_chart = 'hourly' else: display_chart = 'daily' # check metric is valid if metric not in ['nbcalls', 'duration', 'billsec', 'buy_cost', 'sell_cost']: metric = 'nbcalls' extra_serie = { "tooltip": {"y_start": "", "y_end": " " + metric}, "date_format": "%d %b %y %H:%M%p" } if display_chart == 'hourly': hourly_data = get_report_cdr_per_switch(request.user, 'hour', start_date, end_date, switch_id) for switch in hourly_data[metric]["columns"]: hourly_chartdata['x'] = hourly_data[metric]["x_timestamp"] hourly_chartdata['name' + str(switch)] = get_switch_ip_addr(switch) hourly_chartdata['y' + str(switch)] = hourly_data[metric]["values"][str(switch)] hourly_chartdata['extra' + str(switch)] = extra_serie total_calls = hourly_data["nbcalls"]["total"] total_duration = hourly_data["duration"]["total"] total_billsec = hourly_data["billsec"]["total"] total_buy_cost = hourly_data["buy_cost"]["total"] total_sell_cost = hourly_data["sell_cost"]["total"] elif display_chart == 'daily': daily_data = get_report_cdr_per_switch(request.user, 'day', start_date, end_date, switch_id) for switch in daily_data[metric]["columns"]: daily_chartdata['x'] = daily_data[metric]["x_timestamp"] daily_chartdata['name' + str(switch)] = get_switch_ip_addr(switch) daily_chartdata['y' + str(switch)] = daily_data[metric]["values"][str(switch)] daily_chartdata['extra' + str(switch)] = extra_serie total_calls = daily_data["nbcalls"]["total"] total_duration = daily_data["duration"]["total"] total_billsec = daily_data["billsec"]["total"] total_buy_cost = daily_data["buy_cost"]["total"] total_sell_cost = daily_data["sell_cost"]["total"] # Calculate the Average Time of Call metric_aggr = calculate_act_acd(total_calls, total_duration) # Get top 10 of country calls country_data = custom_sql_aggr_top_country(request.user, switch_id, 10, start_date, end_date) variables = { 'action': action, 'form': form, 'display_chart': display_chart, 'start_date': start_date, 'end_date': end_date, 'metric': metric, 'hourly_chartdata': hourly_chartdata, 'hourly_charttype': hourly_charttype, 'hourly_chartcontainer': 'hourly_container', 'hourly_extra': { 'x_is_date': True, 'x_axis_format': '%d %b %y %H%p', 'tag_script_js': True, 'jquery_on_ready': True, }, 'daily_chartdata': daily_chartdata, 'daily_charttype': daily_charttype, 'daily_chartcontainer': 'daily_container', 'daily_extra': { 'x_is_date': True, 'x_axis_format': '%d %b %Y', 'tag_script_js': True, 'jquery_on_ready': True, }, 'total_calls': total_calls, 'total_duration': total_duration, 'total_billsec': total_billsec, 'total_buy_cost': total_buy_cost, 'total_sell_cost': total_sell_cost, 'metric_aggr': metric_aggr, 'country_data': country_data, } return render_to_response('cdr/overview.html', variables, context_instance=RequestContext(request))
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 billing_report(request): """CDR billing graph by daily basis **Attributes**: * ``template`` - voip_billing/billing_report.html * ``form`` - BillingReportForm **Logic Description**: Retrieve call records from PostgreSQL and build the daily billing analytics for given date range """ switch_id = 0 tday = datetime.today() total_data = [] charttype = "lineWithFocusChart" hourly_chartdata = {"x": []} form = BillingReportForm(request.POST or None, initial={'from_date': tday.strftime('%Y-%m-%d 00:00'), 'to_date': tday.strftime('%Y-%m-%d 23:55'), 'switch_id': switch_id}) start_date = trunc_date_start(tday) end_date = trunc_date_end(tday) if form.is_valid(): from_date = getvar(request, 'from_date') to_date = getvar(request, 'to_date') start_date = trunc_date_start(from_date) end_date = trunc_date_end(to_date) switch_id = getvar(request, 'switch_id') metrics = ['buy_cost', 'sell_cost'] hourly_data = get_report_cdr_per_switch(request.user, 'hour', start_date, end_date, switch_id) hourly_chartdata['x'] = hourly_data["nbcalls"]["x_timestamp"] i = 0 for metric in metrics: extra_serie = { "tooltip": {"y_start": "", "y_end": " " + metric}, "date_format": "%d %b %y %H:%M%p" } for switch in hourly_data[metric]["columns"]: i = i + 1 hourly_chartdata['name' + str(i)] = get_switch_ip_addr(switch) + "_" + metric hourly_chartdata['y' + str(i)] = hourly_data[metric]["values"][str(switch)] hourly_chartdata['extra' + str(i)] = extra_serie total_calls = hourly_data["nbcalls"]["total"] total_duration = hourly_data["duration"]["total"] total_billsec = hourly_data["billsec"]["total"] total_buy_cost = hourly_data["buy_cost"]["total"] total_sell_cost = hourly_data["sell_cost"]["total"] # Calculate the Average Time of Call metric_aggr = calculate_act_acd(total_calls, total_duration) # Get top 10 of country calls country_data = custom_sql_aggr_top_country(request.user, switch_id, 10, start_date, end_date) data = { 'form': form, 'total_data': total_data, 'start_date': start_date, 'end_date': end_date, 'charttype': charttype, 'chartdata': hourly_chartdata, 'chartcontainer': 'chart_container', 'extra': { 'x_is_date': True, 'x_axis_format': '%d %b %Y', 'tag_script_js': True, 'jquery_on_ready': True, }, 'total_calls': total_calls, 'total_duration': total_duration, 'total_billsec': total_billsec, 'total_buy_cost': total_buy_cost, 'total_sell_cost': total_sell_cost, 'metric_aggr': metric_aggr, 'country_data': country_data, } return render_to_response('voip_billing/billing_report.html', data, context_instance=RequestContext(request))
def billing_report(request): """CDR billing graph by daily basis **Attributes**: * ``template`` - voip_billing/billing_report.html * ``form`` - BillingReportForm **Logic Description**: Retrieve call records from PostgreSQL and build the daily billing analytics for given date range """ switch_id = 0 tday = datetime.today() total_data = [] charttype = "lineWithFocusChart" hourly_chartdata = {"x": []} form = BillingReportForm(request.POST or None, initial={ 'from_date': tday.strftime('%Y-%m-%d 00:00'), 'to_date': tday.strftime('%Y-%m-%d 23:55'), 'switch_id': switch_id }) start_date = trunc_date_start(tday) end_date = trunc_date_end(tday) if form.is_valid(): from_date = getvar(request, 'from_date') to_date = getvar(request, 'to_date') start_date = trunc_date_start(from_date) end_date = trunc_date_end(to_date) switch_id = getvar(request, 'switch_id') metrics = ['buy_cost', 'sell_cost'] hourly_data = get_report_cdr_per_switch(request.user, 'hour', start_date, end_date, switch_id) hourly_chartdata['x'] = hourly_data["nbcalls"]["x_timestamp"] i = 0 for metric in metrics: extra_serie = { "tooltip": { "y_start": "", "y_end": " " + metric }, "date_format": "%d %b %y %H:%M%p" } for switch in hourly_data[metric]["columns"]: i = i + 1 hourly_chartdata[ 'name' + str(i)] = get_switch_ip_addr(switch) + "_" + metric hourly_chartdata['y' + str(i)] = hourly_data[metric]["values"][str( switch)] hourly_chartdata['extra' + str(i)] = extra_serie total_calls = hourly_data["nbcalls"]["total"] total_duration = hourly_data["duration"]["total"] total_billsec = hourly_data["billsec"]["total"] total_buy_cost = hourly_data["buy_cost"]["total"] total_sell_cost = hourly_data["sell_cost"]["total"] # Calculate the Average Time of Call metric_aggr = calculate_act_acd(total_calls, total_duration) # Get top 10 of country calls country_data = custom_sql_aggr_top_country(request.user, switch_id, 10, start_date, end_date) data = { 'form': form, 'total_data': total_data, 'start_date': start_date, 'end_date': end_date, 'charttype': charttype, 'chartdata': hourly_chartdata, 'chartcontainer': 'chart_container', 'extra': { 'x_is_date': True, 'x_axis_format': '%d %b %Y', 'tag_script_js': True, 'jquery_on_ready': True, }, 'total_calls': total_calls, 'total_duration': total_duration, 'total_billsec': total_billsec, 'total_buy_cost': total_buy_cost, 'total_sell_cost': total_sell_cost, 'metric_aggr': metric_aggr, 'country_data': country_data, } return render_to_response('voip_billing/billing_report.html', data, context_instance=RequestContext(request))