Skip to content

Commit

Permalink
⚡️ Améliore la vitesse d'éxécution du script d'attribution des assets…
Browse files Browse the repository at this point in the history
… aux questions
  • Loading branch information
marouria committed Nov 13, 2024
1 parent 16079b1 commit 3eb0114
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 60 deletions.
28 changes: 14 additions & 14 deletions lib/google_drive_storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

class GoogleDriveStorage
class Error < StandardError; end

attr_reader :session

GOOGLE_DRIVE_ACCOUNT_CONFIG = {
'type' => 'service_account',
'project_id' => ENV.fetch('GOOGLE_PROJECT_ID', nil),
Expand All @@ -18,29 +21,26 @@ class Error < StandardError; end
'universe_domain' => 'googleapis.com'
}.freeze

def self.recupere_session
def initialize
@session = recupere_session
end

def recupere_session
Tempfile.create(['google_drive_service_account', '.json']) do |file|
file.write(GOOGLE_DRIVE_ACCOUNT_CONFIG.to_json)
file.rewind
GoogleDrive::Session.from_service_account_key(file.path)
end
end

def self.existe_dossier?(dossier_id)
session = recupere_session
begin
@dossier = session.collection_by_id(dossier_id)
rescue Google::Apis::ClientError => e
raise Error, "Le dossier avec l'id '#{dossier_id}' n'est pas accessible : #{e.message}"
end
def existe_dossier?(dossier_id)
true if @session.collection_by_id(dossier_id)
rescue Google::Apis::ClientError => e
raise Error, "Le dossier avec l'id '#{dossier_id}' n'est pas accessible : #{e.message}"
end

def self.recupere_fichier(dossier_id, nom_fichier)
unless existe_dossier?(dossier_id)
raise "Le dossier avec l'id '#{dossier_id}' n'est pas accessible"
end

file = @dossier.files.find { |f| f.name == nom_fichier }
def recupere_fichier(dossier_id, nom_fichier)
file = @session.file_by_title(nom_fichier)
raise Error, "Fichier '#{nom_fichier}' n'existe pas dans le dossier '#{dossier_id}'" unless file

file
Expand Down
21 changes: 10 additions & 11 deletions lib/tasks/questions.rake
Original file line number Diff line number Diff line change
Expand Up @@ -46,36 +46,35 @@ namespace :questions do
dossier_id = 'DOSSIER_ID'
logger = RakeLogger.logger
unless ENV.key?(dossier_id)
logger.error "La variable d'environnement #{dossier_id} est manquante"
logger.error "La variable d'environnement DOSSIER_ID est manquante"
logger.info 'Usage : rake questions:attache_assets DOSSIER_ID=<dossier_id>'
next
end

@drive = GoogleDriveStorage.new
begin
GoogleDriveStorage.existe_dossier?(ENV.fetch(dossier_id, nil))
@drive.existe_dossier?(ENV.fetch(dossier_id, nil))
rescue GoogleDriveStorage::Error => e
logger.error e.message
next
end

NOM_TECHNIQUES_QCM.each do |question_nom_technique, nom_illustration|
question = Question.find_by(nom_technique: question_nom_technique)
if question.blank?
if question
recupere_fichier(dossier_id, question, "#{nom_illustration}.png")
recupere_fichier(dossier_id, question, "#{question.nom_technique}.mp3",
question.transcription_intitule)
attach_audio_choix(dossier_id, question)
else
puts "Pas de question trouvée pour le nom_technique '#{question_nom_technique}'"
end
next if question.blank?

recupere_fichier(ENV.fetch(dossier_id), question, "#{nom_illustration}.png")
recupere_fichier(ENV.fetch(dossier_id), question, "#{question.nom_technique}.mp3",
question.transcription_intitule)
attach_audio_choix(ENV.fetch(dossier_id), question)
end
puts 'Création des assets finie.'
end
end

def recupere_fichier(dossier_id, question, fichier_path, model = nil)
file = GoogleDriveStorage.recupere_fichier(dossier_id, fichier_path)
file = @drive.recupere_fichier(dossier_id, fichier_path)
if file
file_content = file.download_to_string
attach_file_to(model || question, file_content, fichier_path, question.nom_technique)
Expand Down
38 changes: 18 additions & 20 deletions spec/lib/google_drive_storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,71 +5,69 @@

describe GoogleDriveStorage do
let(:mock_session) { instance_double(GoogleDrive::Session) }
let(:mock_folder) { instance_double(GoogleDrive::Collection) }
let(:mock_file) { instance_double(GoogleDrive::File, name: 'programme_tele.png') }
let(:mock_folder) { instance_double(GoogleDrive::Collection) }
let(:folder_id) { 'fake-folder-id' }
let(:file_name) { 'programme_tele.png' }
let(:storage) { described_class.new }

before do
allow(described_class).to receive(:recupere_session).and_return(mock_session)
allow(storage).to receive(:recupere_session).and_return(mock_session)
end

