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

update macro events real-time #294

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
69 changes: 49 additions & 20 deletions src/data/EventStream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useUid, useUserInfo } from 'data/UserInfo';
import { useUid } from 'data/UserInfo';
import { addInstance, deleteInstance, updateInstance } from 'data/InstanceList';
import { LodestoneContext } from 'data/LodestoneContext';
import { useQueryClient } from '@tanstack/react-query';
Expand Down Expand Up @@ -248,7 +248,10 @@ export const useEventStream = () => {
['user', 'list'],
(oldList: { [uid: string]: PublicUser } | undefined) => {
if (!oldList) return oldList;
const newUser = {...oldList[uid], permissions: new_permissions};
const newUser = {
...oldList[uid],
permissions: new_permissions,
};
const newList = { ...oldList };
newList[uid] = newUser;
return newList;
Expand All @@ -270,6 +273,15 @@ export const useEventStream = () => {
match(event_inner, {
Started: () => {
console.log(`Macro ${macro_pid} started on ${uuid}`);
queryClient.setQueryData(
['instance', uuid, 'taskList'],
(oldList: number[] | undefined) => {
if (!oldList) oldList = [];
const newList = [...oldList];
newList.push(macro_pid);
return newList;
}
);
dispatch({
title: `Macro ${macro_pid} started on ${uuid}`,
event,
Expand All @@ -278,16 +290,25 @@ export const useEventStream = () => {
});
},
Detach: () => {
console.log(`Macro ${macro_pid} detached on ${uuid}`);
dispatch({
title: `Macro ${macro_pid} detached on ${uuid}`,
event,
type: 'add',
fresh,
});
console.log(`Macro ${macro_pid} detached on ${uuid}`);
dispatch({
title: `Macro ${macro_pid} detached on ${uuid}`,
event,
type: 'add',
fresh,
});
},
Stopped: ({ exit_status }) => {
console.log(`Macro ${macro_pid} stopped on ${uuid} with status ${exit_status.type}`);
console.log(
`Macro ${macro_pid} stopped on ${uuid} with status ${exit_status.type}`
);
queryClient.setQueryData(
['instance', uuid, 'taskList'],
(oldList: number[] | undefined) => {
if (!oldList) return oldList;
return oldList.filter((task_pid) => task_pid !== macro_pid);
}
);
dispatch({
title: `Macro ${macro_pid} stopped on ${uuid} with status ${exit_status.type}`,
event,
Expand Down Expand Up @@ -320,14 +341,22 @@ export const useEventStream = () => {
addInstance(instance_info, queryClient),
InstanceDelete: ({ instance_uuid: uuid }) =>
deleteInstance(uuid, queryClient),
FSOperationCompleted: ({ instance_uuid, success, message }) => {
FSOperationCompleted: ({
instance_uuid,
success,
message,
}) => {
if (success) {
toast.success(message)
toast.success(message);
} else {
toast.error(message)
toast.error(message);
}
queryClient.invalidateQueries(['instance', instance_uuid, 'fileList']);
}
queryClient.invalidateQueries([
'instance',
instance_uuid,
'fileList',
]);
},
},
// eslint-disable-next-line @typescript-eslint/no-empty-function
(_) => {}
Expand Down Expand Up @@ -407,11 +436,11 @@ export const useEventStream = () => {
if (!token) return;

const connectWebsocket = () => {
const wsAddress = `${core.protocol === 'https' ? 'wss' : 'ws'}://${core.address}:${
core.port ?? LODESTONE_PORT
}/api/${core.apiVersion}/events/all/stream?filter=${JSON.stringify(
eventQuery
)}`;
const wsAddress = `${core.protocol === 'https' ? 'wss' : 'ws'}://${
core.address
}:${core.port ?? LODESTONE_PORT}/api/${
core.apiVersion
}/events/all/stream?filter=${JSON.stringify(eventQuery)}`;

if (wsRef.current) wsRef.current.close();

Expand Down
44 changes: 35 additions & 9 deletions src/pages/macros.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { InstanceContext } from 'data/InstanceContext';
import { useContext, useEffect, useState, useMemo } from 'react';
import { MacroEntry } from 'bindings/MacroEntry';
import clsx from 'clsx';
import { useQueryClient } from '@tanstack/react-query';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { toast } from 'react-toastify';

export type MacrosPage = 'All Macros' | 'Running Tasks' | 'History';
Expand Down Expand Up @@ -69,6 +69,10 @@ const Macros = () => {
} as TableRow)
)
);
queryClient.setQueryData(
['instance', instanceUuid, 'tasks'],
response.map((task) => task.pid)
);
};

const fetchHistory = async (instanceUuid: string) => {
Expand All @@ -89,19 +93,41 @@ const Macros = () => {
);
};

useEffect(() => {
const fetchAll = async () => {
if (!selectedInstance) return;
fetchMacros(selectedInstance.uuid);
fetchTasks(selectedInstance.uuid);
fetchHistory(selectedInstance.uuid);
};

const fetchAll = async () => {
if (!selectedInstance) return;
fetchMacros(selectedInstance.uuid);
fetchTasks(selectedInstance.uuid);
fetchHistory(selectedInstance.uuid);
};

useEffect(() => {
fetchAll();
}, [selectedInstance]);

const { data: runningMacros } = useQuery([
hanmindev marked this conversation as resolved.
Show resolved Hide resolved
'instance',
selectedInstance?.uuid,
'taskList',
]);

useEffect(() => {
if (!runningMacros) return;
const runningMacrosTyped = runningMacros as number[];

// if there is a new task running fetchall
for (const runningMacroId of runningMacrosTyped) {
if (!tasks.find((task) => task.pid === runningMacroId)) {
fetchAll();
return;
}
}

const newTasks = tasks.filter((task) => {
return runningMacrosTyped.find((runningTask) => runningTask === task.id);
});
setTasks(newTasks);
}, [runningMacros]);

const [selectedPage, setSelectedPage] = useState<MacrosPage>('All Macros');

const MacrosPageMap: Record<
Expand Down