Skip to content

Commit

Permalink
Merge pull request #257 from datosgobar/212-analytics-export-dates
Browse files Browse the repository at this point in the history
Corrijo timezone de fechas exportadas en analytics
  • Loading branch information
lucaslavandeira committed Apr 13, 2018
2 parents 29c1deb + f5af01f commit cd89c33
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
20 changes: 10 additions & 10 deletions series_tiempo_ar_api/apps/analytics/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import json

import unicodecsv
from django.conf import settings
from django_rq import job

from django.conf import settings
from django.utils.timezone import localtime
from .models import Query
from .utils import kong_milliseconds_to_tzdatetime

Expand All @@ -23,17 +23,17 @@ def export(path=None):
queryset = Query.objects.all()
filepath = path or os.path.join(settings.PROTECTED_MEDIA_DIR, settings.ANALYTICS_CSV_FILENAME)

fields = [
Query.timestamp,
Query.ip_address,
Query.ids,
Query.params
]
fields = {
'timestamp': lambda x: localtime(x.timestamp),
'ip_address': lambda x: x.ip_address,
'ids': lambda x: x.ids,
'params': lambda x: x.params,
}

with open(filepath, 'wb') as f:
writer = unicodecsv.writer(f)
# header
writer.writerow([field.field_name for field in fields])
writer.writerow([field for field in fields.keys()])
for query in queryset.iterator():

writer.writerow([getattr(query, field.field_name) for field in fields])
writer.writerow([val(query) for val in fields.values()])
16 changes: 16 additions & 0 deletions series_tiempo_ar_api/apps/analytics/tests/export_tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!coding=utf8
import os

import unicodecsv
from django.utils.timezone import localtime
from iso8601 import iso8601
from django.test import TestCase
from django.utils import timezone

Expand Down Expand Up @@ -30,3 +33,16 @@ def test_export_empty(self):
def tearDown(self):
if os.path.exists(self.filepath):
os.remove(self.filepath)

def test_export_dates(self):
query = Query(args='test', params='test', ip_address='ip_addr', timestamp=timezone.now(), ids='')
query.save()

export(path=self.filepath)
with open(self.filepath) as f:
reader = unicodecsv.reader(f)
reader.next()
for line in unicodecsv.reader(f):
date = iso8601.parse_date(line[0])
# Timestamp del modelo en UTC, pasandola a localtime debe ser igual a la del CSV
self.assertEqual(localtime(query.timestamp), date)

0 comments on commit cd89c33

Please sign in to comment.