def test_cdr_overview(self): """Test Function to check cdr_overview""" response = self.client.get('/cdr_overview/') self.assertTrue(response.context['form'], CdrOverviewForm()) self.assertTemplateUsed(response, 'frontend/cdr_overview.html') self.assertEqual(response.status_code, 200) request = self.factory.get('/cdr_overview/') request.user = self.user request.session = {} response = cdr_overview(request) self.assertEqual(response.status_code, 200) data = { 'switch_id': 1, 'from_date': datetime.now().strftime("%Y-%m-%d"), 'to_date': datetime.now().strftime("%Y-%m-%d") } response = self.client.post('/cdr_overview/', data) self.assertTrue(response.context['form'], CdrOverviewForm(data)) self.assertEqual(response.status_code, 200) request = self.factory.post('/cdr_overview/', data) request.user = self.user request.session = {} response = cdr_overview(request) self.assertEqual(response.status_code, 200) data = {'switch_id': -1, 'from_date': '', 'to_date': ''} request = self.factory.post('/cdr_overview/', data) request.user = self.user request.session = {} response = cdr_overview(request) self.assertEqual(response.status_code, 200)
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))