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

Chore more database optimizations #1562

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions app/controllers/exercise_solutions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class ExerciseSolutionsController < AjaxController
include Mumuki::Laboratory::Controllers::ExerciseSeed

before_action :set_messages, only: :create
before_action :set_guide!, only: :create
before_action :set_stats!, only: :create
before_action :validate_accessible!, only: :create

def create
Expand All @@ -27,4 +29,12 @@ def solution_params
client_result: params[:client_result].try { |it| it.permit(:status, test_results: [:title, :status, :result, :summary]).to_h }
}
end

def set_guide!
@guide = @exercise.guide
end

def set_stats!
@stats = @guide.stats_for(current_user)
end
end
27 changes: 21 additions & 6 deletions app/controllers/exercises_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
class ExercisesController < ApplicationController
before_action :set_parents!, only: :show

include Mumuki::Laboratory::Controllers::Content
include Mumuki::Laboratory::Controllers::ExerciseSeed
include Mumuki::Laboratory::Controllers::ImmersiveNavigation

before_action :set_guide!, only: :show
before_action :set_progress!, only: :show, if: :current_user?
before_action :set_assignment!, only: :show, if: :current_user?

before_action :validate_accessible!, only: :show
before_action :start!, only: :show

Expand All @@ -20,15 +23,15 @@ def show_transparently
private

def subject
@exercise ||= Exercise.find_by(id: params[:id])
@exercise ||= Exercise.find(params[:id])
end

def accessible_subject
subject.navigable_parent
@navigable_parent
end

def start!
@exercise.navigable_parent.start! current_user
@navigable_parent.start! current_user
end

def set_assignment!
Expand All @@ -38,9 +41,13 @@ def set_assignment!
@default_content = @assignment.default_content
end

def set_guide!
raise Mumuki::Domain::NotFoundError if @exercise.nil?
def set_parents!
@guide = @exercise.guide
@navigable_parent = @exercise.navigable_parent
end

def set_progress!
@assignments, @stats = @guide.assignments_and_stats_for(current_user)
end

def exercise_params
Expand All @@ -50,4 +57,12 @@ def exercise_params
:guide_id, :number,
:layout, :expectations_yaml)
end


def subject_used_here?
# overriden because of performance reasons
# this method will be called only on show, so it is safe
# to check for navigable_parent presence
@navigable_parent.present?
end
end
4 changes: 3 additions & 1 deletion app/helpers/breadcrumbs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module BreadcrumbsHelper
include DiscussionsHelper

def breadcrumbs(e, extra=nil)
breadcrumbs0(e.navigable_name, e, extra, 'last')
Rails.cache.fetch [:breadcrumb, e, extra, Organization.current] do
breadcrumbs0(e.navigable_name, e, extra, 'last')
end
end

def header_breadcrumbs(link_for_organization: true)
Expand Down
14 changes: 3 additions & 11 deletions app/helpers/icons_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def fixed_fa_icon(name, options = {})
fa_icon name, options.merge(class: 'fa-fw fixed-icon')
end

def exercise_status_icon(exercise)
link_to exercise_status_fa_icon(exercise),
exercise_path(exercise) if current_user?
def assignment_status_icon(assignment)
link_to contextualization_fa_icon(assignment),
exercise_path(assignment.exercise) if current_user?
end

def language_icon(language)
Expand All @@ -22,10 +22,6 @@ def contextualization_fa_icon(contextualization)
fa_icon(*icon_for(contextualization))
end

def exercise_status_fa_icon(exercise)
contextualization_fa_icon(exercise.assignment_for(current_user))
end

def discussion_status_fa_icon(discussion)
contextualization_fa_icon(discussion)
end
Expand All @@ -35,10 +31,6 @@ def icon_for(iconizable)
[iconized[:type], class: "text-#{iconized[:class]} status-icon"]
end

def class_for_exercise(exercise)
icon_class_for(exercise.assignment_for(current_user))
end

def icon_class_for(iconizable)
iconizable.iconize[:class].to_s
end
Expand Down
17 changes: 6 additions & 11 deletions app/helpers/links_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module LinksHelper

def link_to_path_element(element, options={})
name = extract_name element, options
link_to name, element, options
mode = options[:mode]
Rails.cache.fetch [:link, element, mode, Organization.current] do
link_to extract_name(element, mode), element
end
end

def link_to_error_404
Expand Down Expand Up @@ -65,15 +67,8 @@ def turbolinks_enable_for(exercise)

