diff --git a/cloud/endagaweb/stats_app/stats_client.py b/cloud/endagaweb/stats_app/stats_client.py index 5ebe043f..5dbdc930 100644 --- a/cloud/endagaweb/stats_app/stats_client.py +++ b/cloud/endagaweb/stats_app/stats_client.py @@ -369,3 +369,149 @@ def timeseries(self, key=None, **kwargs): if 'aggregation' not in kwargs: kwargs['aggregation'] = 'average_value' return self.aggregate_timeseries(key, **kwargs) + + +class WaterfallStatsClient(StatsClientBase): + """ waterfall reports data """ + + def __init__(self, *args, **kwargs): + super(WaterfallStatsClient, self).__init__(*args, **kwargs) + + def timeseries(self, kind=None, **kwargs): + # Get report data in timeseries format + start_time_epoch = kwargs.pop('start_time_epoch', 0) + end_time_epoch = kwargs.pop('end_time_epoch', -1) + + start = datetime.fromtimestamp(start_time_epoch, pytz.utc) + if end_time_epoch != -1: + end = datetime.fromtimestamp(end_time_epoch, pytz.utc) + else: + end = datetime.fromtimestamp(time.time(), pytz.utc) + + response = {'header': [{'label': "Months", 'name': 'month', + 'frozen': True}, + {'label': "Subscriber Activation", + 'name': 'activation', 'frozen': True}], + 'data': []}; + + months = rrule(MONTHLY, dtstart=start, until=end) + for mnth in months: + key = mnth.strftime("%b") + "-" + mnth.strftime("%Y") + response['header'].append({'label': key, 'name': key}) + + # Get last/first date of month from selected month + next_month = mnth.replace(day=28) + timedelta(days=4) + stats_end_dt = next_month - timedelta(days=next_month.day) + stats_start_dt = mnth + + kwargs['start_time_epoch'] = int(stats_start_dt.strftime("%s")) + kwargs['end_time_epoch'] = int(stats_end_dt.strftime("%s")) + kwargs['query'] = Q(subscriber__role='subscriber') + kind_key = 'Provisioned' + kwargs['report_view'] = 'value' + subscribers = self.aggregate_timeseries(kind_key, **kwargs) + + month_row = {'month': key, 'activation': len(subscribers)} + for col_mnth in months: + col_key = col_mnth.strftime("%b") + "-" + col_mnth.strftime("%Y") + month_start_dt = col_mnth + # Get last date of month from selected month + next_month = col_mnth.replace(day=28) + timedelta(days=4) + month_end_dt = next_month - timedelta(days=next_month.day) + + kwargs['start_time_epoch'] = int(month_start_dt.strftime("%s")) + kwargs['end_time_epoch'] = int(month_end_dt.strftime("%s")) + kwargs['query'] = Q(subscriber_id__in=subscribers) + if kind in ['loader', 'reload_rate']: + kwargs['aggregation'] = 'loader' + kwargs['report_view'] = 'value' + elif kind in ['reload_transaction', 'average_frequency']: + kwargs['aggregation'] = 'count' + kwargs['report_view'] = 'summary' + elif kind in ['reload_amount', 'average_load']: + kwargs['aggregation'] = 'reload_transcation_sum' + kwargs['report_view'] = 'summary' + + result = self.aggregate_timeseries('transfer', **kwargs) + if isinstance(result, (list, tuple)): + result = len(result) + + if kind == 'reload_rate': + try: + pers = round(float(result) / len(subscribers), 2) * 100 + except: + pers = 0 + result = str(pers) + " %" + elif kind in ['average_load', 'average_frequency']: + kwargs['aggregation'] = 'loader' + kwargs['report_view'] = 'value' + loader = self.aggregate_timeseries('transfer', **kwargs) + if isinstance(loader, (list, tuple)): + loader = len(loader) + try: + result = round(float(result) / float(loader), 2) + except: + result = 0 + month_row.update({col_key: result}) + response['data'].append(month_row) + return response + + +class NonLoaderStatsClient(StatsClientBase): + """ waterfall reports data """ + + def __init__(self, *args, **kwargs): + super(NonLoaderStatsClient, self).__init__(*args, **kwargs) + + def timeseries(self, kind=None, **kwargs): + # Get report data in timeseries format + # Oldest subscriber provision date + start_time_epoch = 1406680050 + last_month = datetime.fromtimestamp(time.time(), + pytz.utc) - timedelta(days=30) + end_epoch = last_month.replace(day=calendar.monthrange( + last_month.year, last_month.month)[1]) + start_epoch = end_epoch - timedelta(6 * 365 / 12) + + response = {'header': [{'label': "Months", 'name': 'month', + 'frozen': True}, + # {'label': "Activation", 'name': 'activation', + # 'frozen': True}, + {'label': "Non Loader", 'name': 'nonloader', + 'frozen': True}], + 'data': []}; + + months = list(rrule(MONTHLY, dtstart=start_epoch, until=end_epoch)) + months.sort(reverse=True) + kwargs2 = kwargs + + counter = 1 + + for mnth in months: + key = mnth.strftime("%b") + "-" + mnth.strftime("%Y") + + # Get last/first date of month from selected month + next_month = mnth.replace(day=28) + timedelta(days=4) + stats_end_dt = next_month - timedelta(days=next_month.day) + stats_start_dt = mnth.replace(day=1) + + kwargs[ + 'start_time_epoch'] = start_time_epoch # int(stats_start_dt.strftime("%s")) + kwargs['end_time_epoch'] = int(stats_end_dt.strftime("%s")) + kwargs['query'] = Q(subscriber__role='retailer') + kwargs['report_view'] = 'value' + subscribers = self.aggregate_timeseries('Provisioned', **kwargs) + + kwargs2['start_time_epoch'] = int(stats_start_dt.strftime("%s")) + kwargs2['end_time_epoch'] = int(end_epoch.strftime("%s")) + kwargs2['query'] = Q(subscriber__role='retailer') + kwargs2['aggregation'] = 'count' + kwargs2['report_view'] = 'summary' + + result = self.aggregate_timeseries('transfer', **kwargs2) + month_row = {'month': "%d months" % (counter), + # 'activation': len(subscribers), + 'nonloader': result - len(subscribers)} + response['data'].append(month_row) + counter += 1 + return response diff --git a/cloud/endagaweb/templates/dashboard/report/billing.html b/cloud/endagaweb/templates/dashboard/report/billing.html new file mode 100644 index 00000000..6ca41407 --- /dev/null +++ b/cloud/endagaweb/templates/dashboard/report/billing.html @@ -0,0 +1,491 @@ +{% extends "dashboard/layout.html" %} +{% comment %} +Copyright (c) 2016-present, Facebook, Inc. +All rights reserved. + +This source code is licensed under the BSD-style license found in the +LICENSE file in the root directory of this source tree. An additional grant +of patent rights can be found in the PATENTS file in the same directory. +{% endcomment %} +{% load apptags %} +{% load humanize %} +{% load crispy_forms_tags %} +{% load guardian_tags %} + +{% block title %} + {% tmpl_const "SITENAME" %} | Report + {% if report_summary %} + | "{{ report_summary }}" + {% endif %} +{% endblock %} + +{% block pagestyle %} + + + + + + + + +{% endblock %} + +{% block content %} + {% include "dashboard/report/header.html" with header='Billing' %} + {% get_obj_perms request.user for network as 'user_permission' %} + +
There is no network activity to display.
+ {% endif %} +