From 3aa739fce034187b12bb4b88f0c96af23ee68fc5 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Mon, 29 Nov 2021 13:25:30 +0000 Subject: [PATCH] add Docker image presets, populate algo Docker field from them --- content/publish/form.json | 4 +- src/@types/DDO/Metadata.d.ts | 14 +++--- src/components/Publish/Metadata/index.tsx | 22 ++++----- src/components/Publish/_constants.tsx | 33 ++++++-------- src/components/Publish/_types.ts | 1 + src/components/Publish/_utils.ts | 55 +++++++++++++++++------ 6 files changed, 79 insertions(+), 50 deletions(-) diff --git a/content/publish/form.json b/content/publish/form.json index 8e8d9e5df..a1ecef957 100644 --- a/content/publish/form.json +++ b/content/publish/form.json @@ -47,7 +47,9 @@ "label": "Docker Image", "help": "Please select an image to run your algorithm.", "type": "boxSelection", - "options": ["node:latest", "python:latest", "Custom"], + "options": [ + "populated from algorithmContainerPresets in Publish/_constants" + ], "required": true }, { diff --git a/src/@types/DDO/Metadata.d.ts b/src/@types/DDO/Metadata.d.ts index 4227acd75..4ab62ec36 100644 --- a/src/@types/DDO/Metadata.d.ts +++ b/src/@types/DDO/Metadata.d.ts @@ -1,12 +1,14 @@ +interface MetadataAlgorithmContainer { + entrypoint: string + image: string + tag: string + checksum: string +} + interface MetadataAlgorithm { language?: string version?: string - container: { - entrypoint: string - image: string - tag: string - checksum: string - } + container: MetadataAlgorithmContainer } interface Metadata { diff --git a/src/components/Publish/Metadata/index.tsx b/src/components/Publish/Metadata/index.tsx index 12fb7c465..46fd34f0b 100644 --- a/src/components/Publish/Metadata/index.tsx +++ b/src/components/Publish/Metadata/index.tsx @@ -8,22 +8,13 @@ import { getFieldContent } from '../_utils' import IconDataset from '@images/dataset.svg' import IconAlgorithm from '@images/algorithm.svg' import styles from './index.module.css' +import { algorithmContainerPresets } from '../_constants' const assetTypeOptionsTitles = getFieldContent( 'type', content.metadata.fields ).options -const dockerImageOptionsTitles = getFieldContent( - 'dockerImage', - content.metadata.fields -).options - -const dockerImageOptions = dockerImageOptionsTitles.map((title) => ({ - name: title.toLowerCase(), - title -})) - export default function MetadataFields(): ReactElement { // connect with Form state, use for conditional field rendering const { values } = useFormikContext() @@ -45,6 +36,17 @@ export default function MetadataFields(): ReactElement { } ] + // Populate the Docker image field with our presets in _constants, + // transformPublishFormToDdo will do the rest. + const dockerImageOptions: BoxSelectionOption[] = + algorithmContainerPresets.map((preset) => ({ + name: `${preset.image}:${preset.tag}`, + title: `${preset.image}:${preset.tag}`, + checked: values.metadata.dockerImage === `${preset.image}:${preset.tag}` + })) + + dockerImageOptions.push({ name: 'custom', title: 'Custom', checked: false }) + return ( <> = { -// name: '', -// author: '', -// dataTokenOptions: { -// name: '', -// symbol: '' -// }, -// dockerImage: 'node:latest', -// image: 'node', -// containerTag: 'latest', -// entrypoint: 'node $ALGO', -// files: '', -// description: '', -// algorithmPrivacy: false, -// termsAndConditions: false, -// tags: '', -// timeout: 'Forever', -// providerUri: '' -// } +export const algorithmContainerPresets: MetadataAlgorithmContainer[] = [ + { + image: 'node', + tag: 'latest', + entrypoint: 'node $ALGO', + checksum: '' // TODO: how to get? + }, + { + image: 'python', + tag: 'latest', + entrypoint: 'python $ALGO', + checksum: '' + } +] diff --git a/src/components/Publish/_types.ts b/src/components/Publish/_types.ts index 6845c1afc..c4db91878 100644 --- a/src/components/Publish/_types.ts +++ b/src/components/Publish/_types.ts @@ -37,6 +37,7 @@ export interface FormPublishData { dockerImageCustom?: string dockerImageCustomTag?: string dockerImageCustomEntrypoint?: string + dockerImageCustomChecksum?: string } services: FormPublishService[] pricing: PriceOptions diff --git a/src/components/Publish/_utils.ts b/src/components/Publish/_utils.ts index a9c50a8df..3439758ca 100644 --- a/src/components/Publish/_utils.ts +++ b/src/components/Publish/_utils.ts @@ -2,6 +2,7 @@ import { mapTimeoutStringToSeconds } from '@utils/ddo' import { getEncryptedFileUrls } from '@utils/provider' import { sha256 } from 'js-sha256' import slugify from 'slugify' +import { algorithmContainerPresets } from './_constants' import { FormPublishData } from './_types' export function getFieldContent( @@ -16,6 +17,17 @@ function getUrlFileExtension(fileUrl: string): string { return splittedFileUrl[splittedFileUrl.length - 1] } +function getAlgorithmContainerPreset( + dockerImage: string +): MetadataAlgorithmContainer { + if (dockerImage === '') return + + const preset = algorithmContainerPresets.find( + (preset) => `${preset.image}:${preset.tag}` === dockerImage + ) + return preset +} + function dateToStringNoMS(date: Date): string { return date.toISOString().replace(/\.[0-9]{3}Z/, 'Z') } @@ -42,9 +54,11 @@ export async function transformPublishFormToDdo( tags, author, termsAndConditions, + dockerImage, dockerImageCustom, dockerImageCustomTag, - dockerImageCustomEntrypoint + dockerImageCustomEntrypoint, + dockerImageCustomChecksum } = metadata const { access, files, links, providerUrl, timeout } = services[0] @@ -68,20 +82,33 @@ export async function transformPublishFormToDdo( additionalInformation: { termsAndConditions }, - ...(type === 'algorithm' && { - // TODO: This needs some set of predefined values for `container`, - // depending on user selection in the form. - algorithm: { - language: files?.length ? getUrlFileExtension(filesTransformed[0]) : '', - version: '0.1', - container: { - entrypoint: dockerImageCustomEntrypoint, - image: dockerImageCustom, - tag: dockerImageCustomTag, - checksum: '' // TODO: how to get? Is it user input? + ...(type === 'algorithm' && + dockerImage !== '' && { + algorithm: { + language: filesTransformed?.length + ? getUrlFileExtension(filesTransformed[0]) + : '', + version: '0.1', + container: { + entrypoint: + dockerImage === 'custom' + ? dockerImageCustomEntrypoint + : getAlgorithmContainerPreset(dockerImage).entrypoint, + image: + dockerImage === 'custom' + ? dockerImageCustom + : getAlgorithmContainerPreset(dockerImage).image, + tag: + dockerImage === 'custom' + ? dockerImageCustomTag + : getAlgorithmContainerPreset(dockerImage).tag, + checksum: + dockerImage === 'custom' + ? dockerImageCustomChecksum + : getAlgorithmContainerPreset(dockerImage).checksum + } } - } - }) + }) } // Encrypt just created string[] of urls