-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #284 from datosgobar/277-import-analytics
Importar analytics desde API mgmt
- Loading branch information
Showing
9 changed files
with
299 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
series_tiempo_ar_api/apps/analytics/management/commands/import_analytics.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#! coding: utf-8 | ||
from django.core.management import BaseCommand | ||
|
||
from series_tiempo_ar_api.apps.analytics.tasks import import_last_day_analytics_from_api_mgmt | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, *args, **options): | ||
import_last_day_analytics_from_api_mgmt() |
27 changes: 27 additions & 0 deletions
27
series_tiempo_ar_api/apps/analytics/migrations/0005_importconfig.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.6 on 2018-06-07 19:14 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('analytics', '0004_auto_20180117_1045'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ImportConfig', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('endpoint', models.URLField()), | ||
('token', models.CharField(max_length=64)), | ||
('kong_api_id', models.CharField(max_length=64)), | ||
], | ||
options={ | ||
'abstract': False, | ||
}, | ||
), | ||
] |
24 changes: 24 additions & 0 deletions
24
series_tiempo_ar_api/apps/analytics/migrations/0006_analyticsimporttask.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.11.6 on 2018-06-08 18:09 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('analytics', '0005_importconfig'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='AnalyticsImportTask', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('status', models.CharField(choices=[('running', 'Corriendo'), ('finished', 'Finalizada')], max_length=64)), | ||
('logs', models.TextField(blank=True)), | ||
('timestamp', models.DateTimeField()), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#!coding=utf8 | ||
|
||
import mock | ||
from django.test import TestCase | ||
from series_tiempo_ar_api.apps.analytics.tasks import import_last_day_analytics_from_api_mgmt | ||
from series_tiempo_ar_api.apps.analytics.models import ImportConfig, Query, AnalyticsImportTask | ||
|
||
|
||
class UninitializedImportConfigTests(TestCase): | ||
|
||
def test_not_initialized_model(self): | ||
import_last_day_analytics_from_api_mgmt() | ||
self.assertTrue('Error' in AnalyticsImportTask.objects.last().logs) | ||
|
||
|
||
def mocked_requests_get(): | ||
class MockResponse: | ||
def __init__(self, json_data): | ||
self.json_data = json_data | ||
|
||
def json(self): | ||
return self.json_data | ||
|
||
def __call__(self, *args, **kwargs): | ||
return self | ||
|
||
return MockResponse( | ||
{ | ||
'next': None, | ||
'count': 2, | ||
'results': [ | ||
{ | ||
'ip_address': '127.0.0.1', | ||
'querystring': '', | ||
'start_time': '2018-06-07T05:00:00-03:00', | ||
} | ||
] | ||
} | ||
) | ||
|
||
|
||
class ImportTests(TestCase): | ||
|
||
def setUp(self): | ||
config_model = ImportConfig.get_solo() | ||
config_model.endpoint = 'http://localhost:80/fake_endpoint' | ||
config_model.token = 'fake-token' | ||
config_model.kong_api_id = 'fake_id' | ||
config_model.save() | ||
|
||
def test_single_empty_result(self): | ||
with mock.patch.object(ImportConfig, 'get_results', return_value={ | ||
'next': None, | ||
'count': 0, | ||
'results': [] | ||
}): | ||
import_last_day_analytics_from_api_mgmt() | ||
|
||
self.assertEqual(Query.objects.count(), 0) | ||
|
||
def test_single_page_results(self): | ||
with mock.patch.object(ImportConfig, 'get_results', return_value={ | ||
'next': None, | ||
'count': 1, | ||
'results': [ | ||
{ | ||
'ip_address': '127.0.0.1', | ||
'querystring': '', | ||
'start_time': '2018-06-07T05:00:00-03:00', | ||
} | ||
] | ||
}): | ||
import_last_day_analytics_from_api_mgmt() | ||
self.assertEqual(Query.objects.count(), 1) | ||
|
||
def test_multiple_page_results(self): | ||
"""Emula una query con dos páginas de resultados, cada una con una query""" | ||
return_value = { | ||
'next': 'next_page_url', | ||
'count': 2, | ||
'results': [ | ||
{ | ||
'ip_address': '127.0.0.1', | ||
'querystring': '', | ||
'start_time': '2018-06-07T05:00:00-03:00', | ||
} | ||
] | ||
} | ||
with mock.patch.object(ImportConfig, 'get_results', return_value=return_value): | ||
with mock.patch('series_tiempo_ar_api.apps.analytics.tasks.requests.get', | ||
new_callable=mocked_requests_get): | ||
import_last_day_analytics_from_api_mgmt() | ||
|
||
self.assertEqual(Query.objects.count(), 2) |