Skip to content

Commit

Permalink
lots of perf fixes and other changes!
Browse files Browse the repository at this point in the history
  • Loading branch information
epugh committed Jan 26, 2024
1 parent 2752ac2 commit da5597e
Show file tree
Hide file tree
Showing 15 changed files with 109 additions and 26 deletions.
4 changes: 4 additions & 0 deletions app/assets/javascripts/components/diff/_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ <h3 class="modal-title">Compare Your Search Results</h3>
<span ng-if="ctrl.selection">
Snapshot has the id {{ ctrl.selection }}
</span>

<div class="alert alert-warning" role="alert" ng-if="ctrl.isProcessingFile()">
This snapshot is currently being processed in the background. You should wait till it completes.
</div>
</div>

<div class="form-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ angular.module('QuepidApp')
) {
var ctrl = this;

// Attributes
ctrl.snapshots = querySnapshotSvc.snapshots;
querySnapshotSvc.getSnapshots().then(function() {
ctrl.snapshots = querySnapshotSvc.snapshots;
}
);

ctrl.which = 'snapshot';
ctrl.selection = initialSelection;
ctrl.inProgress = false;
Expand All @@ -32,6 +35,8 @@ angular.module('QuepidApp')
ctrl.nothingSelected = nothingSelected;
ctrl.ok = ok;
ctrl.toggleDel = toggleDel;
ctrl.isProcessingFile = isProcessingFile;


// Watches
$scope.$watch('ctrl.selection', function(newVal, oldVal) {
Expand Down Expand Up @@ -72,6 +77,25 @@ angular.module('QuepidApp')
flash.success = 'Snapshot deleted successfully.';
});
}

function isProcessingFile() {
if (ctrl.snapshots){
var desiredSnapshot = null;
angular.forEach(ctrl.snapshots, function(snapshot) {
if (snapshot.id === ctrl.selection) {
desiredSnapshot = snapshot;
return; // exit the loop early
}
});
if (desiredSnapshot){
return desiredSnapshot.hasSnapshotFile;
}
else {
return false;
}
}
return false;
}

function isNumber(num) {
return !isNaN(parseInt('' + num, 10));
Expand Down
4 changes: 4 additions & 0 deletions app/assets/javascripts/components/export_case/_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ <h3 class="modal-title">Export Case: <span class="modal-case">{{ ctrl.theCase.ca
>
</select>
</label>

<div class="alert alert-warning" role="alert" ng-if="ctrl.isProcessingFile()">
This snapshot is currently being processed in the background. You should wait till it completes.
</div>
</div>
<hr/>
<div class="form-group">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ angular.module('QuepidApp')

ctrl.theCase = theCase;
ctrl.supportsDetailedExport = supportsDetailedExport;
ctrl.isProcessingFile = isProcessingFile;

// If called from the cases listing page, then we need the call back with the bootstrap,
// otherwise on the main page the querySnapshotSvc.snapshots was bootstrapped.
Expand Down Expand Up @@ -47,5 +48,25 @@ angular.module('QuepidApp')
ctrl.cancel = function () {
$uibModalInstance.dismiss('cancel');
};


function isProcessingFile() {
if (ctrl.options.snapshot){
var desiredSnapshot = null;
angular.forEach(ctrl.snapshots, function(snapshot) {
if (snapshot.id === ctrl.selection) {
desiredSnapshot = snapshot;
return; // exit the loop early
}
});
if (desiredSnapshot){
return desiredSnapshot.hasSnapshotFile;
}
else {
return false;
}
}
return false;
}
}
]);
2 changes: 1 addition & 1 deletion app/assets/javascripts/controllers/mainCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ angular.module('QuepidApp')
bootstrapCase()
.then(function() {
loadQueries();
loadSnapshots();
loadSnapshots(); // this is here just to set the caseNo in the querySnapshotSvc.
updateCaseMetadata();
paneSvc.refreshElements();
}).catch(function(error) {
Expand Down
19 changes: 14 additions & 5 deletions app/assets/javascripts/services/querySnapshotSvc.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ angular.module('QuepidApp')
});
});
};

