Skip to content

Commit

Permalink
improve index page perf
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Aug 19, 2024
1 parent 0ce075f commit a14a947
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions server/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import difflib
import itertools
import mimetypes
import os
import uuid
from datetime import datetime
from pathlib import Path
from typing import NamedTuple

import asyncpg
import litestar
from litestar import Response
from litestar.config.csrf import CSRFConfig
Expand Down Expand Up @@ -79,22 +79,30 @@ async def index(request: Request) -> Template:
)
return Template("index.html.jinja2", context={"rows": rows, "auth": request.auth})

rows1 = await pg.fetch(
"select * from patch where deleted_at is NULL and state = $1 order by created_at",
rows = await pg.fetch(
"""
select * from patch where deleted_at is NULL and state = $1
union
(select * from patch where deleted_at is NULL and state != $1 limit 10)
""",
PatchState.Pending,
)

rows2 = await pg.fetch(
"select * from patch where deleted_at is NULL and state != $1 order by updated_at desc limit 10",
PatchState.Pending,
)
rows.sort(key=__index_row_sorter, reverse=True)

return Template(
"index.html.jinja2",
context={"rows": itertools.chain(rows1, rows2), "auth": request.auth},
context={"rows": rows, "auth": request.auth},
)


def __index_row_sorter(r: asyncpg.Record):

Check failure on line 99 in server/__init__.py

View workflow job for this annotation

GitHub Actions / mypy

Function is missing a return type annotation
if r["state"] == PatchState.Pending:
return 1, r["created_at"]

return 0, r["updated_at"]


@litestar.get("/patch/{patch_id:uuid}")
async def get_patch(patch_id: uuid.UUID, request: Request) -> Template:
p = await pg.fetchrow("""select * from patch where id = $1 and deleted_at is NULL""", patch_id)
Expand Down

0 comments on commit a14a947

Please sign in to comment.