Skip to content

Commit

Permalink
Merge pull request #1217 from LibraryOfCongress/more-database-optimiz…
Browse files Browse the repository at this point in the history
…ations

First pass optimizing the campaign/topic detail stats
  • Loading branch information
rstorey authored Mar 25, 2020
2 parents cb0b7c4 + e3393ac commit 492dec7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
18 changes: 18 additions & 0 deletions concordia/migrations/0049_auto_20200324_2004.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.2.11 on 2020-03-25 00:04

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("concordia", "0048_auto_20200324_1820"),
]

operations = [
migrations.AlterField(
model_name="project",
name="published",
field=models.BooleanField(blank=True, db_index=True, default=False),
),
]
2 changes: 1 addition & 1 deletion concordia/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Project(MetricsModelMixin("project"), models.Model):

campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE)

published = models.BooleanField(default=False, blank=True)
published = models.BooleanField(default=False, blank=True, db_index=True)

title = models.CharField(max_length=80)
slug = models.SlugField(max_length=80, allow_unicode=True)
Expand Down
32 changes: 18 additions & 14 deletions concordia/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.contrib.auth.views import (
LoginView,
PasswordResetConfirmView,
Expand Down Expand Up @@ -456,10 +455,15 @@ def serialize_context(self, context):
def calculate_asset_stats(asset_qs, ctx):
asset_count = asset_qs.count()

trans_qs = Transcription.objects.filter(asset__in=asset_qs)
ctx["contributor_count"] = User.objects.filter(
Q(pk__in=trans_qs.values("user_id")) | Q(pk__in=trans_qs.values("reviewed_by"))
).count()
trans_qs = Transcription.objects.filter(asset__in=asset_qs).values_list(
"user_id", "reviewed_by"
)
user_ids = set()
for i, j in trans_qs.iterator():
user_ids.add(i)
user_ids.add(j)

ctx["contributor_count"] = len(user_ids)

asset_state_qs = asset_qs.values_list("transcription_status")
asset_state_qs = asset_state_qs.annotate(Count("transcription_status")).order_by()
Expand Down Expand Up @@ -1373,7 +1377,7 @@ def reserve_asset(request, *, asset_pk):

# We'll pass the message to the WebSocket listeners before returning it:
msg = {"asset_pk": asset_pk, "reservation_token": reservation_token}
logger.debug("Releasing reservation with token %s" % reservation_token)
logger.info("Releasing reservation with token %s", reservation_token)
reservation_released.send(sender="reserve_asset", **msg)
return JsonResponse(msg)

Expand All @@ -1396,23 +1400,23 @@ def reserve_asset(request, *, asset_pk):
if reservation.tombstoned:
if reservation.reservation_token == reservation_token:
am_i_tombstoned = True
logger.debug("I'm tombstoned %s" % reservation_token)
logger.debug("I'm tombstoned %s", reservation_token)
else:
is_someone_else_tombstoned = True
logger.debug(
"Someone else is tombstoned %s" % reservation.reservation_token
"Someone else is tombstoned %s", reservation.reservation_token
)
else:
if reservation.reservation_token == reservation_token:
is_it_already_mine = True
logger.debug(
"I already have this active reservation %s" % reservation_token
"I already have this active reservation %s", reservation_token
)
if not is_it_already_mine:
is_someone_else_active = True
logger.debug(
"Someone else has this active reservation %s"
% reservation.reservation_token
"Someone else has this active reservation %s",
reservation.reservation_token,
)

if am_i_tombstoned:
Expand All @@ -1424,18 +1428,18 @@ def reserve_asset(request, *, asset_pk):
if is_it_already_mine:
# This user already has the reservation and it's not tombstoned
msg = update_reservation(asset_pk, reservation_token)
logger.debug("Updating reservation %s" % reservation_token)
logger.debug("Updating reservation %s", reservation_token)

if is_someone_else_tombstoned:
msg = obtain_reservation(asset_pk, reservation_token)
logger.debug(
"Obtaining reservation for %s from tombstoned user" % reservation_token
"Obtaining reservation for %s from tombstoned user", reservation_token
)

else:
# No reservations = no activity = go ahead and do an insert
msg = obtain_reservation(asset_pk, reservation_token)
logger.debug("No activity, just get the reservation %s" % reservation_token)
logger.debug("No activity, just get the reservation %s", reservation_token)

return JsonResponse(msg)

Expand Down

0 comments on commit 492dec7

Please sign in to comment.