try handmade pagination

This commit is contained in:
Kenneth Kehl
2024-10-15 14:31:27 -07:00
parent 573098bc61
commit ad60886525
2 changed files with 26 additions and 4 deletions

View File

@@ -6,6 +6,7 @@ from flask import current_app
from sqlalchemy import and_, asc, desc, func, select
from app import db
from app.dao.pagination import Pagination
from app.enums import JobStatus
from app.models import (
FactNotificationStatus,
@@ -65,10 +66,18 @@ def dao_get_jobs_by_service_id(
if statuses is not None and statuses != [""]:
query_filter.append(Job.job_status.in_(statuses))
stmt = select(*query_filter).order_by(
Job.processing_started.desc(),
Job.created_at.desc()).limit(page_size).offset(page)
return db.session.execute(stmt).scalars().all()
total_items = db.session.execute(
select(func.count()).select_from(*query_filter).scalar_one()
)
stmt = (
select(*query_filter)
.order_by(Job.processing_started.desc(), Job.created_at.desc())
.limit(page_size)
.offset(page)
)
items = db.session.execute(stmt).scalars().all()
return Pagination(items, page, page_size, total_items)
def dao_get_scheduled_job_stats(

13
app/dao/pagination.py Normal file
View File

@@ -0,0 +1,13 @@
class Pagination:
def __init__(self, items, page, per_page, total):
self.items = items
self.page = page
self.per_page = per_page
self.total = total
self.pages = (total + per_page - 1) // per_page
def has_next(self):
return self.page < self.pages
def has_prev(self):
return self.page > 1