Skip to content

Commit

Permalink
Allow manual loads for all solar feeds (#3216)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldodds authored Nov 14, 2023
1 parent ae6dd42 commit b3a5172
Show file tree
Hide file tree
Showing 21 changed files with 503 additions and 125 deletions.
10 changes: 10 additions & 0 deletions app/controllers/schools/low_carbon_hub_installations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def destroy
redirect_to school_solar_feeds_configuration_index_path(@school), notice: 'Low carbon hub deleted'
end

def check
@api_ok = Solar::LowCarbonHubInstallationFactory.check(@low_carbon_hub_installation)
respond_to(&:js)
end

def submit_job
Solar::LowCarbonHubLoaderJob.perform_later(installation: @low_carbon_hub_installation, notify_email: current_user.email)
redirect_to school_solar_feeds_configuration_index_path(@school), notice: "Loading job has been submitted. An email will be sent to #{current_user.email} when complete."
end

private

def low_carbon_hub_installation_params
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/schools/rtone_variant_installations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def destroy
redirect_to school_solar_feeds_configuration_index_path(@school), notice: 'New Rtone Variant API feed deleted.'
end

def check
@api_ok = Solar::LowCarbonHubInstallationFactory.check(@rtone_variant_installation)
respond_to(&:js)
end

def submit_job
Solar::RtoneVariantLoaderJob.perform_later(installation: @rtone_variant_installation, notify_email: current_user.email)
redirect_to school_solar_feeds_configuration_index_path(@school), notice: "Loading job has been submitted. An email will be sent to #{current_user.email} when complete."
end

private

def load_non_gas_meters
Expand Down
13 changes: 13 additions & 0 deletions app/jobs/solar/low_carbon_hub_loader_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Solar
class LowCarbonHubLoaderJob < BaseSolarLoaderJob
private

def upserter(start_date, end_date)
Solar::LowCarbonHubDownloadAndUpsert.new(installation: @installation, start_date: start_date, end_date: end_date)
end

def solar_feed_type
"Rtone"
end
end
end
13 changes: 13 additions & 0 deletions app/jobs/solar/rtone_variant_loader_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Solar
class RtoneVariantLoaderJob < BaseSolarLoaderJob
private

def upserter(start_date, end_date)
Solar::RtoneVariantDownloadAndUpsert.new(installation: @installation, start_date: start_date, end_date: end_date)
end

def solar_feed_type
"Rtone Variant"
end
end
end
13 changes: 13 additions & 0 deletions app/services/solar/low_carbon_hub_installation_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ def perform
installation
end

def self.check(installation)
username = installation.username || ENV['ENERGYSPARKSRBEEUSERNAME']
password = installation.password || ENV['ENERGYSPARKSRBEEPASSWORD']
begin
LowCarbonHubMeterReadings.new(username, password).full_installation_information(installation.is_a?(LowCarbonHubInstallation) ? installation.rbee_meter_id : installation.rtone_meter_id)
true
rescue => e
puts e.message
puts e.backtrace
false
end
end

private

def low_carbon_hub_api
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('#low-carbon-hub-<%= @low_carbon_hub_installation.id %>-test').html('<i class="fas <%= @api_ok ? 'text-success fa-circle-check' : 'text-danger fa-circle-xmark' %>"></i> Check API');
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
</tbody>
</table>

<p><%= link_to 'Delete', school_low_carbon_hub_installation_path(@school, @low_carbon_hub_installation), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' %>
<p><%= link_to 'Delete', school_low_carbon_hub_installation_path(@school, @low_carbon_hub_installation), method: :delete, data: { confirm: 'Are you sure? This will delete the meters' }, class: 'btn' %>
1 change: 1 addition & 0 deletions app/views/schools/rtone_variant_installations/check.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('#rtone-variant-<%= @rtone_variant_installation.id %>-test').html('<i class="fas <%= @api_ok ? 'text-success fa-circle-check' : 'text-danger fa-circle-xmark' %>"></i> Check API');
22 changes: 16 additions & 6 deletions app/views/schools/solar_feeds_configuration/_rtone_feeds.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
<table class="table table-sm">
<tbody>
<tr>
<th class="w-25">RBee Id</th>
<th class="w-25">Username</th>
<th>RBee Id</th>
<th>Username</th>
<th>Password</th>
<th class="w-25">Actions</th>
<th>Last updated</th>
<th>Actions</th>
</tr>
<% rtone_installations.each do |installation| %>
<tr>
Expand All @@ -15,11 +16,20 @@
</td>
<td><%= installation.username %></td>
<td><%= installation.password %></td>
<td><%= nice_date_times(installation.updated_at) %></td>
<td>
<p>
<div class="btn-group">
<%= link_to 'Edit', edit_school_low_carbon_hub_installation_path(school, installation), class: 'btn' %>
<%= link_to 'Delete', school_low_carbon_hub_installation_path(school, installation), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' %>
</p>
<%= link_to 'Delete', school_low_carbon_hub_installation_path(school, installation), method: :delete, data: { confirm: 'Are you sure? This will delete the meters' }, class: 'btn' %>

<%= link_to "#{fa_icon('circle-question')} Check API".html_safe, check_school_low_carbon_hub_installation_path(school, installation), id: "low-carbon-hub-#{installation.id}-test", method: :post, remote: true, class: 'btn check-button' %>

<%= form_tag submit_job_school_low_carbon_hub_installation_path(school, installation), method: :post do %>
<%= button_tag(type: 'submit', id: "low-carbon-hub-#{installation.id}-run-load", class: 'btn') do %>
<%= fa_icon('upload') %> Run Loader
<% end %>
<% end %>
</div>
</td>
</tr>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<th>Meter</th>
<th>Username</th>
<th>Password</th>
<th class="w-25">Actions</th>
<th>Last updated</th>
<th>Actions</th>
</tr>
<% rtone_variant_installations.each do |installation| %>
<tr>
Expand All @@ -16,11 +17,19 @@
<td><%= installation.meter.mpan_mprn %></td>
<td><%= installation.username %></td>
<td><%= installation.password %></td>
<td><%= nice_date_times(installation.updated_at) %></td>
<td>
<p>
<div class="btn-group">
<%= link_to 'Edit', edit_school_rtone_variant_installation_path(school, installation), class: 'btn' %>
<%= link_to 'Delete', school_rtone_variant_installation_path(school, installation), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' %>
</p>
<%= link_to 'Delete', school_rtone_variant_installation_path(school, installation), method: :delete, data: { confirm: 'Are you sure? This will delete the meters' }, class: 'btn' %>
<%= link_to "#{fa_icon('circle-question')} Check API".html_safe, check_school_rtone_variant_installation_path(school, installation), id: "rtone-variant-#{installation.id}-test", method: :post, remote: true, class: 'btn check-button' %>

<%= form_tag submit_job_school_rtone_variant_installation_path(school, installation), method: :post do %>
<%= button_tag(type: 'submit', id: "rtone-variant-#{installation.id}-run-load", class: 'btn') do %>
<%= fa_icon('upload') %> Run Loader
<% end %>
<% end %>
</div>
</td>
</tr>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<div class="btn-group">
<%= link_to 'Edit', edit_school_solar_edge_installation_path(school, installation), class: 'btn' %>
<%= link_to 'Delete', school_solar_edge_installation_path(school, installation), method: :delete, data: { confirm: 'Are you sure? This will delete the meters' }, class: 'btn btn-danger' %>
<%= link_to "#{fa_icon('circle-question')} Check API".html_safe, check_school_solar_edge_installation_path(school, installation), id: "solar-edge-#{installation.id}-test", method: :post, remote: true, class: 'btn' %>
<%= link_to "#{fa_icon('circle-question')} Check API".html_safe, check_school_solar_edge_installation_path(school, installation), id: "solar-edge-#{installation.id}-test", method: :post, remote: true, class: 'btn check-button' %>

<%= form_tag submit_job_school_solar_edge_installation_path(school, installation), method: :post do %>
<%= button_tag(type: 'submit', id: "solar-edge-#{installation.id}-run-load", class: 'btn') do %>
Expand Down
14 changes: 12 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,18 @@
post :submit_job
end
end
resources :low_carbon_hub_installations, only: [:new, :show, :create, :edit, :update, :destroy]
resources :rtone_variant_installations, only: [:new, :create, :edit, :update, :destroy]
resources :low_carbon_hub_installations, only: [:new, :show, :create, :edit, :update, :destroy] do
member do
post :check
post :submit_job
end
end
resources :rtone_variant_installations, only: [:new, :create, :edit, :update, :destroy] do
member do
post :check
post :submit_job
end
end

resource :meter_readings_validation, only: [:create]

Expand Down
6 changes: 3 additions & 3 deletions spec/factories/low_carbon_hub_installations_factory.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
FactoryBot.define do
factory :low_carbon_hub_installation do
school
sequence(:rbee_meter_id, (100000..900000).cycle) { |n| n }
sequence(:rbee_meter_id) { |n| n }
amr_data_feed_config

username { |n| "username_#{n}" }
password { |n| "password_#{n}" }
sequence(:username) { |n| "username_#{n}" }
sequence(:password) { |n| "password_#{n}" }

trait :with_electricity_meter do
after(:create) do |low_carbon_hub_installation, _evaluator|
Expand Down
7 changes: 3 additions & 4 deletions spec/factories/rtone_variant_installations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
amr_data_feed_config
association :meter, factory: :electricity_meter

username { |n| "username_#{n}" }
password { |n| "password_#{n}" }

sequence(:rtone_meter_id, (100000..900000).cycle) { |n| n }
sequence(:username) { |n| "username_#{n}" }
sequence(:password) { |n| "password_#{n}" }
sequence(:rtone_meter_id) { |n| n }
rtone_component_type { 1 }
end
end
63 changes: 63 additions & 0 deletions spec/jobs/solar/low_carbon_hub_loader_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rails_helper'

describe Solar::LowCarbonHubLoaderJob do
include Rails.application.routes.url_helpers
let!(:installation) { create(:solar_edge_installation) }
let(:job) { Solar::LowCarbonHubLoaderJob.new }

let!(:import_log) { create(:amr_data_feed_import_log, records_updated: 4, records_imported: 100) }
let(:upserter) { instance_double(Solar::LowCarbonHubDownloadAndUpsert, perform: nil, import_log: import_log) }

include_context "when sending solar loader job emails"

describe '#perform' do
let(:job_result) { job.perform(installation: installation, start_date: start_date, end_date: end_date, notify_email: admin.email) }

context 'when the load is successful' do
before do
allow(Solar::LowCarbonHubDownloadAndUpsert).to receive(:new).and_return(upserter)
end

it 'calls the upserter' do
expect(Solar::LowCarbonHubDownloadAndUpsert).to receive(:new).with(start_date: start_date, end_date: end_date, installation: installation)
job_result
end

context 'when sending the email' do
before do
job_result
end

it_behaves_like 'a successful solar loader job', solar_feed_type: 'Rtone'
end
end

context 'when the load is unsuccessful' do
let!(:import_log) { create(:amr_data_feed_import_log, error_messages: "There are errors here") }

before do
allow(Solar::LowCarbonHubDownloadAndUpsert).to receive(:new).and_return(upserter)
end

context 'with a loading error' do
before do
job_result
end

it_behaves_like 'a solar loader job with loader errors', solar_feed_type: 'Rtone'
end

context 'with an unexpected exception' do
before do
allow(Solar::LowCarbonHubDownloadAndUpsert).to receive(:new).and_raise("Its broken")
#rubocop:disable RSpec/ExpectInHook
expect(Rollbar).to receive(:error).with(anything, job: :import_solar_edge_readings)
#rubocop:enable RSpec/ExpectInHook
job_result
end

it_behaves_like 'a solar loader job that had an exception', solar_feed_type: 'Rtone'
end
end
end
end
63 changes: 63 additions & 0 deletions spec/jobs/solar/rtone_variant_loader_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'rails_helper'

describe Solar::RtoneVariantLoaderJob do
include Rails.application.routes.url_helpers
let!(:installation) { create(:solar_edge_installation) }
let(:job) { Solar::RtoneVariantLoaderJob.new }

let!(:import_log) { create(:amr_data_feed_import_log, records_updated: 4, records_imported: 100) }
let(:upserter) { instance_double(Solar::RtoneVariantDownloadAndUpsert, perform: nil, import_log: import_log) }

include_context "when sending solar loader job emails"

describe '#perform' do
let(:job_result) { job.perform(installation: installation, start_date: start_date, end_date: end_date, notify_email: admin.email) }

context 'when the load is successful' do
before do
allow(Solar::RtoneVariantDownloadAndUpsert).to receive(:new).and_return(upserter)
end

it 'calls the upserter' do
expect(Solar::RtoneVariantDownloadAndUpsert).to receive(:new).with(start_date: start_date, end_date: end_date, installation: installation)
job_result
end

context 'when sending the email' do
before do
job_result
end

it_behaves_like 'a successful solar loader job', solar_feed_type: 'Rtone Variant'
end
end

context 'when the load is unsuccessful' do
let!(:import_log) { create(:amr_data_feed_import_log, error_messages: "There are errors here") }

before do
allow(Solar::RtoneVariantDownloadAndUpsert).to receive(:new).and_return(upserter)
end

context 'with a loading error' do
before do
job_result
end

it_behaves_like 'a solar loader job with loader errors', solar_feed_type: 'Rtone Variant'
end

context 'with an unexpected exception' do
before do
allow(Solar::RtoneVariantDownloadAndUpsert).to receive(:new).and_raise("Its broken")
#rubocop:disable RSpec/ExpectInHook
expect(Rollbar).to receive(:error).with(anything, job: :import_solar_edge_readings)
#rubocop:enable RSpec/ExpectInHook
job_result
end

it_behaves_like 'a solar loader job that had an exception', solar_feed_type: 'Rtone Variant'
end
end
end
end
Loading

0 comments on commit b3a5172

Please sign in to comment.