You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.0 KiB
Python

8 months ago
#!/usr/bin/env python3
8 months ago
import pickle
from matplotlib import pyplot as plt
import requests
import json
from datetime import datetime
from dataclasses import dataclass
from collections import defaultdict
from pathlib import Path
api_url = r'https://review.video.fosdem.org/api/v1/event/1/overview'
poll_rate = 10*60 # seconds
pickle_file = Path(r'./talk_data.pickle')
@dataclass
class TimeSeries:
"The object to pickle and unpickle"
states: set[str]
jobstates: set[str]
data: dict[datetime, tuple[dict[str, int], dict[str, int]]] # time → (states(name → count), jobstates(ditto))
def get_data() -> tuple[datetime, dict[str, int], dict[str, int]]:
resp = requests.get(api_url)
# TODO: error handling?
time = datetime.now().astimezone()
data = json.loads(resp.content.decode())
states = defaultdict(lambda: 0)
jobstates = defaultdict(lambda: 0)
# should be an array/list
for talk in data:
states[talk['state']] += 1
jobstates[talk['progress']] += 1
return time, dict(states), dict(jobstates)
8 months ago
def main():
if pickle_file.exists():
with open(pickle_file, 'rb') as f:
ts = pickle.load(f)
else:
print("New time series.")
8 months ago
ts = TimeSeries(states=set(), jobstates=set(), data=dict())
8 months ago
while True:
time, states, jobstates = get_data()
8 months ago
print(f'data got at {time}')
8 months ago
assert time not in ts.data
ts.data[time] = (states, jobstates)
8 months ago
ts.states |= states.keys()
ts.jobstates |= jobstates.keys()
8 months ago
# Save the pickle
# Should probably save elsewhere and do atomic rename, but whatever.
with open(pickle_file, 'wb') as f:
pickle.dump(ts, f)
8 months ago
print('pickle saved')
8 months ago
# Show the plot
plt.close()
plt.clf()
8 months ago
x = sorted(ts.data.keys()) # times
ys = [tuple(ts.data[time][0].get(state, 0) for state in ts.states) for time in x] # numbers per state
# We do not show jobstates atm. Too lazy.
# ys are transposed we need vectors by times, not by states.
ys = list(zip(*ys))
plt.stackplot(x, *ys)
8 months ago
print('showing plot')
8 months ago
plt.show(block=False)
plt.pause(poll_rate)
8 months ago
main()