def cdr_concurrent_calls(request): """CDR view of concurrent calls **Attributes**: * ``template`` - cdr/graph_concurrent_calls.html * ``form`` - ConcurrentCallForm * ``mongodb_data_set`` - MONGO_CDRSTATS['CONC_CALL_AGG'] (map-reduce collection) **Logic Description**: get all concurrent call records from mongodb map-reduce collection for current date """ logging.debug("CDR concurrent view start") now = datetime.today() from_date = now.strftime("%Y-%m-%d") start_date = datetime(now.year, now.month, now.day, 0, 0, 0, 0) end_date = datetime(now.year, now.month, now.day, 23, 59, 59, 0) query_var = {} switch_id = 0 form = ConcurrentCallForm(request.POST or None, initial={"from_date": from_date}) logging.debug("CDR concurrent view with search option") if form.is_valid(): from_date = getvar(request, "from_date") switch_id = getvar(request, "switch_id") start_date = ceil_strdate(from_date, "start") end_date = ceil_strdate(from_date, "end") if switch_id and int(switch_id) != 0: query_var["switch_id"] = int(switch_id) query_var["date"] = {"$gte": start_date, "$lt": end_date} if not request.user.is_superuser: # not superuser query_var["accountcode"] = request.user.userprofile.accountcode xdata = [] charttype = "stackedAreaChart" call_count_res = defaultdict(list) if query_var: # calls_in_day = mongodb.conc_call_agg.find(query_var).sort([('date', 1)]) calls_in_day = {} for d in calls_in_day: # convert date into timestamp value ts = time.mktime(d["date"].timetuple()) tsint = int(ts * 1000) xdata.append(str(tsint)) call_count_res[d["switch_id"]].append(d["numbercall"]) int_count = 1 chartdata = {"x": xdata} extra_serie = {"tooltip": {"y_start": "", "y_end": " concurrent calls"}, "date_format": "%d %b %Y %I:%M:%S %p"} for i in call_count_res: chartdata["name" + str(int_count)] = str(get_switch_ip_addr(i)) chartdata["y" + str(int_count)] = call_count_res[i] chartdata["extra" + str(int_count)] = extra_serie int_count += 1 logging.debug("CDR concurrent view end") data = { "form": form, "start_date": start_date, "chartdata": chartdata, "charttype": charttype, "chartcontainer": "stacked_area_container", "chart_extra": { "x_is_date": True, "x_axis_format": "%d %b %Y %H:%S", "tag_script_js": True, "jquery_on_ready": True, }, } return render_to_response("cdr/graph_concurrent_calls.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_concurrent_calls(request): """CDR view of concurrent calls **Attributes**: * ``template`` - cdr/graph_concurrent_calls.html * ``form`` - ConcurrentCallForm * ``mongodb_data_set`` - MONGO_CDRSTATS['CONC_CALL_AGG'] (map-reduce collection) **Logic Description**: get all concurrent call records from mongodb map-reduce collection for current date """ logging.debug('CDR concurrent view start') now = datetime.today() from_date = now.strftime('%Y-%m-%d') start_date = datetime(now.year, now.month, now.day, 0, 0, 0, 0) end_date = datetime(now.year, now.month, now.day, 23, 59, 59, 0) query_var = {} switch_id = 0 form = ConcurrentCallForm(request.POST or None, initial={'from_date': from_date}) logging.debug('CDR concurrent view with search option') if form.is_valid(): from_date = getvar(request, 'from_date') switch_id = getvar(request, 'switch_id') start_date = ceil_strdate(from_date, 'start') end_date = ceil_strdate(from_date, 'end') if switch_id and int(switch_id) != 0: query_var['switch_id'] = int(switch_id) query_var['date'] = {'$gte': start_date, '$lt': end_date} if not request.user.is_superuser: # not superuser query_var['accountcode'] = request.user.userprofile.accountcode xdata = [] charttype = "stackedAreaChart" call_count_res = defaultdict(list) if query_var: # calls_in_day = mongodb.conc_call_agg.find(query_var).sort([('date', 1)]) calls_in_day = {} for d in calls_in_day: # convert date into timestamp value ts = time.mktime(d['date'].timetuple()) tsint = int(ts * 1000) xdata.append(str(tsint)) call_count_res[d['switch_id']].append(d['numbercall']) int_count = 1 chartdata = {'x': xdata} extra_serie = {"tooltip": {"y_start": "", "y_end": " concurrent calls"}, "date_format": "%d %b %Y %I:%M:%S %p"} for i in call_count_res: chartdata['name' + str(int_count)] = str(get_switch_ip_addr(i)) chartdata['y' + str(int_count)] = call_count_res[i] chartdata['extra' + str(int_count)] = extra_serie int_count += 1 logging.debug('CDR concurrent view end') data = { 'form': form, 'start_date': start_date, 'chartdata': chartdata, 'charttype': charttype, 'chartcontainer': 'stacked_area_container', 'chart_extra': { 'x_is_date': True, 'x_axis_format': '%d %b %Y %H:%S', 'tag_script_js': True, 'jquery_on_ready': True, }, } return render_to_response('cdr/graph_concurrent_calls.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))
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))
def get_switch_ip(id): """Tag is used to get switch name""" return get_switch_ip_addr(id)
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))