Skip to content
This repository has been archived by the owner on Apr 4, 2018. It is now read-only.

Commit

Permalink
Handle situation when GA response contain no rows
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Badenski authored and Pawel Badenski committed Dec 11, 2012
1 parent a218d5c commit 7cd7899
Show file tree
Hide file tree
Showing 12 changed files with 252 additions and 36 deletions.
3 changes: 2 additions & 1 deletion lib/response/hourly_response.rb
Expand Up @@ -12,7 +12,8 @@ def initialize(response, config_class)
def create_messages(response)
start_date = Date.parse(response["query"]["start-date"])
end_date = Date.parse(response["query"]["end-date"])
response["rows"].map do |row|
rows = (response["rows"] or [])
rows.map do |row|
create_message(create_payload(start_date, end_date, row))
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/response/insidegov_weekly_policy_entries_response.rb
Expand Up @@ -14,7 +14,8 @@ def initialize(response_hash, config_class)
private

def create_messages response_as_hash
condense_to_one_week(response_as_hash["rows"]).map { |slug, entries|
rows = (response_as_hash["rows"] or [])
condense_to_one_week(rows).map { |slug, entries|
create_message({
start_at: extract_start_at(response_as_hash["query"]["start-date"]),
end_at: extract_end_at(response_as_hash["query"]["end-date"]),
Expand Down
3 changes: 2 additions & 1 deletion lib/response/weekly_entry_success_response.rb
Expand Up @@ -16,7 +16,8 @@ def initialize(response_hash, config_class)
SUCCESS_LABEL = "Success"

def create_messages response_as_hash
condense_to_one_week(response_as_hash["rows"]).map do |(format, entries, successes)|
rows = (response_as_hash["rows"] or [])
condense_to_one_week(rows).map do |(format, entries, successes)|
create_message ({
:start_at => extract_start_at(response_as_hash["query"]["start-date"]),
:end_at => extract_end_at(response_as_hash["query"]["end-date"]),
Expand Down
3 changes: 2 additions & 1 deletion lib/response/weekly_response.rb
Expand Up @@ -14,11 +14,12 @@ def initialize(response_hash, config_class)

private
def parse_success(response)
rows = (response["rows"] or [])
{
:start_at => extract_start_at(response["query"]["start-date"]),
:end_at => extract_end_at(response["query"]["end-date"]),
:value => {
@metric => get_total_metric(response["rows"]),
@metric => get_total_metric(rows),
:site => @site
}
}
Expand Down
42 changes: 42 additions & 0 deletions spec/data/hourly_unique_visitors_response_no_results.json
@@ -0,0 +1,42 @@
{
"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",
"ids": "ga:53872948",
"dimensions": "ga:hour",
"metrics": [
"ga:visitors"
],
"start-index": 1,
"max-results": 1000
},
"itemsPerPage": 1000,
"totalResults": 24,
"selfLink": "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",
"profileInfo": {
"profileId": "53872948",
"accountId": "26179049",
"webPropertyId": "UA-26179049-1",
"internalWebPropertyId": "50705554",
"profileName": "1. GOV.UK (Entire site - Filtered)",
"tableId": "ga:53872948"
},
"containsSampledData": false,
"columnHeaders": [
{
"name": "ga:hour",
"columnType": "DIMENSION",
"dataType": "STRING"
},
{
"name": "ga:visitors",
"columnType": "METRIC",
"dataType": "INTEGER"
}
],
"totalsForAllResults": {
"ga:visitors": "0"
}
}
35 changes: 35 additions & 0 deletions spec/data/insidegov_weekly_policy_entries_response_no_results.json
@@ -0,0 +1,35 @@
{"kind":"analytics#gaData", "id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53699180&dimensions=ga:week,ga:eventAction&metrics=ga:totalEvents&filters=ga:customVarValue2%3D%3Dpolicy&start-date=2012-05-20&end-date=2012-05-26", "query":{
"start-date":"2012-05-20",
"end-date":"2012-05-26",
"ids":"ga:53699180",
"dimensions":"ga:week,ga:eventAction",
"metrics":["ga:totalEvents"],
"filters":"ga:customVarValue2==policy",
"start-index":1,
"max-results":1000
}, "itemsPerPage":1000, "totalResults":0, "selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53699180&dimensions=ga:week,ga:eventAction&metrics=ga:totalEvents&filters=ga:customVarValue2%3D%3Dpolicy&start-date=2012-05-20&end-date=2012-05-26", "profileInfo":{
"profileId":"53699180",
"accountId":"26179049",
"webPropertyId":"UA-26179049-1",
"internalWebPropertyId":"50705554",
"profileName":"2. Inside Government - ALL",
"tableId":"ga:53699180"
}, "containsSampledData":false, "columnHeaders":[
{
"name":"ga:week",
"columnType":"DIMENSION",
"dataType":"STRING"
},
{
"name":"ga:eventAction",
"columnType":"DIMENSION",
"dataType":"STRING"
},
{
"name":"ga:totalEvents",
"columnType":"METRIC",
"dataType":"INTEGER"
}
], "totalsForAllResults":{
"ga:totalEvents":"0"
}}
47 changes: 47 additions & 0 deletions spec/data/weekly_dummy_response_no_results.json
@@ -0,0 +1,47 @@
{
"kind": "analytics#gaData",
"id": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:week,+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",
"ids": "ga:53872948",
"dimensions": "ga:week, ga:year",
"metrics": [
"ga:visits"
],
"start-index": 1,
"max-results": 1000
},
"itemsPerPage": 1000,
"totalResults": 1,
"selfLink": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:week,+ga:year&metrics=ga:visits&start-date=2012-07-29&end-date=2012-08-04&start-index=1&max-results=1000",
"profileInfo": {
"profileId": "53872948",
"accountId": "26179049",
"webPropertyId": "UA-26179049-1",
"internalWebPropertyId": "50705554",
"profileName": "1. GOV.UK (Entire site - Filtered)",
"tableId": "ga:53872948"
},
"containsSampledData": false,
"columnHeaders": [
{
"name": "ga:week",
"columnType": "DIMENSION",
"dataType": "STRING"
},
{
"name": "ga:year",
"columnType": "DIMENSION",
"dataType": "STRING"
},
{
"name": "ga:visits",
"columnType": "METRIC",
"dataType": "INTEGER"
}
],
"totalsForAllResults": {
"ga:visits": "0"
}
}
40 changes: 40 additions & 0 deletions spec/data/weekly_entry_success_response_no_results.json
@@ -0,0 +1,40 @@
{"kind":"analytics#gaData", "id":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:week,ga:eventCategory,ga:eventLabel&metrics=ga:totalEvents&filters=ga:eventCategory%3D~%5EIG_.*&start-date=2012-05-20&end-date=2012-05-26", "query":{
"start-date":"2012-05-20",
"end-date":"2012-05-26",
"ids":"ga:53872948",
"dimensions":"ga:week,ga:eventCategory,ga:eventLabel",
"metrics":["ga:totalEvents"],
"filters":"ga:eventCategory=~^IG_.*",
"start-index":1,
"max-results":1000
}, "itemsPerPage":1000, "totalResults":0, "selfLink":"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:53872948&dimensions=ga:week,ga:eventCategory,ga:eventLabel&metrics=ga:totalEvents&filters=ga:eventCategory%3D~%5EIG_.*&start-date=2012-05-20&end-date=2012-05-26", "profileInfo":{
"profileId":"53872948",
"accountId":"26179049",
"webPropertyId":"UA-26179049-1",
"internalWebPropertyId":"50705554",
"profileName":"1. GOV.UK (Entire site - Filtered)",
"tableId":"ga:53872948"
}, "containsSampledData":false, "columnHeaders":[
{
"name":"ga:week",
"columnType":"DIMENSION",
"dataType":"STRING"
},
{
"name":"ga:eventCategory",
"columnType":"DIMENSION",
"dataType":"STRING"
},
{
"name":"ga:eventLabel",
"columnType":"DIMENSION",
"dataType":"STRING"
},
{
"name":"ga:totalEvents",
"columnType":"METRIC",
"dataType":"INTEGER"
}
], "totalsForAllResults":{
"ga:totalEvents":"0"
}}
78 changes: 46 additions & 32 deletions spec/unit/response/hourly_response_spec.rb
Expand Up @@ -4,43 +4,57 @@

describe "Hourly Response" do

describe "example from 2012-08-14 12:07:00+01:00"

before(:all) do
response_hash = load_json("hourly_unique_visitors_response.json")
@response = HourlyResponse.new(response_hash, DummyConfig)
describe "example from 2012-08-14 12:07:00+01:00" do
before(:each) do
response_hash = load_json("hourly_unique_visitors_response.json")
@response = HourlyResponse.new(response_hash, DummyConfig)
end

it "should use dummy count if only week is present (middle of the year)" do
@response.messages.should be_an(Array)
@response.messages.should have(24).item
end

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][: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][: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][:value][:dummy].should eql(0)
message[:payload][:value][:site].should eql("govuk")
end
end

it "should use dummy count if only week is present (middle of the year)" do
@response.messages.should be_an(Array)
@response.messages.should have(24).item
end

it "should check the first hour" do
message = @response.messages[0]
describe "response with no results" do
before(:all) do
response_hash = load_json("hourly_unique_visitors_response_no_results.json")
@response = HourlyResponse.new(response_hash, DummyConfig)
end

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][:value][:dummy].should eql(75)
message[:payload][:value][:site].should eql("govuk")
it "should create no messages" do
@response.messages.should be_an(Array)
@response.messages.should be_empty
end
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][: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][:value][:dummy].should eql(0)
message[:payload][:value][:site].should eql("govuk")
end

