Skip to content

Commit 5203a93

Browse files
committed
Cache stats in background daemon
1 parent 1a33080 commit 5203a93

File tree

3 files changed

+71
-49
lines changed

3 files changed

+71
-49
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
3+
class SandboxStatusFetcherTask
4+
def initialize
5+
end
6+
7+
def run
8+
data = {
9+
high_priority_submissions_count: Submission.to_be_reprocessed.where(processing_priority: 0).count
10+
sandbox_queue_length: Submission.to_be_reprocessed.count
11+
unprocessed_submissions_count: Submission.where(processed: false).count
12+
submissions_count_minute: Submission.where(created_at: (Time.current - 1.minute)..Time.current).count
13+
submissions_count_five_minutes: Submission.where(created_at: (Time.current - 5.minute)..Time.current).count
14+
submissions_count_hour: Submission.where(created_at: (Time.current - 1.hour)..Time.current).count
15+
submissions_count_today: Submission.where(created_at: Time.current.all_day).count
16+
submissions_count_yesterday: Submission.where(created_at: Time.current.yesterday.all_day).count
17+
submissions_count_week: Submission.where(created_at: Time.current.all_week).count
18+
}
19+
Rails.cache.write("stats-cache", data.to_json, expires_in: 1.minute)
20+
end
21+
22+
def wait_delay
23+
5
24+
end
25+
end

app/controllers/status_controller.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,8 @@
44
class StatusController < ApplicationController
55
def index
66
authorize! :read_instance_state, nil
7-
@high_priority_submissions_count = Submission.to_be_reprocessed.where(processing_priority: 0).count
8-
@sandbox_queue_length = Submission.to_be_reprocessed.count
9-
@unprocessed_submissions_count = Submission.where(processed: false).count
10-
@submissions_count_minute = Submission.where(created_at: (Time.current - 1.minute)..Time.current).count
11-
@submissions_count_five_minutes = Submission.where(created_at: (Time.current - 5.minute)..Time.current).count
12-
@submissions_count_hour = Submission.where(created_at: (Time.current - 1.hour)..Time.current).count
13-
@submissions_count_today = Submission.where(created_at: Time.current.all_day).count
14-
@submissions_count_yesterday = Submission.where(created_at: Time.current.yesterday.all_day).count
15-
@submissions_count_week = Submission.where(created_at: Time.current.all_week).count
7+
@stats = Rails.cache.fetch("stats-cache")
8+
@stats = JSON.parse(@stats) if @stats
169
@sandboxes = Rails.cache.fetch("sandbox-status-cache")
1710
@sandboxes = JSON.parse(@sandboxes) if @sandboxes
1811
end

app/views/status/index.html.erb

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,50 @@
66

77
<h2>Submissions</h2>
88

9-
<table class="table">
10-
<tbody>
11-
<tr>
12-
<th scope="row">High priority submissions in queue </th scope="row">
13-
<td><%= @high_priority_submissions_count %></td>
14-
</tr>
15-
<tr>
16-
<th scope="row">All submissions in queue</th scope="row">
17-
<td><%= @sandbox_queue_length %></td>
18-
</tr>
19-
<tr>
20-
<th scope="row">Submissions, processing not complete</th>
21-
<td><%= @unprocessed_submissions_count %></td>
22-
</tr>
23-
<tr>
24-
<th scope="row">Submissions during the last minute</th>
25-
<td><%= @submissions_count_minute %></td>
26-
</tr>
27-
<tr>
28-
<th scope="row">Submissions during the last five minutes</th>
29-
<td><%= @submissions_count_five_minutes %></td>
30-
</tr>
31-
<tr>
32-
<th scope="row">Submissions during the latest hour</th>
33-
<td><%= @submissions_count_hour %></td>
34-
</tr>
35-
<tr>
36-
<th scope="row">Submissions today</th>
37-
<td><%= @submissions_count_today %></td>
38-
</tr>
39-
<tr>
40-
<th scope="row">Submissions yesterday</th>
41-
<td><%= @submissions_count_yesterday %></td>
42-
</tr>
43-
<tr>
44-
<th scope="row">Submissions this week</th>
45-
<td><%= @submissions_count_week %></td>
46-
</tr>
47-
</tbody>
48-
</table>
9+
<% if @stats %>
10+
<table class="table">
11+
<tbody>
12+
<tr>
13+
<th scope="row">High priority submissions in queue </th scope="row">
14+
<td><%= @stats['high_priority_submissions_count'] %></td>
15+
</tr>
16+
<tr>
17+
<th scope="row">All submissions in queue</th scope="row">
18+
<td><%= @stats['sandbox_queue_length'] %></td>
19+
</tr>
20+
<tr>
21+
<th scope="row">Submissions, processing not complete</th>
22+
<td><%= @stats['unprocessed_submissions_count'] %></td>
23+
</tr>
24+
<tr>
25+
<th scope="row">Submissions during the last minute</th>
26+
<td><%= @stats['submissions_count_minute'] %></td>
27+
</tr>
28+
<tr>
29+
<th scope="row">Submissions during the last five minutes</th>
30+
<td><%= @stats['submissions_count_five_minutes'] %></td>
31+
</tr>
32+
<tr>
33+
<th scope="row">Submissions during the latest hour</th>
34+
<td><%= @stats['submissions_count_hour'] %></td>
35+
</tr>
36+
<tr>
37+
<th scope="row">Submissions today</th>
38+
<td><%= @stats['submissions_count_today'] %></td>
39+
</tr>
40+
<tr>
41+
<th scope="row">Submissions yesterday</th>
42+
<td><%= @stats['submissions_count_yesterday'] %></td>
43+
</tr>
44+
<tr>
45+
<th scope="row">Submissions this week</th>
46+
<td><%= @stats['submissions_count_week'] %></td>
47+
</tr>
48+
</tbody>
49+
</table>
50+
<% else %>
51+
<p>No stats cached within the last minute. Check if the background daemon is running.</p>
52+
<% end %>
4953

5054

5155
<h2>Sandboxes</h2>

0 commit comments

Comments
 (0)