From 2681103199d557883fa470a7dc293bb6c4f43680 Mon Sep 17 00:00:00 2001 From: codeSafari10 Date: Wed, 7 Aug 2024 19:26:32 +0530 Subject: [PATCH] migrate design validation Signed-off-by: codeSafari10 --- .../PatternServiceFormCore.js | 11 +- ui/machines/validator/designValidator.js | 29 +++-- ui/machines/validator/schemaValidator.js | 100 ++---------------- ui/utils/utils.js | 8 +- 4 files changed, 42 insertions(+), 106 deletions(-) diff --git a/ui/components/MesheryMeshInterface/PatternServiceFormCore.js b/ui/components/MesheryMeshInterface/PatternServiceFormCore.js index cfa375c6cde..c1a9662c57f 100644 --- a/ui/components/MesheryMeshInterface/PatternServiceFormCore.js +++ b/ui/components/MesheryMeshInterface/PatternServiceFormCore.js @@ -5,6 +5,7 @@ import PatternService from './PatternService'; import { getPatternAttributeName, createPatternFromConfig } from './helpers'; import React, { useEffect, useState } from 'react'; import { scrollToTop } from '../../utils/utils'; +import { ErrorBoundary } from '@layer5/sistent'; /** * usePatternServiceForm seperates the form logic from its UI representation @@ -21,7 +22,7 @@ import { scrollToTop } from '../../utils/utils'; * scroll?: Boolean; // If the window should be scrolled to zero after re-rendering * }} param0 props for the component */ -function PatternServiceFormCore({ +function PatternServiceFormCore_({ formData, schemaSet, onSubmit, @@ -117,4 +118,12 @@ function PatternServiceFormCore({ return child.current; } +const PatternServiceFormCore = (props) => { + return ( + + + + ); +}; + export default PatternServiceFormCore; diff --git a/ui/machines/validator/designValidator.js b/ui/machines/validator/designValidator.js index 6944000a037..d9d8e2d36c9 100644 --- a/ui/machines/validator/designValidator.js +++ b/ui/machines/validator/designValidator.js @@ -143,13 +143,16 @@ const dryRunValidatorMachine = dataValidatorMachine.provide({ }); const getAllComponentsDefsInDesign = async (design) => { + console.log("getting", design) const { components } = processDesign(design); + console.log("get All Components", components) + const componentDefs = ( await Promise.allSettled( components.map(async (component) => - getComponentDefinition(component.type, component.model, { - apiVersion: component.apiVersion, + getComponentDefinition(component.component.kind, component.model.name, { + apiVersion: component.component.version, annotations: 'include', }), ), @@ -158,16 +161,16 @@ const getAllComponentsDefsInDesign = async (design) => { .filter((result) => result.status === 'fulfilled') .map((result) => result.value); + console.log("Component Defs", componentDefs) + const componentStore = componentDefs.reduce((acc, componentDef) => { - const key = componentKey({ - type: componentDef.component.kind, - model: componentDef.model.name, - apiVersion: componentDef.component.version, - }); + const key = componentKey(componentDef); acc[key] = componentDef; return acc; }, {}); + console.log("component store", componentStore) + return componentStore; }; @@ -178,7 +181,7 @@ export const designValidationMachine = createMachine({ states: { init: { - entry: assign({ + entry: [() => console.log("spawning design validation actor wooo"), assign({ schemaValidator: ({ spawn }) => spawn( fromWorkerfiedActor( @@ -197,7 +200,7 @@ export const designValidationMachine = createMachine({ id: 'dryRunValidator', syncSnapshot: true, }), - }), + })], always: 'idle', }, @@ -229,10 +232,13 @@ export const designValidationMachine = createMachine({ input: ({ context, event }) => ({ context, event }), src: fromPromise(async ({ input }) => { const { component } = input.event.data; - const def = await getComponentDefinition(component.type, component.model, { - apiVersion: component.apiVersion, + + const def = await getComponentDefinition(component.component.kind, component.model.name, { + apiVersion: component.component.version, annotations: 'include', }); + + console.log("bundle component for validation", component, def) return { validationPayload: { ...input.event.data, @@ -262,6 +268,7 @@ export const designValidationMachine = createMachine({ invoke: { input: ({ context, event }) => ({ context, event }), src: fromPromise(async ({ input }) => { + console.log("validate design schema wooo", input) const { event } = input; const def = await getAllComponentsDefsInDesign(event.data.design); return { diff --git a/ui/machines/validator/schemaValidator.js b/ui/machines/validator/schemaValidator.js index 2e6949fa560..e97fd620614 100644 --- a/ui/machines/validator/schemaValidator.js +++ b/ui/machines/validator/schemaValidator.js @@ -25,50 +25,9 @@ const validateSchema = (schema, data, id) => { }; }; -// const AjvValidatorActor = createMachine({ -// id: 'AjvValidatorActor', -// initial: 'idle', -// context: { -// ajv, -// }, -// states: { -// idle: { -// entry: DeferEvents.recall, -// on: { -// VALIDATE: 'validating', -// }, -// }, -// validating: { -// entry: [ -// assign({ -// validationResults: ({ event, context }) => { -// const schema = JSON.parse(event.data.schema); -// const results = validateSchema(context.ajv, schema, event.data.data, event.data.id); -// return results; -// }, -// }), -// sendTo( -// ({ event }) => event.returnAddress, -// ({ context }) => ({ -// type: 'VALIDATION_DONE', -// data: context.validationResults, -// }), -// ), -// ], - -// on: { -// VALIDATE: DeferEvents.defer, -// }, -// }, -// }, -// }); const validateComponent = (component, validateAnnotations = false, componentDef) => { - // const componentDef = await getComponentDefinition(component.type, component.model, { - // apiVersion: component.apiVersion, - // annotations: 'include', - // }); - + console.log("validating component", component) if (!componentDef || (componentDef?.metadata?.isAnnotation && !validateAnnotations)) { // skip validation for annotations return { @@ -78,64 +37,25 @@ const validateComponent = (component, validateAnnotations = false, componentDef) }; } const schema = JSON.parse(componentDef.component.schema); - const results = validateSchema(schema, component.settings || {}, componentDef.id); + const results = validateSchema(schema, component.configuration || {}, componentDef.id); const validationResults = { ...results, componentDefinition: componentDef, component, }; + console.log("component results", validationResults) return validationResults; }; -// const componentSchemaValidationMachine = createMachine({ -// id: 'componentSchemaValidationMachine', -// initial: 'validating', -// context: ({ input }) => ({ -// component: input.component, -// validateAnnotations: input.validateAnnotations, -// }), - -// states: { -// getDefinition: { -// entry: sendToActors([ACTOR_SYSTEM.RTK_QUERY], ({ context }) => -// rtkQueryActorCommands.initiateQuery({ -// endpointName: 'getComponentDefinition', -// params: { -// type: context.component.type, -// model: context.component.model, -// params: { -// apiVersion: context.component.apiVersion, -// annotations: 'include', -// }, -// }, -// }), -// ), - -// on: { -// [RTK_EVENTS.QUERY_RESULT]: { -// actions: ({ event }) => console.log('Query result wooo', event), -// target: 'done', -// }, -// }, -// }, -// done: { -// type: 'final', -// output: () => { -// console.log('done'); -// return { -// errors: [], -// }; -// }, -// }, -// }, -// }); - -export const componentKey = ({ type, model, apiVersion }) => `${type}-${model}-${apiVersion}`; + +export const componentKey = (component) => `${component.component.kind}-${component.model.name}-${component.component.version}`; const validateDesign = (design, componentDefsStore) => { - const { configurableComponents } = processDesign(design); + console.log("validating design in worker", design) + + const configurableComponents = design.components const validationResults = {}; @@ -147,11 +67,13 @@ const validateDesign = (design, componentDefsStore) => { false, componentDef, ); - validationResults[configurableComponent.name] = componentValidationResults; + validationResults[configurableComponent.id] = componentValidationResults; } catch (error) { console.error('Error validating component', error); } } + + console.log("validationResults", validationResults) return validationResults; }; diff --git a/ui/utils/utils.js b/ui/utils/utils.js index 27dabecddf0..a8110afcc5a 100644 --- a/ui/utils/utils.js +++ b/ui/utils/utils.js @@ -372,12 +372,10 @@ export const ResizableCell = ({ value }) => ( ); export const processDesign = (design) => { - const designJson = jsyaml.load(design.pattern_file); - const isAnnotation = (component) => - component?.traits?.meshmap?.['meshmodel-metadata']?.isAnnotation; + const isAnnotation = (component) =>component?.metadata?.isAnnotation - const components = Object.values(designJson.services); + const components = design.components const configurableComponents = components.filter(_.negate(isAnnotation)); const annotationComponents = components.filter(isAnnotation); @@ -385,7 +383,7 @@ export const processDesign = (design) => { configurableComponents, annotationComponents, components, - designJson, + designJson:design, }; };