Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
Add more in-depth image filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
sourque committed Jul 17, 2020
1 parent f1acee4 commit 0824de3
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 60 deletions.
12 changes: 8 additions & 4 deletions engine/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def get_css_images(remote):
if "images" in remote:
images = remote["images"]
else:
ugly_images = execute("SELECT DISTINCT `image` FROM `css_results`")
ugly_images = execute("SELECT DISTINCT image FROM css_results ORDER BY image")
images = []
for image in ugly_images:
images.append(image[0])
Expand Down Expand Up @@ -271,13 +271,17 @@ def get_css_scores(remote, image=None):
return(team_scores)

# TODO optimize this
def get_css_score(team, remote):
def get_css_score(team, remote, image=None):
image_data = {}

#print("GETTING scores for team", team)
# Get all scores from one team
try:
team_scores = execute("SELECT time, image, points, vulns FROM css_results WHERE team=? ORDER BY time ASC", (team,))
# Filter by image if provided
if image:
team_scores = execute("SELECT time, image, points, vulns FROM css_results WHERE team=? and image=? ORDER BY time ASC", (team, image))
else:
team_scores = execute("SELECT time, image, points, vulns FROM css_results WHERE team=? ORDER BY time ASC", (team,))
#print("TEAM SCORES IS", team_scores)
current_block = datetime.strptime(team_scores[0][0], "%Y-%m-%d %H:%M:%S")
except:
Expand Down Expand Up @@ -472,7 +476,7 @@ def read_running_config():

def get_css_colors():
try:
return read_config()["remote"]["colors"]
return read_running_config()["remote"]["colors"]
except: pass

def write_running_config(config_tmp):
Expand Down
109 changes: 63 additions & 46 deletions engine/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,67 +194,84 @@ def css():
else:
event = None

# If filter by image:
images = db.get_css_images(em.remote)
image = None
if "image" in request.args:
image = request.args["image"]
if image not in images:
print("[ERROR] Invalid image specified:", image)
image = None

colors = {}
color_settings = db.get_css_colors()
image_list = db.get_css_images(em.remote)
if color_settings is not None:
for index, image_item in enumerate(image_list):
colors[image_item] = color_settings[index]
else:
for index, image_item in enumerate(image_list):
colors[image_item] = 'rgb(255, 255, 255)'

# Details view for CSS
if "team" in request.args:
teams = db.get_css_teams(em.remote)
team = request.args["team"]
team_name = team
if not team in teams:
team = db.remove_alias(team, em.remote)
try:
team = db.remove_alias(team, em.remote)
except:
# Invalid team
team = None
if team in teams:
if team not in team_data or (team in team_data and (datetime.now() - team_data[team]["refresh_time"]) > refresh_threshold):
print("[INFO] Refreshing data for team", team)
labels, image_data, scores = db.get_css_score(team, em.remote)
if image:
print("[INFO] Fetching image-specific data for", team, "image", image)
labels, image_data, scores = db.get_css_score(team, em.remote, image=image)
total_score = 0
for image in image_data.values():
total_score += image[3]

for image_item in image_data.values():
total_score += image_item[3]
team_info = (db.get_css_elapsed_time(team), \
db.get_css_play_time(team), \
total_score)

# Wonderful caching
team_data[team] = {}
team_data[team]["labels"] = labels
team_data[team]["image_data"] = image_data
team_data[team]["scores"] = scores
team_data[team]["refresh_time"] = datetime.now()
team_data[team]["elapsed_time"] = team_info[0]
team_data[team]["play_time"] = team_info[1]
team_data[team]["total_score"] = team_info[2]

else:

labels = team_data[team]["labels"]
image_data = team_data[team]["image_data"]
scores = team_data[team]["scores"]
team_info = (team_data[team]["elapsed_time"], \
team_data[team]["play_time"],
team_data[team]["total_score"])

