From b459581637d0d9c11d05fa1b584af10d701e6e77 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 17 Aug 2016 16:26:14 -0400 Subject: [PATCH] Fix handling of dates in logs view - Fixes #1742 - Also fixes the time zone on the aggregated logs API --- endpoints/api/logs.py | 3 ++- requirements-nover.txt | 1 + static/js/graphing.js | 16 ++++++---------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/endpoints/api/logs.py b/endpoints/api/logs.py index 1bb5bf320..05d340d20 100644 --- a/endpoints/api/logs.py +++ b/endpoints/api/logs.py @@ -14,6 +14,7 @@ from auth.auth_context import get_authenticated_user from data import model, database from auth import scopes from app import avatar +from tzlocal import get_localzone LOGS_PER_PAGE = 20 SERVICE_LEVEL_LOG_KINDS = set(['service_key_create', 'service_key_approve', 'service_key_delete', @@ -41,7 +42,7 @@ def aggregated_log_view(log, kinds, start_time): # Because we aggregate based on the day of the month in SQL, we only have that information. # Therefore, create a synthetic date based on the day and the month of the start time. # Logs are allowed for a maximum period of one week, so this calculation should always work. - synthetic_date = datetime(start_time.year, start_time.month, int(log.day)) + synthetic_date = datetime(start_time.year, start_time.month, int(log.day), tzinfo=get_localzone()) if synthetic_date.day < start_time.day: synthetic_date = synthetic_date + relativedelta(months=1) diff --git a/requirements-nover.txt b/requirements-nover.txt index 8e2bf26e9..01fce31d3 100644 --- a/requirements-nover.txt +++ b/requirements-nover.txt @@ -62,3 +62,4 @@ bencode cryptography httmock moto +tzlocal diff --git a/static/js/graphing.js b/static/js/graphing.js index 0d4f308cd..e179ff1cc 100644 --- a/static/js/graphing.js +++ b/static/js/graphing.js @@ -1125,18 +1125,14 @@ LogUsageChart.prototype.buildData_ = function(aggregatedLogs) { var aggregated = aggregatedLogs[i]; var title = this.titleMap_[aggregated.kind] || aggregated.kind; var datetime = parseDate(aggregated.datetime); - var dateDay = datetime.getDate(); - if (dateDay < 10) { - dateDay = '0' + dateDay; - } - var formatted = (datetime.getMonth() + 1) + '/' + dateDay; - var adjusted = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()); + var formatted = (datetime.getMonth() + 1) + '/' + datetime.getDate(); + var justdate = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()); var key = title + '_' + formatted; var entry = { 'kind': aggregated.kind, 'title': title, - 'adjusted': adjusted, + 'justdate': justdate, 'formatted': datetime.getDate(), 'count': aggregated.count }; @@ -1162,11 +1158,11 @@ LogUsageChart.prototype.buildData_ = function(aggregatedLogs) { } found.values.push({ - 'x': entry.adjusted, + 'x': entry.justdate, 'y': entry.count }); - dateMap[entry.adjusted.toString()] = entry.adjusted; + dateMap[entry.justdate.toString()] = entry.justdate; } // Note: nvd3 has a bug that causes d3 to fail if there is not an entry for every single @@ -1197,7 +1193,7 @@ LogUsageChart.prototype.buildData_ = function(aggregatedLogs) { } datum.values.sort(function(a, b) { - return a['x'].getDate() - b['x'].getDate(); + return (a['x'].getDate() * 1) - (b['x'].getDate() * 1); }); }