From d03f2c7bf4fce33e551910781b5d83689c3587a0 Mon Sep 17 00:00:00 2001 From: Sebastian Serth Date: Tue, 21 Feb 2023 01:00:30 +0100 Subject: [PATCH] Slightly improve Statistics Controller performance --- app/helpers/statistics_helper.rb | 5 ++--- app/models/request_for_comment.rb | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/helpers/statistics_helper.rb b/app/helpers/statistics_helper.rb index 0b4a95d7..b4260563 100644 --- a/app/helpers/statistics_helper.rb +++ b/app/helpers/statistics_helper.rb @@ -43,9 +43,7 @@ module StatisticsHelper { key: 'currently_active', name: t('statistics.entries.users.currently_active'), - data: ExternalUser.joins(:submissions) - .where(['submissions.created_at >= ?', DateTime.now - 5.minutes]) - .distinct('external_users.id').count, + data: Submission.where(created_at: 5.minutes.ago.., user_type: ExternalUser.name).distinct.count(:user_id), url: statistics_graphs_path, }, ] @@ -81,6 +79,7 @@ module StatisticsHelper { key: 'container_requests_per_minute', name: t('statistics.entries.exercises.container_requests_per_minute'), + # This query is actually quite expensive since we do not have an index on the created_at column. data: (Testrun.where(created_at: DateTime.now - 1.hour..).count.to_f / 60).round(2), unit: '/min', }, diff --git a/app/models/request_for_comment.rb b/app/models/request_for_comment.rb index 0d1b6288..98ce50c6 100644 --- a/app/models/request_for_comment.rb +++ b/app/models/request_for_comment.rb @@ -17,7 +17,7 @@ class RequestForComment < ApplicationRecord has_many :subscriptions, dependent: :destroy scope :unsolved, -> { where(solved: [false, nil]) } - scope :in_range, ->(from, to) { where(created_at: from..to) } + scope :in_range, ->(from, to) { from == DateTime.new(0) && to > 5.seconds.ago ? all : where(created_at: from..to) } scope :with_comments, -> { select {|rfc| rfc.comments.any? } } # after_save :trigger_rfc_action_cable