Skip to content

PAgination improvements in SketchList API #3503

@jaegeral

Description

@jaegeral

Optional, but maybe worth it, since you already touched the SketchListResource class in this PR:

  • The recent scope bypasses the pagination logic passed via the form. We could use the chance to add in support for that as well instead of hardcoding it to last 10?
  • The recent scope currently fetches the last 10 View objects and then, for each view, it fetches the associated Sketch and its Status. This requires basically N+1 database queries which can slow down the API endpoint. We can fetch all the "last activity" timestamps in a single, efficient query instead after you have the list of sketches from the paginator, and then use a dictionary lookup in the loop.

This could look something like this (untested):

from sqlalchemy.orm import joinedload, subqueryload
from timesketch.models.sketch import Sketch, View
[...]
if scope == "recent":
    views = (
        View.query.options(
            joinedload(View.sketch).subqueryload(Sketch.status)
        )
        .filter_by(user=current_user, name="")
        .order_by(View.updated_at.desc())
        .limit(10) # Or use pagination logic here
    )
    sketches = [
        view.sketch
        for view in views
        if view.sketch.get_status.status != "deleted"
    ]
    total_items = len(sketches)
[...]

Originally posted by @jkppr in #3470 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions