Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
"react-hook-form": "^7.43.8",
"react-icons": "^4.8.0",
"react-query": "^3.39.3",
"react-router-dom": "^6.10.0"
"react-router-dom": "^6.10.0",
"zod": "^3.21.4"
},
"devDependencies": {
"@testing-library/react": "^14.0.0",
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions src/components/Files/FilesUpload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { MdClose, MdOutlineFolderZip, MdOutlineTask } from 'react-icons/md';

import { uploadArtifacts } from '@/services/ArtifactService';
import { ENVS } from '@/services/BaseService';
import { ArtifactSchema } from '@/types/artifact';
import { showErrorNotification, showSuccessNotification } from '@/utils/notifications';

type FileRejection = {
Expand All @@ -27,10 +28,18 @@ function FilesUpload() {
file: file.toString(),
size: file.size.toString(),
};

uploadArtifacts(newArtifact, ENVS);
try {
const validArtifact = ArtifactSchema.parse(newArtifact);
uploadArtifacts(validArtifact, ENVS);
showSuccessNotification(
`${fileNames.toString()} uploaded successfully.`,
'Upload Successful',
);
} catch (error) {
showErrorNotification(`Invalid artifact upload: ${String(error)}`);
console.error(`Invalid artifact upload: ${String(error)}`);
}
});
showSuccessNotification(`${fileNames.toString()} uploaded successfully.`, 'Upload Successful');
};

