perf: implement Phase 1 homepage diet via Next.js Cache Components
P1-0: V8 heap cap (HOMARR_MAX_OLD_SPACE_SIZE, default 512MB)
P1-1: Module graph cuts
- Dynamic integration imports (55 static → lazy loaders)
- Conditional DB driver loading (only configured driver loaded)
- Replace jsdom with linkedom for SVG sanitization
- Delete eager widget barrel (55 import * as → 0)
- Extract createRetryableLoader to @homarr/common
P1-2: Enable cacheComponents + restructure root layout
- Suspense boundaries around session-dependent providers
- ColorSchemeScript replaces server-side cookie reads
- Board/manage/auth layouts wrapped in Suspense
P1-3: Cache read paths
- Cache tag registry + invalidation helpers
- Cache invalidation wired into all mutation routers
- Translation deep-merge memoized per locale
P1-4: Prerendered shells + loading.tsx files
P1-5: Shared integration data cache (60s TTL, stale-while-revalidate)
P1-6: Redis-backed Next.js cache handler (@homarr/next-cache-handler)
P1-7: Redis optional (memory channel fallback), TTL on pubSub:last:*,
Redis clients consolidated from 6 to 2
This commit is contained in:
86 files changed
+2219
-1232
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import { revalidateTag } from "next/cache";
|
||||
|
||||
import { cacheTags } from "./cache-tags";
|
||||
import { invalidateIntegrationDataCache } from "./integration-data-cache";
|
||||
|
||||
export const invalidateBoardCache = (boardId: string, boardName?: string) => {
|
||||
revalidateTag(cacheTags.board(boardId), "max");
|
||||
if (boardName) revalidateTag(cacheTags.boardByName(boardName), "max");
|
||||
revalidateTag(cacheTags.boardList(), "max");
|
||||
};
|
||||
|
||||
export const invalidateIntegrationCache = (integrationId: string) => {
|
||||
invalidateIntegrationDataCache(integrationId);
|
||||
revalidateTag(cacheTags.integration(integrationId), "max");
|
||||
};
|
||||
|
||||
export const invalidateUserCache = (userId: string) => {
|
||||
revalidateTag(cacheTags.user(userId), "max");
|
||||
};
|
||||
|
||||
export const invalidateServerSettingsCache = () => {
|
||||
revalidateTag(cacheTags.serverSettings(), "max");
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { cacheTags } from "./cache-tags";
|
||||
|
||||
describe("cacheTags", () => {
|
||||
it("produces stable tag shapes", () => {
|
||||
expect(cacheTags.serverSettings()).toBe("server-settings");
|
||||
expect(cacheTags.board("abc")).toBe("board:abc");
|
||||
expect(cacheTags.boardByName("My Board")).toBe("board-name:MY BOARD");
|
||||
expect(cacheTags.boardList()).toBe("board-list");
|
||||
expect(cacheTags.user("u1")).toBe("user:u1");
|
||||
expect(cacheTags.integration("i1")).toBe("integration:i1");
|
||||
expect(cacheTags.integrationData("i1", "downloads")).toBe("integration-data:i1:downloads");
|
||||
expect(cacheTags.app("a1")).toBe("app:a1");
|
||||
});
|
||||
|
||||
it("boardByName uppercases consistently", () => {
|
||||
expect(cacheTags.boardByName("home")).toBe(cacheTags.boardByName("HOME"));
|
||||
expect(cacheTags.boardByName("Home")).toBe(cacheTags.boardByName("hOmE"));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,10 @@
|
||||
export const cacheTags = {
|
||||
serverSettings: () => "server-settings" as const,
|
||||
board: (boardId: string) => `board:${boardId}` as const,
|
||||
boardByName: (name: string) => `board-name:${name.toUpperCase()}` as const,
|
||||
boardList: () => "board-list" as const,
|
||||
user: (userId: string) => `user:${userId}` as const,
|
||||
integration: (integrationId: string) => `integration:${integrationId}` as const,
|
||||
integrationData: (integrationId: string, query: string) => `integration-data:${integrationId}:${query}` as const,
|
||||
app: (appId: string) => `app:${appId}` as const,
|
||||
} as const;
|
||||
@@ -0,0 +1,80 @@
|
||||
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { getCachedIntegrationData, invalidateIntegrationDataCache } from "./integration-data-cache";
|
||||
|
||||
afterEach(() => {
|
||||
invalidateIntegrationDataCache("integration-a");
|
||||
invalidateIntegrationDataCache("integration-b");
|
||||
});
|
||||
|
||||
describe("getCachedIntegrationData", () => {
|
||||
test("returns cached data within TTL without refetching", async () => {
|
||||
const fetcher = vi.fn(async () => ({ value: 1 }));
|
||||
|
||||
await expect(getCachedIntegrationData("integration-a", "widget:procedure", fetcher, 60_000)).resolves.toEqual({
|
||||
value: 1,
|
||||
});
|
||||
await expect(getCachedIntegrationData("integration-a", "widget:procedure", fetcher, 60_000)).resolves.toEqual({
|
||||
value: 1,
|
||||
});
|
||||
|
||||
expect(fetcher).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("serves stale data while revalidating after TTL", async () => {
|
||||
vi.useFakeTimers();
|
||||
const fetcher = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ value: 1 })
|
||||
.mockImplementationOnce(() => new Promise((resolve) => setTimeout(() => resolve({ value: 2 }), 100)));
|
||||
|
||||
await getCachedIntegrationData("integration-a", "widget:stale", fetcher, 50);
|
||||
vi.advanceTimersByTime(60);
|
||||
|
||||
const staleResult = await getCachedIntegrationData("integration-a", "widget:stale", fetcher, 50);
|
||||
expect(staleResult).toEqual({ value: 1 });
|
||||
expect(fetcher).toHaveBeenCalledTimes(2);
|
||||
|
||||
vi.advanceTimersByTime(100);
|
||||
await expect(getCachedIntegrationData("integration-a", "widget:stale", fetcher, 50)).resolves.toEqual({ value: 2 });
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
test("returns stale data when refetch fails", async () => {
|
||||
vi.useFakeTimers();
|
||||
const fetcher = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce({ value: 1 })
|
||||
.mockRejectedValueOnce(new Error("upstream unavailable"));
|
||||
|
||||
await getCachedIntegrationData("integration-a", "widget:error", fetcher, 50);
|
||||
vi.advanceTimersByTime(60);
|
||||
|
||||
await expect(getCachedIntegrationData("integration-a", "widget:error", fetcher, 50)).resolves.toEqual({
|
||||
value: 1,
|
||||
});
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
test("invalidates all cache entries for an integration", async () => {
|
||||
const fetcherOne = vi.fn(async () => ({ value: "one" }));
|
||||
const fetcherTwo = vi.fn(async () => ({ value: "two" }));
|
||||
const fetcherOther = vi.fn(async () => ({ value: "other" }));
|
||||
|
||||
await getCachedIntegrationData("integration-a", "widget:one", fetcherOne);
|
||||
await getCachedIntegrationData("integration-a", "widget:two", fetcherTwo);
|
||||
await getCachedIntegrationData("integration-b", "widget:one", fetcherOther);
|
||||
|
||||
invalidateIntegrationDataCache("integration-a");
|
||||
|
||||
await getCachedIntegrationData("integration-a", "widget:one", fetcherOne);
|
||||
await getCachedIntegrationData("integration-a", "widget:two", fetcherTwo);
|
||||
await getCachedIntegrationData("integration-b", "widget:one", fetcherOther);
|
||||
|
||||
expect(fetcherOne).toHaveBeenCalledTimes(2);
|
||||
expect(fetcherTwo).toHaveBeenCalledTimes(2);
|
||||
expect(fetcherOther).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
// ponytail: module-level cache — one snapshot per integration per query key.
|
||||
// Ceiling: stale data for up to `ttlMs` (60s default). Upgrade: Redis-backed with pub/sub invalidation.
|
||||
|
||||
interface CachedEntry<T> {
|
||||
data: T;
|
||||
timestamp: number;
|
||||
pending?: Promise<T>;
|
||||
}
|
||||
|
||||
const cache = new Map<string, CachedEntry<unknown>>();
|
||||
const DEFAULT_TTL_MS = 60_000;
|
||||
const MAX_ENTRIES = 200;
|
||||
|
||||
const cacheKey = (integrationId: string, queryKey: string) => `${integrationId}:${queryKey}`;
|
||||
|
||||
export const getCachedIntegrationData = async <T>(
|
||||
integrationId: string,
|
||||
queryKey: string,
|
||||
fetcher: () => Promise<T>,
|
||||
ttlMs = DEFAULT_TTL_MS,
|
||||
): Promise<T> => {
|
||||
const key = cacheKey(integrationId, queryKey);
|
||||
const existing = cache.get(key) as CachedEntry<T> | undefined;
|
||||
const now = Date.now();
|
||||
|
||||
if (existing) {
|
||||
if (now - existing.timestamp < ttlMs) {
|
||||
return existing.data;
|
||||
}
|
||||
if (existing.pending) {
|
||||
return existing.pending;
|
||||
}
|
||||
}
|
||||
|
||||
const pending = fetcher()
|
||||
.then((data) => {
|
||||
cache.set(key, { data, timestamp: Date.now() });
|
||||
|
||||
if (cache.size > MAX_ENTRIES) {
|
||||
let oldest: string | undefined;
|
||||
let oldestTime = Infinity;
|
||||
for (const [k, v] of cache) {
|
||||
if (v.timestamp < oldestTime) {
|
||||
oldestTime = v.timestamp;
|
||||
oldest = k;
|
||||
}
|
||||
}
|
||||
if (oldest) cache.delete(oldest);
|
||||
}
|
||||
|
||||
return data;
|
||||
})
|
||||
.catch((error) => {
|
||||
const entry = cache.get(key) as CachedEntry<T> | undefined;
|
||||
if (entry) {
|
||||
delete entry.pending;
|
||||
if (entry.timestamp > 0) return entry.data;
|
||||
}
|
||||
cache.delete(key);
|
||||
throw error;
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
existing.pending = pending;
|
||||
return existing.data;
|
||||
}
|
||||
|
||||
cache.set(key, { data: undefined as T, timestamp: 0, pending });
|
||||
return pending;
|
||||
};
|
||||
|
||||
export const invalidateIntegrationDataCache = (integrationId: string) => {
|
||||
for (const [key] of cache) {
|
||||
if (key.startsWith(`${integrationId}:`)) {
|
||||
cache.delete(key);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -5,7 +5,7 @@ import type { Database } from "@homarr/db";
|
||||
import { eq, or } from "@homarr/db";
|
||||
import { items } from "@homarr/db/schema";
|
||||
|
||||
import type { WidgetComponentProps } from "../../../../widgets/src";
|
||||
import type { WidgetComponentProps } from "../../../../widgets/src/definition";
|
||||
|
||||
export class AppAccessControl {
|
||||
constructor(
|
||||
|
||||
@@ -59,6 +59,7 @@ import { zodUnionFromArray } from "@homarr/validation/enums";
|
||||
import type { BoardItemAdvancedOptions } from "@homarr/validation/shared";
|
||||
import { sectionSchema, sharedItemSchema } from "@homarr/validation/shared";
|
||||
|
||||
import { invalidateBoardCache, invalidateUserCache } from "../cache-invalidation";
|
||||
import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure, publicProcedure } from "../trpc";
|
||||
import { throwIfActionForbiddenAsync } from "./board/board-access";
|
||||
import { generateResponsiveGridFor } from "./board/grid-algorithm";
|
||||
@@ -327,6 +328,8 @@ export const boardRouter = createTRPCRouter({
|
||||
await ctx.db.update(users).set({ homeBoardId: boardId }).where(eq(users.id, ctx.session.user.id));
|
||||
}
|
||||
|
||||
invalidateBoardCache(boardId, input.name);
|
||||
|
||||
return { boardId };
|
||||
}),
|
||||
duplicateBoard: permissionRequiredProcedure
|
||||
@@ -555,6 +558,8 @@ export const boardRouter = createTRPCRouter({
|
||||
},
|
||||
});
|
||||
|
||||
invalidateBoardCache(newBoardId, input.name);
|
||||
|
||||
return { boardId: newBoardId };
|
||||
}),
|
||||
renameBoard: protectedProcedure
|
||||
@@ -573,7 +578,17 @@ export const boardRouter = createTRPCRouter({
|
||||
|
||||
await noBoardWithSimilarNameAsync(ctx.db, input.name, [input.id]);
|
||||
|
||||
const existingBoard = await ctx.db.query.boards.findFirst({
|
||||
where: eq(boards.id, input.id),
|
||||
columns: { name: true },
|
||||
});
|
||||
|
||||
await ctx.db.update(boards).set({ name: input.name }).where(eq(boards.id, input.id));
|
||||
|
||||
invalidateBoardCache(input.id, input.name);
|
||||
if (existingBoard && existingBoard.name !== input.name) {
|
||||
invalidateBoardCache(input.id, existingBoard.name);
|
||||
}
|
||||
}),
|
||||
changeBoardVisibility: protectedProcedure
|
||||
.meta({
|
||||
@@ -604,6 +619,8 @@ export const boardRouter = createTRPCRouter({
|
||||
.update(boards)
|
||||
.set({ isPublic: input.visibility === "public" })
|
||||
.where(eq(boards.id, input.id));
|
||||
|
||||
invalidateBoardCache(input.id);
|
||||
}),
|
||||
deleteBoard: protectedProcedure
|
||||
.meta({
|
||||
@@ -619,7 +636,14 @@ export const boardRouter = createTRPCRouter({
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "full");
|
||||
|
||||
const existingBoard = await ctx.db.query.boards.findFirst({
|
||||
where: eq(boards.id, input.id),
|
||||
columns: { name: true },
|
||||
});
|
||||
|
||||
await ctx.db.delete(boards).where(eq(boards.id, input.id));
|
||||
|
||||
invalidateBoardCache(input.id, existingBoard?.name);
|
||||
}),
|
||||
setHomeBoard: protectedProcedure
|
||||
.meta({
|
||||
@@ -636,6 +660,8 @@ export const boardRouter = createTRPCRouter({
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "view");
|
||||
|
||||
await ctx.db.update(users).set({ homeBoardId: input.id }).where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
setMobileHomeBoard: protectedProcedure
|
||||
.meta({
|
||||
@@ -652,6 +678,8 @@ export const boardRouter = createTRPCRouter({
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "view");
|
||||
|
||||
await ctx.db.update(users).set({ mobileHomeBoardId: input.id }).where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
getHomeBoard: publicProcedure.query(async ({ ctx }) => {
|
||||
const userId = ctx.session?.user.id;
|
||||
@@ -806,6 +834,8 @@ export const boardRouter = createTRPCRouter({
|
||||
if (removedLayoutIds.length > 0) {
|
||||
await ctx.db.delete(layouts).where(inArray(layouts.id, removedLayoutIds));
|
||||
}
|
||||
|
||||
invalidateBoardCache(board.id, board.name);
|
||||
}),
|
||||
savePartialBoardSettings: protectedProcedure
|
||||
.meta({
|
||||
@@ -850,6 +880,8 @@ export const boardRouter = createTRPCRouter({
|
||||
disableStatus: input.disableStatus,
|
||||
})
|
||||
.where(eq(boards.id, input.id));
|
||||
|
||||
invalidateBoardCache(input.id);
|
||||
}),
|
||||
saveBoard: protectedProcedure.input(boardSaveSchema).mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "modify");
|
||||
@@ -1266,6 +1298,8 @@ export const boardRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
invalidateBoardCache(dbBoard.id, dbBoard.name);
|
||||
}),
|
||||
getBoardPermissions: protectedProcedure.input(byIdSchema).query(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.id), "full");
|
||||
@@ -1342,6 +1376,11 @@ export const boardRouter = createTRPCRouter({
|
||||
saveUserBoardPermissions: protectedProcedure.input(boardSavePermissionsSchema).mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.entityId), "full");
|
||||
|
||||
const existingUserPermissions = await ctx.db.query.boardUserPermissions.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(boardUserPermissions.boardId, input.entityId),
|
||||
});
|
||||
|
||||
await handleTransactionsAsync(ctx.db, {
|
||||
async handleAsync(db, schema) {
|
||||
await db.transaction(async (transaction) => {
|
||||
@@ -1377,10 +1416,25 @@ export const boardRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const userIds = [
|
||||
...new Set([
|
||||
...existingUserPermissions.map((permission) => permission.userId),
|
||||
...input.permissions.map((permission) => permission.principalId),
|
||||
]),
|
||||
];
|
||||
for (const userId of userIds) {
|
||||
invalidateUserCache(userId);
|
||||
}
|
||||
}),
|
||||
saveGroupBoardPermissions: protectedProcedure.input(boardSavePermissionsSchema).mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.entityId), "full");
|
||||
|
||||
const existingGroupPermissions = await ctx.db.query.boardGroupPermissions.findMany({
|
||||
columns: { groupId: true },
|
||||
where: eq(boardGroupPermissions.boardId, input.entityId),
|
||||
});
|
||||
|
||||
await handleTransactionsAsync(ctx.db, {
|
||||
async handleAsync(db, schema) {
|
||||
await db.transaction(async (transaction) => {
|
||||
@@ -1418,6 +1472,22 @@ export const boardRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const groupIds = [
|
||||
...new Set([
|
||||
...existingGroupPermissions.map((permission) => permission.groupId),
|
||||
...input.permissions.map((permission) => permission.principalId),
|
||||
]),
|
||||
];
|
||||
if (groupIds.length > 0) {
|
||||
const groupMembersForBoard = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: inArray(groupMembers.groupId, groupIds),
|
||||
});
|
||||
for (const member of groupMembersForBoard) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}
|
||||
}),
|
||||
importOldmarrConfig: permissionRequiredProcedure
|
||||
.requiresPermission("board-create")
|
||||
@@ -1554,6 +1624,8 @@ export const boardRouter = createTRPCRouter({
|
||||
.values(input.integrationIds.map((integrationId) => ({ itemId, integrationId })));
|
||||
}
|
||||
|
||||
invalidateBoardCache(input.boardId, board.name);
|
||||
|
||||
return { itemId };
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
} from "@homarr/validation/group";
|
||||
|
||||
import { createTRPCRouter, onboardingProcedure, permissionRequiredProcedure, protectedProcedure } from "../trpc";
|
||||
import { invalidateUserCache } from "../cache-invalidation";
|
||||
import { nextOnboardingStepAsync } from "./onboard/onboard-queries";
|
||||
|
||||
export const groupRouter = createTRPCRouter({
|
||||
@@ -245,6 +246,14 @@ export const groupRouter = createTRPCRouter({
|
||||
mobileHomeBoardId: input.settings.mobileHomeBoardId,
|
||||
})
|
||||
.where(eq(groups.id, input.id));
|
||||
|
||||
const members = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(groupMembers.groupId, input.id),
|
||||
});
|
||||
for (const member of members) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}),
|
||||
savePositions: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -275,6 +284,11 @@ export const groupRouter = createTRPCRouter({
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
await throwIfGroupNotFoundAsync(ctx.db, input.groupId);
|
||||
|
||||
const members = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(groupMembers.groupId, input.groupId),
|
||||
});
|
||||
|
||||
await ctx.db.delete(groupPermissions).where(eq(groupPermissions.groupId, input.groupId));
|
||||
|
||||
if (input.permissions.length > 0) {
|
||||
@@ -285,6 +299,10 @@ export const groupRouter = createTRPCRouter({
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
for (const member of members) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}),
|
||||
transferOwnership: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -307,7 +325,16 @@ export const groupRouter = createTRPCRouter({
|
||||
await throwIfGroupNotFoundAsync(ctx.db, input.id);
|
||||
await throwIfGroupNameIsReservedAsync(ctx.db, input.id);
|
||||
|
||||
const members = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(groupMembers.groupId, input.id),
|
||||
});
|
||||
|
||||
await ctx.db.delete(groups).where(eq(groups.id, input.id));
|
||||
|
||||
for (const member of members) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}),
|
||||
addMember: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -339,6 +366,8 @@ export const groupRouter = createTRPCRouter({
|
||||
groupId: input.groupId,
|
||||
userId: input.userId,
|
||||
});
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
}),
|
||||
removeMember: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -351,6 +380,8 @@ export const groupRouter = createTRPCRouter({
|
||||
await ctx.db
|
||||
.delete(groupMembers)
|
||||
.where(and(eq(groupMembers.groupId, input.groupId), eq(groupMembers.userId, input.userId)));
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -37,6 +37,7 @@ import {
|
||||
import { mediaRequestOptionsSchema, mediaRequestRequestSchema } from "@homarr/validation/widgets/media-request";
|
||||
|
||||
import { createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { invalidateIntegrationCache, invalidateUserCache } from "../../cache-invalidation";
|
||||
import { createTRPCRouter, permissionRequiredProcedure, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
import { throwIfActionForbiddenAsync } from "./integration-access";
|
||||
import { MissingSecretError, testConnectionAsync } from "./integration-test-connection";
|
||||
@@ -368,6 +369,8 @@ export const integrationRouter = createTRPCRouter({
|
||||
kind: input.kind,
|
||||
url: input.url,
|
||||
});
|
||||
|
||||
invalidateIntegrationCache(integrationId);
|
||||
}),
|
||||
update: protectedProcedure.input(integrationUpdateSchema).mutation(async ({ ctx, input }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(integrations.id, input.id), "full");
|
||||
@@ -474,6 +477,7 @@ export const integrationRouter = createTRPCRouter({
|
||||
// Invalidate all cached data for this integration so that widgets pick up the
|
||||
// new configuration immediately instead of serving stale (or errored) data.
|
||||
await invalidateIntegrationCacheAsync(input.id);
|
||||
invalidateIntegrationCache(input.id);
|
||||
}),
|
||||
delete: protectedProcedure
|
||||
.meta({
|
||||
@@ -498,6 +502,7 @@ export const integrationRouter = createTRPCRouter({
|
||||
|
||||
// Clean up any cached data left behind by the deleted integration.
|
||||
await invalidateIntegrationCacheAsync(input.id);
|
||||
invalidateIntegrationCache(input.id);
|
||||
}),
|
||||
getIntegrationPermissions: protectedProcedure.input(byIdSchema).query(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(integrations.id, input.id), "full");
|
||||
@@ -576,6 +581,11 @@ export const integrationRouter = createTRPCRouter({
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(integrations.id, input.entityId), "full");
|
||||
|
||||
const existingUserPermissions = await ctx.db.query.integrationUserPermissions.findMany({
|
||||
columns: { userId: true },
|
||||
where: eq(integrationUserPermissions.integrationId, input.entityId),
|
||||
});
|
||||
|
||||
await handleTransactionsAsync(ctx.db, {
|
||||
async handleAsync(db, schema) {
|
||||
await ctx.db.transaction(async (transaction) => {
|
||||
@@ -616,12 +626,28 @@ export const integrationRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const userIds = [
|
||||
...new Set([
|
||||
...existingUserPermissions.map((permission) => permission.userId),
|
||||
...input.permissions.map((permission) => permission.principalId),
|
||||
]),
|
||||
];
|
||||
for (const userId of userIds) {
|
||||
invalidateUserCache(userId);
|
||||
}
|
||||
invalidateIntegrationCache(input.entityId);
|
||||
}),
|
||||
saveGroupIntegrationPermissions: protectedProcedure
|
||||
.input(integrationSavePermissionsSchema)
|
||||
.mutation(async ({ input, ctx }) => {
|
||||
await throwIfActionForbiddenAsync(ctx, eq(integrations.id, input.entityId), "full");
|
||||
|
||||
const existingGroupPermissions = await ctx.db.query.integrationGroupPermissions.findMany({
|
||||
columns: { groupId: true },
|
||||
where: eq(integrationGroupPermissions.integrationId, input.entityId),
|
||||
});
|
||||
|
||||
await handleTransactionsAsync(ctx.db, {
|
||||
async handleAsync(db, schema) {
|
||||
await db.transaction(async (transaction) => {
|
||||
@@ -662,6 +688,23 @@ export const integrationRouter = createTRPCRouter({
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const groupIds = [
|
||||
...new Set([
|
||||
...existingGroupPermissions.map((permission) => permission.groupId),
|
||||
...input.permissions.map((permission) => permission.principalId),
|
||||
]),
|
||||
];
|
||||
if (groupIds.length > 0) {
|
||||
const members = await ctx.db.query.groupMembers.findMany({
|
||||
columns: { userId: true },
|
||||
where: inArray(groupMembers.groupId, groupIds),
|
||||
});
|
||||
for (const member of members) {
|
||||
invalidateUserCache(member.userId);
|
||||
}
|
||||
}
|
||||
invalidateIntegrationCache(input.entityId);
|
||||
}),
|
||||
searchInIntegration: protectedProcedure
|
||||
.meta({
|
||||
|
||||
@@ -4,6 +4,7 @@ import { z } from "zod/v4";
|
||||
import { and, eq } from "@homarr/db";
|
||||
import { sectionCollapseStates, sections } from "@homarr/db/schema";
|
||||
|
||||
import { invalidateUserCache } from "../../cache-invalidation";
|
||||
import { createTRPCRouter, protectedProcedure } from "../../trpc";
|
||||
|
||||
export const sectionRouter = createTRPCRouter({
|
||||
@@ -37,6 +38,7 @@ export const sectionRouter = createTRPCRouter({
|
||||
userId: ctx.session.user.id,
|
||||
collapsed: input.collapsed,
|
||||
});
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -48,5 +50,7 @@ export const sectionRouter = createTRPCRouter({
|
||||
.where(
|
||||
and(eq(sectionCollapseStates.sectionId, section.id), eq(sectionCollapseStates.userId, ctx.session.user.id)),
|
||||
);
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
});
|
||||
@@ -14,6 +14,7 @@ import { defaultServerSettingsKeys } from "@homarr/server-settings";
|
||||
import { settingsInitSchema } from "@homarr/validation/settings";
|
||||
|
||||
import { createTRPCRouter, onboardingProcedure, permissionRequiredProcedure, publicProcedure } from "../trpc";
|
||||
import { invalidateServerSettingsCache } from "../cache-invalidation";
|
||||
import { nextOnboardingStepAsync } from "./onboard/onboard-queries";
|
||||
|
||||
const boardServerSettingsSchema = z.object({
|
||||
@@ -91,6 +92,8 @@ export const serverSettingsRouter = createTRPCRouter({
|
||||
await insertServerSettingByKeyAsync(ctx.db, "board", next);
|
||||
}
|
||||
|
||||
invalidateServerSettingsCache();
|
||||
|
||||
return next;
|
||||
}),
|
||||
saveSettings: permissionRequiredProcedure
|
||||
@@ -107,6 +110,8 @@ export const serverSettingsRouter = createTRPCRouter({
|
||||
...current,
|
||||
...input.value,
|
||||
} as ServerSettings[keyof ServerSettings]);
|
||||
|
||||
invalidateServerSettingsCache();
|
||||
}),
|
||||
initSettings: onboardingProcedure
|
||||
.requiresStep("settings")
|
||||
@@ -116,5 +121,7 @@ export const serverSettingsRouter = createTRPCRouter({
|
||||
await updateServerSettingByKeyAsync(ctx.db, "analytics", { ...currentAnalytics, ...input.analytics });
|
||||
await updateServerSettingByKeyAsync(ctx.db, "crawlingAndIndexing", input.crawlingAndIndexing);
|
||||
await nextOnboardingStepAsync(ctx.db, undefined);
|
||||
|
||||
invalidateServerSettingsCache();
|
||||
}),
|
||||
});
|
||||
@@ -31,6 +31,7 @@ import {
|
||||
} from "@homarr/validation/user";
|
||||
|
||||
import { convertIntersectionToZodObject } from "../schema-merger";
|
||||
import { invalidateUserCache } from "../cache-invalidation";
|
||||
import {
|
||||
createTRPCRouter,
|
||||
isDemoMode,
|
||||
@@ -221,6 +222,8 @@ export const userRouter = createTRPCRouter({
|
||||
image: input.image,
|
||||
})
|
||||
.where(eq(users.id, input.userId));
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
}),
|
||||
getAll: permissionRequiredProcedure
|
||||
.requiresPermission("admin")
|
||||
@@ -452,6 +455,8 @@ export const userRouter = createTRPCRouter({
|
||||
emailVerified: emailDirty === true ? null : undefined,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
delete: protectedProcedure
|
||||
.input(z.object({ userId: z.string() }))
|
||||
@@ -598,6 +603,8 @@ export const userRouter = createTRPCRouter({
|
||||
mobileHomeBoardId: input.mobileHomeBoardId,
|
||||
})
|
||||
.where(eq(users.id, input.userId));
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
}),
|
||||
changeDefaultSearchEngine: protectedProcedure
|
||||
.input(
|
||||
@@ -653,6 +660,8 @@ export const userRouter = createTRPCRouter({
|
||||
colorScheme: input.colorScheme,
|
||||
})
|
||||
.where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
changeEnableRightClickOnWidgets: protectedProcedure
|
||||
.meta({
|
||||
@@ -679,6 +688,8 @@ export const userRouter = createTRPCRouter({
|
||||
enableRightClickOnWidgets: input.enableRightClickOnWidgets,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
changePingIconsEnabled: protectedProcedure
|
||||
.input(userPingIconsEnabledSchema.and(byIdSchema))
|
||||
@@ -697,6 +708,8 @@ export const userRouter = createTRPCRouter({
|
||||
pingIconsEnabled: input.pingIconsEnabled,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
changeDdgBangs: protectedProcedure
|
||||
.input(convertIntersectionToZodObject(userDdgBangsSchema.and(byIdSchema)))
|
||||
@@ -724,6 +737,8 @@ export const userRouter = createTRPCRouter({
|
||||
ddgBangs: input.ddgBangs,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
changeFirstDayOfWeek: protectedProcedure
|
||||
.input(convertIntersectionToZodObject(userFirstDayOfWeekSchema.and(byIdSchema)))
|
||||
@@ -765,6 +780,8 @@ export const userRouter = createTRPCRouter({
|
||||
firstDayOfWeek: input.firstDayOfWeek,
|
||||
})
|
||||
.where(eq(users.id, input.id));
|
||||
|
||||
invalidateUserCache(input.id);
|
||||
}),
|
||||
completeTour: protectedProcedure
|
||||
.input(z.object({ tour: z.enum(["manage", "board"]) }))
|
||||
@@ -775,6 +792,8 @@ export const userRouter = createTRPCRouter({
|
||||
} as const;
|
||||
|
||||
await ctx.db.update(users).set(columnByTour[input.tour]).where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
resetTours: protectedProcedure.mutation(async ({ ctx }) => {
|
||||
await ctx.db
|
||||
@@ -784,6 +803,8 @@ export const userRouter = createTRPCRouter({
|
||||
completedBoardTour: false,
|
||||
})
|
||||
.where(eq(users.id, ctx.session.user.id));
|
||||
|
||||
invalidateUserCache(ctx.session.user.id);
|
||||
}),
|
||||
getTourStatus: protectedProcedure.query(async ({ ctx }) => {
|
||||
if (isDemoMode) {
|
||||
|
||||
@@ -8,6 +8,8 @@ import type { Database } from "@homarr/db";
|
||||
import { users } from "@homarr/db/schema";
|
||||
import { userChangeSearchPreferencesSchema } from "@homarr/validation/user";
|
||||
|
||||
import { invalidateUserCache } from "../../cache-invalidation";
|
||||
|
||||
export const changeSearchPreferencesInputSchema = userChangeSearchPreferencesSchema.and(
|
||||
z.object({ userId: z.string() }),
|
||||
);
|
||||
@@ -51,4 +53,6 @@ export const changeSearchPreferencesAsync = async (
|
||||
ddgBangs: input.ddgBangsEnabled,
|
||||
})
|
||||
.where(eq(users.id, input.userId));
|
||||
|
||||
invalidateUserCache(input.userId);
|
||||
};
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
beszelSystemsRequestHandler,
|
||||
} from "@homarr/request-handler/beszel";
|
||||
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
import { BoundedAsyncQueue } from "./bounded-async-queue";
|
||||
@@ -47,6 +47,7 @@ export const beszelRouter = createTRPCRouter({
|
||||
};
|
||||
},
|
||||
{
|
||||
queryKey: integrationQueryKey("beszel", "getSystems"),
|
||||
fallback: (integration, error) => ({
|
||||
integrationId: integration.id,
|
||||
integrationName: integration.name,
|
||||
@@ -119,6 +120,10 @@ export const beszelRouter = createTRPCRouter({
|
||||
};
|
||||
},
|
||||
{
|
||||
queryKey: integrationQueryKey("beszel", "getAlerts", {
|
||||
includeHistory: input.includeHistory,
|
||||
maxHistoryItems: input.maxHistoryItems,
|
||||
}),
|
||||
fallback: (integration, error) => ({
|
||||
integrationId: integration.id,
|
||||
integrationName: integration.name,
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { coolifyRequestHandler } from "@homarr/request-handler/coolify";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const coolifyRouter = createTRPCRouter({
|
||||
getInstancesInfo: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", "coolify"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = coolifyRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -19,6 +21,8 @@ export const coolifyRouter = createTRPCRouter({
|
||||
instanceInfo: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("coolify", "getInstancesInfo") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -5,7 +5,7 @@ import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import { dnsHoleRequestHandler } from "@homarr/request-handler/dns-hole";
|
||||
|
||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
export const dnsHoleRouter = createTRPCRouter({
|
||||
@@ -19,13 +19,17 @@ export const dnsHoleRouter = createTRPCRouter({
|
||||
})
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("dnsHole")))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await dnsHoleRequestHandler.handler(integration, {}).getDataAsync();
|
||||
return {
|
||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: timestamp },
|
||||
summary: data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("dns-hole", "summary") },
|
||||
);
|
||||
}),
|
||||
|
||||
enable: protectedProcedure
|
||||
|
||||
@@ -6,7 +6,7 @@ import { downloadClientRequestHandler } from "@homarr/request-handler/downloads"
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
const createDownloadClientIntegrationMiddleware = (action: IntegrationAction) =>
|
||||
@@ -24,14 +24,18 @@ export const downloadsRouter = createTRPCRouter({
|
||||
.concat(createDownloadClientIntegrationMiddleware("query"))
|
||||
.input(z.object({ limitPerIntegration: z.number().default(50) }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = downloadClientRequestHandler.handler(integration, { limit: input.limitPerIntegration });
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
return {
|
||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: timestamp },
|
||||
data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("downloads", "getJobsAndStatuses", { limitPerIntegration: input.limitPerIntegration }) },
|
||||
);
|
||||
}),
|
||||
pause: protectedProcedure
|
||||
.meta({
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
} from "@homarr/request-handler/firewall";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const firewallMiddleware = createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("firewall"));
|
||||
@@ -18,20 +18,34 @@ const queryFirewall = <
|
||||
},
|
||||
>(
|
||||
handler: THandler,
|
||||
queryKey: string,
|
||||
) =>
|
||||
publicProcedure.concat(firewallMiddleware).query(async ({ ctx }) =>
|
||||
settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await handler.handler(integration, {}).getDataAsync();
|
||||
return {
|
||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: timestamp },
|
||||
summary: data as Awaited<ReturnType<ReturnType<THandler["handler"]>["getDataAsync"]>>["data"],
|
||||
};
|
||||
}),
|
||||
},
|
||||
{ queryKey },
|
||||
),
|
||||
);
|
||||
|
||||
export const firewallRouter = createTRPCRouter({
|
||||
getFirewallCpuStatus: queryFirewall(firewallCpuRequestHandler),
|
||||
getFirewallInterfacesStatus: queryFirewall(firewallInterfacesRequestHandler),
|
||||
getFirewallVersionStatus: queryFirewall(firewallVersionRequestHandler),
|
||||
getFirewallMemoryStatus: queryFirewall(firewallMemoryRequestHandler),
|
||||
getFirewallCpuStatus: queryFirewall(firewallCpuRequestHandler, integrationQueryKey("firewall", "getFirewallCpuStatus")),
|
||||
getFirewallInterfacesStatus: queryFirewall(
|
||||
firewallInterfacesRequestHandler,
|
||||
integrationQueryKey("firewall", "getFirewallInterfacesStatus"),
|
||||
),
|
||||
getFirewallVersionStatus: queryFirewall(
|
||||
firewallVersionRequestHandler,
|
||||
integrationQueryKey("firewall", "getFirewallVersionStatus"),
|
||||
),
|
||||
getFirewallMemoryStatus: queryFirewall(
|
||||
firewallMemoryRequestHandler,
|
||||
integrationQueryKey("firewall", "getFirewallMemoryStatus"),
|
||||
),
|
||||
});
|
||||
@@ -3,7 +3,7 @@ import { SynologyIntegration } from "@homarr/integrations";
|
||||
import { clusterInfoRequestHandler, systemInfoRequestHandler } from "@homarr/request-handler/health-monitoring";
|
||||
|
||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const healthMonitoringIntegrationKinds = [
|
||||
@@ -27,7 +27,9 @@ export const healthMonitoringRouter = createTRPCRouter({
|
||||
})
|
||||
.concat(createManyIntegrationMiddleware("query", ...healthMonitoringIntegrationKinds))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await systemInfoRequestHandler.handler(integration, {}).getDataAsync();
|
||||
return {
|
||||
integrationId: integration.id,
|
||||
@@ -35,7 +37,9 @@ export const healthMonitoringRouter = createTRPCRouter({
|
||||
healthInfo: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("health-monitoring", "getSystemHealthStatus") },
|
||||
);
|
||||
}),
|
||||
listStorageVolumes: publicProcedure
|
||||
.meta({
|
||||
|
||||
@@ -6,7 +6,7 @@ import { indexerManagerRequestHandler } from "@homarr/request-handler/indexer-ma
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
const createIndexerManagerIntegrationMiddleware = (action: IntegrationAction) =>
|
||||
@@ -16,7 +16,9 @@ export const indexerManagerRouter = createTRPCRouter({
|
||||
getIndexersStatus: publicProcedure
|
||||
.concat(createIndexerManagerIntegrationMiddleware("query"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = indexerManagerRequestHandler.handler(integration, {});
|
||||
const { data: indexers } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -24,7 +26,9 @@ export const indexerManagerRouter = createTRPCRouter({
|
||||
integrationId: integration.id,
|
||||
indexers,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("indexer-manager", "getIndexersStatus") },
|
||||
);
|
||||
}),
|
||||
testAllIndexers: protectedProcedure
|
||||
.concat(createIndexerManagerIntegrationMiddleware("interact"))
|
||||
|
||||
@@ -2,14 +2,16 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { mediaReleaseRequestHandler } from "@homarr/request-handler/media-release";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const mediaReleaseRouter = createTRPCRouter({
|
||||
getMediaReleases: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("mediaRelease")))
|
||||
.query(async ({ ctx }) => {
|
||||
const results = await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
const results = await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = mediaReleaseRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -22,7 +24,9 @@ export const mediaReleaseRouter = createTRPCRouter({
|
||||
},
|
||||
releases: data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("media-release", "getMediaReleases") },
|
||||
);
|
||||
return results.flatMap((result) =>
|
||||
result.releases.map((release) => ({
|
||||
...release,
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
import { mediaRequestStatsRequestHandler } from "@homarr/request-handler/media-request-stats";
|
||||
|
||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
|
||||
export const mediaRequestsRouter = createTRPCRouter({
|
||||
@@ -25,10 +25,14 @@ export const mediaRequestsRouter = createTRPCRouter({
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("mediaRequest")))
|
||||
.input(mediaRequestListInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const results = await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
const results = await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data } = await mediaRequestListRequestHandler.handler(integration, input).getDataAsync();
|
||||
return { integration: { id: integration.id, name: integration.name, kind: integration.kind }, data };
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("media-requests", "getLatestRequests", input) },
|
||||
);
|
||||
return results
|
||||
.flatMap(({ data, integration }) =>
|
||||
data.map((request) => ({
|
||||
@@ -57,10 +61,14 @@ export const mediaRequestsRouter = createTRPCRouter({
|
||||
})
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("mediaRequest")))
|
||||
.query(async ({ ctx }) => {
|
||||
const results = await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
const results = await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data } = await mediaRequestStatsRequestHandler.handler(integration, {}).getDataAsync();
|
||||
return { integration: { id: integration.id, name: integration.name, kind: integration.kind }, data };
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("media-requests", "getStats") },
|
||||
);
|
||||
return {
|
||||
stats: results.flatMap((result) => result.data.stats),
|
||||
users: results
|
||||
|
||||
@@ -5,7 +5,7 @@ import { mediaServerRequestHandler } from "@homarr/request-handler/media-server"
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const createMediaServerIntegrationMiddleware = (action: IntegrationAction) =>
|
||||
@@ -23,11 +23,15 @@ export const mediaServerRouter = createTRPCRouter({
|
||||
.concat(createMediaServerIntegrationMiddleware("query"))
|
||||
.input(z.object({ showOnlyPlaying: z.boolean() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data } = await mediaServerRequestHandler
|
||||
.handler(integration, { showOnlyPlaying: input.showOnlyPlaying })
|
||||
.getDataAsync();
|
||||
return { integrationId: integration.id, integrationKind: integration.kind, sessions: data };
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("media-server", "getCurrentStreams", { showOnlyPlaying: input.showOnlyPlaying }) },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -2,14 +2,16 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { networkControllerRequestHandler } from "@homarr/request-handler/network-controller";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const networkControllerRouter = createTRPCRouter({
|
||||
summary: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("networkController")))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = networkControllerRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -22,6 +24,8 @@ export const networkControllerRouter = createTRPCRouter({
|
||||
summary: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("network-controller", "summary") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -3,7 +3,7 @@ import { notificationsRequestHandler } from "@homarr/request-handler/notificatio
|
||||
|
||||
import type { IntegrationAction } from "../../middlewares/integration";
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const createNotificationsIntegrationMiddleware = (action: IntegrationAction) =>
|
||||
@@ -13,7 +13,9 @@ export const notificationsRouter = createTRPCRouter({
|
||||
getNotifications: publicProcedure
|
||||
.unstable_concat(createNotificationsIntegrationMiddleware("query"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = notificationsRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -26,6 +28,8 @@ export const notificationsRouter = createTRPCRouter({
|
||||
},
|
||||
data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("notifications", "getNotifications") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -6,7 +6,7 @@ import { eq } from "@homarr/db";
|
||||
import { getServerSettingsAsync } from "@homarr/db/queries";
|
||||
import { boards, items } from "@homarr/db/schema";
|
||||
|
||||
import type { WidgetOptionsSettings } from "../../../../widgets/src";
|
||||
import type { WidgetOptionsSettings } from "../../../../widgets/src/definition";
|
||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||
import { throwIfActionForbiddenAsync } from "../board/board-access";
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import { items, users } from "@homarr/db/schema";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { rssFeedsRequestHandler } from "@homarr/request-handler/rss-feeds";
|
||||
|
||||
import type { WidgetComponentProps } from "../../../../widgets/src";
|
||||
import type { WidgetComponentProps } from "../../../../widgets/src/definition";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const logger = createLogger({ module: "rssFeed" });
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
import { speedtestTrackerRequestHandler } from "@homarr/request-handler/speedtest-tracker";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const speedtestTrackerRouter = createTRPCRouter({
|
||||
getDashboard: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", "speedtestTracker"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = speedtestTrackerRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -19,6 +21,8 @@ export const speedtestTrackerRouter = createTRPCRouter({
|
||||
dashboard: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("speedtest-tracker", "getDashboard") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -1,12 +1,14 @@
|
||||
import { tracearrRequestHandler } from "@homarr/request-handler/tracearr";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const tracearrRouter = createTRPCRouter({
|
||||
getDashboard: publicProcedure.concat(createManyIntegrationMiddleware("query", "tracearr")).query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = tracearrRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -17,6 +19,8 @@ export const tracearrRouter = createTRPCRouter({
|
||||
dashboard: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("tracearr", "getDashboard") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
import { traefikRequestHandler } from "@homarr/request-handler/traefik";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const traefikRouter = createTRPCRouter({
|
||||
@@ -15,7 +15,9 @@ export const traefikRouter = createTRPCRouter({
|
||||
})
|
||||
.concat(createManyIntegrationMiddleware("query", "traefik"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = traefikRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -26,6 +28,8 @@ export const traefikRouter = createTRPCRouter({
|
||||
dashboard: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("traefik", "getDashboard") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
} from "@homarr/request-handler/umami";
|
||||
|
||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
const logger = createLogger({ module: "umami-router" });
|
||||
@@ -39,7 +39,9 @@ export const umamiRouter = createTRPCRouter({
|
||||
)
|
||||
.concat(createManyIntegrationMiddleware("query", "umami"))
|
||||
.query(async ({ ctx, input }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = umamiRequestHandler.handler(integration, {
|
||||
websiteId: input.websiteId,
|
||||
timeFrame: input.timeFrame,
|
||||
@@ -54,7 +56,15 @@ export const umamiRouter = createTRPCRouter({
|
||||
visitorStats: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{
|
||||
queryKey: integrationQueryKey("umami", "getVisitorStats", {
|
||||
websiteId: input.websiteId,
|
||||
timeFrame: input.timeFrame,
|
||||
eventName: input.eventName,
|
||||
}),
|
||||
},
|
||||
);
|
||||
}),
|
||||
|
||||
getEventNames: publicProcedure
|
||||
|
||||
@@ -2,14 +2,16 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { upsSummariesRequestHandler } from "@homarr/request-handler/ups";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const upsRouter = createTRPCRouter({
|
||||
getSummaries: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("ups")))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = upsSummariesRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -20,6 +22,8 @@ export const upsRouter = createTRPCRouter({
|
||||
summaries: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("ups", "getSummaries") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -1,14 +1,16 @@
|
||||
import { uptimeKumaRequestHandler } from "@homarr/request-handler/uptime-kuma";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const uptimeKumaRouter = createTRPCRouter({
|
||||
getDashboard: publicProcedure
|
||||
.concat(createManyIntegrationMiddleware("query", "uptimeKuma"))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const innerHandler = uptimeKumaRequestHandler.handler(integration, {});
|
||||
const { data, timestamp } = await innerHandler.getDataAsync();
|
||||
|
||||
@@ -19,6 +21,8 @@ export const uptimeKumaRouter = createTRPCRouter({
|
||||
dashboard: data,
|
||||
updatedAt: timestamp,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("uptime-kuma", "getDashboard") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -2,14 +2,16 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
||||
import { vpnSummaryHandler } from "@homarr/request-handler/vpn";
|
||||
|
||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { integrationQueryKey, settleIntegrationQueries } from "../../settle-integrations";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const vpnRouter = createTRPCRouter({
|
||||
getSummaries: publicProcedure
|
||||
.unstable_concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("vpn")))
|
||||
.query(async ({ ctx }) => {
|
||||
return await settleIntegrationQueries(ctx.integrations, async (integration) => {
|
||||
return await settleIntegrationQueries(
|
||||
ctx.integrations,
|
||||
async (integration) => {
|
||||
const { data, timestamp } = await vpnSummaryHandler.handler(integration, {}).getDataAsync();
|
||||
return {
|
||||
integration: {
|
||||
@@ -20,6 +22,8 @@ export const vpnRouter = createTRPCRouter({
|
||||
},
|
||||
summary: data,
|
||||
};
|
||||
});
|
||||
},
|
||||
{ queryKey: integrationQueryKey("vpn", "getSummaries") },
|
||||
);
|
||||
}),
|
||||
});
|
||||
@@ -1,8 +1,13 @@
|
||||
import "server-only";
|
||||
|
||||
import { cache } from "react";
|
||||
|
||||
import { db } from "@homarr/db";
|
||||
import { getServerSettingsAsync } from "@homarr/db/queries";
|
||||
|
||||
// One settings snapshot per RSC request, shared by the root layout, theme, and
|
||||
// widget prefetching. React invalidates this cache between requests.
|
||||
export const getRscServerSettingsAsync = cache(async () => await getServerSettingsAsync(db));
|
||||
// ponytail: React cache() dedupes within a single request render.
|
||||
// The `use cache` layer (when enabled) dedupes across requests.
|
||||
// Keep both: inner avoids repeated deserialization of cached payload per render.
|
||||
export const getRscServerSettingsAsync = cache(async () => {
|
||||
return await getServerSettingsAsync(db);
|
||||
});
|
||||
@@ -1,6 +1,8 @@
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
|
||||
import { getCachedIntegrationData } from "./integration-data-cache";
|
||||
|
||||
const logger = createLogger({ module: "settleIntegrations" });
|
||||
|
||||
interface IntegrationLike {
|
||||
@@ -11,14 +13,29 @@ interface IntegrationLike {
|
||||
|
||||
interface Options<TIntegration extends IntegrationLike, TResult> {
|
||||
fallback?: (integration: TIntegration, error: unknown) => TResult;
|
||||
/** Widget procedure key, e.g. `downloads:getJobsAndStatuses:{"limitPerIntegration":50}` */
|
||||
queryKey?: string;
|
||||
}
|
||||
|
||||
export const integrationQueryKey = (widget: string, procedure: string, params?: unknown) => {
|
||||
if (params === undefined) return `${widget}:${procedure}`;
|
||||
return `${widget}:${procedure}:${JSON.stringify(params)}`;
|
||||
};
|
||||
|
||||
export async function settleIntegrationQueries<TIntegration extends IntegrationLike, TResult>(
|
||||
integrations: TIntegration[],
|
||||
fn: (integration: TIntegration) => Promise<TResult>,
|
||||
options?: Options<TIntegration, TResult>,
|
||||
): Promise<TResult[]> {
|
||||
const settled = await Promise.allSettled(integrations.map(async (integration) => fn(integration)));
|
||||
const queryKey = options?.queryKey;
|
||||
const runQuery = async (integration: TIntegration) => {
|
||||
if (queryKey) {
|
||||
return getCachedIntegrationData(integration.id, queryKey, () => fn(integration));
|
||||
}
|
||||
return fn(integration);
|
||||
};
|
||||
|
||||
const settled = await Promise.allSettled(integrations.map(runQuery));
|
||||
const results: TResult[] = [];
|
||||
const errors: unknown[] = [];
|
||||
|
||||
|
||||
Reference in new issue
Block a user