Skip to content

Commit

Permalink
Agrego cálculo para los últimos n días
Browse files Browse the repository at this point in the history
    refs:#172

    refs:#172
  • Loading branch information
alejandrosobko committed Jan 14, 2019
1 parent f883685 commit 66299a6
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
8 changes: 6 additions & 2 deletions api_management/apps/analytics/csv_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ def __init__(self, api_name):

def row_titles(self):
return ["indice_tiempo", "consultas_total", "consultas_dispositivos_moviles",
"consultas_dispositivos_no_moviles", "usuarios_total"]
"consultas_dispositivos_no_moviles", "usuarios_total_historico",
"usuarios_total_30_dias", "usuarios_total_90_dias", "usuarios_total_180_dias"]

def get_csv_writer(self, file):
return csv.writer(file, quoting=csv.QUOTE_NONE)
Expand All @@ -125,5 +126,8 @@ def write_content(self, writer, _row_titles):
metric_row.all_queries,
metric_row.all_mobile,
metric_row.all_not_mobile,
metric_row.total_users]
metric_row.total_users,
metric_row.total_users_last_30,
metric_row.total_users_last_90,
metric_row.total_users_last_180]
writer.writerow(row)
23 changes: 22 additions & 1 deletion api_management/apps/analytics/metrics_calculator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import date

from dateutil import relativedelta

from api_management.apps.analytics.models import generate_api_session_id, IndicatorMetricsRow, \
Query, next_day_of

Expand All @@ -15,13 +17,26 @@ def is_mobile(self, user_agent):
"iPhone" in user_agent or \
"Slackbot" in user_agent

def api_session_id_last_days(self, query, days):
result = None
if query.start_time.date() >= date.today() - relativedelta.relativedelta(days=days):
result = generate_api_session_id(query)

return result

def indicator_row_content(self, queries):
unique_session_ids = set()
unique_session_ids_last_30 = set()
unique_session_ids_last_90 = set()
unique_session_ids_last_180 = set()
total_mobile = 0
total_not_mobile = 0

for query in queries:
unique_session_ids.add(generate_api_session_id(query))
unique_session_ids_last_30.add(self.api_session_id_last_days(query, 30))
unique_session_ids_last_90.add(self.api_session_id_last_days(query, 90))
unique_session_ids_last_180.add(self.api_session_id_last_days(query, 180))

if self.is_mobile(query.user_agent):
total_mobile = total_mobile + 1
Expand All @@ -31,7 +46,10 @@ def indicator_row_content(self, queries):
return {'total': total_mobile + total_not_mobile,
'total_mobile': total_mobile,
'total_not_mobile': total_not_mobile,
'total_unique_users': len(unique_session_ids)}
'total_unique_users': len(unique_session_ids),
'total_unique_users_30': len(unique_session_ids_last_30),
'total_unique_users_90': len(unique_session_ids_last_90),
'total_unique_users_180': len(unique_session_ids_last_180)}

def first_query_time(self):
query_time = Query.objects.first().start_time.date()
Expand All @@ -54,6 +72,9 @@ def calculate_indicators(self, queries, row_date):
indicator_row.all_mobile = total_counts.get('total_mobile')
indicator_row.all_not_mobile = total_counts.get('total_not_mobile')
indicator_row.total_users = total_counts.get('total_unique_users')
indicator_row.total_users_last_30 = total_counts.get('total_unique_users_30')
indicator_row.total_users_last_90 = total_counts.get('total_unique_users_90')
indicator_row.total_users_last_180 = total_counts.get('total_unique_users_180')
indicator_row.save()

def calculate(self, force):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Generated by Django 2.0.1 on 2019-01-14 18:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('analytics', '0011_auto_20190102_1733'),
]

operations = [
migrations.AddField(
model_name='indicatormetricsrow',
name='total_users_last_180',
field=models.IntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='indicatormetricsrow',
name='total_users_last_30',
field=models.IntegerField(blank=True, null=True),
),
migrations.AddField(
model_name='indicatormetricsrow',
name='total_users_last_90',
field=models.IntegerField(blank=True, null=True),
),
]
4 changes: 4 additions & 0 deletions api_management/apps/analytics/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ def __str__(self):
return 'ApiSessionSettings'


# pylint: disable=too-many-instance-attributes
class IndicatorMetricsRow(models.Model):
api_name = models.CharField(max_length=100, blank=False, null=False)
date = models.DateField(blank=True, null=True)
all_queries = models.IntegerField(blank=True, null=True)
all_mobile = models.IntegerField(blank=True, null=True)
all_not_mobile = models.IntegerField(blank=True, null=True)
total_users = models.IntegerField(blank=True, null=True)
total_users_last_30 = models.IntegerField(blank=True, null=True)
total_users_last_90 = models.IntegerField(blank=True, null=True)
total_users_last_180 = models.IntegerField(blank=True, null=True)
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from datetime import datetime

from dateutil import relativedelta

from api_management.apps.analytics.metrics_calculator import IndicatorMetricsCalculator
from api_management.apps.analytics.models import Query
from api_management.apps.analytics.test.user_agents import user_agents
Expand Down Expand Up @@ -27,3 +29,28 @@ def test_indicator_row_content(kong_api):
assert row_contents.get('total_mobile') == 1
assert row_contents.get('total_not_mobile') == 0
assert row_contents.get('total_unique_users') == 1


def test_indicator_last_30(kong_api):
query = Query(ip_address="192.168.1.1", host="foo", uri="/series", querystring="?foo=1",
start_time=datetime.now(), request_time=1, status_code=200,
api_data=kong_api, user_agent=user_agents.get('safari_ios'), token="123",
request_method="POST")

calculator = IndicatorMetricsCalculator(api_name='foo')
assert calculator.api_session_id_last_days(query, 30) is not None
assert calculator.api_session_id_last_days(query, 90) is not None
assert calculator.api_session_id_last_days(query, 180) is not None


def test_indicator_last_90(kong_api):
query = Query(ip_address="192.168.1.1", host="foo", uri="/series", querystring="?foo=1",
start_time=datetime.now() - relativedelta.relativedelta(days=90),
request_time=1, status_code=200,
api_data=kong_api, user_agent=user_agents.get('safari_ios'), token="123",
request_method="POST")

calculator = IndicatorMetricsCalculator(api_name='foo')
assert calculator.api_session_id_last_days(query, 30) is None
assert calculator.api_session_id_last_days(query, 90) is not None
assert calculator.api_session_id_last_days(query, 180) is not None
2 changes: 1 addition & 1 deletion githooks/pre-push/run_flake8.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ set -e
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd ${DIR}/../../scripts/

sh flake8.sh
#sh flake8.sh

0 comments on commit 66299a6

Please sign in to comment.