Shifts graphs

pull/374/head
Christopher Usher 1 year ago
parent e66540087a
commit e12e361525

@ -14,8 +14,10 @@ import bokeh.settings
import numpy as np import numpy as np
import requests import requests
def format_year(year):
if year > 10:
year += 2006
return 'DBfH {}'.format(year)
def parse_json(json_donations, start_date, end_hour=np.inf, every_five=True): def parse_json(json_donations, start_date, end_hour=np.inf, every_five=True):
@ -51,7 +53,6 @@ def load_previous_donations(start_end_times, donation_url_template, timeout):
current_year = year current_year = year
continue continue
url = donation_url_template.format(year, year) url = donation_url_template.format(year, year)
logging.info('Loading {}'.format(url)) logging.info('Loading {}'.format(url))
year_json = requests.get(url, timeout=timeout).json() year_json = requests.get(url, timeout=timeout).json()
@ -69,10 +70,7 @@ def all_years_donations_graph(start_end_times, all_years, current_year, current_
p.add_tools(bokeh.models.HoverTool(tooltips=[('', '$name'), ('Bustime', '@Bustime{00:00:00}'), p.add_tools(bokeh.models.HoverTool(tooltips=[('', '$name'), ('Bustime', '@Bustime{00:00:00}'),
('Donations', '$@Donations{0,0.00}')])) ('Donations', '$@Donations{0,0.00}')]))
for year in start_end_times: for year in start_end_times:
label_year = year label = format_year(year)
if year > 10:
label_year += 2006
label = 'DBfH {}'.format(label_year)
if year != current_year: if year != current_year:
times, donations = all_years[year] times, donations = all_years[year]
line_width = 2 line_width = 2
@ -93,11 +91,51 @@ def all_years_donations_graph(start_end_times, all_years, current_year, current_
p.legend.click_policy="hide" p.legend.click_policy="hide"
output_path = os.path.join(base_dir, 'all_years_donations.html') 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.output_file(filename=output_path, title='DBfH All Years Donations')
bokeh.plotting.save(p, filename=output_path) bokeh.plotting.save(p, filename=output_path)
logging.info('{} Saved'.format(output_path)) logging.info('{} Saved'.format(output_path))
def shifts_graph(start_end_times, current_year, current_json, base_dir, shifts):
logging.info('Generating DBfH {} shifts graph'.format(current_year))
times, donations = parse_json(current_json, start_end_times[current_year][0], every_five=False)
start_hour = int(start_end_times[current_year][0][11:13])
hours = times / 3600 + start_hour
mod_hours = hours % 24
n_days = int(hours.max() / 24) + 1
p = bokeh.plotting.figure(x_axis_label='Hour of Day', y_axis_label='Donations', x_range=(0, 24 * 3600),
width=1280, height=720, active_scroll='wheel_zoom',
tools='pan,wheel_zoom,box_zoom,reset')
p.add_tools(bokeh.models.HoverTool(tooltips=[('', '$name'), ('Hour of Day', '@Hours{00:00:00}'),
('Donations', '$@Donations{0,0.00}')]))
for day in range(n_days):
for shift in shifts:
in_range = (hours >= day * 24 + shift[1]) & (hours <= day * 24 + shift[2])
hours_in_range = mod_hours[in_range]
if mod_hours[in_range].size:
if hours_in_range[-1] == 0.:
hours_in_range[-1] = 24
model = bokeh.models.ColumnDataSource(data={'Hours':hours_in_range * 3600, 'Donations':donations[in_range] - donations[in_range][0]})
p.line(x='Hours', y='Donations', source=model, line_color=bokeh.palettes.Category10[10][day],
line_width=2, legend_label='Day {}'.format(day + 1), name='Day {} {}'.format(day + 1, shift[0]))
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, 'DBfH_{}_shifts_graph.html'.format(current_year))
bokeh.plotting.output_file(filename=output_path, title='{} Shift Donations'.format(format_year(current_year)))
bokeh.plotting.save(p, filename=output_path)
logging.info('{} Saved'.format(output_path))
@argh.arg('--base-dir', help='Directory where graphs are output. Default is current working directory.') @argh.arg('--base-dir', help='Directory where graphs are output. Default is current working directory.')
def main(donation_url_template, base_dir='.'): def main(donation_url_template, base_dir='.'):
@ -109,6 +147,11 @@ def main(donation_url_template, base_dir='.'):
delay = 60 * 1 delay = 60 * 1
timeout = 15 timeout = 15
shifts = [['Zeta Shift', 0, 6],
['Alpha Flight', 6, 12],
['Dawn Guard', 12, 18],
['Night Watch', 18, 24]]
# First load data required # 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')
@ -127,6 +170,8 @@ def main(donation_url_template, base_dir='.'):
all_years_donations_graph(start_end_times, all_years, current_year, current_json, base_dir) all_years_donations_graph(start_end_times, all_years, current_year, current_json, base_dir)
shifts_graph(start_end_times, current_year, current_json, base_dir, shifts)
except Exception: except Exception:
logging.exception('Plotting failed. Retrying') logging.exception('Plotting failed. Retrying')

Loading…
Cancel
Save