daily export three graphs with values from last three days

This commit is contained in:
simon 2021-02-24 11:52:25 +07:00
parent 379f0acdbb
commit fb8fe182bf
3 changed files with 50 additions and 3 deletions

3
.gitignore vendored
View File

@ -12,4 +12,5 @@ postgres.env
*.ttf
# dynamic files
**/dyn/*.json
**/dyn/*.json
**/dyn/

View File

@ -49,6 +49,47 @@ def create_current(config):
print(message)
def rebuild_3days(config):
""" wrapper to recreate all three days of graphs """
now = datetime.now()
# get axis
x_1, y_1, plt_title_1, x_ticks_1 = get_axis(1, now, config)
x_2, y_2, plt_title_2, x_ticks_2 = get_axis(2, now, config)
x_3, y_3, plt_title_3, x_ticks_3 = get_axis(3, now, config)
# set max
y_max = max(y_1.append(y_2).append(y_3)) + 50
# write plot
write_plt(x_1, y_1, plt_title_1, x_ticks_1, 'day-1', y_max)
write_plt(x_2, y_2, plt_title_2, x_ticks_2, 'day-2', y_max)
write_plt(x_3, y_3, plt_title_3, x_ticks_3, 'day-3', y_max)
print('recreaded last three days plt')
def get_axis(day, now, config):
""" recreate plot for single days """
day_delta = now.date() - timedelta(days = day)
day_from = int(day_delta.strftime('%s'))
day_until = int(day_delta.strftime('%s')) + 60 * 60 * 24
# make the SELECT
conn, cur = db_connect(config)
cur.execute(
f'SELECT epoch_time, aqi_value FROM aqi \
WHERE epoch_time > {day_from} \
AND epoch_time < {day_until} \
ORDER BY epoch_time DESC LIMIT 720;'
)
rows = cur.fetchall()
db_close(conn, cur)
# title
time_stamp = day_delta.strftime('%Y-%m-%d')
plt_title = f'AQI values from: {time_stamp}'
# build plt
x_ticks = np.arange(0, 97, step=8)
sample_rate = '15min'
x, y = build_plt(rows, sample_rate)
return x, y, plt_title, x_ticks
def build_plt(rows, sample_rate):
""" parse rows returns axis"""
# build x y
@ -71,10 +112,11 @@ def build_plt(rows, sample_rate):
return x, y
def write_plt(x, y, plt_title, x_ticks, file_name):
def write_plt(x, y, plt_title, x_ticks, file_name, y_max=''):
""" save plot to file """
# calc ticks
y_max = np.ceil(y.max()/50)*50 + 50
if not y_max:
y_max = np.ceil(y.max()/50)*50 + 50
# setup plot
plt.style.use('seaborn')
plt.plot(x, y, color='#313131',)

View File

@ -38,6 +38,7 @@ auth = HTTPBasicAuth()
config = get_config()
weather.handle_weather(config)
graph.create_current(config)
graph.rebuild_3days(config)
# build username / pw dict for basic auth
USER_DATA = {}
USER_DATA[config['authUsername']] = config['authPassword']
@ -51,6 +52,9 @@ scheduler.add_job(
scheduler.add_job(
graph.create_current, args=[config], trigger="cron", minute='*/5', name='current_graph'
)
scheduler.add_job(
graph.rebuild_3days, args=[config], trigger="cron", day='*', hour='1', minute='3', name='3_days'
)
scheduler.start()