Skip to content

Commit

Permalink
Add last 24h top contributors table
Browse files Browse the repository at this point in the history
  • Loading branch information
ffont committed Apr 5, 2017
1 parent e4d6b9c commit 112357a
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
20 changes: 15 additions & 5 deletions datasets/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import json
import math
import logging
import datetime

logger = logging.getLogger('tasks')

Expand Down Expand Up @@ -153,22 +154,31 @@ def compute_dataset_taxonomy_stats(store_key, dataset_id):


@shared_task
def compute_annotators_ranking(store_key, dataset_id, user_id, N=10):
def compute_annotators_ranking(store_key, dataset_id, N=15):
logger.info('Start computing data for {0}'.format(store_key))
try:
dataset = Dataset.objects.get(id=dataset_id)
user = User.objects.get(id=user_id)

reference_date = datetime.datetime.today() - datetime.timedelta(days=1)
ranking = list()
ranking_24h = list()
for user in User.objects.all():
n_annotations = Annotation.objects.filter(created_by=user, sound_dataset__dataset=dataset).count()
n_votes = Vote.objects.filter(created_by=user, annotation__sound_dataset__dataset=dataset).count()
ranking.append(
(user.username, n_annotations + n_votes)
)
ranking = sorted(ranking, key=lambda x: x[1], reverse=True) # Sort by number of annotations
n_annotations_24h = Annotation.objects.filter(
created_at__gt=reference_date, created_by=user, sound_dataset__dataset=dataset).count()
n_votes_24h = Vote.objects.filter(
created_at__gt=reference_date, created_by=user, annotation__sound_dataset__dataset=dataset).count()
ranking_24h.append(
(user.username, n_annotations_24h + n_votes_24h)
)

ranking = sorted(ranking, key=lambda x: x[1], reverse=True) # Sort by number of annotations
ranking_24h = sorted(ranking_24h, key=lambda x: x[1], reverse=True) # Sort by number of annotations

store.set(store_key, {'ranking': ranking[:N]})
store.set(store_key, {'ranking': ranking[:N], 'ranking_24h': ranking_24h[:N]})
logger.info('Finished computing data for {0}'.format(store_key))
except Dataset.DoesNotExist:
pass
Expand Down
2 changes: 1 addition & 1 deletion datasets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def contribute(request, short_name):
dataset = get_object_or_404(Dataset, short_name=short_name)

# Get previously stored annotators ranking
annotators_ranking = data_from_async_task(compute_annotators_ranking, [dataset.id, request.user.id], {},
annotators_ranking = data_from_async_task(compute_annotators_ranking, [dataset.id], {},
DATASET_ANNOTATORS_RANKING_TEMPLATE.format(dataset.id), 60 * 1)

return render(request, 'contribute.html', {'dataset': dataset, 'annotators_ranking': annotators_ranking})
Expand Down
98 changes: 68 additions & 30 deletions templates/contribute.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,77 @@ <h1 class="ui header">Contribute to {{ dataset.name }}</h1>
<br><br>
</p>

<h2>Top 10 contributors</h2>
<table class="ui unstackable table" width="100%">
<thead>
<tr>
<th>#</th>
<th class="center aligned">Username</th>
<th class="center aligned">Number of contributions</th>
</tr>
</thead>
<tbody>
{% for username, n_annotations in annotators_ranking.ranking %}
<tr>
<td>{{ forloop.counter }}</td>
<td class="center aligned">
{% if forloop.counter == 1 %}
&#x1F947;
{% elif forloop.counter == 2 %}
&#x1F948;
{% elif forloop.counter == 3 %}
&#x1F949;
{% endif %}
{{ username }}
</td>
<td class="center aligned">{{ n_annotations}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="ui grid">
<div class="right aligned column">
<span class="small_gray_text">Last updated {{ annotators_ranking.redis_timestamp | timestamp_to_datetime | timesince }} ago</span>
<div class="two column row">
<div class="column">
<h2>All time top contributors</h2>
<table class="ui unstackable table" width="100%">
<thead>
<tr>
<th>#</th>
<th class="center aligned">Username</th>
<th class="center aligned">Number of contributions</th>
</tr>
</thead>
<tbody>
{% for username, n_annotations in annotators_ranking.ranking %}
<tr>
<td>{{ forloop.counter }}</td>
<td class="center aligned">
{% if forloop.counter == 1 %}
&#x1F947;
{% elif forloop.counter == 2 %}
&#x1F948;
{% elif forloop.counter == 3 %}
&#x1F949;
{% endif %}
{{ username }}
</td>
<td class="center aligned">{{ n_annotations}}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="column">
<h2>Last 24h top contributors</h2>
<table class="ui unstackable table" width="100%">
<thead>
<tr>
<th>#</th>
<th class="center aligned">Username</th>
<th class="center aligned">Number of contributions</th>
</tr>
</thead>
<tbody>
{% for username, n_annotations in annotators_ranking.ranking_24h %}
<tr>
<td>{{ forloop.counter }}</td>
<td class="center aligned">
{% if forloop.counter == 1 %}
&#x1F947;
{% elif forloop.counter == 2 %}
&#x1F948;
{% elif forloop.counter == 3 %}
&#x1F949;
{% endif %}
{{ username }}
</td>
<td class="center aligned">{{ n_annotations}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="ui grid">
<div class="right aligned column">
<span class="small_gray_text">Last updated {{ annotators_ranking.redis_timestamp | timestamp_to_datetime | timesince }} ago</span>
</div>
</div>
</div>
</div>
</div>



{% endblock %}

Expand Down

0 comments on commit 112357a

Please sign in to comment.