Skip to content

Commit f53345d

Browse files
authored
AWS: don't refresh the page when contest phase changes (#1510)
1 parent cdc24e9 commit f53345d

File tree

4 files changed

+17
-28
lines changed

4 files changed

+17
-28
lines changed

cms/db/contest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ def phase(self, timestamp: datetime) -> int:
306306
307307
timestamp: the time we are iterested in.
308308
"""
309+
# NOTE: this logic is duplicated in aws_utils.js.
309310
if timestamp < self.start:
310311
return -1
311312
if timestamp <= self.stop:

cms/server/admin/handlers/base.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,6 @@ def render_params(self) -> dict:
334334
if self.current_user is not None:
335335
params["admin"] = self.current_user
336336
if self.contest is not None:
337-
params["phase"] = self.contest.phase(params["timestamp"])
338337
params["unanswered"] = self.sql_session.query(Question)\
339338
.join(Participation)\
340339
.filter(Participation.contest_id == self.contest.id)\

cms/server/admin/static/aws_utils.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var CMS = CMS || {};
3030
CMS.AWSUtils = function(url_root, timestamp,
3131
contest_start, contest_stop,
3232
analysis_start, analysis_stop,
33-
phase) {
33+
analysis_enabled) {
3434
this.url = CMS.AWSUtils.create_url_builder(url_root);
3535
this.first_date = new Date();
3636
this.last_notification = timestamp;
@@ -39,7 +39,7 @@ CMS.AWSUtils = function(url_root, timestamp,
3939
this.contest_stop = contest_stop;
4040
this.analysis_start = analysis_start;
4141
this.analysis_stop = analysis_stop;
42-
this.phase = phase;
42+
this.analysis_enabled = analysis_enabled;
4343
this.file_asked_name = "";
4444
this.file_asked_url = "";
4545

@@ -476,42 +476,35 @@ CMS.AWSUtils.prototype.two_digits = function(n) {
476476

477477
/**
478478
* Update the remaining time showed in the "remaining" div.
479-
*
480-
* timer (int): handle for the timer that called this function, or -1 if none
481479
*/
482-
CMS.AWSUtils.prototype.update_remaining_time = function(timer = -1) {
483-
// We assume this.phase always is the correct phase (since this
484-
// method also refreshes the page when the phase changes).
480+
CMS.AWSUtils.prototype.update_remaining_time = function() {
485481
var relevant_timestamp = null;
486482
var text = null;
487-
if (this.phase === -1) {
483+
var now_timestamp = this.timestamp + (new Date() - this.first_date) / 1000;
484+
485+
// based on the phase logic from cms/db/contest.py.
486+
if (now_timestamp < this.contest_start) {
488487
relevant_timestamp = this.contest_start;
489488
text = "To start of contest: "
490-
} else if (this.phase === 0) {
489+
} else if (now_timestamp <= this.contest_stop) {
491490
relevant_timestamp = this.contest_stop;
492491
text = "To end of contest: "
493-
} else if (this.phase === 1) {
492+
} else if (this.analysis_enabled && now_timestamp < this.analysis_start) {
494493
relevant_timestamp = this.analysis_start;
495494
text = "To start of analysis: "
496-
} else if (this.phase === 2) {
495+
} else if (this.analysis_enabled && now_timestamp <= this.analysis_stop) {
497496
relevant_timestamp = this.analysis_stop;
498497
text = "To end of analysis: "
499498
}
500499

501500
// We are in phase 3, nothing to show.
502501
if (relevant_timestamp === null) {
502+
$("#remaining_text").text("");
503+
$("#remaining_value").text("");
503504
return;
504505
}
505506

506-
// Compute actual seconds to next phase value, and if negative we
507-
// refresh to update the phase.
508-
var now = new Date();
509-
var countdown_sec =
510-
relevant_timestamp - this.timestamp - (now - this.first_date) / 1000;
511-
if (countdown_sec <= 0) {
512-
clearInterval(timer);
513-
location.reload();
514-
}
507+
var countdown_sec = relevant_timestamp - now_timestamp;
515508

516509
$("#remaining_text").text(text);
517510
$("#remaining_value").text(this.format_countdown(countdown_sec));

cms/server/admin/templates/base.html

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,18 @@
2626
function init()
2727
{
2828
{% if contest is none %}
29-
utils = new CMS.AWSUtils("{{ url() }}", {{ timestamp|make_timestamp }}, 0, 0, 0, 0, -1);
30-
utils.update_notifications();
31-
setInterval(function() { utils.update_notifications(); }, 15000);
29+
utils = new CMS.AWSUtils("{{ url() }}", {{ timestamp|make_timestamp }}, 0, 0, 0, 0, 0);
3230
{% else %}
3331
utils = new CMS.AWSUtils("{{ url() }}", {{ timestamp|make_timestamp }},
3432
{{ contest.start|make_timestamp }}, {{ contest.stop|make_timestamp }},
3533
{{ contest.analysis_start|make_timestamp }}, {{ contest.analysis_stop|make_timestamp }},
36-
{{ phase }});
34+
{{ contest.analysis_enabled | int }});
3735

38-
{% if phase < 3 %}
3936
utils.update_remaining_time();
40-
var timer = setInterval(function() { utils.update_remaining_time(timer); }, 1000);
37+
setInterval(function() { utils.update_remaining_time(); }, 1000);
4138
{% endif %}
4239
utils.update_notifications();
4340
setInterval(function() { utils.update_notifications(); }, 15000);
44-
{% endif %}
4541

4642
$(document).click(utils.hide_subpage);
4743

0 commit comments

Comments
 (0)