private

def extract_name(named, options)
case options.delete(:mode)
when :plain
named.name
when :friendly
named.friendly
else
named.navigable_name
end
def extract_name(named, mode )
mode == :plain ? named.name : named.navigable_name
end

def edit_link_to_bibliotheca
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/progress_bar_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module ProgressBarHelper
include IconsHelper

def class_for_progress_list_item(exercise, active)
"progress-list-item text-center #{class_for_exercise(exercise)} #{active ? 'active' : ''}"
def class_for_progress_list_item(assignment, active)
"progress-list-item text-center #{icon_class_for(assignment)} #{active ? 'active' : ''}"
end

end
4 changes: 2 additions & 2 deletions app/views/chapters/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
<div>
<h3><%= t(:lessons) %></h3>

<% @chapter.lessons.each do |lesson| %>
<% @chapter.lessons.includes(guide: :exercises).each do |lesson| %>
<h4><%= lesson.number %>. <%= link_to_path_element lesson, mode: :plain %></h4>
<%= render partial: 'layouts/progress_listing', locals: { exercises: lesson.exercises } %>
<%= render partial: 'layouts/progress_listing', locals: { guide: lesson.guide } %>
<% end %>
</div>
<% end %>
Expand Down
5 changes: 0 additions & 5 deletions app/views/exercise_solutions/_results.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
<%
@guide = @exercise.guide
@stats = @guide.stats_for(current_user)
%>

<% if assignment.results_hidden? %>
<%= render partial: 'exercise_solutions/results_hidden', locals: {assignment: assignment} %>
<% elsif !assignment.attempts_left? %>
Expand Down
12 changes: 4 additions & 8 deletions app/views/exercises/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@

<%= render partial: 'layouts/authoring', locals: {guide: @guide} %>

<% @stats = @exercise.stats_for(current_user) %>

<% if @exercise.navigable_parent.timed? && !current_user.teacher? %>
<%= render partial: 'layouts/timer', locals: {end_time: @exercise.navigable_parent.real_end_time(current_user)} %>
<% if @navigable_parent.timed? && !current_user.teacher? %>
<%= render partial: 'layouts/timer', locals: {end_time: @navigable_parent.real_end_time(current_user)} %>
<% end %>

<% unless @exercise.input_kids? %>
Expand All @@ -29,9 +27,7 @@
<% end %>

<div>
<% if @stats && standalone_mode? %>
<%= render partial: 'layouts/progress_bar', locals: {actual: @exercise, guide: @exercise.guide, stats: @stats} %>
<% end %>
<%= render partial: 'layouts/progress_bar' if current_user? && standalone_mode? %>
</div>

<% content_for :assignment do %>
Expand Down Expand Up @@ -65,6 +61,6 @@
<%= render partial: 'layouts/modals/level_up' %>
<% end %>

<%= render partial: 'layouts/modals/guide_corollary', locals: {guide: @guide} %>
<%= render partial: 'layouts/modals/guide_corollary', locals: {guide: @guide} if @stats.almost_done? %>
<%= render partial: 'layouts/modals/new_message', locals: {exercise: @exercise} if should_render_message_input?(@exercise) %>
<% end if current_user? %>
6 changes: 3 additions & 3 deletions app/views/layouts/_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

<%= render partial: 'layouts/authoring', locals: {guide: @guide} %>

<% if subject.timed? && subject.started?(current_user) && !current_user.teacher? %>
<% if subject.timed? && @stats.started? && !current_user.teacher? %>
<%= render partial: 'layouts/timer', locals: {end_time: subject.real_end_time(current_user)} %>
<% end %>

<%= yield if block_given? %>

<h3>
<%= t :exercises %>
<%= restart_guide_link(@guide) if current_user && @guide.started?(current_user) && @guide.resettable? %>
<%= restart_guide_link(@guide) if current_user && @stats.started? && @guide.resettable? %>
</h3>

<%= render partial: 'layouts/progress_listing', locals: {exercises: @guide.exercises} %>
<%= render partial: 'layouts/progress_listing', locals: { guide: @guide } %>

<% if @stats&.done? %>
<div class="text-box">
Expand Down
23 changes: 13 additions & 10 deletions app/views/layouts/_progress_bar.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<div class="progress-list-flex">
<% guide.exercises.each do |e| %>
<a
<%= turbolinks_enable_for e %>
href="<%= exercise_path(e)%>"
aria-label="<%= e.navigable_name %>"
title="<%= e.navigable_name %>"
data-mu-exercise-id="<%= e.id %>"
class="<%= class_for_progress_list_item(e, e == actual)%>">
</a>
<% @assignments.each do |assignment| %>
<% exercise = assignment.exercise %>
<% cache [:progress_list_item, exercise, assignment.status, Organization.current, exercise == @exercise] do %>
<a
<%= turbolinks_enable_for exercise %>
href="<%= exercise_path(exercise)%>"
aria-label="<%= exercise.navigable_name %>"
title="<%= exercise.navigable_name %>"
data-mu-exercise-id="<%= exercise.id %>"
class="<%= class_for_progress_list_item(assignment, exercise == @exercise)%>">
</a>
<% end %>
<% end %>
</div>
</div>
21 changes: 14 additions & 7 deletions app/views/layouts/_progress_listing.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<ul class="progress-listing">
<% exercises.each do |exercise| %>
<% cache [exercise, exercise.status_for(current_user), Organization.current] do %>
<li <%= turbolinks_enable_for(exercise) %>>
<%= exercise_status_icon exercise %>
<%= link_to_path_element(exercise) %>
</li>
<% end %>
<%
assignments = guide.assignments_for(current_user)
statuses = assignments.map(&:status)
%>
<% cache_if statuses.chunk_while(&:==).count <= 2, [guide, statuses, Organization.current] do %>
<% assignments.each do |assignment| %>
<% cache [assignment.exercise, assignment.status, Organization.current] do %>
<li <%= turbolinks_enable_for(assignment.exercise) %>>
<%= assignment_status_icon assignment %>
<%= link_to_path_element(assignment.exercise) %>
</li>
<% end %>
<% end %>
<% end %>

</ul>
6 changes: 5 additions & 1 deletion lib/mumuki/laboratory/controllers/content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module Mumuki::Laboratory::Controllers::Content

# ensures contents are in current organization's path
def validate_used_here! #TODO refactor subject logic (e.g. we can't move validate_accessible! here because ExerciseSolutionsController does not declare a subject)
raise Mumuki::Domain::NotFoundError unless subject&.used_in?(Organization.current)
raise Mumuki::Domain::NotFoundError unless subject_used_here?
end

def subject_used_here?
subject&.used_in?(Organization.current)
end
end
7 changes: 1 addition & 6 deletions lib/mumuki/laboratory/controllers/results_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ module Mumuki::Laboratory::Controllers::ResultsRendering

included do
include ProgressBarHelper, ExerciseInputHelper

before_action :set_guide_previously_done!
end

def render_results_json(assignment, results = {})
Expand Down Expand Up @@ -56,10 +54,7 @@ def render_results_button(assignment)
end

def guide_finished_by_solution?
!@guide_previously_done && @exercise.guide_done_for?(current_user)
@stats.almost_but_not_done? && @exercise.guide_done_for?(current_user)
end

def set_guide_previously_done!
@guide_previously_done = @exercise.guide_done_for?(current_user)
end
end
2 changes: 1 addition & 1 deletion mumuki-laboratory.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |s|

s.add_dependency "rails", "~> 5.1.6"

s.add_dependency 'mumuki-domain', '~> 8.3.0'
s.add_dependency 'mumuki-domain', '~> 8.4.0'
s.add_dependency 'mumukit-bridge', '~> 4.1'
s.add_dependency 'mumukit-login', '~> 7.3'
s.add_dependency 'mumukit-nuntius', '~> 6.1'
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@
t.datetime "legal_terms_accepted_at"
t.datetime "forum_terms_accepted_at"
t.boolean "banned_from_forum"
t.boolean "uppercase_mode"
t.index ["avatar_type", "avatar_id"], name: "index_users_on_avatar_type_and_avatar_id"
t.index ["disabled_at"], name: "index_users_on_disabled_at"
t.index ["last_organization_id"], name: "index_users_on_last_organization_id"
Expand Down
1 change: 0 additions & 1 deletion spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
before { reindex_current_organization! }

it { expect(link_to_path_element(exercise, mode: :plain)).to eq '<a href="/exercises/30-c1-bar-foo3">foo3</a>' }
it { expect(link_to_path_element(exercise, mode: :friendly)).to eq '<a href="/exercises/30-c1-bar-foo3">C1: bar - foo3</a>' }
it { expect(link_to_path_element(exercise)).to eq '<a href="/exercises/30-c1-bar-foo3">3. foo3</a>' }
end

Expand Down