Skip to content

Commit

Permalink
Add flags to prevent code running in envs where dfe-analytics isn't s…
Browse files Browse the repository at this point in the history
…et up (#130)

* Add flags to prevnt code running in envs where dfe-analytics isn't set up
  • Loading branch information
ericaporter authored Apr 22, 2024
1 parent 8001546 commit 9d89664
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/dfe/analytics/entity_table_check_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Analytics
# To ensure BigQuery is in sync with the database
class EntityTableCheckJob < AnalyticsJob
def perform
return unless DfE::Analytics.entity_table_checks_enabled?
return unless DfE::Analytics.enabled? && DfE::Analytics.entity_table_checks_enabled?

entity_tag = Time.now.strftime('%Y%m%d%H%M%S')
DfE::Analytics.entities_for_analytics.each do |entity_name|
Expand Down
6 changes: 5 additions & 1 deletion lib/dfe/analytics/send_events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ module DfE
module Analytics
class SendEvents < AnalyticsJob
def self.do(events)
# The initialise event is a one-off event that must be sent to BigQuery once only
unless DfE::Analytics.enabled?
Rails.logger.info('DfE::Analytics::SendEvents.do() called but DfE::Analytics is disabled. Please check DfE::Analytics.enabled? before sending events to BigQuery')
return
end

# The initialise event is a one-off event that must be sent to BigQuery once only
DfE::Analytics::InitialisationEvents.trigger_initialisation_events unless DfE::Analytics::InitialisationEvents.initialisation_events_sent?

events = events.map { |event| event.is_a?(Event) ? event.as_json : event }
Expand Down
8 changes: 8 additions & 0 deletions lib/dfe/analytics/tasks/import_entities.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ namespace :dfe do
namespace :analytics do
desc 'Send Analytics events for the (allowlisted) state of all records in the database'
task :import_all_entities, %i[batch_size] => :environment do |_, args|
return unless DfE::Analytics.enabled?

puts 'DfE Analytics is not enabled - Ignoring import_all_entities'

entity_tag = Time.now.strftime('%Y%m%d%H%M%S')
DfE::Analytics.entities_for_analytics.each do |entity_name|
DfE::Analytics::LoadEntities.new(entity_name: entity_name, **args).run(entity_tag: entity_tag)
Expand All @@ -11,6 +15,10 @@ namespace :dfe do

desc 'Send Analytics events for the state of all records in a specified model'
task :import_entity, %i[entity_name batch_size] => :environment do |_, args|
return unless DfE::Analytics.enabled?

puts 'DfE Analytics is not enabled - Ignoring import_entity'

abort('You need to specify a model name as an argument to the Rake task, eg dfe:analytics:import_entity[Model]') unless args[:entity_name]

entity_tag = Time.now.strftime('%Y%m%d%H%M%S')
Expand Down
17 changes: 17 additions & 0 deletions spec/dfe/analytics/entity_table_check_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,25 @@
end

describe '#perform' do
context 'when dfe-analytics is not enabled' do
before do
allow(DfE::Analytics).to receive(:enabled?).and_return(false)
allow(DfE::Analytics).to receive(:entity_table_checks_enabled?).and_return(true)
allow(DfE::Analytics).to receive(:entities_for_analytics).and_return(%w[Candidate Application])
end

it 'does not call EntityTableChecks' do
expect(DfE::Analytics::Services::EntityTableChecks).not_to receive(:call)

described_class.new.perform
end
end

context 'when entity table checks are not enabled' do
before do
allow(DfE::Analytics).to receive(:enabled?).and_return(true)
allow(DfE::Analytics).to receive(:entity_table_checks_enabled?).and_return(false)
allow(DfE::Analytics).to receive(:entities_for_analytics).and_return(%w[Candidate Application])
end

it 'does not call EntityTableChecks' do
Expand All @@ -34,6 +50,7 @@

context 'when entity table checks are enabled' do
before do
allow(DfE::Analytics).to receive(:enabled?).and_return(true)
allow(DfE::Analytics).to receive(:entity_table_checks_enabled?).and_return(true)
allow(DfE::Analytics).to receive(:entities_for_analytics).and_return(%w[Candidate Application])
end
Expand Down
1 change: 1 addition & 0 deletions spec/dfe/analytics/load_entity_batch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
end

before do
allow(DfE::Analytics).to receive(:enabled?).and_return(true)
allow(DfE::Analytics::SendEvents).to receive(:perform_now)

allow(DfE::Analytics).to receive(:allowlist).and_return({
Expand Down

0 comments on commit 9d89664

Please sign in to comment.