1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

improve trainings load times

- do not eager load trainings availabilities in INDEX action if not requsted
- in trainings monitoring (font), do not load all availabilities for all traninings by default, by select a training first to load its availabilities
This commit is contained in:
Sylvain 2016-06-15 12:01:09 +02:00
parent b154c37656
commit 08244d53dc
9 changed files with 42 additions and 27 deletions

View File

@ -13,6 +13,10 @@ Application.Controllers.controller "TrainingsController", ["$scope", "$state", "
## simplified list of machines
$scope.machines = machinesPromise
## Training to monitor, binded with drop-down selection
$scope.monitoring =
training: null
## list of training availabilies, grouped by date
$scope.groupedAvailabilities = {}
@ -188,16 +192,18 @@ Application.Controllers.controller "TrainingsController", ["$scope", "$state", "
##
# Callback when the drop-down selection is changed.
# The selected training details will be loaded from the API and rendered into the accordions.
##
$scope.selectTrainingToMonitor = ->
Training.get {id: $scope.monitoring.training.id}, (training) ->
$scope.groupedAvailabilities = groupAvailabilities([training])
### PRIVATE SCOPE ###
##
# Kind of constructor: these actions will be realized first when the controller is loaded
##
initialize = ->
$scope.groupedAvailabilities = groupAvailabilities($scope.trainings)
##
# Group the trainings availabilites by trainings and by dates and return the resulting tree
# @param trainings {Array} $scope.trainings is expected here
@ -223,8 +229,4 @@ Application.Controllers.controller "TrainingsController", ["$scope", "$state", "
tree[training.name][start.year()][start.month()][start.date()].push( availability )
tree
# init the controller (call at the end !)
initialize()
]

View File

@ -515,7 +515,7 @@ angular.module('application.router', ['ui.router']).
controller: 'TrainingsController'
resolve:
trainingsPromise: ['Training', (Training)->
Training.query({requested_attributes:'[availabilities]'}).$promise
Training.query().$promise
]
machinesPromise: ['Machine', (Machine)->
Machine.query().$promise

View File

@ -80,6 +80,12 @@
</uib-tab>
<uib-tab heading="{{ 'trainings_monitoring' | translate }}">
<div class="m-lg">
<label for="training_select" translate>{{ 'select_a_training' }}</label>
<select ng-options="training as training.name for training in trainings" ng-model="monitoring.training" class="form-control" ng-change="selectTrainingToMonitor()" name="training_select">
</select>
</div>
<table class="table">
<thead>
<tr>

View File

@ -1,13 +1,21 @@
class API::TrainingsController < API::ApiController
include ApplicationHelper
before_action :authenticate_user!, except: [:index, :show]
before_action :set_training, only: [:show, :update, :destroy]
before_action :set_training, only: [:update, :destroy]
def index
@requested_attributes = params[:requested_attributes]
@trainings = policy_scope(Training)
if attribute_requested?(@requested_attributes, 'availabilities')
@trainings = @trainings.includes(:availabilities => [:slots => [:reservation => [:user => [:profile, :trainings]]]]).order('availabilities.start_at DESC')
end
end
def show
@training = Training.includes(availabilities: {slots: {reservation: {user: [:profile, :trainings] }}})
.where(id: params[:id]).first
end
def create

View File

@ -1,16 +1,14 @@
class TrainingPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.includes(:plans, :machines, :availabilities => [:slots => [:reservation => [:user => [:profile, :trainings]]]]).order('availabilities.start_at DESC')
scope.includes(:plans, :machines)
end
end
def create?
user.is_admin?
end
def update?
user.is_admin?
%w(show create update).each do |action|
define_method "#{action}?" do
user.is_admin?
end
end
def destroy?

View File

@ -6,6 +6,6 @@ json.availabilities @training.availabilities.order('start_at DESC') do |a|
json.reservation_users a.slots.map do |slot|
json.id slot.reservation.user.id
json.full_name slot.reservation.user.profile.full_name
json.is_valid slot.reservation.user.trainings.include?(training)
json.is_valid slot.reservation.user.trainings.include?(@training)
end
end

View File

@ -35,6 +35,7 @@ en:
# left menu (admin)
trainings_monitoring: "Trainings monitoring"
select_a_training: "Select a training"
manage_the_calendar: "Manage the Calendar"
manage_the_users: "Manage the Users"
manage_the_invoices: "Manage the invoices"

View File

@ -35,6 +35,7 @@ fr:
# menu de gauche (partie admin)
trainings_monitoring: "Suivi formations"
select_a_training: "Sélectionnez une formation"
manage_the_calendar: "Gérer le calendrier"
manage_the_users: "Gérer les utilisateurs"
manage_the_invoices: "Gérer les factures"

View File

@ -227,7 +227,7 @@ ActiveRecord::Schema.define(version: 20160613093842) do
t.boolean "is_read", default: false
t.datetime "created_at"
t.datetime "updated_at"
t.string "receiver_type", limit: 255
t.string "receiver_type"
t.boolean "is_send", default: false
t.jsonb "meta_data", default: {}
end
@ -339,7 +339,6 @@ ActiveRecord::Schema.define(version: 20160613093842) do
t.datetime "created_at"
t.datetime "updated_at"
t.string "title", limit: 255
t.integer "step_nb"
end
add_index "project_steps", ["project_id"], name: "index_project_steps_on_project_id", using: :btree
@ -369,7 +368,7 @@ ActiveRecord::Schema.define(version: 20160613093842) do
t.datetime "published_at"
end
add_index "projects", ["slug"], name: "index_projects_on_slug", unique: true, using: :btree
add_index "projects", ["slug"], name: "index_projects_on_slug", using: :btree
create_table "projects_components", force: :cascade do |t|
t.integer "project_id"
@ -439,8 +438,8 @@ ActiveRecord::Schema.define(version: 20160613093842) do
t.datetime "updated_at"
t.integer "availability_id"
t.datetime "ex_start_at"
t.datetime "canceled_at"
t.datetime "ex_end_at"
t.datetime "canceled_at"
t.boolean "offered", default: false
end
@ -598,7 +597,6 @@ ActiveRecord::Schema.define(version: 20160613093842) do
add_index "user_trainings", ["user_id"], name: "index_user_trainings_on_user_id", using: :btree
create_table "users", force: :cascade do |t|
t.string "username", limit: 255
t.string "email", limit: 255, default: "", null: false
t.string "encrypted_password", limit: 255, default: "", null: false
t.string "reset_password_token", limit: 255
@ -621,6 +619,7 @@ ActiveRecord::Schema.define(version: 20160613093842) do
t.boolean "is_allow_contact", default: true
t.integer "group_id"
t.string "stp_customer_id", limit: 255
t.string "username", limit: 255
t.string "slug", limit: 255
t.boolean "is_active", default: true
t.boolean "invoicing_disabled", default: false