const onReject = (fileRejections: FileRejection[]) => {
Expand Down
4 changes: 1 addition & 3 deletions src/mirage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ export function makeServer(baseUrl: string) {

this.get('/v1/jobClusters/:clusterName', (schema, request) => {
const { clusterName } = request.params;
const cluster = schema.db.clusters.find((cluster: Cluster) => {
return cluster.name === clusterName;
}) as Cluster | undefined;
const cluster = schema.db.clusters.findBy({ name: clusterName }) as Cluster | undefined;

if (!cluster) {
return new Response(404, { Error: 'No cluster with that cluster name was found' });
Expand Down
21 changes: 16 additions & 5 deletions src/services/ArtifactService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import ky from 'ky';

import { getApiClientEntryForEnv } from '@/services/BaseService';
import { ArtifactSchema } from '@/types/artifact';
import type { Artifact } from '@/types/artifact';
import { showErrorNotification } from '@/utils/notifications';

export async function fetchArtifacts(envs: string[]) {
// Fix this API call to not call hard coded UI APIs
Expand All @@ -12,11 +14,20 @@ export async function fetchArtifacts(envs: string[]) {
const data = responses.flatMap((response, index) => {
if (response.status === 'fulfilled') {
const items = response.value as Artifact[];
return items.map((item) => ({
...item,
lastModified: item.lastModified * 1000,
env: envs[index],
}));
return items.map((item) => {
try {
const validatedItem = ArtifactSchema.parse(item);
return {
...validatedItem,
lastModified: validatedItem.lastModified * 1000,
env: envs[index],
};
} catch (error) {
showErrorNotification(`Invalid artifact: ${String(error)}`);
console.error(`Invalid artifact: ${String(error)}`);
return null;
}
});
}
return [];
});
Expand Down
21 changes: 15 additions & 6 deletions src/types/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { KyInstance } from 'ky/distribution/types/ky';
import { z } from 'zod';

export type ApiClientEntry = {
client: KyInstance;
Expand All @@ -7,11 +8,19 @@ export type ApiClientEntry = {
url: string;
};

export type Env = 'prod' | 'test';
const EnvSchema = z.union([z.literal('prod'), z.literal('test')]);
export type Env = z.infer<typeof EnvSchema>;

export type Region = 'eu-west-1' | 'us-east-1' | 'us-east-2' | 'us-west-2';
const RegionSchema = z.union([
z.literal('eu-west-1'),
z.literal('us-east-1'),
z.literal('us-east-2'),
z.literal('us-west-2'),
]);
export type Region = z.infer<typeof RegionSchema>;

export type EnvRegion = {
env: string;
region: string;
};
const EnvRegionSchema = z.object({
env: z.string(),
region: z.string(),
});
export type EnvRegion = z.infer<typeof EnvRegionSchema>;
15 changes: 9 additions & 6 deletions src/types/artifact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export type Artifact = {
file: string;
key: string;
lastModified: number;
size: string;
};
import { z } from 'zod';

export const ArtifactSchema = z.object({
file: z.string(),
key: z.string(),
lastModified: z.number(),
size: z.string(),
});
export type Artifact = z.infer<typeof ArtifactSchema>;
132 changes: 73 additions & 59 deletions src/types/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,73 @@
import type { HardConstraints, SoftConstraints } from '@/types/constraints';
import type { Label, MachineDefinition, MigrationConfig, Sla } from '@/types/machine';

export type ClusterListItem = {
labels: Label[];
name: string;
owners: string[];
versions: Version[];
};

export type Version = {
disabled: boolean;
env: string;
region: string;
version: string;
};

export type Cluster = {
cronActive: boolean;
disabled: boolean;
isReadyForJobMaster: boolean;
jars: Jar[];
labels: Label[];
lastJobCount: number;
latestVersion: string;
migrationConfig: MigrationConfig;
name: string;
owner: Owner;
parameters: Label[];
sla: Sla;
};

export type Jar = {
schedulingInfo: SchedulingInfo;
uploadedAt: number;
url: string;
version: string;
};

export type SchedulingInfo = {
stages: { [key: number]: Stage };
};

export type Stage = {
hardConstraints: HardConstraints[];
machineDefinition: MachineDefinition;
numberOfInstances: number;
scalable: boolean;
scalingPolicy: null;
softConstraints: SoftConstraints[];
};

export type Owner = {
contactEmail: string;
description: string;
name: string;
repo: string;
teamName: string;
};
import { z } from 'zod';

import { HardConstraintsSchema, SoftConstraintsSchema } from '@/types/constraints';
import {
LabelSchema,
MachineDefinitionSchema,
MigrationConfigSchema,
SlaSchema,
} from '@/types/machine';

const VersionSchema = z.object({
disabled: z.boolean(),
env: z.string(),
region: z.string(),
version: z.string(),
});
export type Version = z.infer<typeof VersionSchema>;

const OwnerSchema = z.object({
contactEmail: z.string(),
description: z.string(),
name: z.string(),
repo: z.string(),
teamName: z.string(),
});
export type Owner = z.infer<typeof OwnerSchema>;

const ClusterListItemSchema = z.object({
labels: z.array(LabelSchema),
name: z.string(),
owners: z.array(z.string()),
versions: z.array(VersionSchema),
});
export type ClusterListItem = z.infer<typeof ClusterListItemSchema>;

const StageSchema = z.object({
hardConstraints: z.array(HardConstraintsSchema),
machineDefinition: MachineDefinitionSchema,
numberOfInstances: z.number(),
scalable: z.boolean(),
scalingPolicy: z.null(),
softConstraints: z.array(SoftConstraintsSchema),
});
export type Stage = z.infer<typeof StageSchema>;

const SchedulingInfoSchema = z.object({
stages: z.record(StageSchema),
});
export type SchedulingInfo = z.infer<typeof SchedulingInfoSchema>;

const JarSchema = z.object({
schedulingInfo: SchedulingInfoSchema,
uploadedAt: z.number(),
url: z.string(),
version: z.string(),
});
export type Jar = z.infer<typeof JarSchema>;

const ClusterSchema = z.object({
cronActive: z.boolean(),
disabled: z.boolean(),
isReadyForJobMaster: z.boolean(),
jars: z.array(JarSchema),
labels: z.array(LabelSchema),
lastJobCount: z.number(),
latestVersion: z.string(),
migrationConfig: MigrationConfigSchema,
name: z.string(),
owner: OwnerSchema,
parameters: z.array(LabelSchema),
sla: SlaSchema,
});
export type Cluster = z.infer<typeof ClusterSchema>;
16 changes: 10 additions & 6 deletions src/types/constraints.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
export type HardConstraints = {
id: number;
};
import { z } from 'zod';

export type SoftConstraints = {
id: number;
};
export const HardConstraintsSchema = z.object({
id: z.number(),
});
export type HardConstraints = z.infer<typeof HardConstraintsSchema>;

export const SoftConstraintsSchema = z.object({
id: z.number(),
});
export type SoftConstraints = z.infer<typeof SoftConstraintsSchema>;
Loading