Skip to content
This repository has been archived by the owner on Sep 26, 2018. It is now read-only.

Waterfall reports for subscriber activation #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions cloud/endagaweb/stats_app/stats_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading