-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support api based migration of data between Quepid's (#834)
After trying to get the existing api's to do migration, realized it was putting a square peg in round hole, so added new dedicated endpoints.
- Loading branch information
Showing
55 changed files
with
1,086 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
module Export | ||
class BooksController < Api::ApiController | ||
before_action :find_book | ||
before_action :check_book | ||
|
||
def show | ||
respond_with @book | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
module Export | ||
class CasesController < Api::ApiController | ||
before_action :find_case | ||
before_action :check_case | ||
|
||
def show | ||
respond_with @case | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
module Import | ||
class BooksController < Api::ApiController | ||
# rubocop:disable Metrics/MethodLength | ||
# rubocop:disable Metrics/AbcSize | ||
# rubocop:disable Metrics/CyclomaticComplexity | ||
# rubocop:disable Metrics/PerceivedComplexity | ||
# rubocop:disable Layout/LineLength | ||
def create | ||
team_id = params.require(:team_id) | ||
params_to_use = book_params.to_h.deep_symbolize_keys | ||
|
||
@book = Book.new | ||
|
||
@book.team = Team.find(team_id) | ||
|
||
scorer_name = params_to_use[:scorer][:name] | ||
unless Scorer.exists?(name: scorer_name) | ||
@book.errors.add(:scorer, "Scorer with name '#{scorer_name}' needs to be migrated over first.") | ||
end | ||
|
||
selection_strategy_name = params_to_use[:selection_strategy][:name] | ||
unless SelectionStrategy.exists?(name: selection_strategy_name) | ||
@book.errors.add(:selection_strategy, | ||
"Selection strategy with name '#{selection_strategy_name}' needs to be migrated over first.") | ||
end | ||
|
||
if params_to_use[:query_doc_pairs] | ||
list_of_emails_of_users = [] | ||
params_to_use[:query_doc_pairs].each do |query_doc_pair| | ||
next unless query_doc_pair[:judgements] | ||
|
||
query_doc_pair[:judgements].each do |judgement| | ||
list_of_emails_of_users << judgement[:user_email] | ||
end | ||
end | ||
list_of_emails_of_users.uniq! | ||
list_of_emails_of_users.each do |email| | ||
unless User.exists?(email: email) | ||
@book.errors.add(:base, "User with email '#{email}' needs to be migrated over first.") | ||
end | ||
end | ||
end | ||
|
||
unless @book.errors.empty? | ||
render json: @book.errors, status: :bad_request | ||
return | ||
end | ||
|
||
# passed first set of validations. | ||
@book.name = params_to_use[:name] | ||
@book.show_rank = params_to_use[:show_rank] | ||
@book.support_implicit_judgements = params_to_use[:support_implicit_judgements] | ||
|
||
@book.scorer = Scorer.find_by(name: scorer_name) | ||
@book.selection_strategy = SelectionStrategy.find_by(name: selection_strategy_name) | ||
|
||
params_to_use[:query_doc_pairs]&.each do |query_doc_pair| | ||
qdp = @book.query_doc_pairs.build(query_doc_pair.except(:judgements)) | ||
next unless query_doc_pair[:judgements] | ||
|
||
query_doc_pair[:judgements].each do |judgement| | ||
judgement[:user] = User.find_by(email: judgement[:user_email]) | ||
qdp.judgements.build(judgement.except(:user_email)) | ||
end | ||
end | ||
|
||
if @book.save | ||
respond_with @book | ||
else | ||
render json: @book.errors, status: :bad_request | ||
end | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
# rubocop:enable Metrics/AbcSize | ||
# rubocop:enable Metrics/CyclomaticComplexity | ||
# rubocop:enable Metrics/PerceivedComplexity | ||
# rubocop:enable Layout/LineLength | ||
|
||
private | ||
|
||
def book_params | ||
params.require(:book).permit! | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
module Import | ||
class CasesController < Api::ApiController | ||
# rubocop:disable Metrics/MethodLength | ||
# rubocop:disable Metrics/AbcSize | ||
# rubocop:disable Metrics/CyclomaticComplexity | ||
# rubocop:disable Metrics/PerceivedComplexity | ||
def create | ||
list_of_emails_of_users = [] | ||
params_to_use = case_params.to_h.deep_symbolize_keys | ||
|
||
@case = Case.new | ||
|
||
list_of_emails_of_users << params_to_use[:owner_email] | ||
|
||
scorer_name = params_to_use[:scorer][:name] | ||
unless Scorer.exists?(name: scorer_name) | ||
@case.errors.add(:scorer, "Scorer with name '#{scorer_name}' needs to be migrated over first.") | ||
end | ||
|
||
params_to_use[:queries]&.each do |query| | ||
next unless query[:ratings] | ||
|
||
query[:ratings].each do |rating| | ||
list_of_emails_of_users << rating[:user_email] if rating[:user_email].present? | ||
end | ||
end | ||
|
||
list_of_emails_of_users.uniq.each do |email| | ||
unless User.exists?(email: email) | ||
@case.errors.add(:base, "User with email '#{email}' needs to be migrated over first.") | ||
end | ||
end | ||
|
||
unless @case.errors.empty? | ||
render json: @case.errors, status: :bad_request | ||
return | ||
end | ||
|
||
# passed first set of validations. | ||
@case.case_name = params_to_use[:case_name] | ||
|
||
@case.scorer = Scorer.find_by(name: scorer_name) | ||
|
||
@case.owner = User.find_by(email: params_to_use[:owner_email]) | ||
|
||
# For some reason we can't do @case.queries.build with out forcing a save. | ||
# Works fine with book however. | ||
unless @case.save | ||
render json: @case.errors, status: :bad_request | ||
return | ||
end | ||
|
||
params_to_use[:queries]&.each do |query| | ||
new_query = @case.queries.build(query.except(:ratings)) | ||
next unless query[:ratings] | ||
|
||
query[:ratings].each do |rating| | ||
rating[:user] = User.find_by(email: rating[:user_email]) if rating[:user_email].present? | ||
new_query.ratings.build(rating.except(:user_email)) | ||
end | ||
end | ||
|
||
params_to_use[:try][:try_number] = 1 | ||
|
||
@case.tries.first.update(params_to_use[:try].except(:curator_variables)) | ||
params_to_use[:try][:curator_variables].each do |curator_variable| | ||
# not sure why build and then the @case.save doesn't cascade down. | ||
@case.tries.first.curator_variables.create curator_variable | ||
end | ||
|
||
if @case.save | ||
respond_with @case | ||
else | ||
render json: @case.errors, status: :bad_request | ||
end | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
# rubocop:enable Metrics/AbcSize | ||
# rubocop:enable Metrics/CyclomaticComplexity | ||
# rubocop:enable Metrics/PerceivedComplexity | ||
|
||
private | ||
|
||
def case_params | ||
params.require(:case).permit! | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.