Skip to content

Commit

Permalink
feat: add api to fetch cpu usage
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rw committed Jul 27, 2024
1 parent 1a7aed8 commit b602105
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/middlewares/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const createManyIntegrationMiddleware = <TKind extends IntegrationKind>(
if (offset !== 0) {
throw new TRPCError({
code: "NOT_FOUND",
message: `${offset} of the specified integrations not found or not of kinds ${kinds.join(",")}`,
message: `${offset} of the specified integrations [${input.integrationIds.join(",")}] not found or not of kinds ${kinds.join(",")} in db [${dbIntegrations.map(i => i.id).join(",")}]`,
});
}

Expand Down
21 changes: 21 additions & 0 deletions packages/api/src/router/widgets/hardware-usage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {createTRPCRouter, publicProcedure} from "../../trpc";
import {createManyIntegrationMiddleware} from "../../middlewares/integration";
import {createItemAndIntegrationChannel} from "@homarr/redis";
import {CpuLoad} from "@homarr/integrations";

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

View workflow job for this annotation

GitHub Actions / lint

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

export const hardwareUsageRouter = createTRPCRouter({
getCpuHistory: publicProcedure
.unstable_concat(createManyIntegrationMiddleware("query", "getDashDot"))
.query(async ({ ctx }) => {
return await Promise.all(
ctx.integrations.map(async (integration) => {
const channel = createItemAndIntegrationChannel<CpuLoad[]>("hardwareUsage", integration.id);
const data = await channel.getAsync();
return {
integrationId: integration.id,
sessions: data?.data ?? [],
};
}),
);
})
});
2 changes: 2 additions & 0 deletions packages/api/src/router/widgets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { mediaServerRouter } from "./media-server";
import { notebookRouter } from "./notebook";
import { smartHomeRouter } from "./smart-home";
import { weatherRouter } from "./weather";
import {hardwareUsageRouter} from "./hardware-usage";

export const widgetRouter = createTRPCRouter({
notebook: notebookRouter,
Expand All @@ -15,4 +16,5 @@ export const widgetRouter = createTRPCRouter({
smartHome: smartHomeRouter,
mediaServer: mediaServerRouter,
calendar: calendarRouter,
hardwareUsage: hardwareUsageRouter
});
16 changes: 16 additions & 0 deletions packages/integrations/src/dashdot/dashdot-integration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import {Integration} from "../base/integration";
import type {CpuLoad} from "../interfaces/hardware-usage/cpu-load";

export class DashDotIntegration extends Integration {
public async testConnectionAsync(): Promise<void> {
const response = await fetch(this.integration.url + (this.integration.url.endsWith("/") ? "info" : "/info"));
await response.json();
}

public async getCurrentCpuLoadAsync(): Promise<CpuLoad> {
const cpu = await fetch(this.integration.url + (this.integration.url.endsWith("/") ? "load/cpu" : "/load/cpu"));
const data = (await cpu.json()) as CpuLoadApi[];
return {
sumLoad: data.reduce((acc, current) => acc + current.load, 0),
};
}
}

/**
* CPU load per core
*/
interface CpuLoadApi {
load: number;
}
2 changes: 2 additions & 0 deletions packages/integrations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ export { PiHoleIntegration } from "./pi-hole/pi-hole-integration";
export { HomeAssistantIntegration } from "./homeassistant/homeassistant-integration";
export { JellyfinIntegration } from "./jellyfin/jellyfin-integration";
export { SonarrIntegration } from "./media-organizer/sonarr/sonarr-integration";
export { DashDotIntegration } from "./dashdot/dashdot-integration";

// Types
export type { StreamSession } from "./interfaces/media-server/session";
export type { CpuLoad } from "./interfaces/hardware-usage/cpu-load";

// Helpers
export { IntegrationTestConnectionError } from "./base/test-connection-error";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface CpuLoad {
sumLoad: number;
}
6 changes: 4 additions & 2 deletions packages/widgets/src/hardware-usage/serverData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ export default async function getServerDataAsync({ integrationIds }: WidgetProps
};
}

const currentStreams = await api.widget.mediaServer.getCurrentStreams({
const cpuHistory = await api.widget.hardwareUsage.getCpuHistory({
integrationIds,
});

return {
initialData: currentStreams,
initialData: {
cpuHistory: cpuHistory
},
};
}

0 comments on commit b602105

Please sign in to comment.