zerohedge
zerohedge

Reputation: 3725

Django: Complex annotation, how to avoid for loop?

For an analytics app that utilizes Google Realtime Analytics API, I have my models.py definitions as follows:

class Report(BaseModel):
    ios_report = JSONField()
    android_report = JSONField()

class Article(BaseModel):

    internal_id = models.IntegerField(unique=True)
    title = models.CharField(max_length=500)
    short_title = models.CharField(max_length=500)
    picture_url = models.URLField()
    published_date = models.DateField()
    clip_link = models.URLField()
    reports = models.ManyToManyField(
        "Report", through="ArticleInReport", related_name="articles"
    )

class ArticleInReport(BaseModel):

    article = models.ForeignKey("core.Article", on_delete=models.CASCADE, related_name='articleinreports')
    report = models.ForeignKey("core.Report", on_delete=models.CASCADE, related_name='articleinreports')
    ios_views = models.IntegerField()
    android_views = models.IntegerField()

    @property
    def total_views(self):
        return self.ios_views + self.android_views

Everything starts with a Report object that is created at set intervals. This report contains data about articles and their respective views. A Report will have a relationship with an Article through ArticleInReport, which holds the total number of users in Article at the time the report was imported.

In my view, I need to display the following information:

I'm achieving this as follows in my views.py:

reports_in_time_range = Report.objects.filter(created_date__range=[starting_range, right_now])
last_report = Report.objects.last()
unique_articles = Article.objects.filter(articleinreports__report__in=reports_in_time_range).distinct('id')

    articles = Article.objects.filter(id__in=unique_articles).distinct('id').annotate(
        total_views=Case(
                When(articleinreports__report=last_report,
                     then=(F("articleinreports__ios_views") + F("articleinreports__android_views"))), default=0, output_field=IntegerField(),
        ))

    sorted_articles = sorted(articles, key=operator.attrgetter('total_views'), reverse=True)

But I also need a "trend graph" for each article displayed, with the following information:

  1. X axis: All reports (or rather, report dates) imported in the last 6 hours, whether the article ID appears in them or not.
  2. Y axis: The value of total_views in each respective report: if the article is present, then display total_views, if not, return 0.

I can't find an efficient way to do this without resorting to multiple for loops. The way I have it currently is by adding the following method to the Article model:

class Article(BaseModel):

    def get_article_data_for_reports(self, report_objs):
        graph_dict = {}
        graph_dict['x_vals'] = [x.created_date for x in report_objs]
        graph_dict['y_vals'] = []
        for passed_report in report_objs:
            try:
                graph_dict['y_vals'].append(ArticleInReport.objects.get(article=self, report=passed_report).total_views)
            except ArticleInReport.DoesNotExist:
                graph_dict['y_vals'].append(0)
        print(graph_dict)
        return graph_dict

and in the views.py file I do this:

    context["articles"] = sorted_articles
    context["article_graphs"] = {}

    for article in sorted_articles:
        context["article_graphs"][article.internal_id]= article.get_article_data_for_reports(xhours_ago_reports)

I can then somehow use this in the view's context. But before proceeding, I'm wondering if there's a better way to do this. The page loading time spiked from milliseconds to a 5-9 seconds on each refresh.

Upvotes: 1

Views: 884

Answers (1)

Alexandr Tatarinov
Alexandr Tatarinov

Reputation: 4034

from django.db.models import F


reports = Report.objects.all()  # Filter reports here

# This creates LEFT OUTER JOIN with all ArticleInReport, so each
# Article will appear in result once per each report which includes it
articles_with_reports = Article.objects.annotate(
    report_id=F('articleinreports__report_id')
)
# We are only interested in some reports
articles_in_reports = articles_with_reports.filter(
    report_id__in=reports.values('id')
)
# As each result row is actually ArticleInReport, this effectively gives
# amount of views per article per report
articles_with_views = articles_in_reports.annotate(
    views=(
            F('articleinreports__ios_views')
            + F('articleinreports__android_views')
    )
)
# Now do some processing in python - it's cheap
# We need dictionary to create final chart data
articles_map = {}  # {Article: {report_id: article_with_view}}
for article in articles_with_views:
    articles_map.setdefault(article, {})
    articles_map[article][article.report_id] = article.views

article_graphs = {}
# Force-evaluate to cache Reports
# Actually this would happen automatically, but to be certain...
reports = list(reports)
# As we want all Articles, we have to fetch them
for article in Article.objects.all():
    x_vals = []
    y_vals = []
    # Now for each report we will set article.views or 0
    # this will execute only once
    for report in reports:
        x_vals.append(report.created_date)
        if (
            article in articles_map
            and report.id in articles_map[article]
        ):
            # We have views for this article in this record
            y_vals.append(articles_map[article][report.id])
        else:
            # Defaults
            y_vals.append(0)
    article_graphs[article] = {
        'x_vals': x_vals,
        'y_vals': y_vals
    }

# Finally, we have article_graphs
# {
#    Article: {
#        'x_vals': [Date, Date, Date],
#        'y_vals': [100, 0, 50]
#    },
#    ....
# }

UPDATE

To build graphs only for Articles which have at least 1 appearance in recent Report, we just wanna use articles_map directly

article_graphs = {}
# Force-evaluate to cache Reports
# Actually this would happen automatically, but to be certain...
reports = list(reports)
for article, views_by_report in articles_map.items():
    x_vals = []
    y_vals = []
    # Now for each report we will set article.views or 0
    for report in reports:
        x_vals.append(report.created_date)
        y_vals.append(views_by_report.get(report.id, 0))
    article_graphs[article] = {
        'x_vals': x_vals,
        'y_vals': y_vals
    }

Upvotes: 3

Related Questions