Skip to content

Commit

Permalink
Merge pull request #4374 from alphagov/redo-chart-data
Browse files Browse the repository at this point in the history
Build statistics block data from new format of csv files
  • Loading branch information
leenagupte authored Nov 6, 2024
2 parents b32e624 + 86e75c0 commit 22086c6
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 93 deletions.
53 changes: 26 additions & 27 deletions app/models/landing_page/block/statistics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,16 @@ def x_axis_keys
end

def rows
rows = []

csv_rows.each do |row|
variable_name = row.values.second
value = row.values.last

existing_row = rows.find { |item| item[:label].include?(variable_name) }

if existing_row.present?
existing_row[:values] << value.to_i
else
rows << {
label: variable_name,
values: [value.to_i],
}
csv_headers[1..].map do |header|
values = csv_rows.map do |row|
row[header].to_f
end
end

rows
{
label: header,
values:,
}
end
end

def attachment
Expand All @@ -39,16 +30,24 @@ def attachment
private

def csv_rows
@csv_rows ||= begin
rows = if attachment
CSV.new(URI.parse(attachment.url).open, headers: true).map(&:to_h)
else
# SCAFFOLDING
csv_file_path = Rails.root.join("#{STATISTICS_DATA_PATH}/#{data['csv_file']}")
CSV.read(csv_file_path, headers: true).map(&:to_h)
end
rows.each(&:deep_symbolize_keys!)
end
@csv_rows ||= opened_csv.map(&:to_h)
end

def csv_headers
opened_csv.headers
end

def opened_csv
@opened_csv ||= attachment ? csv_from_url : csv_from_file
end

def csv_from_url
CSV.parse(URI.parse(attachment.url).open, headers: true)
end

