Rewrite the event logic in javascript

The current events extension is computing the statuses at build time,
which is getting incorrect when an event is outdated until there is
a rebuild. This change fix that by doing the event status computation
at load time in javascript.

Moreover this change removes an unused import in utils.

Change-Id: Ifd1b84ff0182cf9bb7950da3d59f36c83019ac30
This commit is contained in:
Tristan Cacqueray 2017-01-30 01:01:23 +00:00
parent a297ab13ca
commit 8791d36b37
5 changed files with 30 additions and 38 deletions

View File

@ -16,6 +16,6 @@
| Events | Start Date | End Date | Status | | Events | Start Date | End Date | Status |
+====================================+====================================+====================================+===========+ +====================================+====================================+====================================+===========+
{% for event in events %} {% for event in events %}
| {{ "%29s" % event['name_str'] }} | {{ "%29s" % event['start_str'] }} | {{ "%29s" % event['end_str'] }} | {{ "%7s" % event['status'] }} | | {{ "%29s" % event['name'] }} | {{ "%29s" % event['start_str'] }} | {{ "%29s" % event['end_str'] }} | |
+------------------------------------+------------------------------------+------------------------------------+-----------+ +------------------------------------+------------------------------------+------------------------------------+-----------+
{% endfor %} {% endfor %}

View File

@ -13,40 +13,15 @@
"""Add election timer data """Add election timer data
""" """
import datetime
import jinja2 import jinja2
import jinja2.environment import jinja2.environment
import os import os
import pytz
from openstack_election import utils from openstack_election import utils
def build_timer(app): def build_timer(app):
app.add_javascript("event_timer.js") app.add_javascript("event_timer.js")
now = datetime.datetime.utcnow().replace(tzinfo=pytz.utc)
first_event = True
for ev in utils.conf['timeline']:
if ev['start'] > now:
ev['status'] = 'future'
elif ev['end'] > now:
ev['status'] = 'current'
else:
ev['status'] = 'past'
if first_event and ev['status'] == 'future':
ev['status'] = 'next'
first_event = False
mark = ''
if ev['status'] == 'current':
mark = '**'
elif ev['status'] == 'past':
mark = '*'
ev['start_iso'] = ev['start'].strftime("%Y-%m-%dT%H:%M")
ev['end_iso'] = ev['end'].strftime("%Y-%m-%dT%H:%M")
ev['name_str'] = "%s%s%s" % (mark, ev['name'], mark)
output_file = os.path.join(".", "doc", "source", "events.rst") output_file = os.path.join(".", "doc", "source", "events.rst")
with open(output_file, "w") as out: with open(output_file, "w") as out:
template_dir = os.path.join(".", "doc", "source", "_exts") template_dir = os.path.join(".", "doc", "source", "_exts")

View File

@ -1,7 +1,6 @@
/* Licensed under the Apache License, Version 2.0 /* Licensed under the Apache License, Version 2.0
*/ */
var event_date; var event_date;
var now = parseInt((new Date).getTime() / 1000);
function startTime() { function startTime() {
var delta = parseInt(event_date - (new Date).getTime() / 1000); var delta = parseInt(event_date - (new Date).getTime() / 1000);
@ -13,20 +12,37 @@ function startTime() {
var t = setTimeout(startTime, 500); var t = setTimeout(startTime, 500);
} }
function set_event_status(event_name, event_status) {
var td = $("td:contains('"+event_name+"')");
if (event_status == 'Next' || event_status == 'Current') {
td.css("font-weight", "bold")
}
td.next().next().next().html(event_status);
}
function setup_timeline() { function setup_timeline() {
var now = parseInt((new Date).getTime() / 1000);
for (i = 0; i < events_timeline.length; i++) { for (i = 0; i < events_timeline.length; i++) {
var current_event = events_timeline[i]; var current_event = events_timeline[i];
event_date = Date.parse(current_event.start) / 1000; var current_event_start = Date.parse(current_event.start) / 1000;
if (event_date > now) { var current_event_end = Date.parse(current_event.end) / 1000;
document.getElementById('eventname').innerHTML = current_event.name+' starts in'; if (now > current_event_end) {
startTime(); set_event_status(current_event.name, 'Past');
break;
} }
event_date = Date.parse(current_event.end) / 1000; else if (event_date == undefined && now < current_event_start) {
if (event_date > now) { document.getElementById('eventname').innerHTML = current_event.name+' starts in';
document.getElementById('eventname').innerHTML = current_event.name+' ends in'; set_event_status(current_event.name, 'Next');
event_date = current_event_start;
startTime(); startTime();
break; }
else if (event_date == undefined && now < current_event_end) {
document.getElementById('eventname').innerHTML = current_event.name+' ends in';
set_event_status(current_event.name, 'Current');
event_date = current_event_end;
startTime();
}
else if (now < current_event_start) {
set_event_status(current_event.name, 'Future');
} }
} }
} }

View File

@ -16,11 +16,12 @@ import yaml
TIME_FMT = "%b %d, %Y %H:%M %Z" TIME_FMT = "%b %d, %Y %H:%M %Z"
ISO_FMT = "%Y-%m-%dT%H:%M"
# Load configuration.yaml and create datetime objects # Load configuration.yaml and create datetime objects
def parse_datetime(iso_format): def parse_datetime(iso_format):
date = datetime.datetime.strptime(iso_format, "%Y-%m-%dT%H:%M") date = datetime.datetime.strptime(iso_format, ISO_FMT)
return date.replace(tzinfo=pytz.utc) return date.replace(tzinfo=pytz.utc)
@ -34,5 +35,6 @@ def load_conf():
for key in ('start', 'end'): for key in ('start', 'end'):
date = parse_datetime(event[key]) date = parse_datetime(event[key])
event[key] = date event[key] = date
event[key+'_iso'] = date.strftime(ISO_FMT)
event[key+'_str'] = date.strftime(TIME_FMT) event[key+'_str'] = date.strftime(TIME_FMT)
return conf return conf

View File

@ -21,7 +21,6 @@ import pickle
import pytz import pytz
import re import re
import requests import requests
import six
import subprocess import subprocess
import time import time
import yaml import yaml