Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flags to prevent code running in envs where dfe-analytics isn't set up #130

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 4 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,8 @@ 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?
ericaporter marked this conversation as resolved.
Show resolved Hide resolved

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 +13,8 @@ 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?
ericaporter marked this conversation as resolved.
Show resolved Hide resolved

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
ericaporter marked this conversation as resolved.
Show resolved Hide resolved

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
Loading