end
Expand Up @@ -67,4 +67,15 @@ def message_for_format format
it "should have slugs for policies"
end

describe "response with no results" do
before(:each) do
response_as_hash = load_json("insidegov_weekly_policy_entries_response_no_results.json")
@response = InsideGovWeeklyPolicyEntriesResponse.new(response_as_hash, GoogleAnalytics::Config::InsideGovWeeklyPolicyEntries)
end

it "should send no messages" do
@response.messages.should be_an(Array)
@response.messages.should be_empty
end
end
end
11 changes: 11 additions & 0 deletions spec/unit/response/weekly_entry_success_response_spec.rb
Expand Up @@ -89,4 +89,15 @@ def message_for_format format
end
end

describe "response with no results" do
before(:each) do
response_as_hash = load_json("weekly_entry_success_response_no_results.json")
@response = WeeklyEntrySuccessResponse.new(response_as_hash, GoogleAnalytics::Config::WeeklyEntrySuccess)
end

it "should create no messages" do
@response.messages.should be_an(Array)
@response.messages.should be_empty
end
end
end
12 changes: 12 additions & 0 deletions spec/unit/response/weekly_response_spec.rb
Expand Up @@ -33,4 +33,16 @@
message[:payload][:value][:site].should eql("govuk")
end

it "should report 0 when no results have been provided" do
response_hash = load_json("weekly_dummy_response_no_results.json")

response = WeeklyResponse.new(response_hash, DummyConfig)
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][:value][:dummy].should eql(0)
message[:payload][:value][:site].should eql("govuk")
end
end

0 comments on commit 7cd7899

Please sign in to comment.