Skip to content

Commit

Permalink
User Submission History and User Stats (#131)
Browse files Browse the repository at this point in the history
* User Submission History and User Stats

User Submission History and User Stats

* Updated to include new db changes and user specific data

Updated to include new db changes and user specific data

* Updated Code

Updated Code

* Updated as per comments

Updated to check user session cookie, get count from already read database and directly convert to list

* Updated to handle Session changes

Updated to handle statsitics even if the session changes

* Updated as per @rishabhKalakoti and @ theSage21 comments

Updated as per @rishabhKalakoti and @ theSage21 comments

* Formatted code

Formatted code
  • Loading branch information
rsvarma95 authored and theSage21 committed May 25, 2019
1 parent 96f91d9 commit 11c89f0
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
31 changes: 30 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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 +122,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 @@ -147,6 +148,33 @@ def dashboard():
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()
.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
def question(code, number):
Expand Down Expand Up @@ -232,6 +260,7 @@ def rankings():
]
return bottle.template("rankings.html", people=order)


def logggedIn():
if not bottle.request.get_cookie("s_id"):
return False
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>

0 comments on commit 11c89f0

Please sign in to comment.