def test_cdr_hourly_report(self): """Test Function to check cdr hourly report""" response = self.client.get('/hourly_report/') self.assertTrue(response.context['form'], CompareCallSearchForm()) self.assertTemplateUsed(response, 'frontend/cdr_report_by_hour.html') self.assertEqual(response.status_code, 200) request = self.factory.get('/hourly_report/') request.user = self.user request.session = {} response = cdr_report_by_hour(request) self.assertEqual(response.status_code, 200) data = { 'switch_id': 0, 'from_date': datetime.now().strftime("%Y-%m-%d"), 'comp_days': 2, 'graph_view': 1, 'check_days': 1 } response = self.client.post('/hourly_report/', data) self.assertTrue(response.context['form'], CompareCallSearchForm(data)) self.assertEqual(response.status_code, 200) data = { 'switch_id': 0, 'from_date': datetime.now().strftime("%Y-%m-%d"), 'comp_days': 2, 'graph_view': 2, 'check_days': 2 } request = self.factory.post('/hourly_report/', data) request.user = self.user request.session = {} response = cdr_report_by_hour(request) self.assertEqual(response.status_code, 200) data = { 'switch_id': 0, 'from_date': datetime.now().strftime("%Y-%m-%d"), 'comp_days': 10, 'graph_view': 3, 'check_days': 7 } request = self.factory.post('/hourly_report/', data) request.user = self.user request.session = {} response = cdr_report_by_hour(request) self.assertEqual(response.status_code, 200)
def cdr_daily_comparison(request): """ Hourly CDR graph that compare with previous dates **Attributes**: * ``template`` - cdr/daily_comparison.html * ``form`` - CompareCallSearchForm **Logic Description**: get the call records aggregated from the CDR table using the materialized view and compare with other date records # hourly_charttype = "lineWithFocusChart" # daily_charttype = "lineWithFocusChart" # hourly_chartdata = {'x': []} # daily_chartdata = {'x': []} # metric = 'nbcalls' # Default metric """ # Default metric = 'nbcalls' switch_id = 0 hourly_charttype = "multiBarChart" hourly_chartdata = {'x': []} compare_days = 2 compare_type = COMPARE_WITH.previous_days today_date = datetime.today() form = CompareCallSearchForm(request.POST or None, initial={'from_date': today_date.strftime('%Y-%m-%d'), 'compare_days': compare_days, 'compare_type': compare_type, 'switch_id': 0}) today_date = datetime(today_date.year, today_date.month, today_date.day) current_date = today_date if form.is_valid(): from_date = getvar(request, 'from_date') current_date = ceil_strdate(str(from_date), 'start') # current_date = trunc_date_start(from_date) switch_id = getvar(request, 'switch_id') compare_days = int(getvar(request, 'compare_days')) metric = getvar(request, 'metric') kwargs = {} if switch_id and switch_id != '0': kwargs['switch_id'] = int(switch_id) xdata = [i for i in range(0, 24)] hourly_chartdata = {'x': xdata} y_count = 1 for nday in range(1, compare_days + 1): start_date = current_date + relativedelta(days=-int(nday-1)) start_date = datetime(start_date.year, start_date.month, start_date.day, 0, 0, 0, 0) end_date = current_date + relativedelta(days=-int(nday-1)) end_date = datetime(end_date.year, end_date.month, end_date.day, 23, 59, 59, 999999) # Get hourly Data hourly_data = get_report_compare_cdr(request.user, 'hour', start_date, end_date, switch_id) extra_serie = { "tooltip": {"y_start": "", "y_end": " " + metric} } # We only need to set x axis once, so let's do it for nbcalls # hourly_chartdata['x'] = hourly_data["nbcalls"]["x_timestamp"] for switch in hourly_data[metric]["columns"]: serie = get_switch_ip_addr(switch) + "_day_" + str(nday) hourly_chartdata['name' + str(y_count)] = serie hourly_chartdata['y' + str(y_count)] = hourly_data[metric]["values"][str(switch)] hourly_chartdata['extra' + str(y_count)] = extra_serie y_count += 1 variables = { 'form': form, 'from_date': current_date, 'metric': metric, 'compare_days': compare_days, 'hourly_charttype': hourly_charttype, 'hourly_chartdata': hourly_chartdata, 'hourly_chartcontainer': 'hourly_chartcontainer', 'hourly_extra': { 'x_is_date': False, 'x_axis_format': '', 'tag_script_js': True, 'jquery_on_ready': True, }, } return render_to_response('cdr/daily_comparison.html', variables, context_instance=RequestContext(request))