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

Convert entryPointStore to composition API + TS #17356

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 0 additions & 74 deletions client/src/stores/entryPointStore.js

This file was deleted.

2 changes: 1 addition & 1 deletion client/src/stores/entryPointStore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("stores/EntryPointStore", () => {
expect(store.entryPoints.length).toBe(2);
});
it("stops polling", async () => {
expect(store.pollTimeout >= 0).toBeTruthy();
expect(store.pollTimeout !== undefined).toBeTruthy();
store.stopPollingEntryPoints();
expect(store.pollTimeout === undefined).toBeTruthy();
});
Expand Down
103 changes: 103 additions & 0 deletions client/src/stores/entryPointStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import axios from "axios";
import isEqual from "lodash.isequal";
import { defineStore } from "pinia";
import { computed, ref } from "vue";

import { getAppRoot } from "@/onload/loadConfig";
import { rethrowSimple } from "@/utils/simple-error";

// TODO: replace with the corresponding autogenerated model when ready
interface EntryPoint {
model_class: "InteractiveToolEntryPoint";
id: string;
job_id: string;
name: string;
active: boolean;
created_time: string;
modified_time: string;
output_datasets_ids: string[];
target?: string;
}

export const useEntryPointStore = defineStore("entryPointStore", () => {
const pollTimeout = ref<NodeJS.Timeout | undefined>(undefined);
const entryPoints = ref<EntryPoint[]>([]);

const entryPointsForJob = computed(() => {
return (jobId: string) => entryPoints.value.filter((entryPoint) => entryPoint["job_id"] === jobId);
});

const entryPointsForHda = computed(() => {
return (hdaId: string) =>
entryPoints.value.filter((entryPoint) => entryPoint["output_datasets_ids"].includes(hdaId));
});

async function ensurePollingEntryPoints() {
await fetchEntryPoints();
pollTimeout.value = setTimeout(() => {
ensurePollingEntryPoints();
}, 10000);
}

function stopPollingEntryPoints() {
clearTimeout(pollTimeout.value);
pollTimeout.value = undefined;
}

async function fetchEntryPoints() {
const url = `${getAppRoot()}api/entry_points`;
const params = { running: true };
try {
const response = await axios.get(url, { params: params });
updateEntryPoints(response.data);
} catch (e) {
rethrowSimple(e);
}
}

function updateEntryPoints(data: EntryPoint[]) {
let hasChanged = entryPoints.value.length !== data.length ? true : false;
if (entryPoints.value.length === 0) {
entryPoints.value = data;
} else {
const newEntryPoints = [];
for (const ep of data) {
const olderEntryPoint = entryPoints.value.filter((item) => item.id === ep.id)[0];
if (!hasChanged && !isEqual(olderEntryPoint, ep)) {
hasChanged = true;
}
if (olderEntryPoint) {
newEntryPoints.push(mergeEntryPoints(olderEntryPoint, ep));
}
}
if (hasChanged) {
entryPoints.value = newEntryPoints;
}
}
}

function mergeEntryPoints(original: EntryPoint, updated: EntryPoint) {
return { ...original, ...updated };
}

function removeEntryPoint(toolId: string) {
const index = entryPoints.value.findIndex((ep) => {
return ep.id === toolId ? true : false;
});
if (index >= 0) {
entryPoints.value.splice(index, 1);
}
}

return {
entryPoints,
entryPointsForJob,
entryPointsForHda,
fetchEntryPoints,
updateEntryPoints,
removeEntryPoint,
pollTimeout,
ensurePollingEntryPoints,
stopPollingEntryPoints,
};
});