Skip to content

Commit

Permalink
feat: add debug info
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rw committed Oct 20, 2024
1 parent e145f33 commit 3049584
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 10 deletions.
13 changes: 12 additions & 1 deletion packages/api/src/router/widgets/hardware-usage.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { observable } from "@trpc/server/observable";

import type { CpuLoad, MemoryLoad, NetworkLoad } from "@homarr/integrations";
import type { CpuLoad, MemoryLoad, NetworkLoad, ServerInfo } from "@homarr/integrations";
import { createItemAndIntegrationChannel } from "@homarr/redis";

import { createOneIntegrationMiddleware } from "../../middlewares/integration";
import { createTRPCRouter, publicProcedure } from "../../trpc";

export const hardwareUsageRouter = createTRPCRouter({
getServerInfo: publicProcedure
.unstable_concat(createOneIntegrationMiddleware("query", "getDashDot"))
.query(async ({ ctx }) => {
const channel = createItemAndIntegrationChannel<{
info: ServerInfo;
}>("hardwareUsage", ctx.integration.id);
const data = await channel.getAsync();
return {
info: data?.data.info ?? ({} as ServerInfo),
};
}),
getHardwareInformationHistory: publicProcedure
.unstable_concat(createOneIntegrationMiddleware("query", "getDashDot"))
.query(async ({ ctx }) => {
Expand Down
7 changes: 6 additions & 1 deletion packages/cron-jobs/src/jobs/integrations/hardware-usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { decryptSecret } from "@homarr/common/server";
import { EVERY_SECOND } from "@homarr/cron-jobs-core/expressions";
import { db, eq } from "@homarr/db";
import { items } from "@homarr/db/schema/sqlite";
import type { CpuLoad, MemoryLoad, NetworkLoad } from "@homarr/integrations";
import type { CpuLoad, MemoryLoad, NetworkLoad, ServerInfo } from "@homarr/integrations";
import { DashDotIntegration } from "@homarr/integrations";
import { createItemAndIntegrationChannel } from "@homarr/redis";

Expand Down Expand Up @@ -38,11 +38,14 @@ export const hardwareUsageJob = createCronJob("hardwareUsage", EVERY_SECOND).wit
value: decryptSecret(secret.value),
})),
});

const info = await dashDotIntegration.getInfoAsync();
const cpuLoad = await dashDotIntegration.getCurrentCpuLoadAsync();
const memoryLoad = await dashDotIntegration.getCurrentMemoryLoadAsync();
const networkLoad = await dashDotIntegration.getCurrentNetworkLoadAsync();

const cache = createItemAndIntegrationChannel<{
info: ServerInfo;
cpuLoad: CpuLoad;
memoryLoad: MemoryLoad;
networkLoad: NetworkLoad;
Expand All @@ -51,11 +54,13 @@ export const hardwareUsageJob = createCronJob("hardwareUsage", EVERY_SECOND).wit
memoryLoad,
networkLoad,
cpuLoad,
info
});
await cache.publishAndUpdateLastStateAsync({
cpuLoad,
networkLoad,
memoryLoad,
info
});
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/integrations/src/dashdot/dashdot-integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Integration } from "../base/integration";
import type { CpuLoad } from "../interfaces/hardware-usage/cpu-load";
import type { MemoryLoad } from "../interfaces/hardware-usage/memory-load";
import type { NetworkLoad } from "../interfaces/hardware-usage/network-load";
import type { ServerInfo } from "../interfaces/hardware-usage/server-info";

export class DashDotIntegration extends Integration {
public async testConnectionAsync(): Promise<void> {
Expand All @@ -11,7 +12,10 @@ export class DashDotIntegration extends Integration {

public async getInfoAsync(): Promise<ServerInfo> {
const infoResponse = await fetch(this.appendPathToUrlWithEndingSlash(this.integration.url, "info"));
return (await infoResponse.json()) as ServerInfo;
const serverInfo = (await infoResponse.json()) as InternalServerInfo;
return {
maxAvailableMemoryBytes: serverInfo.ram.size
};
}

public async getCurrentCpuLoadAsync(): Promise<CpuLoad> {
Expand Down Expand Up @@ -56,7 +60,7 @@ interface NetworkLoadApi {
down: number;
}

interface ServerInfo {
interface InternalServerInfo {
ram: {
/**
* Available memory in bytes
Expand Down
5 changes: 1 addition & 4 deletions packages/integrations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ export type { ExtendedClientStatus } from "./interfaces/downloads/download-clien
export type { CpuLoad } from "./interfaces/hardware-usage/cpu-load";
export type { MemoryLoad } from "./interfaces/hardware-usage/memory-load";
export type { NetworkLoad } from "./interfaces/hardware-usage/network-load";
export type { ServerInfo } from "./interfaces/hardware-usage/server-info";
export type { HealthMonitoring } from "./interfaces/health-monitoring/healt-monitoring";
export { MediaRequestStatus } from "./interfaces/media-requests/media-request";
export type { MediaRequestList, MediaRequestStats } from "./interfaces/media-requests/media-request";
export type { StreamSession } from "./interfaces/media-server/session";
export type { ExtendedClientStatus } from "./interfaces/downloads/download-client-status";
export type { ExtendedDownloadClientItem } from "./interfaces/downloads/download-client-items";
export type { DownloadClientJobsAndStatus } from "./interfaces/downloads/download-client-data";
export type { IntegrationInput } from "./base/integration";

// Schemas
export { downloadClientItemSchema } from "./interfaces/downloads/download-client-items";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface ServerInfo {
maxAvailableMemoryBytes: number;
}
1 change: 1 addition & 0 deletions packages/widgets/src/hardware-usage/component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function HardwareUsageWidget({ serverData, integrationIds }: Widg
<CpuGraph cpuLoad={hardwareUsage.map(usage => usage.cpuLoad)} hasLast={hasLast}/>
<MemoryGraph memoryLoad={hardwareUsage.map(usage => usage.memoryLoad)} maxAvailableBytes={6000000000} hasLast={hasLast} />
<Code block>{JSON.stringify(hardwareUsage[hardwareUsage.length - 1])}</Code>
<Code block>{JSON.stringify(serverData?.initialData.serverInfo)}</Code>

Check failure on line 45 in packages/widgets/src/hardware-usage/component.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe member access .initialData on an `error` typed value
</Stack>
);
}
10 changes: 8 additions & 2 deletions packages/widgets/src/hardware-usage/serverData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use server";

import { api } from "@homarr/api/server";
import { CpuLoad, MemoryLoad, NetworkLoad } from "@homarr/integrations";
import { CpuLoad, MemoryLoad, NetworkLoad, ServerInfo } from "@homarr/integrations";

Check warning on line 4 in packages/widgets/src/hardware-usage/serverData.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

import type { WidgetProps } from "../definition";

Expand All @@ -12,8 +12,9 @@ export default async function getServerDataAsync({ integrationIds }: WidgetProps
hardwareInformationHistory: {
cpuLoad: {} as CpuLoad,
memoryLoad: {} as MemoryLoad,
networkLoad: {} as NetworkLoad,
networkLoad: {} as NetworkLoad
},
serverInfo: {} as ServerInfo
},
};
}
Expand All @@ -22,9 +23,14 @@ export default async function getServerDataAsync({ integrationIds }: WidgetProps
integrationId: integrationIds[0] ?? "",
});

const serverInfo = await api.widget.hardwareUsage.getServerInfo({
integrationId: integrationIds[0] ?? "",
});

return {
initialData: {
hardwareInformationHistory: hardwareInformationHistory,
serverInfo
},
};
}

0 comments on commit 3049584

Please sign in to comment.