clan-app: Add logging middleware

This commit is contained in:
Qubasa
2025-07-02 17:59:05 +07:00
parent 26680bc784
commit 9cf6cecdae
9 changed files with 144 additions and 93 deletions

View File

@@ -46,21 +46,22 @@ interface BackendOpts {
}
interface BackendReturnType<K extends OperationNames> {
result: OperationResponse<K>;
metadata: Record<string, any>;
body: OperationResponse<K>;
header: Record<string, any>;
}
const _callApi = <K extends OperationNames>(
method: K,
args: OperationArgs<K>,
backendOpts?: BackendOpts,
): { promise: Promise<OperationResponse<K>>; op_key: string } => {
): { promise: Promise<BackendReturnType<K>>; op_key: string } => {
// if window[method] does not exist, throw an error
if (!(method in window)) {
console.error(`Method ${method} not found on window object`);
// return a rejected promise
return {
promise: Promise.resolve({
body: {
status: "error",
errors: [
{
@@ -69,17 +70,19 @@ const _callApi = <K extends OperationNames>(
},
],
op_key: "noop",
},
header: {},
}),
op_key: "noop",
};
}
let metadata: BackendOpts | undefined = undefined;
let header: BackendOpts = {};
if (backendOpts != undefined) {
metadata = { ...backendOpts };
header = { ...backendOpts };
let group = backendOpts?.logging?.group;
if (group != undefined && isMachine(group)) {
metadata = {
header = {
logging: { group: group.flake.identifier + "#" + group.name },
};
}
@@ -90,9 +93,10 @@ const _callApi = <K extends OperationNames>(
OperationNames,
(
args: OperationArgs<OperationNames>,
) => Promise<OperationResponse<OperationNames>>
metadata: BackendOpts,
) => Promise<BackendReturnType<OperationNames>>
>
)[method](args) as Promise<OperationResponse<K>>;
)[method](args, header) as Promise<BackendReturnType<K>>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const op_key = (promise as any)._webviewMessageId as string;
@@ -102,7 +106,7 @@ const _callApi = <K extends OperationNames>(
const handleCancel = async <K extends OperationNames>(
ops_key: string,
orig_task: Promise<OperationResponse<K>>,
orig_task: Promise<BackendReturnType<K>>,
) => {
console.log("Canceling operation: ", ops_key);
const { promise, op_key } = _callApi("cancel_task", { task_id: ops_key });
@@ -122,7 +126,7 @@ const handleCancel = async <K extends OperationNames>(
});
const resp = await promise;
if (resp.status === "error") {
if (resp.body.status === "error") {
toast.custom(
(t) => (
<ErrorToastComponent
@@ -186,14 +190,14 @@ export const callApi = <K extends OperationNames>(
console.log("Not printing toast because operation was cancelled");
}
const result = response;
if (result.status === "error" && !cancelled) {
const body = response.body;
if (body.status === "error" && !cancelled) {
toast.remove(toastId);
toast.custom(
(t) => (
<ErrorToastComponent
t={t}
message={"Error: " + result.errors[0].message}
message={"Error: " + body.errors[0].message}
/>
),
{
@@ -203,7 +207,7 @@ export const callApi = <K extends OperationNames>(
} else {
toast.remove(toastId);
}
return result;
return body;
});
return { promise: new_promise, op_key: op_key };

View File

@@ -207,7 +207,11 @@ export function RemoteForm(props: RemoteFormProps) {
flake: props.machine.flake,
field: props.field || "targetHost",
},
{logging: { group: { name: props.machine.name, flake: props.machine.flake } },},
{
logging: {
group: { name: props.machine.name, flake: props.machine.flake },
},
},
).promise;
if (result.status === "error")

View File

@@ -53,7 +53,8 @@ export const MachineListItem = (props: MachineListItemProps) => {
field: "targetHost",
flake: { identifier: active_clan },
name: name,
}, {logging: { group: { name, flake: { identifier: active_clan } } }}
},
{ logging: { group: { name, flake: { identifier: active_clan } } } },
).promise;
if (target_host.status == "error") {
@@ -106,13 +107,17 @@ export const MachineListItem = (props: MachineListItemProps) => {
}
setUpdating(true);
const target_host = await callApi("get_host", {
field: "targetHost",
flake: { identifier: active_clan },
name: name,
}, {
logging: { group: { name, flake: { identifier: active_clan } } },
}).promise;
const target_host = await callApi(
"get_host",
{
field: "targetHost",
flake: { identifier: active_clan },
name: name,
},
{
logging: { group: { name, flake: { identifier: active_clan } } },
},
).promise;
if (target_host.status == "error") {
console.error("No target host found for the machine");
@@ -129,11 +134,15 @@ export const MachineListItem = (props: MachineListItemProps) => {
return;
}
const build_host = await callApi("get_host", {
field: "buildHost",
flake: { identifier: active_clan },
name: name,
}, {logging: { group: { name, flake: { identifier: active_clan } } }}).promise;
const build_host = await callApi(
"get_host",
{
field: "buildHost",
flake: { identifier: active_clan },
name: name,
},
{ logging: { group: { name, flake: { identifier: active_clan } } } },
).promise;
if (build_host.status == "error") {
console.error("No target host found for the machine");
@@ -145,16 +154,20 @@ export const MachineListItem = (props: MachineListItemProps) => {
return;
}
await callApi("deploy_machine", {
machine: {
name: name,
flake: {
identifier: active_clan,
await callApi(
"deploy_machine",
{
machine: {
name: name,
flake: {
identifier: active_clan,
},
},
target_host: target_host.data!.data,
build_host: build_host.data?.data || null,
},
target_host: target_host.data!.data,
build_host: build_host.data?.data || null,
}, {logging: { group: { name, flake: { identifier: active_clan } } }}).promise;
{ logging: { group: { name, flake: { identifier: active_clan } } } },
).promise;
setUpdating(false);
};

View File

@@ -55,7 +55,7 @@ export function MachineForm(props: MachineFormProps) {
...values.machine,
tags: Array.from(values.machine.tags || detailed.machine.tags || []),
},
} ).promise;
}).promise;
await queryClient.invalidateQueries({
queryKey: [
@@ -77,10 +77,18 @@ export function MachineForm(props: MachineFormProps) {
if (!machine_name || !base_dir) {
return [];
}
const result = await callApi("get_generators_closure", {
base_dir: base_dir,
machine_name: machine_name,
}, {logging: {group: { name: machine_name, flake: {identifier: base_dir} }}}).promise;
const result = await callApi(
"get_generators_closure",
{
base_dir: base_dir,
machine_name: machine_name,
},
{
logging: {
group: { name: machine_name, flake: { identifier: base_dir } },
},
},
).promise;
if (result.status === "error") throw new Error("Failed to fetch data");
return result.data;
},
@@ -112,13 +120,18 @@ export function MachineForm(props: MachineFormProps) {
return;
}
const target = await callApi("get_host", {
field: "targetHost",
name: machine,
flake: {
identifier: curr_uri,
const target = await callApi(
"get_host",
{
field: "targetHost",
name: machine,
flake: {
identifier: curr_uri,
},
},
{
logging: { group: { name: machine, flake: { identifier: curr_uri } } },
},
}, {logging: { group: { name: machine, flake: { identifier: curr_uri } } }}
).promise;
if (target.status === "error") {
@@ -133,18 +146,24 @@ export function MachineForm(props: MachineFormProps) {
const target_host = target.data.data;
setIsUpdating(true);
const r = await callApi("deploy_machine", {
machine: {
name: machine,
flake: {
identifier: curr_uri,
const r = await callApi(
"deploy_machine",
{
machine: {
name: machine,
flake: {
identifier: curr_uri,
},
},
target_host: {
...target_host,
},
build_host: null,
},
target_host: {
...target_host,
{
logging: { group: { name: machine, flake: { identifier: curr_uri } } },
},
build_host: null,
}, {logging: { group: { name: machine, flake: { identifier: curr_uri } } }}).promise.finally(() => {
).promise.finally(() => {
setIsUpdating(false);
});
};

View File

@@ -149,14 +149,22 @@ export const VarsStep = (props: VarsStepProps) => {
const generatorsQuery = createQuery(() => ({
queryKey: [props.dir, props.machine_id, "generators", props.fullClosure],
queryFn: async () => {
const result = await callApi("get_generators_closure", {
base_dir: props.dir,
machine_name: props.machine_id,
full_closure: props.fullClosure,
}, {logging: {group: { name: props.machine_id, flake: {identifier: props.dir} }}}).promise;
const result = await callApi(
"get_generators_closure",
{
base_dir: props.dir,
machine_name: props.machine_id,
full_closure: props.fullClosure,
},
{
logging: {
group: { name: props.machine_id, flake: { identifier: props.dir } },
},
},
).promise;
if (result.status === "error") throw new Error("Failed to fetch data");
return result.data;
},
},
}));
const handleSubmit: SubmitHandler<VarsValues> = async (values, event) => {