Improve latency of the job list endpoint
This commit is contained in:
1 parent
3a94591c89
commit
6c0d1c4679
2 files changed
+30
-7
No files matched your search
@@ -9,6 +9,7 @@ export async function GET(request: Request) {
|
|||||||
const id = searchParams.get('id');
|
const id = searchParams.get('id');
|
||||||
const job_ref = searchParams.get('job_ref');
|
const job_ref = searchParams.get('job_ref');
|
||||||
const job_type = searchParams.get('job_type');
|
const job_type = searchParams.get('job_type');
|
||||||
|
const only_active = searchParams.get('only_active');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (id) {
|
if (id) {
|
||||||
@@ -25,8 +26,16 @@ export async function GET(request: Request) {
|
|||||||
return NextResponse.json(job);
|
return NextResponse.json(job);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const where: any = {};
|
||||||
|
if (job_type) {
|
||||||
|
where.job_type = job_type;
|
||||||
|
}
|
||||||
|
if (only_active === 'true') {
|
||||||
|
where.status = { in: ['running', 'queued', 'stopping'] };
|
||||||
|
}
|
||||||
|
|
||||||
const jobs = await prisma.job.findMany({
|
const jobs = await prisma.job.findMany({
|
||||||
where: job_type ? { job_type } : undefined,
|
where,
|
||||||
orderBy: { created_at: 'desc' },
|
orderBy: { created_at: 'desc' },
|
||||||
});
|
});
|
||||||
return NextResponse.json({ jobs: jobs });
|
return NextResponse.json({ jobs: jobs });
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
import { Job } from '@prisma/client';
|
import { Job } from '@prisma/client';
|
||||||
import { apiClient } from '@/utils/api';
|
import { apiClient } from '@/utils/api';
|
||||||
|
|
||||||
@@ -10,14 +10,28 @@ type UseJobsListProps = {
|
|||||||
job_type?: string | null;
|
job_type?: string | null;
|
||||||
};
|
};
|
||||||
|
|
||||||
export default function useJobsList({ onlyActive = false, reloadInterval = null, job_type=null }: UseJobsListProps = {}) {
|
export default function useJobsList({
|
||||||
|
onlyActive = false,
|
||||||
|
reloadInterval = null,
|
||||||
|
job_type = null,
|
||||||
|
}: UseJobsListProps = {}) {
|
||||||
const [jobs, setJobs] = useState<Job[]>([]);
|
const [jobs, setJobs] = useState<Job[]>([]);
|
||||||
const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
|
const [status, setStatus] = useState<'idle' | 'loading' | 'success' | 'error'>('idle');
|
||||||
|
const isFetchingRef = useRef(false);
|
||||||
|
|
||||||
const refreshJobs = () => {
|
const refreshJobs = () => {
|
||||||
|
if (isFetchingRef.current) return;
|
||||||
|
isFetchingRef.current = true;
|
||||||
setStatus('loading');
|
setStatus('loading');
|
||||||
|
const params: Record<string, string> = {};
|
||||||
|
if (job_type) {
|
||||||
|
params.job_type = job_type;
|
||||||
|
}
|
||||||
|
if (onlyActive) {
|
||||||
|
params.only_active = 'true';
|
||||||
|
}
|
||||||
apiClient
|
apiClient
|
||||||
.get('/api/jobs', { params: job_type ? { job_type } : undefined })
|
.get('/api/jobs', { params })
|
||||||
.then(res => res.data)
|
.then(res => res.data)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
console.log('Jobs:', data);
|
console.log('Jobs:', data);
|
||||||
@@ -25,9 +39,6 @@ export default function useJobsList({ onlyActive = false, reloadInterval = null,
|
|||||||
console.log('Error fetching jobs:', data.error);
|
console.log('Error fetching jobs:', data.error);
|
||||||
setStatus('error');
|
setStatus('error');
|
||||||
} else {
|
} else {
|
||||||
if (onlyActive) {
|
|
||||||
data.jobs = data.jobs.filter((job: Job) => ['running', 'queued', 'stopping'].includes(job.status));
|
|
||||||
}
|
|
||||||
setJobs(data.jobs);
|
setJobs(data.jobs);
|
||||||
setStatus('success');
|
setStatus('success');
|
||||||
}
|
}
|
||||||
@@ -35,6 +46,9 @@ export default function useJobsList({ onlyActive = false, reloadInterval = null,
|
|||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error('Error fetching jobs:', error);
|
console.error('Error fetching jobs:', error);
|
||||||
setStatus('error');
|
setStatus('error');
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
isFetchingRef.current = false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
Reference in new issue
Block a user