Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: paginate API calls #1332

Draft
wants to merge 28 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
89f9012
Switch to limit offset pagination in the backend
nas-tabchiche Jan 7, 2025
3c030c1
Cast PAGIKNATE_BY to int
nas-tabchiche Jan 8, 2025
606d7a9
Backend pagination PoC
nas-tabchiche Jan 8, 2025
d1fb264
Merge branch 'main' into CA-582-handle-backend-pagination
nas-tabchiche Jan 28, 2025
9af35cf
Fix RequirementAssessmentViewSet search fields
nas-tabchiche Jan 28, 2025
b0775bd
Handle search
nas-tabchiche Jan 28, 2025
9824759
handle rowsPerPage
nas-tabchiche Jan 28, 2025
6782d19
Invalidate handler onMount
nas-tabchiche Jan 28, 2025
ac296de
Invalidate handler on seach
nas-tabchiche Jan 28, 2025
93f8671
Return response instead of response.results
nas-tabchiche Jan 28, 2025
cfbcfda
Fix composer regression
nas-tabchiche Jan 28, 2025
2f2de8c
Some tidying
nas-tabchiche Jan 28, 2025
19e2a66
Allow ordering against all model fields by default
nas-tabchiche Jan 28, 2025
5900564
Fix Th sort styling
nas-tabchiche Jan 28, 2025
d41b317
Merge branch 'main' into CA-582-handle-backend-pagination
nas-tabchiche Jan 28, 2025
1358a24
chore: some tidying
nas-tabchiche Jan 28, 2025
09f720b
Merge branch 'main' into CA-582-handle-backend-pagination
nas-tabchiche Jan 29, 2025
f1a4c3d
chore: some tidying
nas-tabchiche Jan 29, 2025
07eab67
Dynamically import either server or client ModelTable component
nas-tabchiche Jan 29, 2025
db7e502
refactor: loaded libraries table
nas-tabchiche Jan 29, 2025
e148c88
Forward slots to client/server ModelTable
nas-tabchiche Jan 29, 2025
4681a18
Remove dead code
nas-tabchiche Jan 29, 2025
9049fc1
Refactor load functions
nas-tabchiche Jan 29, 2025
e26a0ff
Improve libraries routing
nas-tabchiche Jan 29, 2025
337d3a9
Improve slot forwarding on ModelTable
nas-tabchiche Jan 29, 2025
d738d1d
Add null checks
nas-tabchiche Jan 29, 2025
2d7b08f
Use new loaded-libraries routes
nas-tabchiche Jan 29, 2025
bf24bc6
Remove dead code
nas-tabchiche Jan 29, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/ciso_assistant/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def set_ciso_assistant_url(_, __, event_dict):
MEDIA_ROOT = LOCAL_STORAGE_DIRECTORY
MEDIA_URL = ""

PAGINATE_BY = os.environ.get("PAGINATE_BY", default=5000)
PAGINATE_BY = int(os.environ.get("PAGINATE_BY", default=5000))

# Application definition

Expand Down Expand Up @@ -211,7 +211,7 @@ def set_ciso_assistant_url(_, __, event_dict):
"core.permissions.RBACPermissions",
],
"DEFAULT_FILTER_CLASSES": ["django_filters.rest_framework.DjangoFilterBackend"],
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"PAGE_SIZE": PAGINATE_BY,
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"EXCEPTION_HANDLER": "core.helpers.handle",
Expand Down
4 changes: 2 additions & 2 deletions enterprise/backend/enterprise_core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def set_ciso_assistant_url(_, __, event_dict):
MEDIA_ROOT = LOCAL_STORAGE_DIRECTORY
MEDIA_URL = ""

PAGINATE_BY = os.environ.get("PAGINATE_BY", default=5000)
PAGINATE_BY = int(os.environ.get("PAGINATE_BY", default=5000))

# Application definition

Expand Down Expand Up @@ -214,7 +214,7 @@ def set_ciso_assistant_url(_, __, event_dict):
"enterprise_core.permissions.LicensePermission",
],
"DEFAULT_FILTER_CLASSES": ["django_filters.rest_framework.DjangoFilterBackend"],
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"PAGE_SIZE": PAGINATE_BY,
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
"EXCEPTION_HANDLER": "core.helpers.handle",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/lib/components/ModelTable/ModelTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
}
}
const rows = handler.getRows();

const _filters = handler.getFilters();

function getFilterCount(filters: typeof $_filters): number {
Expand Down
69 changes: 30 additions & 39 deletions frontend/src/lib/components/ModelTable/Pagination.svelte
Original file line number Diff line number Diff line change
@@ -1,60 +1,51 @@
<script lang="ts">
import type { DataHandler } from '@vincjo/datatables';
import type { DataHandler } from '@vincjo/datatables/remote';
import { page } from '$app/stores';
import { onMount } from 'svelte';
import * as m from '$paraglide/messages';

import { afterNavigate } from '$app/navigation';
export let handler: DataHandler;

const pageNumber = handler.getPageNumber();
const pageCount = handler.getPageCount();
const pages = handler.getPages({ ellipsis: true });
let urlModel: string | undefined;
let _sessionStorage = null;

onMount(() => {
_sessionStorage = sessionStorage;
});

$: if ($page.url && _sessionStorage) {
const pageData = JSON.parse(_sessionStorage.getItem('model_table_page_data') ?? '{}');
urlModel = `${$page.url}`.replace('//+$/', '').split('/').pop();

const currentPage = pageData[urlModel] ?? 1;
handler.setPage(currentPage);
pageData[urlModel] = currentPage;

_sessionStorage.setItem('model_table_page_data', JSON.stringify(pageData));
}
const setPage = (value: 'previous' | 'next' | number) => {
handler.setPage(value);
handler.invalidate();
};
$: console.log('$pages', $pages);

$: if ($pageNumber && urlModel && _sessionStorage) {
const pageData = JSON.parse(_sessionStorage.getItem('model_table_page_data') ?? '{}');
pageData[urlModel] = $pageNumber;
_sessionStorage.setItem('model_table_page_data', JSON.stringify(pageData));
}
afterNavigate(() => {
if ($page.url) {
handler.setPage(1);
}
});
</script>

<section class="flex">
<button
type="button"
class:disabled={$pageNumber === 1}
on:click={() => handler.setPage('previous')}
>
<button type="button" class:disabled={$pageNumber === 1} on:click={() => setPage('previous')}>
{m.previous()}
</button>
{#each $pages as page}
<button
type="button"
class:active={$pageNumber === page}
class:ellipse={page === null}
on:click={() => handler.setPage(page)}
>
{page ?? '...'}
{#if $pages === undefined}
<button type="button" on:click={() => setPage($pageNumber)}>
{$pageNumber}
</button>
{/each}
{:else}
{#each $pages as page}
<button
type="button"
class:active={$pageNumber === page}
class:ellipse={page === null}
on:click={() => setPage(page)}
>
{page ?? '...'}
</button>
{/each}
{/if}
<button
type="button"
class:disabled={$pageNumber === $pageCount}
on:click={() => handler.setPage('next')}
on:click={() => setPage('next')}
>
{m.next()}
</button>
Expand Down
Loading
Loading