colors = {}
color_settings = db.get_css_colors()
if color_settings is not None:
for index, image in enumerate(image_data):
colors[image] = color_settings[index]
else:
for index, image in enumerate(image_data):
colors[image] = 'rgb(255, 255, 255)'
return render_template("scores_css_details.html", labels=labels, team_name=team_name, team_info=team_info, image_data=image_data, scores=scores, css_mode=css_mode, colors=colors)
if team not in team_data or (team in team_data and (datetime.now() - team_data[team]["refresh_time"]) > refresh_threshold):
print("[INFO] Refreshing data for team", team)
labels, image_data, scores = db.get_css_score(team, em.remote)
total_score = 0
for image_item in image_data.values():
total_score += image_item[3]

team_info = (db.get_css_elapsed_time(team), \
db.get_css_play_time(team), \
total_score)

# Wonderful caching
team_data[team] = {}
team_data[team]["labels"] = labels
team_data[team]["image_data"] = image_data
team_data[team]["scores"] = scores
team_data[team]["refresh_time"] = datetime.now()
team_data[team]["elapsed_time"] = team_info[0]
team_data[team]["play_time"] = team_info[1]
team_data[team]["total_score"] = team_info[2]

else:

labels = team_data[team]["labels"]
image_data = team_data[team]["image_data"]
scores = team_data[team]["scores"]
team_info = (team_data[team]["elapsed_time"], \
team_data[team]["play_time"],
team_data[team]["total_score"])

return render_template("scores_css_details.html", labels=labels, team_name=team_name, team_info=team_info, image_data=image_data, scores=scores, css_mode=css_mode, colors=colors, image=image)
else:
print("[ERROR] Invalid team specified:", request.args["team"])

# If filter by image:
images = db.get_css_images(em.remote)
image = None
if "image" in request.args:
image = request.args["image"]
if image not in images:
print("[ERROR] Invalid image specified:", image)
image = None
else:
print("[INFO] Fetching CSS scoreboard for image", image)
team_scores = db.get_css_scores(em.remote, image=image)
if image:
print("[INFO] Fetching CSS scoreboard for image", image)
team_scores = db.get_css_scores(em.remote, image=image)

# Main scoreboard view
if not image:
Expand Down
15 changes: 10 additions & 5 deletions engine/templates/scores_css.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,32 +113,37 @@ <h4><i>{{ event }}</i></h4>
</thead>
<tbody>
{% for team in team_scores %}
{% if image %}
{% set linktext = '/scores/css?team=' + team[0] + '&image=' + image %}
{% else %}
{% set linktext = '/scores/css?team=' + team[0] %}
{% endif %}
<tr>
<td>
<a href="/scores/css?team={{ team[0] }}">
<a href="{{ linktext }}">
{{ team[0] }}
</a>
</td>
{% if not image %}
<td>
<a href="/scores/css?team={{ team[0] }}">
<a href="{{ linktext }}">
{{ team[1] }}
</a>
</td>
{% else %}
<td>
<a href="/scores/css?team={{ team[0] }}">
<a href="{{ linktext }}">
{{ image }}
</a>
</td>
{% endif %}
<td>
<a href="/scores/css?team={{ team[0] }}">
<a href="{{ linktext }}">
{{ team[2] }}
</a>
</td>
<td>
<a href="/scores/css?team={{ team[0] }}">
<a href="{{ linktext }}">
{{ team[3] }}
</a>
</td>
Expand Down
27 changes: 22 additions & 5 deletions engine/templates/scores_css_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@

{% block content %}

<style>
button a {
display: block;
text-decoration: none;
color: white;
}

button a:hover {
color: white;
text-decoration: none;
}
{% if css_mode %}
<style>
body {
background-color: #212529;
}
Expand All @@ -29,9 +39,7 @@
margin: auto;
text-align: center;
}
</style>
{% else %}
<style>
h2, h4, h6 {
text-align: center;
}
Expand All @@ -43,12 +51,21 @@
td {
cursor: pointer;
}
</style>
{% endif %}
</style>
<br/>

<h2>Leaderboard</h2><br>
<h2>{{ team_name }}</h2><br>
{% if image %}
<h4>
<button class="btn btn-lg btn-dark">
<a href="/scores/css?team={{ team_name }}">
Only showing data for {{ image }}.
</a>
</button>
</h4>
<br>
{% endif %}

<!-- Team info -->
{% if css_mode %}
Expand Down

0 comments on commit 0824de3

Please sign in to comment.