This commit is contained in:
Hendrik Borras 2018-02-07 11:08:03 +01:00
parent 186b52a706
commit 01ded9cd34
3 changed files with 43 additions and 13 deletions

1
.gitignore vendored
View file

@ -4,6 +4,7 @@ storage/sqlite_db_edit
backend/1-5_raw_output.log backend/1-5_raw_output.log
*.log *.log
some_file_location some_file_location
/frontend/static/images/dashboard_histogram.png
# Compiled python modules. # Compiled python modules.
*.pyc *.pyc

View file

@ -35,7 +35,7 @@
<input type="checkbox" onclick="toggleAutoRefresh(this);" id="reloadCB"> Auto refresh dashboard every 5 seconds <input type="checkbox" onclick="toggleAutoRefresh(this);" id="reloadCB"> Auto refresh dashboard every 5 seconds
</div> </div>
<div class="panel-body"> <div class="panel-body">
<img id="img" src="/histogram.png?start_time=-120&end_time=9000000000&bin_size_seconds=2"/> <img id="img" src="{{ url_for('static',filename='images/dashboard_histogram.png') }}"/>
</div> </div>
</div> </div>
</div> </div>

View file

@ -360,12 +360,8 @@ def about_page():
return render_template('about.html') return render_template('about.html')
@app.route('/histogram.png') # returns the histogram as a png byte stream
def build_plot(): def build_histogram(start_time, end_time, bin_size_seconds):
# get user set parameters
start_time = request.args.get('start_time', type=int)
end_time = request.args.get('end_time', type=int)
bin_size_seconds = request.args.get('bin_size_seconds', type=int)
plot_title = '' plot_title = ''
# get some data # get some data
@ -374,7 +370,8 @@ def build_plot():
cursor = conn.cursor() cursor = conn.cursor()
# only get the last n seconds if the start time was negative # only get the last n seconds if the start time was negative
if start_time < 0: if start_time < 0:
plot_title += "Histogram of events over the last {0:.1f} minutes\nbin size: {1:d} [s]".format(-start_time/60., bin_size_seconds) plot_title += "Histogram of events over the last {0:.1f} minutes\nbin size: {1:d} [s]".format(-start_time / 60.,
bin_size_seconds)
cursor.execute("SELECT * FROM Events ORDER BY UTCUnixTime DESC, SubSeconds DESC;") cursor.execute("SELECT * FROM Events ORDER BY UTCUnixTime DESC, SubSeconds DESC;")
start_time = cursor.fetchone()[0] + start_time start_time = cursor.fetchone()[0] + start_time
end_time = 9000000000 end_time = 9000000000
@ -426,13 +423,45 @@ def build_plot():
plt.savefig(img, format='png') plt.savefig(img, format='png')
img.seek(0) img.seek(0)
plt.close() plt.close()
response = make_response(img.getvalue()) return img.getvalue()
@app.route('/histogram.png')
def serve_histogram_request():
# get user set parameters
start_time = request.args.get('start_time', type=int)
end_time = request.args.get('end_time', type=int)
bin_size_seconds = request.args.get('bin_size_seconds', type=int)
# render the plot
img = build_histogram(start_time, end_time, bin_size_seconds)
# return the plot
response = make_response(img)
response.headers['Content-Type'] = 'image/png' response.headers['Content-Type'] = 'image/png'
return response return response
def periodically_render_dashboard_histogram():
while True:
# standard values for the dashboard histogram
start_time = -120
end_time = 9000000000
bin_size_seconds = 2
# place where we need to save the image
path_to_static_image = "static/images/dashboard_histogram.png"
# render the plot
img = build_histogram(start_time, end_time, bin_size_seconds)
# save the image to disk
with open(path_to_static_image, 'wb') as f:
f.write(img)
# wait four seconds until we render the next plot
time.sleep(4)
if __name__ == '__main__': if __name__ == '__main__':
# do necessary inits # do necessary inits
initDB() initDB()
# Launch the periodical histogram renderer for the dashboard
thread.start_new_thread(periodically_render_dashboard_histogram, ())
app.run() app.run()