Permalink
Browse files

Merge pull request #5 from alphagov/timezone_fix

Make sure dates have a DST timezone set
  • Loading branch information...
2 parents f1322cb + 0d63c30 commit 215ef92c9288c5e706eebece84c689ec9a6162bc @maxfliri maxfliri committed Mar 21, 2013
View
@@ -9,7 +9,8 @@ gem "bunny"
gem "gli", "1.6.0"
gem "whenever"
gem "datainsight_logging", '0.0.3'
-gem "datainsight_collector", "0.0.9"
+gem "datainsight_collector", "0.0.10"
+gem "tzinfo", "~> 0.3.37"
group :test do
View
@@ -19,10 +19,11 @@ GEM
ci_reporter (1.7.1)
builder (>= 2.1.2)
connection_pool (0.9.2)
- datainsight_collector (0.0.9)
+ datainsight_collector (0.0.10)
bunny
datainsight_logging
gli (= 1.6.0)
+ tzinfo (~> 0.3.37)
datainsight_logging (0.0.3)
logging
diff-lcs (1.1.3)
@@ -83,6 +84,7 @@ GEM
jwt (>= 0.1.5)
multi_json (>= 1.0.0)
timecop (0.5.2)
+ tzinfo (0.3.37)
uuidtools (2.1.3)
whenever (0.7.3)
activesupport (>= 2.3.4)
@@ -95,7 +97,7 @@ DEPENDENCIES
airbrake (= 3.1.5)
bunny
ci_reporter (= 1.7.1)
- datainsight_collector (= 0.0.9)
+ datainsight_collector (= 0.0.10)
datainsight_logging (= 0.0.3)
fakeweb
gli (= 1.6.0)
@@ -104,4 +106,5 @@ DEPENDENCIES
rake
rspec (= 2.11.0)
timecop (= 0.5.2)
+ tzinfo (~> 0.3.37)
whenever
View
@@ -2,6 +2,7 @@
require 'open-uri'
require 'json'
require 'faraday'
+require 'timezone_helper'
module Faraday
module Utils
@@ -16,8 +16,8 @@ def create_payload(response)
end_at = (start_at+1)
{
- :start_at => start_at.strftime,
- :end_at => end_at.strftime,
+ :start_at => start_at.with_tz_offset("Europe/London").strftime,
+ :end_at => end_at.with_tz_offset("Europe/London").strftime,
:value => {
@metric => value,
:site => @site
@@ -1,9 +1,9 @@
module ExtractWeeklyDates
def extract_start_at(start_date)
- DateTime.parse(start_date).strftime
+ DateTime.parse(start_date).with_tz_offset("Europe/London").strftime
end
def extract_end_at(end_date)
- (DateTime.parse(end_date)+1).strftime
+ (DateTime.parse(end_date)+1).with_tz_offset("Europe/London").strftime
end
end
@@ -33,7 +33,7 @@ def create_payload(start_date, end_date, row)
end
def format_datetime(date, hour)
- DateTime.new(date.year, date.month, date.day, hour).strftime
+ DateTime.new(date.year, date.month, date.day, hour).with_tz_offset("Europe/London").strftime
end
end
@@ -1,9 +1,9 @@
{
"kind":"analytics#gaData",
- "id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:day&metrics=ga:visitors&start-date=2012-10-17&end-date=2012-10-17&start-index=1&max-results=50",
+ "id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:day&metrics=ga:visitors&start-date=2012-01-17&end-date=2012-01-17&start-index=1&max-results=50",
"query":{
- "start-date":"2012-10-17",
- "end-date":"2012-10-17",
+ "start-date":"2012-01-17",
+ "end-date":"2012-01-17",
"ids":"ga:53872948",
"dimensions":"ga:day",
"metrics":["ga:visitors"],
@@ -12,7 +12,7 @@
},
"itemsPerPage":50,
"totalResults":1,
- "selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:day&metrics=ga:visitors&start-date=2012-10-17&end-date=2012-10-17&start-index=1&max-results=50",
+ "selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:day&metrics=ga:visitors&start-date=2012-01-17&end-date=2012-01-17&start-index=1&max-results=50",
"profileInfo":{
"profileId":"53872948",
"accountId":"26179049",
@@ -2,8 +2,8 @@
"kind": "analytics#gaData",
"id": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:hour&metrics=ga:visitors&start-date=2012-08-14&end-date=2012-08-14&start-index=1&max-results=1000",
"query": {
- "start-date": "2012-08-14",
- "end-date": "2012-08-14",
+ "start-date": "2012-02-14",
+ "end-date": "2012-02-14",
"ids": "ga:53872948",
"dimensions": "ga:hour",
"metrics": [
@@ -1,6 +1,6 @@
{"kind":"analytics#gaData", "id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:eventCategory,ga:eventLabel&metrics=ga:totalEvents&filters=ga:eventCategory%3D~%5EMS_.*&start-date=2012-09-09&end-date=2012-09-15&start-index=1&max-results=1000", "query":{
- "start-date":"2012-09-09",
- "end-date":"2012-09-15",
+ "start-date":"2012-01-09",
+ "end-date":"2012-01-15",
"ids":"ga:53872948",
"dimensions":"ga:eventCategory,ga:eventLabel",
"metrics":["ga:totalEvents"],
@@ -2,8 +2,8 @@
"kind": "analytics#gaData",
"id": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:year&metrics=ga:visits&start-date=2012-07-29&end-date=2012-08-04&start-index=1&max-results=1000",
"query": {
- "start-date": "2012-07-29",
- "end-date": "2012-08-04",
+ "start-date": "2012-01-29",
+ "end-date": "2012-02-04",
"ids": "ga:53872948",
"dimensions": "ga:year",
"metrics": [
@@ -2,8 +2,8 @@
"kind": "analytics#gaData",
"id": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:year&metrics=ga:visits&start-date=2012-07-29&end-date=2012-08-04&start-index=1&max-results=1000",
"query": {
- "start-date": "2012-07-29",
- "end-date": "2012-08-04",
+ "start-date": "2012-01-29",
+ "end-date": "2012-02-04",
"ids": "ga:53872948",
"dimensions": "ga:year",
"metrics": [
@@ -25,13 +25,49 @@ class StubConfig
message = @response.messages[0]
message[:payload].should == {
- start_at: "2012-10-17T00:00:00+00:00",
- end_at: "2012-10-18T00:00:00+00:00",
+ start_at: "2012-01-17T00:00:00+00:00",
+ end_at: "2012-01-18T00:00:00+00:00",
value: {
dummy: 909706,
site: 'govuk'
}
}
end
end
+
+ describe "example from 2012-10-17 13:48:00+01:00" do
+
+ before(:all) do
+ response_hash = load_json("daily_unique_visitors_response.json")
+ @response = DailyResponse.new([response_hash], StubConfig)
+ end
+
+ it "should use visitors count" do
+ @response.messages.should be_an(Array)
+ @response.messages.should have(1).item
+ end
+
+ it "should have the daily visitors" do
+ message = @response.messages[0]
+
+ message[:payload].should == {
+ start_at: "2012-01-17T00:00:00+00:00",
+ end_at: "2012-01-18T00:00:00+00:00",
+ value: {
+ dummy: 909706,
+ site: 'govuk'
+ }
+ }
+ end
+ end
+
+ it "should convert date to local (London) timezone if required" do
+ response_hash = load_json("daily_unique_visitors_response.json")
+ response_hash["query"]["start-date"] = "2012-10-17T00:00:00+00:00"
+
+ @response = DailyResponse.new([response_hash], StubConfig)
+ message = @response.messages[0]
+
+ message[:payload][:start_at].should == "2012-10-17T00:00:00+01:00"
+ end
end
@@ -4,7 +4,52 @@
describe "Hourly Response" do
- describe "example from 2012-08-14 12:07:00+01:00" do
+ it "should convert date to local (London) timezone if required" do
+ response_hash = load_json("hourly_unique_visitors_response.json")
+ response_hash["query"]["start-date"] = "2012-10-17T00:00:00+00:00"
+
+ @response = HourlyResponse.new([response_hash], StubConfig)
+ message = @response.messages[0]
+
+ message[:payload][:start_at].should == "2012-10-17T00:00:00+01:00"
+ end
+
+ describe "example from 2012-03-25 when the DST timezone change happens" do
+ before(:each) do
+ response_hash = load_json("hourly_unique_visitors_response.json")
+ response_hash["query"]["start-date"] = "2012-03-25T00:00:00+00:00"
+ @response = HourlyResponse.new([response_hash], DummyConfig)
+ end
+
+ it "should contain the first hour" do
+ message = @response.messages[0]
+
+ message[:payload][:start_at].should eql("2012-03-25T00:00:00+00:00")
+ message[:payload][:end_at].should eql("2012-03-25T01:00:00+01:00")
+ message[:payload][:value][:dummy].should eql(75)
+ message[:payload][:value][:site].should eql("govuk")
+ end
+
+ it "should contain the tenth hour" do
+ message = @response.messages[10]
+
+ message[:payload][:start_at].should eql("2012-03-25T10:00:00+01:00")
+ message[:payload][:end_at].should eql("2012-03-25T11:00:00+01:00")
+ message[:payload][:value][:dummy].should eql(429)
+ message[:payload][:value][:site].should eql("govuk")
+ end
+
+ it "should contain the last hour" do
+ message = @response.messages[23]
+
+ message[:payload][:start_at].should eql("2012-03-25T23:00:00+01:00")
+ message[:payload][:end_at].should eql("2012-03-26T00:00:00+01:00")
+ message[:payload][:value][:dummy].should eql(0)
+ message[:payload][:value][:site].should eql("govuk")
+ end
+ end
+
+ describe "example from 2012-02-14" do
before(:each) do
response_hash = load_json("hourly_unique_visitors_response.json")
@response = HourlyResponse.new([response_hash], DummyConfig)
@@ -18,26 +63,26 @@
it "should check the first hour" do
message = @response.messages[0]
- message[:payload][:start_at].should eql("2012-08-14T00:00:00+00:00")
- message[:payload][:end_at].should eql("2012-08-14T01:00:00+00:00")
+ message[:payload][:start_at].should eql("2012-02-14T00:00:00+00:00")
+ message[:payload][:end_at].should eql("2012-02-14T01:00:00+00:00")
message[:payload][:value][:dummy].should eql(75)
message[:payload][:value][:site].should eql("govuk")
end
it "should check the tenth hour" do
message = @response.messages[10]
- message[:payload][:start_at].should eql("2012-08-14T10:00:00+00:00")
- message[:payload][:end_at].should eql("2012-08-14T11:00:00+00:00")
+ message[:payload][:start_at].should eql("2012-02-14T10:00:00+00:00")
+ message[:payload][:end_at].should eql("2012-02-14T11:00:00+00:00")
message[:payload][:value][:dummy].should eql(429)
message[:payload][:value][:site].should eql("govuk")
end
it "should check the last hour" do
message = @response.messages[23]
- message[:payload][:start_at].should eql("2012-08-14T23:00:00+00:00")
- message[:payload][:end_at].should eql("2012-08-15T00:00:00+00:00")
+ message[:payload][:start_at].should eql("2012-02-14T23:00:00+00:00")
+ message[:payload][:end_at].should eql("2012-02-15T00:00:00+00:00")
message[:payload][:value][:dummy].should eql(0)
message[:payload][:value][:site].should eql("govuk")
end
@@ -30,8 +30,8 @@ def message_for_format format
it "should have start_at, end_at and site data" do
message_payload = @response.messages.first[:payload]
- message_payload[:start_at].should eql("2012-09-09T00:00:00+00:00")
- message_payload[:end_at].should eql("2012-09-16T00:00:00+00:00")
+ message_payload[:start_at].should eql("2012-01-09T00:00:00+00:00")
+ message_payload[:end_at].should eql("2012-01-16T00:00:00+00:00")
message_payload[:value][:site].should eql("govuk")
end
@@ -3,6 +3,30 @@
include GoogleAnalytics
describe "Weekly Response" do
+ it "should convert date to local (London) timezone if required" do
+ response_hash = load_json("weekly_dummy_response.json")
+ response_hash["query"]["start-date"] = "2012-10-17T00:00:00+00:00"
+
+ @response = WeeklyResponse.new([response_hash], StubConfig)
+ message = @response.messages[0]
+
+ message[:payload][:start_at].should == "2012-10-17T00:00:00+01:00"
+ end
+
+ describe "example when the DST timezone change happens" do
+ it "should have use datetimes with correct timezones" do
+ response_hash = load_json("weekly_dummy_response.json")
+ response_hash["query"]["start-date"] = "2012-03-25T00:00:00+00:00"
+ response_hash["query"]["end-date"] = "2012-03-31T00:00:00+00:00"
+
+ response = WeeklyResponse.new([response_hash], DummyConfig)
+ message = response.messages.first
+
+ message[:payload][:start_at].should eql("2012-03-25T00:00:00+00:00")
+ message[:payload][:end_at].should eql("2012-04-01T00:00:00+01:00")
+ end
+ end
+
it "should use dummy count if only one week is present (middle of the year)" do
response_hash = load_json("weekly_dummy_response.json")
@@ -12,8 +36,8 @@
response.messages.should have(1).item
message = response.messages.first
- message[:payload][:start_at].should eql("2012-07-29T00:00:00+00:00")
- message[:payload][:end_at].should eql("2012-08-05T00:00:00+00:00")
+ message[:payload][:start_at].should eql("2012-01-29T00:00:00+00:00")
+ message[:payload][:end_at].should eql("2012-02-05T00:00:00+00:00")
message[:payload][:value][:dummy].should eql(32199)
message[:payload][:value][:site].should eql("govuk")
end
@@ -25,8 +49,8 @@
response.messages.should have(1).item
message = response.messages.first
- message[:payload][:start_at].should eql("2012-07-29T00:00:00+00:00")
- message[:payload][:end_at].should eql("2012-08-05T00:00:00+00:00")
+ message[:payload][:start_at].should eql("2012-01-29T00:00:00+00:00")
+ message[:payload][:end_at].should eql("2012-02-05T00:00:00+00:00")
message[:payload][:value][:dummy].should eql(0)
message[:payload][:value][:site].should eql("govuk")
end

0 comments on commit 215ef92

Please sign in to comment.