def csv_from_file
csv_file_path = Rails.root.join("#{STATISTICS_DATA_PATH}/#{data['csv_file']}")
CSV.read(csv_file_path, headers: true)
end
end
end
4 changes: 2 additions & 2 deletions lib/data/landing_page_content_items/homepage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ blocks:
title: "Chart to visually represent some data"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
csv_file: "data_two.csv"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
minimal: true
- type: card
Expand All @@ -106,7 +106,7 @@ blocks:
title: "Chart to visually represent some data"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
csv_file: "data_three.csv"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
minimal: true
- type: govspeak
Expand Down
14 changes: 7 additions & 7 deletions lib/data/landing_page_content_items/landing_page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ blocks:
card_content:
blocks:
- type: statistics
title: "Chart to visually represent some data"
title: "Chart to visually represent some data with float data"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
Expand All @@ -143,10 +143,10 @@ blocks:
card_content:
blocks:
- type: statistics
title: "Chart to visually represent some data"
title: "Chart to visually represent some data with whole numbers"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
csv_file: "data_three.csv"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
minimal: true
- type: card
Expand All @@ -156,10 +156,10 @@ blocks:
card_content:
blocks:
- type: statistics
title: "Chart to visually represent some data"
title: "Chart to visually represent some data with missing data"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
csv_file: "data_three.csv"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
minimal: true
- type: two_column_layout
Expand Down Expand Up @@ -193,7 +193,7 @@ blocks:
- type: statistics
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
csv_file: "data_three.csv"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
padding: true
- type: statistics
Expand All @@ -207,7 +207,7 @@ blocks:
title: "Chart to visually represent some more data"
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_two.csv"
csv_file: "data_three.csv"
data_source_link: https://www.ons.gov.uk/economy/inflationandpriceindices/timeseries/l55o/mm23
- type: share_links
links:
Expand Down
5 changes: 5 additions & 0 deletions lib/data/landing_page_content_items/statistics/data_four.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,Generation X,Millenials
2020,10,15
2021,20,25
2023,25,30
2024,30,35
19 changes: 12 additions & 7 deletions lib/data/landing_page_content_items/statistics/data_one.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Date,variable,value
2024-01-01,variable_name,10
2024-02-01,variable_name,11
2024-03-01,variable_name,12
2024-04-01,variable_name,13
2024-05-01,variable_name,14
2024-06-01,variable_name,15
,Some decimal data
1/6/2013,0.500
1/6/2014,0.600
1/6/2015,0.700
1/6/2016,0.500
1/6/2017,0.600
1/6/2018,0.700
1/6/2019,0.500
1/6/2020,
1/6/2021,
1/6/2022,0.500
1/6/2023,0.600
8 changes: 8 additions & 0 deletions lib/data/landing_page_content_items/statistics/data_three.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
,The number of people representing some data point
31/12/2022,45775
31/3/2023,47518
30/6/2023,50546
30/9/2023,56042
31/12/2023,45768
31/3/2024,34530
30/6/2024,29585
14 changes: 7 additions & 7 deletions lib/data/landing_page_content_items/statistics/data_two.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Date,variable,value
2023-01-01,variable_name,20
2023-02-01,variable_name,21
2023-03-01,variable_name,22
2023-01-01,variable_name_two,23
2023-02-01,variable_name_two,24
2023-03-01,variable_name_two,25
,Percentage of people representing some data point
1/6/2018,70%
1/6/2019,50%
1/6/2020,60%
1/6/2021,64%
1/6/2022,52%
1/6/2023,91%
6 changes: 3 additions & 3 deletions lib/data/landing_page_content_items/tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ blocks:
hide_heading: true
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
csv_file: "data_two.csv"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
padding: true
- type: card
Expand All @@ -101,7 +101,7 @@ blocks:
hide_heading: true
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
csv_file: "data_three.csv"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
padding: true
- type: card
Expand All @@ -114,7 +114,7 @@ blocks:
hide_heading: true
x_axis_label: "X Axis"
y_axis_label: "Y Axis"
csv_file: "data_one.csv"
csv_file: "data_four.csv"
data_source_link: https://www.ons.gov.uk/economy/grossdomesticproductgdp/timeseries/ihyq/qna
padding: true
- type: card
Expand Down
22 changes: 22 additions & 0 deletions spec/factories/content_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@
title: "Data Two",
url: "https://www.asset.test.gov.uk/data_two.csv",
},
{
accessible: false,
attachment_type: "document",
content_type: "text/csv",
file_size: 123,
filename: "data_three.csv",
id: 12_347,
preview_url: "https://www.asset.test.gov.uk/data_three.csv/preview",
title: "Data Three",
url: "https://www.asset.test.gov.uk/data_three.csv",
},
{
accessible: false,
attachment_type: "document",
content_type: "text/csv",
file_size: 123,
filename: "data_four.csv",
id: 12_348,
preview_url: "https://www.asset.test.gov.uk/data_four.csv/preview",
title: "Data Four",
url: "https://www.asset.test.gov.uk/data_four.csv",
},
],
}
end
Expand Down
5 changes: 5 additions & 0 deletions spec/fixtures/landing_page_statistics_data/data_four.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,Generation X,Millenials
2020,10,15
2021,20,25
2023,25,30
2024,30,35
15 changes: 8 additions & 7 deletions spec/fixtures/landing_page_statistics_data/data_one.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Date,variable,value
2024-01-01,variable_name,10
2024-02-01,variable_name,11
2024-03-01,variable_name,12
2024-04-01,variable_name,13
2024-05-01,variable_name,14
2024-06-01,variable_name,15
,Percentage of people being kinder to one and other
1/6/2013,0.500
1/6/2014,0.600
1/6/2015,0.700
1/6/2016,0.500
1/6/2017,0.600
1/6/2018,0.700
1/6/2019,0.550
8 changes: 8 additions & 0 deletions spec/fixtures/landing_page_statistics_data/data_three.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
,The number of people working smarter not harder
31/12/2022,45775
31/3/2023,47518
30/6/2023,50546
30/9/2023,56042
31/12/2023,45768
31/3/2024,34530
30/6/2024,29585
19 changes: 12 additions & 7 deletions spec/fixtures/landing_page_statistics_data/data_two.csv
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
Date,variable,value
2024-01-01,variable_name,10
2024-02-01,variable_name,11
2024-03-01,variable_name,12
2024-01-01,variable_name_two,13
2024-02-01,variable_name_two,14
2024-03-01,variable_name_two,15
,Percentage of people being kinder to one and other
1/6/2013,0.500
1/6/2014,0.600
1/6/2015,0.700
1/6/2016,0.500
1/6/2017,0.600
1/6/2018,0.700
1/6/2019,0.500
1/6/2020,
1/6/2021,
1/6/2022,0.500
1/6/2023,0.600
2 changes: 1 addition & 1 deletion spec/models/content_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@

describe "#attachments" do
it "loads the attachment data from the content item" do
expect(subject.attachments.count).to eq(2)
expect(subject.attachments.count).to eq(4)
expect(subject.attachments[0].title).to eq("Data One")
end
end
Expand Down
Loading

0 comments on commit 22086c6

Please sign in to comment.