Refactor in preperation for multiple graphs

pull/370/head
Christopher Usher 1 year ago
parent a6e990db68
commit 9a3b1fd5c3

@ -6,7 +6,6 @@ import logging
import argh import argh
from graphs.main import main from graphs.main import main
# from main import main
LOG_FORMAT = "[%(asctime)s] %(levelname)8s %(name)s(%(module)s:%(lineno)d): %(message)s" LOG_FORMAT = "[%(asctime)s] %(levelname)8s %(name)s(%(module)s:%(lineno)d): %(message)s"

@ -10,6 +10,7 @@ import argh
import bokeh.plotting import bokeh.plotting
import bokeh.models import bokeh.models
import bokeh.palettes import bokeh.palettes
import bokeh.settings
import requests import requests
@ -56,22 +57,65 @@ def load_previous_donations(start_end_times, timeout):
return all_years, current_year return all_years, current_year
def all_years_donations_graph(start_end_times, all_years, current_year, current_json, base_dir):
logging.info('Generating all years donation graph')
p = bokeh.plotting.figure(x_axis_label='Bus Time', y_axis_label='Donations', x_range=(0, 60 * 60 * 172),
width=1280, height=720, active_scroll='wheel_zoom',
tools='pan,wheel_zoom,box_zoom,reset')
p.add_tools(bokeh.models.HoverTool(tooltips=[('', '$name'), ('Bustime', '@Bustime{00:00:00}'),
('Donations', '$@Donations{0,0.00}')]))
for year in start_end_times:
label_year = year
if year > 10:
label_year += 2006
label = 'DBfH {}'.format(label_year)
if year != current_year:
times, donations = all_years[year]
line_width = 2
else:
times, donations = parse_json(current_json, start_end_times[year][0], every_five=False)
line_width = 3
model = bokeh.models.ColumnDataSource(data={'Bustime':times, 'Donations':donations})
p.line(x='Bustime', y='Donations', source=model, line_width=line_width,
line_color=bokeh.palettes.Category20[20][current_year - year],
legend_label=label, name=label)
p.xaxis.ticker = bokeh.models.AdaptiveTicker(mantissas=[60, 120, 300, 600, 1200, 3600, 7200, 10800, 43200, 86400], base=10000000)
p.xaxis.formatter = bokeh.models.NumeralTickFormatter(format='00:00:00')
p.yaxis.formatter = bokeh.models.NumeralTickFormatter(format='$0,0')
p.legend.location = "top_left"
p.legend.click_policy="hide"
output_path = os.path.join(base_dir, 'all_years_donations.html')
bokeh.settings.settings.py_log_level = 'warn'
bokeh.plotting.output_file(filename=output_path, title='DBfH All Years Donations')
bokeh.plotting.save(p, filename=output_path)
logging.info('{} Saved'.format(output_path))
@argh.arg('--base-dir', help='Directory where segments are stored. Default is current working directory.') @argh.arg('--base-dir', help='Directory where segments are stored. Default is current working directory.')
def main(base_dir='.'): def main(base_dir='.'):
stopping = gevent.event.Event() stopping = gevent.event.Event()
logging.getLogger('bokeh').setLevel(logging.WARNING)
delay = 60 * 1 delay = 60 * 1
timeout = 15 timeout = 15
# First load data required
logging.info('Loading start and end times') logging.info('Loading start and end times')
start_end_path = os.path.join(base_dir, 'start_end_times.json') start_end_path = os.path.join(base_dir, 'start_end_times.json')
start_end_times = json.load(open(start_end_path)) start_end_times = json.load(open(start_end_path))
start_end_times = {int(year):start_end_times[year] for year in start_end_times} start_end_times = {int(year):start_end_times[year] for year in start_end_times}
all_years, current_year = load_previous_donations(start_end_times, timeout)
all_years, current_year = load_previous_donations(start_end_times, timeout)
current_url = 'http://example.com/{}/{}'.format(current_year, current_year) current_url = 'http://example.com/{}/{}'.format(current_year, current_year)
while not stopping.is_set(): while not stopping.is_set():
try: try:
@ -79,40 +123,9 @@ def main(base_dir='.'):
logging.info('Loading current data') logging.info('Loading current data')
current_json = requests.get(current_url, timeout=timeout).json() current_json = requests.get(current_url, timeout=timeout).json()
p = bokeh.plotting.figure(x_axis_label='Bus Time', y_axis_label='Donations', x_range=(0, 60 * 60 * 172), all_years_donations_graph(start_end_times, all_years, current_year, current_json, base_dir)
width=1280, height=720, active_scroll='wheel_zoom',
tools='pan,wheel_zoom,box_zoom,reset')
p.add_tools(bokeh.models.HoverTool(tooltips=[('', '$name'), ('Bustime', '@Bustime{00:00:00}'),
('Donations', '$@Donations{0,0.00}')]))
for year in start_end_times:
label_year = year
if year > 10:
label_year += 2006
label = 'DBfH {}'.format(label_year)
if year != current_year:
times, donations = all_years[year]
line_width = 2
else:
times, donations = parse_json(current_json, start_end_times[year][0], every_five=False)
line_width = 3
model = bokeh.models.ColumnDataSource(data={'Bustime':times, 'Donations':donations})
p.line(x='Bustime', y='Donations', source=model, line_width=line_width,
line_color=bokeh.palettes.Category20[20][current_year - year],
legend_label=label, name=label)
p.xaxis.ticker = bokeh.models.AdaptiveTicker(mantissas=[60, 120, 300, 600, 1200, 3600, 7200, 10800, 43200, 86400], base=10000000)
p.xaxis.formatter = bokeh.models.NumeralTickFormatter(format='00:00:00')
p.yaxis.formatter = bokeh.models.NumeralTickFormatter(format='$0,0')
p.legend.location = "top_left"
p.legend.click_policy="hide"
output_path = os.path.join(base_dir, 'all_years_donations.html')
bokeh.plotting.output_file(filename=output_path, title='DBfH All Years Donations')
bokeh.plotting.save(p, filename=output_path)
logging.info('{} Saved'.format(output_path))
except Exception: except Exception:
logging.exception('Plotting failed. Retrying') logging.exception('Plotting failed. Retrying')

Loading…
Cancel
Save