Skip to content

Commit

Permalink
Merge pull request #94 from identity-research-lab/return-graph-export…
Browse files Browse the repository at this point in the history
…-to-bg

Write to graph in background job
  • Loading branch information
CoralineAda authored Aug 25, 2024
2 parents 9aac983 + 4aecc8f commit 10b1004
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 34 deletions.
13 changes: 13 additions & 0 deletions app/jobs/export_to_graph_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This background job performs the graph export process.
class ExportToGraphJob

include Sidekiq::Job

queue_as :default

def perform(id)
Rails.logger.info("ExportToGraphJob running with survey response #{id}")
ExportToGraph.perform(id)
end

end
33 changes: 3 additions & 30 deletions app/models/survey_response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ class SurveyResponse < ApplicationRecord
require 'csv'
require 'openai'

# before_save :sanitize_array_values
after_save_commit :export_to_graph
after_save_commit :enqueue_export_to_graph
after_create :enqueue_keyword_extraction
after_create :enqueue_sentiment_analysis

Expand Down Expand Up @@ -41,8 +40,8 @@ def enqueue_sentiment_analysis
end

# Invokes a service to update the graph databases from this SurveyResponse object.
def export_to_graph
ExportToGraph.perform(self.id)
def enqueue_export_to_graph
ExportToGraphJob.perform_async(self.id)
end

# TODO move this to a service worker
Expand Down Expand Up @@ -76,30 +75,4 @@ def graph_query
}
end

private

# # TODO Clean this up
# def sanitize_array_values
# self.age_exp_codes = age_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.klass_exp_codes = klass_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.race_ethnicity_exp_codes = race_ethnicity_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.religion_exp_codes = religion_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.disability_exp_codes = disability_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.neurodiversity_exp_codes = neurodiversity_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.gender_exp_codes = gender_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.lgbtqia_exp_codes = lgbtqia_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.pronouns_exp_codes = pronouns_exp_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.pronouns_feel_codes = pronouns_feel_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
#
# self.pronouns_id_codes = pronouns_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.age_id_codes = age_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.klass_id_codes = klass_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.race_ethnicity_id_codes = race_ethnicity_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.religion_id_codes = religion_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.disability_id_codes = disability_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.neurodiversity_id_codes = neurodiversity_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.gender_id_codes = gender_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# self.lgbtqia_id_codes = lgbtqia_id_codes.join(", ").split(", ").map(&:strip).map(&:downcase)
# end

end
4 changes: 2 additions & 2 deletions app/services/export_to_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def populate_experience_codes
}.each do |context, codes|
codes.each do |name|
code = Code.find_or_create_by(name: name, context: context)
# Experiences.create(from_node: persona, to_node: code)
Experiences.create(from_node: persona, to_node: code)
end
end

Expand All @@ -69,7 +69,7 @@ def populate_id_codes
}.each do |context, codes|
codes.each do |name|
identity = Identity.find_or_create_by(name: name, context: context)
# IdentifiesWith.create(from_node: persona, to_node: identity)
IdentifiesWith.create(from_node: persona, to_node: identity)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/survey_response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe SurveyResponse do

before do
allow_any_instance_of(SurveyResponse).to receive(:export_to_graph)
allow_any_instance_of(SurveyResponse).to receive(:enqueue_export_to_graph)
allow_any_instance_of(SurveyResponse).to receive(:enqueue_keyword_extraction)
allow_any_instance_of(SurveyResponse).to receive(:enqueue_sentiment_analysis)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/services/import_from_csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
RSpec.describe ImportFromCsv do

before do
allow_any_instance_of(SurveyResponse).to receive(:export_to_graph)
allow_any_instance_of(SurveyResponse).to receive(:enqueue_export_to_graph)
allow_any_instance_of(SurveyResponse).to receive(:enqueue_keyword_extraction)
allow_any_instance_of(SurveyResponse).to receive(:enqueue_sentiment_analysis)
end
Expand Down

0 comments on commit 10b1004

Please sign in to comment.