// Now that we process snapshots async, we
// don't want to cache the data
this.getSnapshots = function() {
this.snapshots = {};

return $http.get('api/cases/' + caseNo + '/snapshots?shallow=true')
.then(function(response) {
return addSnapshotResp(response.data.snapshots)
.then(function() {
version++;
});
});
};

this.addSnapshot = function(name, recordDocumentFields, queries) {
// we may want to refactor the payload structure in the future.
Expand Down Expand Up @@ -130,11 +144,6 @@ angular.module('QuepidApp')
.then(function() {
version++;
});
// return addSnapshotResp([response.data])
// .then(function() {
// version++;
// });
//});
};

this.deleteSnapshot = function(snapshotId) {
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/services/snapshotFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
self.id = params.id;
self.name = snapshotName;
self.time = params.time;
self.hasSnapshotFile = params.has_snapshot_file;
self.docs = params.docs;
self.queries = params.queries;

Expand Down
11 changes: 10 additions & 1 deletion app/controllers/api/v1/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

module Api
module V1
# rubocop:disable Metrics/ClassLength
class BooksController < Api::ApiController
before_action :set_book, only: [ :show, :update, :destroy ]
before_action :check_book, only: [ :show, :update, :destroy ]
Expand All @@ -28,6 +29,7 @@ def index
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/BlockLength
api :GET, '/api/books/:book_id',
'Show the book with the given ID.'
param :id, :number,
Expand All @@ -40,7 +42,11 @@ def show
csv_headers = %w[query docid]

# Only return rateable judgements, filter out the unrateable ones.
unique_raters = @book.judgements.rateable.preload(:user).collect(&:user).uniq
# unique_raters = @book.judgements.rateable.preload(:user).collect(&:user).uniq
unique_raters = @book.judges.merge(Judgement.rateable)

puts 'HERE COME THE RATERS'
pp unique_raters

# this logic about using email versus name is kind of awful. Think about user.full_name or user.identifier?
unique_raters.each do |rater|
Expand Down Expand Up @@ -72,6 +78,8 @@ def show
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/BlockLength

api :POST, '/api/books', 'Create a new book.'
param_group :book
def create
Expand Down Expand Up @@ -135,5 +143,6 @@ def make_csv_safe str
end
end
end
# rubocop:enable Metrics/ClassLength
end
end
16 changes: 7 additions & 9 deletions app/controllers/api/v1/snapshots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,18 @@ def show
respond_with @snapshot
end

# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
# rubocop:disable Layout/LineLength
def create
@snapshot = @case.snapshots.build(name: params[:snapshot][:name])
@snapshot.scorer = @case.scorer
@snapshot.try = @case.tries.first

puts "Okay, checking snapshot queries: #{@snapshot.snapshot_queries.length}"
if @snapshot.save
puts "Okay2, checking snapshot queries: #{@snapshot.snapshot_queries.length}"
serialized_data = Marshal.dump(snapshot_params)

puts "[SnapshotController] the size of the serialized data is #{number_to_human_size(serialized_data.bytesize)}"
# puts "[SnapshotController] the size of the serialized data is #{number_to_human_size(serialized_data.bytesize)}"
compressed_data = Zlib::Deflate.deflate(serialized_data)
puts "[SnapshotController] the size of the compressed data is #{number_to_human_size(compressed_data.bytesize)}"
# puts "[SnapshotController] the size of the compressed data is #{number_to_human_size(compressed_data.bytesize)}"
@snapshot.snapshot_file.attach(io: StringIO.new(compressed_data), filename: "snapshot_#{@snapshot.id}.bin.zip",
content_type: 'application/zip')
PopulateSnapshotJob.perform_later @snapshot
Expand All @@ -62,11 +58,14 @@ def create
render json: @snapshot.errors, status: :bad_request
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
# rubocop:enable Layout/LineLength

def destroy
# SnapshotDoc.joins(snapshot_query: :snapshot)
# .where(snapshot_queries: { snapshot: @snapshot })
# .delete_all
@snapshot.snapshot_docs.delete_all
@snapshot.snapshot_queries.delete_all
@snapshot.destroy
Analytics::Tracker.track_snapshot_deleted_event current_user, @snapshot

Expand All @@ -78,7 +77,6 @@ def destroy
def set_snapshot
@snapshot = @case.snapshots
.where(id: params[:id])
.includes([ snapshot_queries: [ :snapshot_docs ] ])
.first
end

Expand Down
9 changes: 7 additions & 2 deletions app/controllers/books_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ def show
@book.cases.each do |kase|
@count_of_anonymous_case_judgements += kase.ratings.where(user: nil).count
end

@moar_judgements_needed = @book.judgements.where(user: current_user).count < @book.query_doc_pairs.count
@cases = @book.cases
@leaderboard_data = []
@stats_data = []
unique_judges = @book.judgements.preload(:user).collect(&:user).uniq
unique_judges.each do |judge|

unique_judge_ids = @book.query_doc_pairs.joins(:judgements)
.distinct.pluck(:user_id)
unique_judge_ids.each do |judge_id|
judge = User.find(judge_id) unless judge_id.nil?
@leaderboard_data << { judge: judge.nil? ? 'anonymous' : judge.name,
judgements: @book.judgements.where(user: judge).count }
@stats_data << {
Expand Down
9 changes: 8 additions & 1 deletion app/models/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,17 @@ class Book < ApplicationRecord
belongs_to :selection_strategy
belongs_to :scorer
has_many :query_doc_pairs, dependent: :destroy, autosave: true
has_many :judgements, -> { order('query_doc_pair_id') },
has_many :ordered_judgements, -> { order('query_doc_pair_id') },
through: :query_doc_pairs,
dependent: :destroy,
class_name: 'Judgement'

has_many :judgements,
through: :query_doc_pairs,
dependent: :destroy

# has_many :judges, -> { distinct }, through: :judgements, class_name: 'User', source: :user

has_many :cases, dependent: :nullify

has_many :rated_query_doc_pairs, -> { has_judgements },
Expand Down
2 changes: 1 addition & 1 deletion app/models/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Snapshot < ApplicationRecord
belongs_to :case, optional: true # shouldn't be optional!
belongs_to :try, optional: true # shouldn't be optional!
belongs_to :scorer, optional: true # shouldn't be optional!
has_many :snapshot_queries, dependent: :destroy
has_many :snapshot_queries, dependent: :delete_all
has_many :snapshot_docs,
through: :snapshot_queries

Expand Down
2 changes: 1 addition & 1 deletion app/models/snapshot_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ class SnapshotQuery < ApplicationRecord
belongs_to :snapshot, optional: true # shouldn't be
belongs_to :query, optional: true # shouldn't be
has_many :snapshot_docs, -> { order(position: :asc) },
dependent: :destroy,
dependent: :delete_all,
inverse_of: :snapshot_query
end
2 changes: 1 addition & 1 deletion app/views/home/_case_summary.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</h5>

<div class="card-body">
<h5 class="card-title"><%= kase.last_score.score unless kase.scores.empty? %> <%= kase.scorer.name %></h5>
<h5 class="card-title"><%= number_with_precision(kase.last_score.score, precision: 2) unless kase.scores.empty? %> <%= kase.scorer.name %></h5>
<p class="card-text"><%= kase.created_at.to_date.to_fs(:short) %> - <%= kase.last_score.updated_at.to_date.to_fs(:short) unless kase.scores.empty?%>
</p>
<%
Expand Down
5 changes: 3 additions & 2 deletions app/views/judgements/_moar_judgements_needed.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<% if SelectionStrategy.random_query_doc_based_on_strategy(book, current_user) %>
hi: <%= @moar_judgements_needed %>
<% if @moar_judgements_needed %>
<div class="alert alert-warning" role="alert">
<%= link_to 'More Judgements are Needed!', book_judge_path(book), class: 'btn btn-primary', role: 'button' %>
</div>
Expand Down Expand Up @@ -35,7 +36,7 @@
<% else %>
<div class="alert alert-info" role="alert">
Best practice is to have three independent judgements per query/doc pair, and we don't yet have that.
Currently <%= book.judgements.count %> out of expected <%= book.query_doc_pairs.size * 3 %>.
Currently <%= book.judgements.count %> out of expected <%= book.query_doc_pairs.size * 3 %> total judgements have been made.
</div>
<% end %>
<% end %>
Expand Down

0 comments on commit da5597e

Please sign in to comment.