clan-app: Implement dynamic log groups into javascript callApi
nix fmt
This commit is contained in:
@@ -23,42 +23,25 @@ export type SuccessQuery<T extends OperationNames> = Extract<
|
||||
>;
|
||||
export type SuccessData<T extends OperationNames> = SuccessQuery<T>["data"];
|
||||
|
||||
function isMachine(obj: unknown): obj is Machine {
|
||||
return (
|
||||
!!obj &&
|
||||
typeof obj === "object" &&
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
typeof (obj as any).name === "string" &&
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
typeof (obj as any).flake === "object" &&
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
typeof (obj as any).flake.identifier === "string"
|
||||
);
|
||||
}
|
||||
|
||||
// Machine type with flake for API calls
|
||||
interface Machine {
|
||||
name: string;
|
||||
flake: {
|
||||
identifier: string;
|
||||
};
|
||||
}
|
||||
|
||||
interface BackendOpts {
|
||||
logging?: { group: string | Machine };
|
||||
interface SendHeaderType {
|
||||
logging?: { group_path: string[] };
|
||||
}
|
||||
interface BackendSendType<K extends OperationNames> {
|
||||
body: OperationArgs<K>;
|
||||
header?: SendHeaderType;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
||||
interface ReceiveHeaderType {}
|
||||
interface BackendReturnType<K extends OperationNames> {
|
||||
body: OperationResponse<K>;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
header: Record<string, any>;
|
||||
header: ReceiveHeaderType;
|
||||
}
|
||||
|
||||
const _callApi = <K extends OperationNames>(
|
||||
method: K,
|
||||
args: OperationArgs<K>,
|
||||
backendOpts?: BackendOpts,
|
||||
backendOpts?: SendHeaderType,
|
||||
): { promise: Promise<BackendReturnType<K>>; op_key: string } => {
|
||||
// if window[method] does not exist, throw an error
|
||||
if (!(method in window)) {
|
||||
@@ -82,26 +65,19 @@ const _callApi = <K extends OperationNames>(
|
||||
};
|
||||
}
|
||||
|
||||
let header: BackendOpts = {};
|
||||
if (backendOpts != undefined) {
|
||||
header = { ...backendOpts };
|
||||
const group = backendOpts?.logging?.group;
|
||||
if (group != undefined && isMachine(group)) {
|
||||
header = {
|
||||
logging: { group: group.flake.identifier + "#" + group.name },
|
||||
};
|
||||
}
|
||||
}
|
||||
const message: BackendSendType<OperationNames> = {
|
||||
body: args,
|
||||
header: backendOpts,
|
||||
};
|
||||
|
||||
const promise = (
|
||||
window as unknown as Record<
|
||||
OperationNames,
|
||||
(
|
||||
args: OperationArgs<OperationNames>,
|
||||
metadata: BackendOpts,
|
||||
args: BackendSendType<OperationNames>,
|
||||
) => Promise<BackendReturnType<OperationNames>>
|
||||
>
|
||||
)[method](args, header) as Promise<BackendReturnType<K>>;
|
||||
)[method](message) as Promise<BackendReturnType<K>>;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const op_key = (promise as any)._webviewMessageId as string;
|
||||
@@ -153,7 +129,7 @@ const handleCancel = async <K extends OperationNames>(
|
||||
export const callApi = <K extends OperationNames>(
|
||||
method: K,
|
||||
args: OperationArgs<K>,
|
||||
backendOpts?: BackendOpts,
|
||||
backendOpts?: SendHeaderType,
|
||||
): { promise: Promise<OperationResponse<K>>; op_key: string } => {
|
||||
console.log("Calling API", method, args, backendOpts);
|
||||
|
||||
|
||||
@@ -186,6 +186,7 @@ export function RemoteForm(props: RemoteFormProps) {
|
||||
props.queryFn,
|
||||
props.machine?.name,
|
||||
props.machine?.flake,
|
||||
props.machine?.flake.identifier,
|
||||
props.field || "targetHost",
|
||||
],
|
||||
queryFn: async () => {
|
||||
@@ -209,7 +210,12 @@ export function RemoteForm(props: RemoteFormProps) {
|
||||
},
|
||||
{
|
||||
logging: {
|
||||
group: { name: props.machine.name, flake: props.machine.flake },
|
||||
group_path: [
|
||||
"clans",
|
||||
props.machine.flake.identifier,
|
||||
"machines",
|
||||
props.machine.name,
|
||||
],
|
||||
},
|
||||
},
|
||||
).promise;
|
||||
|
||||
@@ -54,7 +54,9 @@ export const MachineListItem = (props: MachineListItemProps) => {
|
||||
flake: { identifier: active_clan },
|
||||
name: name,
|
||||
},
|
||||
{ logging: { group: { name, flake: { identifier: active_clan } } } },
|
||||
{
|
||||
logging: { group_path: ["clans", active_clan, "machines", name] },
|
||||
},
|
||||
).promise;
|
||||
|
||||
if (target_host.status == "error") {
|
||||
@@ -115,7 +117,9 @@ export const MachineListItem = (props: MachineListItemProps) => {
|
||||
name: name,
|
||||
},
|
||||
{
|
||||
logging: { group: { name, flake: { identifier: active_clan } } },
|
||||
logging: {
|
||||
group_path: ["clans", active_clan, "machines", name],
|
||||
},
|
||||
},
|
||||
).promise;
|
||||
|
||||
@@ -141,7 +145,11 @@ export const MachineListItem = (props: MachineListItemProps) => {
|
||||
flake: { identifier: active_clan },
|
||||
name: name,
|
||||
},
|
||||
{ logging: { group: { name, flake: { identifier: active_clan } } } },
|
||||
{
|
||||
logging: {
|
||||
group_path: ["clans", active_clan, "machines", name],
|
||||
},
|
||||
},
|
||||
).promise;
|
||||
|
||||
if (build_host.status == "error") {
|
||||
@@ -166,7 +174,11 @@ export const MachineListItem = (props: MachineListItemProps) => {
|
||||
target_host: target_host.data!.data,
|
||||
build_host: build_host.data?.data || null,
|
||||
},
|
||||
{ logging: { group: { name, flake: { identifier: active_clan } } } },
|
||||
{
|
||||
logging: {
|
||||
group_path: ["clans", active_clan, "machines", name],
|
||||
},
|
||||
},
|
||||
).promise;
|
||||
|
||||
setUpdating(false);
|
||||
|
||||
@@ -85,7 +85,7 @@ export function MachineForm(props: MachineFormProps) {
|
||||
},
|
||||
{
|
||||
logging: {
|
||||
group: { name: machine_name, flake: { identifier: base_dir } },
|
||||
group_path: ["clans", base_dir, "machines", machine_name],
|
||||
},
|
||||
},
|
||||
).promise;
|
||||
@@ -130,7 +130,9 @@ export function MachineForm(props: MachineFormProps) {
|
||||
},
|
||||
},
|
||||
{
|
||||
logging: { group: { name: machine, flake: { identifier: curr_uri } } },
|
||||
logging: {
|
||||
group_path: ["clans", curr_uri, "machines", machine],
|
||||
},
|
||||
},
|
||||
).promise;
|
||||
|
||||
@@ -161,7 +163,9 @@ export function MachineForm(props: MachineFormProps) {
|
||||
build_host: null,
|
||||
},
|
||||
{
|
||||
logging: { group: { name: machine, flake: { identifier: curr_uri } } },
|
||||
logging: {
|
||||
group_path: ["clans", curr_uri, "machines", machine],
|
||||
},
|
||||
},
|
||||
).promise.finally(() => {
|
||||
setIsUpdating(false);
|
||||
|
||||
@@ -158,7 +158,7 @@ export const VarsStep = (props: VarsStepProps) => {
|
||||
},
|
||||
{
|
||||
logging: {
|
||||
group: { name: props.machine_id, flake: { identifier: props.dir } },
|
||||
group_path: ["clans", props.dir, "machines", props.machine_id],
|
||||
},
|
||||
},
|
||||
).promise;
|
||||
|
||||
@@ -16,14 +16,22 @@ export const MachineInstall = () => {
|
||||
queryFn: async () => {
|
||||
const curr = activeClanURI();
|
||||
if (curr) {
|
||||
const result = await callApi("get_machine_details", {
|
||||
machine: {
|
||||
flake: {
|
||||
identifier: curr,
|
||||
const result = await callApi(
|
||||
"get_machine_details",
|
||||
{
|
||||
machine: {
|
||||
flake: {
|
||||
identifier: curr,
|
||||
},
|
||||
name: params.id,
|
||||
},
|
||||
name: params.id,
|
||||
},
|
||||
}).promise;
|
||||
{
|
||||
logging: {
|
||||
group_path: ["clans", curr, "machines", params.id],
|
||||
},
|
||||
},
|
||||
).promise;
|
||||
if (result.status === "error") throw new Error("Failed to fetch data");
|
||||
return result.data;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ import Icon from "@/src/components/icon";
|
||||
import { Header } from "@/src/layout/header";
|
||||
import { makePersisted } from "@solid-primitives/storage";
|
||||
import { useClanContext } from "@/src/contexts/clan";
|
||||
import { debug } from "console";
|
||||
|
||||
type MachinesModel = Extract<
|
||||
OperationResponse<"list_machines">,
|
||||
|
||||
Reference in New Issue
Block a user