Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Submission History and User Stats #131

Merged
merged 8 commits into from
May 25, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

db = SqliteDatabase(DATABASE_NAME)


class User(Model):
username = CharField(unique=True)
password = CharField()
Expand Down Expand Up @@ -41,7 +40,6 @@ class Contest(Model):
class Meta:
database = db


class Question(Model):
q_no = IntegerField(unique=True)
author = ForeignKeyField(User)
Expand Down Expand Up @@ -73,7 +71,6 @@ class Meta:
db.connect()
db.create_tables([User, Session, Submission, ContestProblems, Contest, Question])


# dummy contest data
practiceContest = Contest.get_or_create(
code="PRACTICE",
Expand Down Expand Up @@ -123,6 +120,8 @@ def login_required(function):
def login_redirect(*args, **kwargs):
if not logggedIn():
return bottle.template("home.html", message="Login required.")
me = Session.get(Session.token == bottle.request.get_cookie('s_id'))
bottle.request.session = me
return function(*args, **kwargs)

return login_redirect
Expand All @@ -146,6 +145,19 @@ def dashboard():
contests = Contest.select().order_by(Contest.start_time)
return bottle.template("dashboard.html", contests=contests)

@app.get("/stats")
@login_required
def statistics():
sub_history = Submission.select(Contest.code, ContestProblems.question, Submission.time, Submission.is_correct)\
.where(Submission.user == bottle.request.session.user)\
.join(ContestProblems, on=(Submission.contestProblem == ContestProblems.id)) \
.switch() \
rsvarma95 marked this conversation as resolved.
Show resolved Hide resolved
.join(Contest, on=(ContestProblems.contest == Contest.id)) \
.order_by(Submission.time.desc()) \
.dicts()
sub_stats_total = len(sub_history)
sub_stats_correct = len([sub_history for sub in sub_history if sub["is_correct"]])
return bottle.template("stats.html", sub_history=sub_history, sub_stats_correct=sub_stats_correct, sub_stats_total=sub_stats_total)

@app.get("/contest/<code>/<number>")
@login_required
Expand Down
1 change: 1 addition & 0 deletions views/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ <h1>Contests</h1>
</table>
</div>
<div class="container">
<a href="/stats" class="btn btn-primary">Statistics</a>
<a href="/logout" class="btn btn-primary">Logout</a>
</div>
</body>
Expand Down
84 changes: 84 additions & 0 deletions views/stats.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
% include('base.html', title="User Statistics")
<body>
<div class="container text-center">
<div>
<h1>User Statistics</h1>
</div>
<div class="container" style="height:3rem;">
<a href="/dashboard" class="btn btn-primary">Dashboard</a>
<a href="/logout" class="btn btn-primary">Logout</a>
</div>
<table class="table">
<thead>
<tr>
<th>Submission History</th>
</tr>
</thead>
</table>
<table class="table">
<tbody style="height:20rem;display:inline-block;overflow-y:scroll">
<tr style="font-weight:bold;">
<td style="padding-right:10rem">Contest</td>
<td style="padding-right:6rem">Question Number</td>
<td style="padding-right:8rem">Time submitted</td>
<td style="padding-right:10rem">Correct/Not Correct</td>
</tr>
% for sub_history in sub_history :
<tr>
<td style="padding-right:10rem">{{sub_history["code"]}}</td>
<td style="padding-right:6rem">{{sub_history["question"]}}</td>
<td style="padding-right:8rem">{{sub_history["time"].strftime("%d-%m-%Y %H:%M")}}</td>
<td style="padding-right:10rem">{{sub_history["is_correct"]}}</td>
</tr>
% end
</tbody>
</table>
<table class="table">
<thead>
<tr>
<th>Submission Statistics</th>
</tr>
</thead>
</table>
<script>
var no_correct = {{sub_stats_correct}};
var no_total = {{sub_stats_total}};
var no_incorrect = no_total - no_correct;
</script>
<table class="table">
<tbody>
<tr>
<td style="color:blue;">Total Submissions : {{sub_stats_total}}</td>
</tr>
<tr>
<td id="piechart" style="padding-left:20rem"></td>
</tr>
<tr>
</tbody>
</table>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js">
</script>

<script type="text/javascript">
// Load google charts
google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);

// Draw the chart and set the chart values
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Category', 'Total'],
['No of Incorrect Questions', no_incorrect],
['No of Correct Questions', no_correct]
]);

// Optional; add a title and set the width and height of the chart
var options = {'width':600, 'height':300};

// Display the chart inside the <div> element with id="piechart"
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
</script>
</div>
</body>