From 6bebaaa9b971d8a885f31735f5634855dc1f666a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nut=CC=A6iu?= Date: Sat, 22 Apr 2017 01:20:01 +0300 Subject: [PATCH] Adding progress bar for the entry --- src/resources/utilities.py | 53 +++++++++++++++++++++++++++++++++++++- src/templates/result.html | 7 +++++ src/views/scoreboard.py | 5 +++- 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/src/resources/utilities.py b/src/resources/utilities.py index c4750a9..616f370 100644 --- a/src/resources/utilities.py +++ b/src/resources/utilities.py @@ -15,7 +15,7 @@ You should have received a copy of the GNU General Public License along with scoreboard-benchmark . If not, see . """ - +from src.models import Result from flask_cache import Cache import os @@ -75,3 +75,54 @@ def get_env_variable(variable, fallback): return os.environ[variable] except KeyError: return fallback + + +@cache.memoize(60*5) +def get_highest_score(): + """ + Will retrieve the result that has the highest score from the database. + + Returns: + The score of the highest rated result else it raises an exception. + + Raises: + LookupError if the database is empty. + """ + result = Result.query.order_by(Result.score.desc()).first() + if result: + return result.score + else: + raise LookupError("The database is empty.") + + +@cache.memoize(60*5) +def get_progress_bar_score(current): + """ + Will compute the procent of the highest score compared to the current score. + + Args: + current - The result with the current score. + + Returns: + The computed score, if it's greater than %100 then returns 100. + """ + highest = get_highest_score() + score = (current * 100) / highest + if score > 100: + return 100.0 + else: + return score + + +@cache.memoize(60*5) +def get_progress_bar_class(score): + """ + Returns the Boostrap class for the progress-bar based on the score. + """ + highest = get_highest_score() + if score > highest / 2: + return 'progress-bar-success' + elif score > highest / 4: + return 'progress-bar-warning' + else: + return 'progress-bar-danger' diff --git a/src/templates/result.html b/src/templates/result.html index b36ab3e..6195932 100644 --- a/src/templates/result.html +++ b/src/templates/result.html @@ -12,6 +12,13 @@

Detailed View

+
+
+ {{ progress_bar['value'] }}% Complete (danger) +
+

Score: {{ name.score }}

GPU: {{ name.gpu | safe}}

CPU: {{ name.cpu | safe}}

diff --git a/src/views/scoreboard.py b/src/views/scoreboard.py index 6b00086..5faadc9 100644 --- a/src/views/scoreboard.py +++ b/src/views/scoreboard.py @@ -81,7 +81,10 @@ def result(id): """ entry_name = Result.query.filter_by(id=id).first() if entry_name: - return flask.render_template("result.html", name=entry_name) + progress_bar_data = dict() + progress_bar_data['value'] = utilities.get_progress_bar_score(entry_name.score) + progress_bar_data['class'] = utilities.get_progress_bar_class(entry_name.score) + return flask.render_template("result.html", name=entry_name, progress_bar=progress_bar_data) else: flask.abort(404)