Speed up external user statistics
This commit is contained in:
@ -14,9 +14,53 @@ class ExternalUsersController < ApplicationController
|
||||
authorize!
|
||||
end
|
||||
|
||||
def working_time_query
|
||||
"""
|
||||
SELECT user_id,
|
||||
exercise_id,
|
||||
max(score) as maximum_score,
|
||||
count(id) as runs,
|
||||
sum(working_time_new) AS working_time
|
||||
FROM
|
||||
(SELECT user_id,
|
||||
exercise_id,
|
||||
score,
|
||||
id,
|
||||
CASE
|
||||
WHEN working_time >= '0:30:00' THEN '0'
|
||||
ELSE working_time
|
||||
END AS working_time_new
|
||||
FROM
|
||||
(SELECT user_id,
|
||||
exercise_id,
|
||||
max(score) AS score,
|
||||
id,
|
||||
(created_at - lag(created_at) over (PARTITION BY user_id, exercise_id
|
||||
ORDER BY created_at)) AS working_time
|
||||
FROM submissions
|
||||
WHERE user_id = #{@user.id}
|
||||
AND user_type = 'ExternalUser'
|
||||
GROUP BY exercise_id,
|
||||
user_id,
|
||||
id) AS foo) AS bar
|
||||
GROUP BY user_id,
|
||||
exercise_id;
|
||||
"""
|
||||
end
|
||||
|
||||
def statistics
|
||||
@user = ExternalUser.find(params[:id])
|
||||
authorize!
|
||||
|
||||
statistics = {}
|
||||
|
||||
ActiveRecord::Base.connection.execute(working_time_query).each do |tuple|
|
||||
statistics[tuple["exercise_id"].to_i] = tuple
|
||||
end
|
||||
|
||||
render locals: {
|
||||
statistics: statistics
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
|
Reference in New Issue
Block a user