Skip to content

Commit

Permalink
migrate design validation
Browse files Browse the repository at this point in the history
Signed-off-by: codeSafari10 <[email protected]>
  • Loading branch information
codeSafari10 committed Aug 7, 2024
1 parent 237d314 commit 2681103
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 106 deletions.
11 changes: 10 additions & 1 deletion ui/components/MesheryMeshInterface/PatternServiceFormCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -117,4 +118,12 @@ function PatternServiceFormCore({
return child.current;
}

const PatternServiceFormCore = (props) => {
return (
<ErrorBoundary>
<PatternServiceFormCore_ {...props} />
</ErrorBoundary>
);
};

export default PatternServiceFormCore;
29 changes: 18 additions & 11 deletions ui/machines/validator/designValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}),
),
Expand All @@ -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;
};

Expand All @@ -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(
Expand All @@ -197,7 +200,7 @@ export const designValidationMachine = createMachine({
id: 'dryRunValidator',
syncSnapshot: true,
}),
}),
})],
always: 'idle',
},

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
100 changes: 11 additions & 89 deletions ui/machines/validator/schemaValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 = {};

Expand All @@ -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;
};

Expand Down
8 changes: 3 additions & 5 deletions ui/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,20 +372,18 @@ 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);

return {
configurableComponents,
annotationComponents,
components,
designJson,
designJson:design,
};
};

Expand Down

0 comments on commit 2681103

Please sign in to comment.