This repository has been archived by the owner on Jan 31, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bug 1139713] Add received_ts field to hb Answer model
* add a received_ts field to the hb Answer model with a silly initial default value * backfill the field with the data from updated_ts
- Loading branch information
Showing
7 changed files
with
138 additions
and
48 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
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,47 @@ | ||
from django.conf import settings | ||
|
||
import pytz | ||
from rest_framework import fields | ||
|
||
|
||
class UTCDateTimeField(fields.DateTimeField): | ||
"""Like DateTimeField, except it is always in UTC in the API""" | ||
def to_native(self, value): | ||
"""Convert outgoing datetimes into UTC | ||
Input currently saves everything in Pacific time, so this | ||
takes the datetimes and converts them from Pacific time to | ||
UTC. | ||
If this situation ever changes, then we'd change | ||
settings.TIME_ZONE and this should continue to work. | ||
""" | ||
if value is not None and value.tzinfo is None: | ||
default_tzinfo = pytz.timezone(settings.TIME_ZONE) | ||
value = default_tzinfo.localize(value) | ||
value = value.astimezone(pytz.utc) | ||
return super(UTCDateTimeField, self).to_native(value) | ||
|
||
def from_native(self, value): | ||
"""Converts incoming strings to localtime | ||
Input currently saves everything in Pacific time, so this | ||
takes the datetime that super().from_native() produces, | ||
converts it to localtime and if USE_TZ=False, drops the timezone. | ||
If this situation ever changes, this should continue to work. | ||
""" | ||
result = super(UTCDateTimeField, self).from_native(value) | ||
|
||
if result is not None and result.tzinfo is not None: | ||
# Convert from whatever timezone it's in to local | ||
# time. | ||
local_tzinfo = pytz.timezone(settings.TIME_ZONE) | ||
result = result.astimezone(local_tzinfo) | ||
|
||
# If USE_TZ = False, drop the timezone | ||
if not settings.USE_TZ: | ||
result = result.replace(tzinfo=None) | ||
return result |
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 -*- | ||
"""Adds "received_ts" field to Heartbeat Answer model. | ||
""" | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
import datetime | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('heartbeat', '0005_auto_20150225_1238'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='answer', | ||
name='received_ts', | ||
field=models.DateTimeField(default=datetime.datetime(2011, 9, 1, 9, 0), help_text='Time the server received the last update packet', auto_now=True), | ||
preserve_default=False, | ||
), | ||
] |
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,54 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Set received_ts using the value in updated_ts. It's not the same | ||
thing, but it's "close enough" for an initial value where we don't | ||
have anything better to base it on. | ||
""" | ||
from __future__ import unicode_literals | ||
|
||
from datetime import datetime | ||
|
||
from django.conf import settings | ||
from django.db import models, migrations | ||
|
||
import pytz | ||
|
||
|
||
def no_op(apps, schema_editor): | ||
pass | ||
|
||
|
||
def set_received_ts(apps, schema_editor): | ||
"""Sets received_ts based on updated_ts""" | ||
Answer = apps.get_model('heartbeat', 'Answer') | ||
|
||
qs = Answer.objects.filter(received_ts=datetime(2011, 9, 1, 9, 0)) | ||
for ans in qs: | ||
# Note: Answer.updated_ts is milliseconds since epoch. We're | ||
# assuming it's in UTC, so we convert it to server time. | ||
dt = datetime.fromtimestamp(ans.updated_ts / 1000) | ||
|
||
# Apply UTC | ||
utc_tz = pytz.timezone('UTC') | ||
dt = utc_tz.localize(dt) | ||
|
||
# Then switch to server time | ||
local_tz = pytz.timezone(settings.TIME_ZONE) | ||
dt = dt.astimezone(local_tz) | ||
|
||
ans.received_ts = dt | ||
ans.save() | ||
break | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('heartbeat', '0006_answer_received_ts'), | ||
] | ||
|
||
operations = [ | ||
# Note: This can't be backed out, but for the sake of testing | ||
# and convenience, we provide a no-op. | ||
migrations.RunPython(set_received_ts, reverse_code=no_op), | ||
] |
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