Skip to content

Commit

Permalink
Make the database do the work
Browse files Browse the repository at this point in the history
  • Loading branch information
itsthejoker committed Feb 28, 2023
1 parent a8abdcc commit 9b0969f
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions blossom/api/views/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
from typing import Union

from django.conf import settings
from django.db.models import Count, F
from django.db.models import Count, F, Value
from django.db.models.functions import (
ExtractHour,
ExtractIsoWeekDay,
Length,
StrIndex,
Substr,
TruncDate,
TruncDay,
TruncHour,
Expand All @@ -37,13 +39,7 @@

from blossom.api.authentication import BlossomApiPermission
from blossom.api.helpers import validate_request
from blossom.api.models import (
Source,
Submission,
Transcription,
TranscriptionCheck,
extract_subreddit_from_url,
)
from blossom.api.models import Source, Submission, Transcription, TranscriptionCheck
from blossom.api.pagination import StandardResultsSetPagination
from blossom.api.serializers import SubmissionSerializer
from blossom.api.slack import client as slack
Expand Down Expand Up @@ -404,19 +400,23 @@ def subreddits(self, request: Request) -> Response:
"""Count the submissions by subreddit."""
subreddit_query = (
self.filter_queryset(Submission.objects)
# Only include Reddit posts with the expected URL format
# https://reddit.com/r/testing/comments/12345/hi/
.filter(url__startswith="https://reddit.com/r/")
# Return only the relevant values
.values("id", "url")
# testing/comments/12345/hi/
.annotate(subreddit=Substr("url", 22, 50))
# 8
.annotate(slash_index=StrIndex("subreddit", Value("/")))
# testing
.annotate(subreddit_name=Substr("subreddit", 1, F("slash_index") - 1))
.values("subreddit_name")
.annotate(Count("subreddit_name"))
)

subreddit_counts: dict[str, int] = dict()

# Count the number of submissions per subreddit
# TODO: Make this more efficient (at the database level)
# Make the query response a little prettier
for item in subreddit_query:
subreddit = extract_subreddit_from_url(item["url"])
subreddit_counts[subreddit] = subreddit_counts.get(subreddit, 0) + 1
subreddit_counts[item["subreddit_name"]] = item["subreddit_name__count"]

# Sort descending by submission count
sorted_subreddits = OrderedDict(
Expand Down

0 comments on commit 9b0969f

Please sign in to comment.