describe '.recupere_session' do
describe '#recupere_session' do
it "s'authentifie avec succès et retourne une session valide" do
expect(described_class.recupere_session).to eq(mock_session)
expect(storage.recupere_session).to eq(mock_session)
end
end

describe '.recupere_fichier' do
before do
allow(mock_session).to receive(:collection_by_id).with(folder_id).and_return(mock_folder)
end

context 'quand le fichier existe dans le dossier' do
describe '#recupere_fichier' do
context 'quand le fichier existe' do
before do
allow(mock_folder).to receive(:files).and_return([mock_file])
allow(storage.session).to receive(:file_by_title).with(file_name).and_return(mock_file)
end

it 'retourne le fichier' do
file = described_class.recupere_fichier(folder_id, file_name)
file = storage.recupere_fichier(folder_id, file_name)
expect(file).not_to be_nil
expect(file.name).to eq(file_name)
end
end

context "quand le fichier n'existe pas dans le dossier" do
context "quand le fichier n'existe pas" do
before do
allow(mock_folder).to receive(:files).and_return([])
allow(storage.session).to receive(:file_by_title).with(file_name).and_return(nil)
end

it 'retourne une erreur' do
expect { described_class.recupere_fichier(folder_id, file_name) }.to raise_error(
expect { storage.recupere_fichier(folder_id, file_name) }.to raise_error(
GoogleDriveStorage::Error,
"Fichier '#{file_name}' n'existe pas dans le dossier '#{folder_id}'"
)
end
end
end

describe '.existe_dossier?' do
describe '#existe_dossier?' do
context 'quand le dossier existe' do
before do
allow(mock_session).to receive(:collection_by_id).with(folder_id).and_return(mock_folder)
allow(storage.session).to receive(:collection_by_id).with(folder_id).and_return(mock_folder)
end

it 'retourne true' do
expect(described_class).to be_existe_dossier(folder_id)
expect(storage.existe_dossier?(folder_id)).to be true
end
end

context "quand le dossier n'existe pas" do
let!(:error) { Google::Apis::ClientError.new("notFound: File not found: #{folder_id}") }

before do
allow(mock_session).to receive(:collection_by_id).with(folder_id).and_raise(error)
allow(storage.session).to receive(:collection_by_id).with(folder_id).and_raise(error)
end

it 'renvoie une erreur' do
expect { described_class.existe_dossier?(folder_id) }.to raise_error(
expect { storage.existe_dossier?(folder_id) }.to raise_error(
GoogleDriveStorage::Error,
"Le dossier avec l'id '#{folder_id}' n'est pas accessible : " \
"notFound: File not found: #{folder_id}"
Expand Down
26 changes: 11 additions & 15 deletions spec/tasks/questions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

context 'appellé avec une dossier id inconnu' do
let(:error_message) do
"Le dossier avec l'id 'inconnue' n'est pas accessible: " \
"Le dossier avec l'id 'inconnue' n'est pas accessible : " \
'notFound: File not found: inconnue.'
end

before do
ENV['DOSSIER_ID'] = 'inconnue'
allow(GoogleDriveStorage).to receive(:existe_dossier?).with('inconnue').and_raise(
allow(GoogleDriveStorage.new).to receive(:existe_dossier?).with('inconnue').and_raise(
GoogleDriveStorage::Error, error_message
)
subject.reenable
Expand Down Expand Up @@ -60,20 +60,16 @@

before do
ENV['DOSSIER_ID'] = 'fake-dossier-id'
allow(GoogleDriveStorage).to receive(:existe_dossier?).with('fake-dossier-id')
.and_return(true)
drive = instance_double(GoogleDriveStorage)
allow(GoogleDriveStorage).to receive(:new).and_return(drive)
allow(drive).to receive(:existe_dossier?).with('fake-dossier-id').and_return(true)

file_names = ['programme_tele.png', 'lodi_1.mp3', 'LOdi_couverture.mp3', 'LOdi_drap.mp3']
file_names.each_with_index do |file_name, index|
allow(drive).to receive(:recupere_fichier).with('fake-dossier-id',
file_name).and_return(fake_files[index])
end

allow(GoogleDriveStorage).to receive(:recupere_fichier).with('fake-dossier-id',
'programme_tele.png')
.and_return(fake_files[0])
allow(GoogleDriveStorage).to receive(:recupere_fichier).with('fake-dossier-id', 'lodi_1.mp3')
.and_return(fake_files[1])
allow(GoogleDriveStorage).to receive(:recupere_fichier).with('fake-dossier-id',
'LOdi_couverture.mp3')
.and_return(fake_files[2])
allow(GoogleDriveStorage).to receive(:recupere_fichier).with('fake-dossier-id',
'LOdi_drap.mp3')
.and_return(fake_files[3])
subject.reenable
subject.invoke
end
Expand Down

0 comments on commit 3eb0114

Please sign in to comment.