Add a bell curve graph and total points calculation (#1325)

* Add a bell curve graph and total points calculation to admin panel statistics
* Closes #608
1318-submissions-api-improvements
Kevin Chung 2020-04-17 03:10:55 -04:00 committed by GitHub
parent ccefe47468
commit cd6439f2eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 153 additions and 2 deletions

View File

@ -31,6 +31,13 @@ def statistics():
challenge_count = Challenges.query.count() challenge_count = Challenges.query.count()
total_points = (
Challenges.query.with_entities(db.func.sum(Challenges.value).label("sum"))
.filter_by(state="visible")
.first()
.sum
) or 0
ip_count = Tracking.query.with_entities(Tracking.ip).distinct().count() ip_count = Tracking.query.with_entities(Tracking.ip).distinct().count()
solves_sub = ( solves_sub = (
@ -73,6 +80,7 @@ def statistics():
wrong_count=wrong_count, wrong_count=wrong_count,
solve_count=solve_count, solve_count=solve_count,
challenge_count=challenge_count, challenge_count=challenge_count,
total_points=total_points,
solve_data=solve_data, solve_data=solve_data,
most_solved=most_solved, most_solved=most_solved,
least_solved=least_solved, least_solved=least_solved,

View File

@ -8,3 +8,4 @@ from CTFd.api.v1.statistics import challenges # noqa: F401
from CTFd.api.v1.statistics import submissions # noqa: F401 from CTFd.api.v1.statistics import submissions # noqa: F401
from CTFd.api.v1.statistics import teams # noqa: F401 from CTFd.api.v1.statistics import teams # noqa: F401
from CTFd.api.v1.statistics import users # noqa: F401 from CTFd.api.v1.statistics import users # noqa: F401
from CTFd.api.v1.statistics import scores # noqa: F401

View File

@ -0,0 +1,43 @@
from collections import defaultdict
from flask_restx import Resource
from CTFd.api.v1.statistics import statistics_namespace
from CTFd.models import db, Challenges
from CTFd.utils.decorators import admins_only
from CTFd.utils.scores import get_standings
@statistics_namespace.route("/scores/distribution")
class ScoresDistribution(Resource):
@admins_only
def get(self):
challenge_count = Challenges.query.count() or 1
total_points = (
Challenges.query.with_entities(db.func.sum(Challenges.value).label("sum"))
.filter_by(state="visible")
.first()
.sum
) or 0
# Convert Decimal() to int in some database backends for Python 2
total_points = int(total_points)
# Divide score by challenges to get brackets with explicit floor division
bracket_size = total_points // challenge_count
# Get standings
standings = get_standings(admin=True)
# Iterate over standings and increment the count for each bracket for each standing within that bracket
bottom, top = 0, bracket_size
count = 1
brackets = defaultdict(lambda: 0)
for t in reversed(standings):
if ((t.score >= bottom) and (t.score <= top)) or t.score <= 0:
brackets[top] += 1
else:
count += 1
bottom, top = (bracket_size, (bracket_size * count))
brackets[top] += 1
return {"success": True, "data": {"brackets": brackets}}

View File

@ -177,6 +177,59 @@ const graph_configs = {
annotations annotations
]; ];
} }
},
"#score-distribution-graph": {
layout: annotations => ({
title: "Score Distribution",
xaxis: {
title: "Score Bracket",
showticklabels: true,
type: "category"
},
yaxis: {
title: "Number of {0}".format(
CTFd.config.userMode.charAt(0).toUpperCase() +
CTFd.config.userMode.slice(1)
)
},
annotations: annotations
}),
data: () =>
CTFd.fetch("/api/v1/statistics/scores/distribution").then(function(
response
) {
return response.json();
}),
fn: () =>
"CTFd_score_distribution_" + new Date().toISOString().slice(0, 19),
format: response => {
const data = response.data.brackets;
const keys = [];
const brackets = [];
const sizes = [];
for (let key in data) {
keys.push(parseInt(key));
}
keys.sort((a, b) => a - b);
let start = "<0";
keys.map(key => {
brackets.push("{0} - {1}".format(start, key));
sizes.push(data[key]);
start = key;
});
return [
{
type: "bar",
x: brackets,
y: sizes,
orientation: "v"
}
];
}
} }
}; };

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -15,6 +15,7 @@
{% endif %} {% endif %}
<h5><b>{{ ip_count }}</b> IP addresses</h5> <h5><b>{{ ip_count }}</b> IP addresses</h5>
<hr> <hr>
<h5><b>{{ total_points }}</b> total possible points</h5>
<h5><b>{{ challenge_count }}</b> challenges</h5> <h5><b>{{ challenge_count }}</b> challenges</h5>
{% if most_solved %} {% if most_solved %}
<h5><b>{{ most_solved }}</b> has the most solves with <br>{{ solve_data[most_solved] }} solves</h5> <h5><b>{{ most_solved }}</b> has the most solves with <br>{{ solve_data[most_solved] }} solves</h5>
@ -35,6 +36,18 @@
<hr> <hr>
<div class="row">
<div class="col-md-12">
<div id="score-distribution-graph">
<div class="text-center">
<i class="fas fa-circle-notch fa-spin fa-3x fa-fw spinner"></i>
</div>
</div>
</div>
</div>
<hr>
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<div id="solve-percentages-graph"> <div id="solve-percentages-graph">

View File

View File

@ -0,0 +1,32 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from CTFd.models import Users
from tests.helpers import (
create_ctfd,
destroy_ctfd,
login_as_user,
register_user,
simulate_user_activity,
)
def test_api_statistics_score_distribution():
app = create_ctfd()
with app.app_context():
# Handle zero data case
client = login_as_user(app, name="admin", password="password")
r = client.get("/api/v1/statistics/scores/distribution")
resp = r.get_json()
assert resp["data"]["brackets"] == {}
# Add user data
register_user(app)
user = Users.query.filter_by(email="user@ctfd.io").first()
simulate_user_activity(app.db, user=user)
# Test again
r = client.get("/api/v1/statistics/scores/distribution")
resp = r.get_json()
assert resp["data"]["brackets"]
destroy_ctfd(app)

View File

@ -15,6 +15,7 @@ def test_api_hint_404():
"/api/v1/statistics/users/{}", "/api/v1/statistics/users/{}",
"/api/v1/configs", "/api/v1/configs",
"/api/v1/statistics/challenges/solves/percentages", "/api/v1/statistics/challenges/solves/percentages",
"/api/v1/statistics/scores/distribution",
"/api/v1/tags/{}", "/api/v1/tags/{}",
"/api/v1/pages", "/api/v1/pages",
"/api/v1/files/{}", "/api/v1/files/{}",