-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EPBR-6817 added co2 data to performance data csv
Refactors existing fetch stats use case and fetch cvs to inherit from the same base class. Moved the core functionality from the fetch_statistics_csv.rb to the existing domain class to better isolate tests. Created a new method of that class to create the object sent to controller to be converted into a csv. Added the key for the this class for the new data point.
- Loading branch information
1 parent
5fbe50c
commit 3b0d36c
Showing
11 changed files
with
272 additions
and
159 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 |
---|---|---|
@@ -1,41 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module UseCase | ||
class FetchStatistics | ||
def initialize(statistics_gateway:, co2_gateway:) | ||
@statistics_gateway = statistics_gateway | ||
@co2_gateway = co2_gateway | ||
end | ||
|
||
class FetchStatistics < UseCase::FetchStatisticsBase | ||
def execute | ||
register_data = @statistics_gateway.fetch[:data] | ||
warehouse_data = @co2_gateway.fetch[:data] | ||
@domain = Domain::StatisticsResults.new(register_data: register_data[:assessments], warehouse_data:) | ||
@domain.set_results unless warehouse_data.empty? | ||
@domain.get_results | ||
end | ||
|
||
private | ||
|
||
def raise_errors_if_exists(response) | ||
return unless response.include?(:errors) | ||
|
||
response[:errors].each do |error| | ||
if error[:code] == "Auth::Errors::TokenMissing" | ||
raise Errors::AuthTokenMissing | ||
end | ||
if error[:code] == "PAYLOAD_TOO_LARGE" | ||
raise Errors::TooManyResults | ||
end | ||
|
||
yield(error) if block_given? | ||
end | ||
|
||
raise Errors::UnknownErrorResponseError, | ||
sprintf( | ||
"Unknown error response from internal API; errors sent were: %s", | ||
response[:errors].to_s, | ||
) | ||
end | ||
end | ||
end |
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,31 @@ | ||
module UseCase | ||
class FetchStatisticsBase | ||
def initialize(statistics_gateway:, co2_gateway:) | ||
@statistics_gateway = statistics_gateway | ||
@co2_gateway = co2_gateway | ||
end | ||
|
||
private | ||
|
||
def raise_errors_if_exists(response) | ||
return unless response.include?(:errors) | ||
|
||
response[:errors].each do |error| | ||
if error[:code] == "Auth::Errors::TokenMissing" | ||
raise Errors::AuthTokenMissing | ||
end | ||
if error[:code] == "PAYLOAD_TOO_LARGE" | ||
raise Errors::TooManyResults | ||
end | ||
|
||
yield(error) if block_given? | ||
end | ||
|
||
raise Errors::UnknownErrorResponseError, | ||
sprintf( | ||
"Unknown error response from internal API; errors sent were: %s", | ||
response[:errors].to_s, | ||
) | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,27 +1,13 @@ | ||
# frozen_string_literal: true | ||
|
||
module UseCase | ||
class FetchStatisticsCsv < UseCase::Base | ||
class FetchStatisticsCsv < UseCase::FetchStatisticsBase | ||
def execute(country = "all") | ||
data = @gateway.fetch[:data][:assessments] | ||
country_key = country == "northern-ireland" ? :northernIreland : country.to_sym | ||
results = data.key?(country_key.to_sym) ? data[country_key.to_sym] : data[:all] | ||
|
||
return_array = [] | ||
months = results.group_by { |h| h[:month] }.keys | ||
types = %w[SAP RdSAP CEPC DEC DEC-RR AC-CERT] | ||
months.each do |month| | ||
hash = { "Month" => Date.parse("#{month}-01").strftime("%b-%Y") } | ||
types.each do |type| | ||
stats_item = results.select { |item| item[:month] == month && item[:assessmentType] == type }.first | ||
hash["#{type}s Lodged"] = !stats_item.nil? && stats_item.key?(:numAssessments) && !stats_item[:numAssessments].nil? ? stats_item[:numAssessments] : nil | ||
if %w[SAP RdSAP CEPC].include?(type) && !stats_item.nil? | ||
hash["Average #{type} Energy Rating"] = stats_item.key?(:ratingAverage) && !stats_item[:ratingAverage].nil? ? stats_item[:ratingAverage].round(2) : nil | ||
end | ||
end | ||
return_array << hash | ||
end | ||
return_array | ||
register_data = @statistics_gateway.fetch[:data] | ||
warehouse_data = @co2_gateway.fetch[:data] | ||
@domain = Domain::StatisticsResults.new(register_data: register_data[:assessments], warehouse_data:) | ||
@domain.set_results unless warehouse_data.empty? | ||
@domain.to_csv_hash(country:) | ||
end | ||
end | ||
end |
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,82 @@ | ||
shared_examples "Domain::Statistics" do | ||
def england | ||
[{ "Month" => "Oct-2021", | ||
"SAPs Lodged" => 20_489, | ||
"Average SAP Energy Rating" => 78.40, | ||
"Average SAP CO2/sqm emissions" => nil, | ||
"RdSAPs Lodged" => 120_045, | ||
"Average RdSAP Energy Rating" => 61.73, | ||
"Average RdSAP CO2/sqm emissions" => nil, | ||
"CEPCs Lodged" => 8074, | ||
"Average CEPC Energy Rating" => 67.33, | ||
"DECs Lodged" => 2781, | ||
"DEC-RRs Lodged" => 462, | ||
"AC-CERTs Lodged" => 1206 }, | ||
{ "Month" => "Sep-2021", | ||
"SAPs Lodged" => 23_834, | ||
"Average SAP Energy Rating" => 77.82, | ||
"Average SAP CO2/sqm emissions" => 15.74, | ||
"RdSAPs Lodged" => 119_033, | ||
"Average RdSAP Energy Rating" => 61.74, | ||
"Average RdSAP CO2/sqm emissions" => 10.88, | ||
"CEPCs Lodged" => 7572, | ||
"Average CEPC Energy Rating" => 68.18, | ||
"DECs Lodged" => 3298, | ||
"DEC-RRs Lodged" => 402, | ||
"AC-CERTs Lodged" => 861 }] | ||
end | ||
|
||
def northern_ireland | ||
[{ "Month" => "Oct-2021", | ||
"SAPs Lodged" => 674, | ||
"Average SAP Energy Rating" => 81.70, | ||
"Average SAP CO2/sqm emissions" => nil, | ||
"RdSAPs Lodged" => 2349, | ||
"Average RdSAP Energy Rating" => 61.11, | ||
"Average RdSAP CO2/sqm emissions" => nil, | ||
"CEPCs Lodged" => 134, | ||
"Average CEPC Energy Rating" => 90.5, | ||
"DECs Lodged" => 38, | ||
"DEC-RRs Lodged" => 8, | ||
"AC-CERTs Lodged" => 45 }, | ||
{ "Month" => "Sep-2021", | ||
"SAPs Lodged" => 643, | ||
"Average SAP Energy Rating" => 82.56, | ||
"Average SAP CO2/sqm emissions" => nil, | ||
"RdSAPs Lodged" => 2466, | ||
"Average RdSAP Energy Rating" => 58.59, | ||
"Average RdSAP CO2/sqm emissions" => nil, | ||
"CEPCs Lodged" => 146, | ||
"Average CEPC Energy Rating" => 79.33, | ||
"DECs Lodged" => 202, | ||
"DEC-RRs Lodged" => 26, | ||
"AC-CERTs Lodged" => 57 }] | ||
end | ||
|
||
def all_countries | ||
[{ "Month" => "Oct-2021", | ||
"SAPs Lodged" => 21_163, | ||
"Average SAP Energy Rating" => 78.41, | ||
"Average SAP CO2/sqm emissions" => nil, | ||
"RdSAPs Lodged" => 122_394, | ||
"Average RdSAP Energy Rating" => 61.71, | ||
"Average RdSAP CO2/sqm emissions" => nil, | ||
"CEPCs Lodged" => 8208, | ||
"Average CEPC Energy Rating" => 67.36, | ||
"DECs Lodged" => 2189, | ||
"DEC-RRs Lodged" => 470, | ||
"AC-CERTs Lodged" => 1251 }, | ||
{ "Month" => "Sep-2021", | ||
"SAPs Lodged" => 24_477, | ||
"Average SAP Energy Rating" => 77.83, | ||
"Average SAP CO2/sqm emissions" => 16.24, | ||
"RdSAPs Lodged" => 121_499, | ||
"Average RdSAP Energy Rating" => 61.71, | ||
"Average RdSAP CO2/sqm emissions" => 10.88, | ||
"CEPCs Lodged" => 7718, | ||
"Average CEPC Energy Rating" => 68.27, | ||
"DECs Lodged" => 3500, | ||
"DEC-RRs Lodged" => 428, | ||
"AC-CERTs Lodged" => 918 }] | ||
end | ||
end |
Oops, something went wrong.