Skip to content

Commit aae34cb

Browse files
committed
Refactor job types
1 parent 67cc6be commit aae34cb

File tree

8 files changed

+57
-59
lines changed

8 files changed

+57
-59
lines changed

client/platform/desktop/backend/native/common.spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Console } from 'console';
66

77
import {
88
AnnotationsCurrentVersion, DesktopJob,
9-
DesktopJobUpdate, JsonMeta, RunTraining, Settings,
9+
DesktopJobUpdate, JobType, JsonMeta, RunTraining, Settings,
1010
} from 'platform/desktop/constants';
1111
import { makeEmptyAnnotationFile } from 'platform/desktop/backend/serializers/dive';
1212

@@ -695,6 +695,7 @@ describe('native.common', () => {
695695

696696
it('processing good Trained Pipeline folder', async () => {
697697
const trainingArgs: RunTraining = {
698+
type: JobType.RunTraining,
698699
datasetIds: ['randomID'],
699700
pipelineName: 'trainedPipelineName',
700701
trainingConfig: 'trainingConfig',
@@ -715,6 +716,7 @@ describe('native.common', () => {
715716

716717
it('processing bad Trained Pipeline folders', async () => {
717718
const trainingArgs: RunTraining = {
719+
type: JobType.RunTraining,
718720
datasetIds: ['randomID'],
719721
pipelineName: 'trainedBadPipelineName',
720722
trainingConfig: 'trainingConfig',
@@ -730,6 +732,7 @@ describe('native.common', () => {
730732

731733
it('getPipelineList lists pipelines with Trained pipelines', async () => {
732734
const trainingArgs: RunTraining = {
735+
type: JobType.RunTraining,
733736
datasetIds: ['randomID'],
734737
pipelineName: 'trainedPipelineName',
735738
trainingConfig: 'trainingConfig',

client/platform/desktop/backend/native/common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
RunTraining, ExportDatasetArgs, DesktopMediaImportResponse,
4040
ExportConfigurationArgs, JobsFolderName, ProjectsFolderName,
4141
PipelinesFolderName, ConversionArgs,
42+
JobType,
4243
} from 'platform/desktop/constants';
4344
import {
4445
cleanString, filterByGlob, makeid, strNumericCompare,
@@ -1187,10 +1188,12 @@ async function finalizeMediaImport(
11871188
if (args.metaFileAbsPath) {
11881189
await dataFileImport(settings, jsonMeta.id, args.metaFileAbsPath);
11891190
}
1190-
return {
1191+
const conversionJobArgs: ConversionArgs = {
1192+
type: JobType.Conversion,
11911193
meta: finalJsonMeta,
11921194
mediaList: srcDstList,
11931195
};
1196+
return conversionJobArgs;
11941197
}
11951198

11961199
async function openLink(url: string) {

client/platform/desktop/constants.ts

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -152,19 +152,33 @@ export interface NvidiaSmiReply {
152152
error: string;
153153
}
154154

155+
export enum JobType {
156+
Conversion,
157+
ExportTrainedPipeline,
158+
RunPipeline,
159+
RunTraining,
160+
}
161+
162+
export interface JobArgs {
163+
type: JobType;
164+
}
165+
155166
/** TODO promote to apispec */
156-
export interface RunPipeline {
167+
export interface RunPipeline extends JobArgs {
168+
type: JobType.RunPipeline;
157169
datasetId: string;
158170
pipeline: Pipe;
159171
}
160172

161-
export interface ExportTrainedPipeline {
173+
export interface ExportTrainedPipeline extends JobArgs {
174+
type: JobType.ExportTrainedPipeline;
162175
path: string;
163176
pipeline: Pipe;
164177
}
165178

166179
/** TODO promote to apispec */
167-
export interface RunTraining {
180+
export interface RunTraining extends JobArgs {
181+
type: JobType.RunTraining;
168182
// datasets to run training on
169183
datasetIds: string[];
170184
// new pipeline name to be created
@@ -184,32 +198,13 @@ export interface RunTraining {
184198
};
185199
}
186200

187-
export type JobArgs = RunPipeline | RunTraining | ExportTrainedPipeline | ConversionArgs;
188-
189-
export function isRunPipeline(spec: JobArgs): spec is RunPipeline {
190-
return 'datasetId' in spec && 'pipeline' in spec;
191-
}
192-
193-
export function isExportTrainedPipeline(spec: JobArgs): spec is ExportTrainedPipeline {
194-
return 'path' in spec && 'pipeline' in spec;
195-
}
196-
197-
export function isRunTraining(spec: JobArgs): spec is RunTraining {
198-
return (
199-
'datasetIds' in spec
200-
&& 'pipelineName' in spec
201-
&& 'trainingConfig' in spec
202-
);
203-
}
204-
205-
export interface ConversionArgs {
201+
export interface ConversionArgs extends JobArgs {
202+
type: JobType.Conversion;
206203
meta: JsonMeta;
207204
mediaList: [string, string][];
208205
}
209206

210-
export function isConversion(spec: JobArgs): spec is ConversionArgs {
211-
return 'meta' in spec && 'mediaList' in spec;
212-
}
207+
export type Job = ConversionArgs | RunPipeline | RunTraining | ExportTrainedPipeline;
213208

214209
export interface DesktopJob {
215210
// key unique identifier for this job

client/platform/desktop/frontend/api.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
import {
2020
DesktopMetadata, NvidiaSmiReply,
2121
RunPipeline, RunTraining, ExportTrainedPipeline, ExportDatasetArgs, ExportConfigurationArgs,
22-
DesktopMediaImportResponse, ConversionArgs,
22+
DesktopMediaImportResponse, ConversionArgs, JobType,
2323
} from 'platform/desktop/constants';
2424

2525
import { gpuJobQueue, cpuJobQueue } from './store/jobs';
@@ -92,6 +92,7 @@ async function getTrainingConfigurations(): Promise<TrainingConfigs> {
9292

9393
async function runPipeline(itemId: string, pipeline: Pipe): Promise<void> {
9494
const args: RunPipeline = {
95+
type: JobType.RunPipeline,
9596
pipeline,
9697
datasetId: itemId,
9798
};
@@ -100,6 +101,7 @@ async function runPipeline(itemId: string, pipeline: Pipe): Promise<void> {
100101

101102
async function exportTrainedPipeline(path: string, pipeline: Pipe): Promise<void> {
102103
const args: ExportTrainedPipeline = {
104+
type: JobType.ExportTrainedPipeline,
103105
path,
104106
pipeline,
105107
};
@@ -120,6 +122,7 @@ async function runTraining(
120122
},
121123
): Promise<void> {
122124
const args: RunTraining = {
125+
type: JobType.RunTraining,
123126
datasetIds: folderIds,
124127
pipelineName,
125128
trainingConfig: config,

client/platform/desktop/frontend/components/JobsQueued.vue

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<script lang="ts">
22
import {
3-
isConversion,
4-
isExportTrainedPipeline,
5-
isRunPipeline,
6-
isRunTraining,
7-
JobArgs,
3+
Job,
4+
JobType,
85
} from 'platform/desktop/constants';
96
import {
107
defineComponent,
@@ -21,25 +18,25 @@ import { datasets } from '../store/dataset';
2118
2219
export default defineComponent({
2320
setup() {
24-
const queuedJobSpecs: Ref<JobArgs[]> = ref([]);
21+
const queuedJobSpecs: Ref<Job[]> = ref([]);
2522
function updateQueuedJobSpecs() {
2623
queuedJobSpecs.value = [];
27-
queuedGpuJobs.value.forEach((spec: JobArgs) => queuedJobSpecs.value.push(spec));
28-
queuedCpuJobs.value.forEach((spec: JobArgs) => queuedJobSpecs.value.push(spec));
24+
queuedGpuJobs.value.forEach((spec: Job) => queuedJobSpecs.value.push(spec));
25+
queuedCpuJobs.value.forEach((spec: Job) => queuedJobSpecs.value.push(spec));
2926
}
3027
watch([() => queuedCpuJobs, () => queuedGpuJobs], updateQueuedJobSpecs, { deep: true });
3128
32-
function getQueuedJobTitle(jobSpec: JobArgs): string {
33-
if (isConversion(jobSpec)) {
29+
function getQueuedJobTitle(jobSpec: Job): string {
30+
if (jobSpec.type === JobType.Conversion) {
3431
return `conversion: ${datasets.value[jobSpec.meta.id]?.name || jobSpec.meta.id}`;
3532
}
36-
if (isRunPipeline(jobSpec)) {
33+
if (jobSpec.type === JobType.RunPipeline) {
3734
return `pipeline: ${datasets.value[jobSpec.datasetId]?.name || jobSpec.datasetId}`;
3835
}
39-
if (isExportTrainedPipeline(jobSpec)) {
36+
if (jobSpec.type === JobType.ExportTrainedPipeline) {
4037
return `export trained pipeline: ${jobSpec.path}`;
4138
}
42-
if (isRunTraining(jobSpec)) {
39+
if (jobSpec.type === JobType.RunTraining) {
4340
const title = `training: ${datasets.value[jobSpec.datasetIds[0]]?.name || jobSpec.datasetIds[0]}`;
4441
if (jobSpec.datasetIds.length > 1) {
4542
return `${title} (and ${jobSpec.datasetIds.length - 1} more)`;
@@ -49,14 +46,14 @@ export default defineComponent({
4946
return 'queued job';
5047
}
5148
52-
function getJobDatasets(jobSpec: JobArgs): string[] {
53-
if (isConversion(jobSpec)) {
49+
function getJobDatasets(jobSpec: Job): string[] {
50+
if (jobSpec.type === JobType.Conversion) {
5451
return [jobSpec.meta.id];
5552
}
56-
if (isRunPipeline(jobSpec)) {
53+
if (jobSpec.type === JobType.RunPipeline) {
5754
return [jobSpec.datasetId];
5855
}
59-
if (isRunTraining(jobSpec)) {
56+
if (jobSpec.type === JobType.RunTraining) {
6057
return jobSpec.datasetIds;
6158
}
6259
return [];
@@ -67,12 +64,11 @@ export default defineComponent({
6764
});
6865
6966
return {
67+
JobType,
7068
queuedJobSpecs,
7169
datasets,
7270
getQueuedJobTitle,
7371
getJobDatasets,
74-
isRunPipeline,
75-
isExportTrainedPipeline,
7672
};
7773
},
7874
});
@@ -98,7 +94,7 @@ export default defineComponent({
9894
</v-card-title>
9995
<v-card-text>
10096
<table class="key-value-table">
101-
<tr v-if="isRunPipeline(jobSpec) || isExportTrainedPipeline(jobSpec)">
97+
<tr v-if="jobSpec.type === JobType.RunPipeline || jobSpec.type === JobType.ExportTrainedPipeline">
10298
<td>Pipe</td>
10399
<td>{{ jobSpec.pipeline.name }}</td>
104100
</tr>

client/platform/desktop/frontend/components/Recent.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
88
import type { DatasetType, MultiCamImportArgs } from 'dive-common/apispec';
99
import { itemsPerPageOptions } from 'dive-common/constants';
10-
import { isConversion, DesktopMediaImportResponse, JobArgs } from 'platform/desktop/constants';
10+
import { JobType, DesktopMediaImportResponse, Job } from 'platform/desktop/constants';
1111
1212
import TooltipBtn from 'vue-media-annotator/components/TooltipButton.vue';
1313
@@ -124,8 +124,8 @@ export default defineComponent({
124124
const queuedConversionDatasetIds = ref([] as string[]);
125125
watch(recentHistory, () => {
126126
queuedConversionDatasetIds.value = [];
127-
cpuJobQueue.jobSpecs.forEach((spec: JobArgs) => {
128-
if (isConversion(spec)) {
127+
cpuJobQueue.jobSpecs.forEach((spec: Job) => {
128+
if (spec.type === JobType.Conversion) {
129129
queuedConversionDatasetIds.value.push(spec.meta.id);
130130
}
131131
});

client/platform/desktop/frontend/store/queues/asyncCpuJobQueue.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import {
2+
JobType,
23
ConversionArgs,
34
ExportTrainedPipeline,
4-
isConversion,
5-
isExportTrainedPipeline,
65
DesktopJob,
76
} from 'platform/desktop/constants';
87
import AsyncJobQueue from './asyncJobQueue';
98

109
export default class AsyncCpuJobQueue extends AsyncJobQueue<ConversionArgs | ExportTrainedPipeline> {
1110
async beginJob(spec: ConversionArgs | ExportTrainedPipeline) {
1211
let newJob: DesktopJob;
13-
if (isConversion(spec)) {
12+
if (spec.type === JobType.Conversion) {
1413
newJob = await this.ipcRenderer.invoke('convert', spec);
15-
} else if (isExportTrainedPipeline(spec)) {
14+
} else if (spec.type === JobType.ExportTrainedPipeline) {
1615
newJob = await this.ipcRenderer.invoke('export-trained-pipeline', spec);
1716
} else {
1817
throw new Error('CPU job type not able to be queued');

client/platform/desktop/frontend/store/queues/asyncGpuJobQueue.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import {
2+
JobType,
23
RunPipeline,
34
RunTraining,
4-
isRunPipeline,
5-
isRunTraining,
65
DesktopJob,
76
} from 'platform/desktop/constants';
87
import AsyncJobQueue from './asyncJobQueue';
98

109
export default class AsyncGpuJobQueue extends AsyncJobQueue<RunPipeline | RunTraining> {
1110
async beginJob(spec: RunPipeline | RunTraining) {
1211
let newJob: DesktopJob;
13-
if (isRunPipeline(spec)) {
12+
if (spec.type === JobType.RunPipeline) {
1413
newJob = await this.ipcRenderer.invoke('run-pipeline', spec);
15-
} else if (isRunTraining(spec)) {
14+
} else if (spec.type === JobType.RunTraining) {
1615
newJob = await this.ipcRenderer.invoke('run-training', spec);
1716
} else {
1817
throw new Error('Unsupported job arguments provided to beginJob.');

0 commit comments

Comments
 (0)