upend/webui/src/lib/components/layout/Jobs.svelte

83 lines
1.7 KiB
Svelte

<script lang="ts" context="module">
import mitt from 'mitt';
export type JobsEvents = {
reload: undefined;
};
export const jobsEmitter = mitt<JobsEvents>();
</script>
<script lang="ts">
import type { IJob } from '@upnd/upend/types';
import { fade } from 'svelte/transition';
import ProgressBar from '../utils/ProgressBar.svelte';
import api from '$lib/api';
import { DEBUG } from '$lib/debug';
interface JobWithId extends IJob {
id: string;
}
let jobs: IJob[] = [];
let activeJobs: JobWithId[] = [];
export let active = 0;
$: active = activeJobs.length;
// eslint-disable-next-line no-undef
let timeout: NodeJS.Timeout;
async function updateJobs() {
clearTimeout(timeout);
if (!DEBUG.mockJobs) {
jobs = await api.fetchJobs();
} else {
jobs = Array(DEBUG.mockJobs)
.fill(0)
.map((_, i) => ({
id: i.toString(),
title: `Job ${i}`,
job_type: `JobType ${i}`,
state: 'InProgress',
progress: Math.floor(Math.random() * 100)
}));
}
activeJobs = Object.entries(jobs)
.filter(([_, job]) => job.state == 'InProgress')
.map(([id, job]) => {
return { id, ...job };
})
.sort((j1, j2) => j1.id.localeCompare(j2.id))
.sort((j1, j2) => (j2.job_type || '').localeCompare(j1.job_type || ''));
if (activeJobs.length) {
timeout = setTimeout(updateJobs, 500);
} else {
timeout = setTimeout(updateJobs, 5000);
}
}
updateJobs();
jobsEmitter.on('reload', () => {
updateJobs();
});
</script>
{#each activeJobs as job (job.id)}
<div class="job" transition:fade>
<div class="job-label">{job.title}</div>
<ProgressBar value={job.progress} />
</div>
{/each}
<style lang="scss">
.job {
display: flex;
.job-label {
white-space: nowrap;
margin-right: 2em;
}
}
</style>