Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refresh model node #1709

Merged
merged 10 commits into from
Aug 15, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ const props = defineProps<{
calibrationConfig?: boolean;
}>();

const emit = defineEmits(['new-model-configuration', 'update-model-configuration']);

const modelConfigInputValue = ref<string>('');
const modelConfigurations = ref<ModelConfiguration[]>([]);
const cellEditStates = ref<any[]>([]);
Expand Down Expand Up @@ -332,6 +334,7 @@ async function addModelConfiguration(config: ModelConfiguration) {
config.configuration
);
setTimeout(() => {
emit('new-model-configuration');
initializeConfigSpace();
}, 800);
}
Expand Down Expand Up @@ -482,6 +485,9 @@ function updateModelConfig(configIndex: number = modalVal.value.configIndex) {
const configToUpdate = modelConfigurations.value[configIndex];
updateModelConfiguration(configToUpdate);
openValueConfig.value = false;
setTimeout(() => {
emit('update-model-configuration');
}, 800);
}

function getParameterValue(parameter, valueName, timeseries) {
Expand Down
15 changes: 13 additions & 2 deletions packages/client/hmi-client/src/components/models/tera-model.vue
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,14 @@
v-if="model.semantics?.span"
:model="model"
:feature-config="featureConfig"
@new-model-configuration="emit('new-model-configuration')"
/>
<tera-model-configuration
v-else
:model="model"
:feature-config="featureConfig"
@new-model-configuration="emit('new-model-configuration')"
/>
<tera-model-configuration v-else :model="model" :feature-config="featureConfig" />
</AccordionTab>
<AccordionTab v-if="!isEmpty(relatedTerariumArtifacts)" header="Associated resources">
<DataTable :value="relatedTerariumModels">
Expand Down Expand Up @@ -596,7 +602,12 @@ enum ModelView {
}

// TODO - Get rid of these emits
const emit = defineEmits(['close-preview', 'asset-loaded', 'close-current-tab']);
const emit = defineEmits([
'close-preview',
'asset-loaded',
'close-current-tab',
'new-model-configuration'
mwdchang marked this conversation as resolved.
Show resolved Hide resolved
]);

const props = defineProps({
project: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ const props = defineProps<{
calibrationConfig?: boolean;
}>();

const emit = defineEmits(['new-model-configuration', 'update-model-configuration']);

const modelConfigInputValue = ref<string>('');
const modelConfigurations = ref<ModelConfiguration[]>([]);
const cellEditStates = ref<any[]>([]);
Expand Down Expand Up @@ -186,6 +188,9 @@ const vFocus = {
function updateModelConfigName(configIndex: number) {
modelConfigurations.value[configIndex].name = modelConfigInputValue.value;
updateModelConfiguration(modelConfigurations.value[configIndex]);
setTimeout(() => {
emit('update-model-configuration');
}, 800);
YohannParis marked this conversation as resolved.
Show resolved Hide resolved
}

async function addModelConfiguration(config: ModelConfiguration) {
Expand All @@ -196,6 +201,7 @@ async function addModelConfiguration(config: ModelConfiguration) {
config.configuration
);
setTimeout(() => {
emit('new-model-configuration');
initializeConfigSpace();
}, 800);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<template>
<tera-model :project="props.project" :asset-id="props.node.state.modelId" />
<tera-model
:project="props.project"
:asset-id="props.node.state.modelId"
@new-model-configuration="refreshModelNode"
@update-model-configuration="refreshModelNode"
YohannParis marked this conversation as resolved.
Show resolved Hide resolved
/>
</template>

<script setup lang="ts">
Expand All @@ -8,9 +13,18 @@
import { IProject } from '@/types/Project';
import { WorkflowNode } from '@/types/workflow';
import TeraModel from '@/components/models/tera-model.vue';
import { workflowEventBus } from '@/services/workflow';

const props = defineProps<{
node: WorkflowNode;
project: IProject;
}>();

// Send refresh event onto the eventBus
const refreshModelNode = () => {
workflowEventBus.emitNodeRefresh({
workflowId: props.node.workflowId,
nodeId: props.node.id
});
};
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@

<script setup lang="ts">
import { isArray, cloneDeep, isEqual } from 'lodash';
import { v4 as uuidv4 } from 'uuid';
import { ref, onMounted, onUnmounted, computed, watch } from 'vue';
import { getModelConfigurations } from '@/services/model';
import TeraInfiniteCanvas from '@/components/widgets/tera-infinite-canvas.vue';
Expand Down Expand Up @@ -344,25 +343,31 @@ const testOperation: Operation = {
const models = computed<Model[]>(() => props.project.assets?.models ?? []);
const datasets = computed<Dataset[]>(() => props.project.assets?.datasets ?? []);

async function selectModel(node: WorkflowNode, data: { id: string }) {
droppedAssetId.value = null;
node.state.modelId = data.id;

const refreshModelNode = async (node: WorkflowNode) => {
// FIXME: Need additional design to work out exactly what to show. June 2023
YohannParis marked this conversation as resolved.
Show resolved Hide resolved
const configurationList = await getModelConfigurations(data.id);
node.outputs = [];
const configurationList = await getModelConfigurations(node.state.modelId);
configurationList.forEach((configuration) => {
// Only add new configurations
if (node.outputs.find((d) => isEqual(d.value, [configuration.id]))) return;
const existingConfig = node.outputs.find((port) => isEqual(port.value, [configuration.id]));
if (existingConfig) {
existingConfig.label = configuration.name;
return;
}

node.outputs.push({
id: uuidv4(),
id: crypto.randomUUID(),
type: 'modelConfigId',
label: configuration.name,
value: [configuration.id],
status: WorkflowPortStatus.NOT_CONNECTED
});
});
};

async function selectModel(node: WorkflowNode, data: { id: string }) {
droppedAssetId.value = null;
node.state.modelId = data.id;
await refreshModelNode(node);
}

async function updateWorkflowName() {
Expand All @@ -379,7 +384,7 @@ async function selectDataset(node: WorkflowNode, data: { id: string; name: strin
node.state.datasetId = data.id;
node.outputs = [
{
id: uuidv4(),
id: crypto.randomUUID(),
type: 'datasetId',
label: data.name,
value: [data.id],
Expand All @@ -390,7 +395,7 @@ async function selectDataset(node: WorkflowNode, data: { id: string; name: strin

function appendOutputPort(node: WorkflowNode, port: { type: string; label?: string; value: any }) {
node.outputs.push({
id: uuidv4(),
id: crypto.randomUUID(),
type: port.type,
label: port.label,
value: isArray(port.value) ? port.value : [port.value],
Expand Down Expand Up @@ -439,6 +444,22 @@ workflowEventBus.on('node-state-change', (payload: any) => {
workflowDirty = true;
});

workflowEventBus.on('node-refresh', (payload: { workflowId: string; nodeId: string }) => {
if (wf.value.id !== payload.workflowId) return;
const node = wf.value.nodes.find((n) => n.id === payload.nodeId);
if (!node) return;

if (node.operationType === WorkflowOperationTypes.MODEL) {
// This part is a bit hacky and slow. Because we allow multiple instances of the
// same model across many nodes in a workflow, they ALL need to be updated. However
// this multi-models setup is also somewhat uncommon so I don't want to go out of the way
// to communicate "model change" instead of "node change", the former seemingly out of
// place when using the WorkflowEventBus mechanism. DC - Aug 2023
const nodesToRefresh = wf.value.nodes.filter((n) => n.state.modelId === node.state.modelId);
nodesToRefresh.forEach(refreshModelNode);
}
});

workflowEventBus.on(
'add-node',
(payload: { id: string; operation: Operation; position: Position; state: any }) => {
Expand Down
4 changes: 4 additions & 0 deletions packages/client/hmi-client/src/services/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ class EventEmitter {
emitNodeStateChange(payload: { workflowId: string; nodeId: string; state: any }) {
this.emit('node-state-change', payload);
}

emitNodeRefresh(payload: { workflowId: string; nodeId: string }) {
this.emit('node-refresh', payload);
}
}

export const workflowEventBus = new EventEmitter();