Merge branch 'dev' into feat/docker-logs
Resolve docker.ts conflict by keeping dev improvements (host field, bestMatch icons, Promise.allSettled with logging, Podman-safe stats) alongside container log streaming and decode utilities.
This commit is contained in:
406 files changed
+54691
-14600
No files matched your search
@@ -0,0 +1,30 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import type { AnchorNote, AnchorNotesListInput, AnchorNoteSummary } from "@homarr/integrations";
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
|
||||
import { createCachedIntegrationRequestHandler } from "./lib/cached-integration-request-handler";
|
||||
|
||||
export const anchorNotesListRequestHandler = createCachedIntegrationRequestHandler<
|
||||
AnchorNoteSummary[],
|
||||
"anchor",
|
||||
AnchorNotesListInput
|
||||
>({
|
||||
queryKey: "anchorNotesList",
|
||||
cacheDuration: dayjs.duration(15, "seconds"),
|
||||
async requestAsync(integration, input) {
|
||||
const instance = await createIntegrationAsync(integration);
|
||||
return instance.listNotesAsync(input);
|
||||
},
|
||||
});
|
||||
|
||||
export const anchorNoteRequestHandler = createCachedIntegrationRequestHandler<AnchorNote, "anchor", { noteId: string }>(
|
||||
{
|
||||
queryKey: "anchorNote",
|
||||
cacheDuration: dayjs.duration(15, "seconds"),
|
||||
async requestAsync(integration, input) {
|
||||
const instance = await createIntegrationAsync(integration);
|
||||
return instance.getNoteAsync(input.noteId);
|
||||
},
|
||||
},
|
||||
);
|
||||
@@ -0,0 +1,19 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import type { CoolifyInstanceInfo } from "@homarr/integrations/types";
|
||||
|
||||
import { createCachedIntegrationRequestHandler } from "./lib/cached-integration-request-handler";
|
||||
|
||||
export const coolifyRequestHandler = createCachedIntegrationRequestHandler<
|
||||
CoolifyInstanceInfo,
|
||||
"coolify",
|
||||
Record<string, never>
|
||||
>({
|
||||
async requestAsync(integration, _input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getInstanceInfoAsync();
|
||||
},
|
||||
cacheDuration: dayjs.duration(30, "seconds"),
|
||||
queryKey: "coolifyInfo",
|
||||
});
|
||||
@@ -1,7 +1,11 @@
|
||||
import type { Readable } from "node:stream";
|
||||
import dayjs from "dayjs";
|
||||
import type { Container, ContainerInfo, ContainerStats } from "dockerode";
|
||||
import type Dockerode from "dockerode";
|
||||
|
||||
import { bestMatch } from "@homarr/common";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { db, like, or } from "@homarr/db";
|
||||
import { icons } from "@homarr/db/schema";
|
||||
import type { ContainerState } from "@homarr/docker";
|
||||
@@ -10,6 +14,8 @@ import { dockerLabels, DockerSingleton } from "@homarr/docker";
|
||||
import { createDockerLogStreamProcessor, decodeDockerLogs } from "./docker-log-decode";
|
||||
import { createCachedWidgetRequestHandler } from "./lib/cached-widget-request-handler";
|
||||
|
||||
const logger = createLogger({ module: "dockerRequestHandler" });
|
||||
|
||||
export const dockerContainersRequestHandler = createCachedWidgetRequestHandler({
|
||||
queryKey: "dockerContainersResult",
|
||||
widgetKind: "dockerContainers",
|
||||
@@ -19,11 +25,10 @@ export const dockerContainersRequestHandler = createCachedWidgetRequestHandler({
|
||||
cacheDuration: dayjs.duration(20, "seconds"),
|
||||
});
|
||||
|
||||
const dockerInstances = DockerSingleton.getInstances();
|
||||
|
||||
const extractImage = (container: ContainerInfo) => container.Image.split("/").at(-1)?.split(":").at(0) ?? "";
|
||||
|
||||
const findContainerByIdAsync = async (id: string) => {
|
||||
const dockerInstances = DockerSingleton.getInstances();
|
||||
const containers = await Promise.all(
|
||||
dockerInstances.map(async ({ instance }) => {
|
||||
const container = instance.getContainer(id);
|
||||
@@ -78,11 +83,6 @@ export const streamContainerLogsAsync = async (
|
||||
follow: true,
|
||||
})) as Readable;
|
||||
|
||||
// Docker uses a multiplexed stream format for logs where each message has an 8-byte header:
|
||||
// - 1 byte: stream type (stdout/stderr)
|
||||
// - 3 bytes: padding
|
||||
// - 4 bytes: payload length (big-endian)
|
||||
// We need to process the stream in chunks and extract complete messages from the buffer.
|
||||
const MAX_MESSAGE_SIZE = 1024 * 1024;
|
||||
const processChunk = createDockerLogStreamProcessor(onData, onError, MAX_MESSAGE_SIZE);
|
||||
|
||||
@@ -106,14 +106,31 @@ export const streamContainerLogsAsync = async (
|
||||
};
|
||||
|
||||
async function getContainersWithStatsAsync() {
|
||||
const containers = await Promise.all(
|
||||
const dockerInstances = DockerSingleton.getInstances();
|
||||
const results = await Promise.allSettled(
|
||||
dockerInstances.map(async ({ instance, host }) => {
|
||||
const instanceContainers = await instance.listContainers({ all: true });
|
||||
return instanceContainers
|
||||
.filter((container) => !(dockerLabels.hide in container.Labels))
|
||||
.map((container) => ({ ...container, instance: host }));
|
||||
}),
|
||||
).then((res) => res.flat());
|
||||
);
|
||||
|
||||
const containers = results.flatMap((result, index) => {
|
||||
if (result.status === "fulfilled") return result.value;
|
||||
logger.warn(
|
||||
new ErrorWithMetadata(
|
||||
"Failed to list containers from Docker host",
|
||||
{
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
host: dockerInstances[index]!.host,
|
||||
},
|
||||
{ cause: result.reason },
|
||||
),
|
||||
);
|
||||
|
||||
return [];
|
||||
});
|
||||
const likeQueries = containers.map((container) => like(icons.name, `%${extractImage(container)}%`));
|
||||
|
||||
const dbIcons =
|
||||
@@ -127,8 +144,6 @@ async function getContainersWithStatsAsync() {
|
||||
const instance = dockerInstances.find(({ host }) => host === container.instance)?.instance;
|
||||
if (!instance) return null;
|
||||
|
||||
// Get stats, falling back to an empty stats object if fetch fails
|
||||
// calculateCpuUsage and calculateMemoryUsage will return 0 for invalid/missing stats
|
||||
const stats = await instance
|
||||
.getContainer(container.Id)
|
||||
.stats({ stream: false, "one-shot": true })
|
||||
@@ -146,27 +161,23 @@ async function getContainersWithStatsAsync() {
|
||||
return {
|
||||
id: container.Id,
|
||||
name: container.Names[0]?.split("/")[1] ?? "Unknown",
|
||||
host: container.instance,
|
||||
state: container.State as ContainerState,
|
||||
iconUrl:
|
||||
dbIcons.find((icon) => {
|
||||
const extractedImage = extractImage(container);
|
||||
if (!extractedImage) return false;
|
||||
return icon.name.toLowerCase().includes(extractedImage.toLowerCase());
|
||||
})?.url ?? null,
|
||||
iconUrl: bestMatch(extractImage(container), dbIcons, (icon) => icon.name)?.url ?? null,
|
||||
cpuUsage,
|
||||
memoryUsage,
|
||||
image: container.Image,
|
||||
// Docker Desktop on macOS can return null instead of an empty ports array
|
||||
ports: (container.Ports as typeof container.Ports | null) ?? [],
|
||||
ports: container.Ports as Dockerode.Port[] | undefined,
|
||||
};
|
||||
});
|
||||
|
||||
return (await Promise.all(containerStatsPromises)).filter((container) => container !== null);
|
||||
}
|
||||
|
||||
function calculateCpuUsage(stats: ContainerStats): number {
|
||||
// Handle containers with missing or invalid stats (e.g., exited, dead containers)
|
||||
if (!stats.cpu_stats.online_cpus || stats.cpu_stats.online_cpus === 0 || !stats.cpu_stats.cpu_usage.total_usage) {
|
||||
export function calculateCpuUsage(stats: ContainerStats): number {
|
||||
// Handle containers with missing or invalid stats (e.g., exited, dead containers, Podman responses)
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!stats.cpu_stats?.online_cpus || stats.cpu_stats.online_cpus === 0 || !stats.cpu_stats.cpu_usage?.total_usage) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -179,14 +190,13 @@ function calculateCpuUsage(stats: ContainerStats): number {
|
||||
return (stats.cpu_stats.cpu_usage.total_usage / usage) * numberOfCpus * 100;
|
||||
}
|
||||
|
||||
function calculateMemoryUsage(stats: ContainerStats): number {
|
||||
// Handle containers with missing or invalid stats (e.g., exited, dead containers)
|
||||
if (!stats.memory_stats.usage) {
|
||||
export function calculateMemoryUsage(stats: ContainerStats): number {
|
||||
// Handle containers with missing or invalid stats (e.g., exited, dead containers, Podman responses)
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!stats.memory_stats?.usage) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// memory usage by default includes cache, which should not be shown as it is also not shown with docker stats command
|
||||
// See https://docs.docker.com/reference/cli/docker/container/stats/ how it is / was calculated
|
||||
return (
|
||||
stats.memory_stats.usage -
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import dayjs from "dayjs";
|
||||
import { z } from "zod";
|
||||
|
||||
import { env } from "@homarr/common/env";
|
||||
import { ResponseError } from "@homarr/common/server";
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
||||
import { withTimeoutAsync } from "@homarr/core/infrastructure/http/timeout";
|
||||
import { createChannelWithLatestAndEvents } from "@homarr/redis";
|
||||
import { createCachedRequestHandler } from "@homarr/request-handler/lib/cached-request-handler";
|
||||
|
||||
export type DuckDuckGoBang = z.infer<typeof duckDuckGoBangSchema>;
|
||||
|
||||
const DUCKDUCKGO_BANGS_URL = "https://duckduckgo.com/bang.js";
|
||||
|
||||
/**
|
||||
* DuckDuckGo `bang.js` uses short keys (to reduce the payload size):
|
||||
* - `t`: bang token (e.g. `"yt"`)
|
||||
* - `s`: display name
|
||||
* - `d`: domain (optional)
|
||||
* - `u`: URL template (contains `{{{s}}}`)
|
||||
* - `c`: category (optional)
|
||||
* - `sc`: subcategory (optional)
|
||||
* - `r`: rank (optional)
|
||||
*/
|
||||
export const duckDuckGoBangSchema = z.object({
|
||||
t: z.string(),
|
||||
s: z.string(),
|
||||
d: z.string().optional(),
|
||||
u: z.string(),
|
||||
c: z.string().optional(),
|
||||
sc: z.string().optional(),
|
||||
r: z.number().optional(),
|
||||
});
|
||||
|
||||
const duckDuckGoBangsResponseSchema = z.array(duckDuckGoBangSchema);
|
||||
|
||||
const normalizeBangToken = (token: string) => token.toLowerCase().trim();
|
||||
|
||||
const compareBangTokenAsc = (left: DuckDuckGoBang, right: DuckDuckGoBang) => left.t.localeCompare(right.t);
|
||||
|
||||
export const duckDuckGoBangsRequestHandler = createCachedRequestHandler({
|
||||
queryKey: "duckduckgo-bangs",
|
||||
cacheDuration: dayjs.duration(1, "day"),
|
||||
async requestAsync(_) {
|
||||
if (env.NO_EXTERNAL_CONNECTION) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const res = await withTimeoutAsync(async (signal) => {
|
||||
return await fetchWithTrustedCertificatesAsync(DUCKDUCKGO_BANGS_URL, {
|
||||
signal,
|
||||
headers: {
|
||||
accept: "application/json,text/plain,*/*",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new ResponseError(res);
|
||||
}
|
||||
|
||||
const json: unknown = await res.json();
|
||||
const bangs = await duckDuckGoBangsResponseSchema.parseAsync(json);
|
||||
|
||||
const normalized = bangs
|
||||
.map((bang) => ({
|
||||
...bang,
|
||||
t: normalizeBangToken(bang.t),
|
||||
}))
|
||||
.filter((bang) => bang.t.length > 0);
|
||||
|
||||
normalized.sort(compareBangTokenAsc);
|
||||
|
||||
return normalized;
|
||||
},
|
||||
createRedisChannel() {
|
||||
return createChannelWithLatestAndEvents<DuckDuckGoBang[]>("homarr:duckduckgo-bangs");
|
||||
},
|
||||
});
|
||||
@@ -8,7 +8,7 @@ import { createCachedIntegrationRequestHandler } from "./lib/cached-integration-
|
||||
|
||||
export const systemInfoRequestHandler = createCachedIntegrationRequestHandler<
|
||||
SystemHealthMonitoring,
|
||||
Exclude<IntegrationKindByCategory<"healthMonitoring">, "proxmox">,
|
||||
Exclude<IntegrationKindByCategory<"healthMonitoring">, "proxmox" | "coolify">,
|
||||
Record<string, never>
|
||||
>({
|
||||
async requestAsync(integration, _input) {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import type { IntegrationKindByCategory } from "@homarr/definitions";
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import type { ImmichAlbum, ImmichServerStats } from "@homarr/integrations";
|
||||
|
||||
import { createCachedIntegrationRequestHandler } from "./lib/cached-integration-request-handler";
|
||||
|
||||
export const immichStatsRequestHandler = createCachedIntegrationRequestHandler<
|
||||
ImmichServerStats,
|
||||
IntegrationKindByCategory<"photoService">,
|
||||
Record<string, never>
|
||||
>({
|
||||
async requestAsync(integration) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getServerStatsAsync();
|
||||
},
|
||||
cacheDuration: dayjs.duration(15, "minute"),
|
||||
queryKey: "immich-server-stats",
|
||||
});
|
||||
|
||||
export const immichAlbumsRequestHandler = createCachedIntegrationRequestHandler<
|
||||
{
|
||||
id: string;
|
||||
albumName: string;
|
||||
assetCount: number;
|
||||
}[],
|
||||
IntegrationKindByCategory<"photoService">,
|
||||
Record<string, never>
|
||||
>({
|
||||
async requestAsync(integration) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getAlbumsAsync();
|
||||
},
|
||||
cacheDuration: dayjs.duration(15, "minute"),
|
||||
queryKey: "immich-albums",
|
||||
});
|
||||
|
||||
export const immichAlbumRequestHandler = createCachedIntegrationRequestHandler<
|
||||
ImmichAlbum,
|
||||
IntegrationKindByCategory<"photoService">,
|
||||
{ albumId: string }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getAlbumAsync(input.albumId);
|
||||
},
|
||||
cacheDuration: dayjs.duration(15, "minute"),
|
||||
queryKey: "immich-album",
|
||||
});
|
||||
@@ -28,7 +28,7 @@ export const rssFeedsRequestHandler = createCachedWidgetRequestHandler({
|
||||
|
||||
return {
|
||||
...result,
|
||||
entries: result.entries?.slice(0, input.count) ?? [],
|
||||
entries: result.entries?.map((entry) => ({ ...entry, feedUrl: input.url })).slice(0, input.count) ?? [],
|
||||
};
|
||||
},
|
||||
cacheDuration: dayjs.duration(5, "minutes"),
|
||||
@@ -108,7 +108,8 @@ const getFirstMediaProperty = (feedObject: object) => {
|
||||
* We extend the feed with custom properties.
|
||||
* This interface adds properties on top of the default ones.
|
||||
*/
|
||||
interface ExtendedFeedEntry extends FeedEntry {
|
||||
export interface ExtendedFeedEntry extends FeedEntry {
|
||||
feedUrl: string;
|
||||
enclosure?: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import type { SpeedtestTrackerDashboardData } from "@homarr/integrations/types";
|
||||
|
||||
import { createCachedIntegrationRequestHandler } from "./lib/cached-integration-request-handler";
|
||||
|
||||
export const speedtestTrackerRequestHandler = createCachedIntegrationRequestHandler<
|
||||
SpeedtestTrackerDashboardData,
|
||||
"speedtestTracker",
|
||||
Record<string, never>
|
||||
>({
|
||||
async requestAsync(integration, _input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getDashboardDataAsync();
|
||||
},
|
||||
cacheDuration: dayjs.duration(10, "minutes"),
|
||||
queryKey: "speedtestTrackerDashboard",
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
import type { ContainerStats } from "dockerode";
|
||||
import { describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { createDb } from "@homarr/db/test";
|
||||
|
||||
import { calculateCpuUsage, calculateMemoryUsage } from "../docker";
|
||||
|
||||
vi.mock("@homarr/db", async (importActual) => {
|
||||
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
|
||||
const actual = await importActual<typeof import("@homarr/db")>();
|
||||
return {
|
||||
...actual,
|
||||
db: createDb(),
|
||||
};
|
||||
});
|
||||
|
||||
// Helper to create a partial stats object, cast to ContainerStats.
|
||||
// This mirrors what Podman and other Docker-compatible runtimes may return
|
||||
// at runtime, even though @types/dockerode declares all fields as required.
|
||||
const createStats = (overrides: Record<string, unknown> = {}) => overrides as unknown as ContainerStats;
|
||||
|
||||
describe("calculateCpuUsage", () => {
|
||||
test("should return 0 when cpu_stats is undefined (Podman)", () => {
|
||||
const stats = createStats({ cpu_stats: undefined });
|
||||
expect(calculateCpuUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should return 0 when online_cpus is 0", () => {
|
||||
const stats = createStats({
|
||||
cpu_stats: { online_cpus: 0, cpu_usage: { total_usage: 1000 }, system_cpu_usage: 5000 },
|
||||
});
|
||||
expect(calculateCpuUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should return 0 when online_cpus is undefined", () => {
|
||||
const stats = createStats({
|
||||
cpu_stats: { cpu_usage: { total_usage: 1000 }, system_cpu_usage: 5000 },
|
||||
});
|
||||
expect(calculateCpuUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should return 0 when cpu_usage is undefined", () => {
|
||||
const stats = createStats({
|
||||
cpu_stats: { online_cpus: 4, system_cpu_usage: 5000 },
|
||||
});
|
||||
expect(calculateCpuUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should return 0 when total_usage is 0", () => {
|
||||
const stats = createStats({
|
||||
cpu_stats: { online_cpus: 4, cpu_usage: { total_usage: 0 }, system_cpu_usage: 5000 },
|
||||
});
|
||||
expect(calculateCpuUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should return 0 when system_cpu_usage is 0", () => {
|
||||
const stats = createStats({
|
||||
cpu_stats: { online_cpus: 4, cpu_usage: { total_usage: 1000 }, system_cpu_usage: 0 },
|
||||
});
|
||||
expect(calculateCpuUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should calculate correct CPU percentage for valid stats", () => {
|
||||
const stats = createStats({
|
||||
cpu_stats: { online_cpus: 4, cpu_usage: { total_usage: 2000 }, system_cpu_usage: 10000 },
|
||||
});
|
||||
// (2000 / 10000) * 4 * 100 = 80
|
||||
expect(calculateCpuUsage(stats)).toBe(80);
|
||||
});
|
||||
|
||||
test("should handle fractional CPU usage", () => {
|
||||
const stats = createStats({
|
||||
cpu_stats: { online_cpus: 2, cpu_usage: { total_usage: 500 }, system_cpu_usage: 100000 },
|
||||
});
|
||||
// (500 / 100000) * 2 * 100 = 1
|
||||
expect(calculateCpuUsage(stats)).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("calculateMemoryUsage", () => {
|
||||
test("should return 0 when memory_stats is undefined (Podman)", () => {
|
||||
const stats = createStats({ memory_stats: undefined });
|
||||
expect(calculateMemoryUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should return 0 when memory_stats.usage is 0", () => {
|
||||
const stats = createStats({ memory_stats: { usage: 0 } });
|
||||
expect(calculateMemoryUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should return 0 when memory_stats.usage is undefined", () => {
|
||||
const stats = createStats({ memory_stats: {} });
|
||||
expect(calculateMemoryUsage(stats)).toBe(0);
|
||||
});
|
||||
|
||||
test("should subtract cache from usage", () => {
|
||||
const stats = createStats({
|
||||
memory_stats: { usage: 1000, stats: { cache: 200 } },
|
||||
});
|
||||
expect(calculateMemoryUsage(stats)).toBe(800);
|
||||
});
|
||||
|
||||
test("should use total_inactive_file when cache is absent", () => {
|
||||
const stats = createStats({
|
||||
memory_stats: { usage: 1000, stats: { total_inactive_file: 300 } },
|
||||
});
|
||||
expect(calculateMemoryUsage(stats)).toBe(700);
|
||||
});
|
||||
|
||||
test("should use inactive_file as last fallback", () => {
|
||||
const stats = createStats({
|
||||
memory_stats: { usage: 1000, stats: { inactive_file: 150 } },
|
||||
});
|
||||
expect(calculateMemoryUsage(stats)).toBe(850);
|
||||
});
|
||||
|
||||
test("should return raw usage when memory_stats.stats is undefined", () => {
|
||||
const stats = createStats({ memory_stats: { usage: 512 } });
|
||||
expect(calculateMemoryUsage(stats)).toBe(512);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,138 @@
|
||||
import type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods";
|
||||
import { Octokit } from "octokit";
|
||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
import { getAvailableUpdatesAsync } from "../update-checker";
|
||||
|
||||
const mockEnv = vi.hoisted(() => ({
|
||||
NO_EXTERNAL_CONNECTION: false,
|
||||
}));
|
||||
|
||||
vi.mock("@homarr/common/env", () => ({
|
||||
env: mockEnv,
|
||||
}));
|
||||
|
||||
vi.mock("octokit", () => {
|
||||
const mockOctokit = {
|
||||
rest: {
|
||||
repos: {
|
||||
listReleases: vi.fn(),
|
||||
},
|
||||
},
|
||||
};
|
||||
return {
|
||||
Octokit: class {
|
||||
rest = mockOctokit.rest;
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
describe("getAvailableUpdatesAsync", () => {
|
||||
beforeEach(() => {
|
||||
mockEnv.NO_EXTERNAL_CONNECTION = false;
|
||||
});
|
||||
|
||||
test("should return available non pre-release updates", async () => {
|
||||
// Arrange
|
||||
const currentVersion = "v1.2.3";
|
||||
const releases = fakeReleasesWithPrereleases();
|
||||
const listReleasesSpy = vi.spyOn(new Octokit().rest.repos, "listReleases");
|
||||
listReleasesSpy.mockReturnValue(Promise.resolve({ data: releases, status: 200, headers: {}, url: "" }));
|
||||
|
||||
// Act
|
||||
const result = await getAvailableUpdatesAsync(currentVersion);
|
||||
|
||||
// Assert
|
||||
expect(result.map((update) => update.tagName)).toEqual(["v1.3.0", "v1.2.4"]);
|
||||
expect(listReleasesSpy).toHaveBeenCalledWith({
|
||||
owner: "homarr-labs",
|
||||
repo: "homarr",
|
||||
});
|
||||
});
|
||||
|
||||
test("should return all available updates", async () => {
|
||||
// Arrange
|
||||
const currentVersion = "v1.2.4-beta.2";
|
||||
const releases = fakeReleasesWithPrereleases();
|
||||
const listReleasesSpy = vi.spyOn(new Octokit().rest.repos, "listReleases");
|
||||
listReleasesSpy.mockReturnValue(Promise.resolve({ data: releases, status: 200, headers: {}, url: "" }));
|
||||
|
||||
// Act
|
||||
const result = await getAvailableUpdatesAsync(currentVersion);
|
||||
|
||||
// Assert
|
||||
expect(result.map((update) => update.tagName)).toEqual([
|
||||
"v1.3.1-beta.1",
|
||||
"v1.3.0",
|
||||
"v1.3.0-beta.1",
|
||||
"v1.2.4",
|
||||
"v1.2.4-beta.3",
|
||||
]);
|
||||
expect(listReleasesSpy).toHaveBeenCalledWith({
|
||||
owner: "homarr-labs",
|
||||
repo: "homarr",
|
||||
query: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
test("should skip releases with invalid semver tags", async () => {
|
||||
// Arrange
|
||||
const currentVersion = "v1.2.3";
|
||||
const releases = fakeReleases(["v1.2.4", false], ["invalid-tag", false], ["v1.3.0", false]);
|
||||
const listReleasesSpy = vi.spyOn(new Octokit().rest.repos, "listReleases");
|
||||
listReleasesSpy.mockReturnValue(Promise.resolve({ data: releases, status: 200, headers: {}, url: "" }));
|
||||
|
||||
// Act
|
||||
const result = await getAvailableUpdatesAsync(currentVersion);
|
||||
|
||||
// Assert
|
||||
expect(result.map((update) => update.tagName)).toEqual(["v1.3.0", "v1.2.4"]);
|
||||
});
|
||||
|
||||
test("should throw error for invalid current version", async () => {
|
||||
// Arrange
|
||||
const currentVersion = "invalid-version";
|
||||
|
||||
// Act
|
||||
const act = () => getAvailableUpdatesAsync(currentVersion);
|
||||
|
||||
// Assert
|
||||
await expect(act()).rejects.toThrow("non semantic current version");
|
||||
});
|
||||
|
||||
test("should return empty array when NO_EXTERNAL_CONNECTION=true", async () => {
|
||||
// Arrange
|
||||
mockEnv.NO_EXTERNAL_CONNECTION = true;
|
||||
const currentVersion = "v1.0.0";
|
||||
|
||||
// Act
|
||||
const result = await getAvailableUpdatesAsync(currentVersion);
|
||||
|
||||
// Assert
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
const fakeReleases = (...inputs: [string, boolean][]) => inputs.map(fakeRelease);
|
||||
const fakeRelease = ([tagName, isPrerelease]: [string, boolean]) =>
|
||||
({
|
||||
tag_name: tagName,
|
||||
html_url: `https://github.com/homarr-labs/homarr/releases/tag/${tagName}`,
|
||||
name: tagName,
|
||||
body_html: undefined,
|
||||
prerelease: isPrerelease,
|
||||
}) as RestEndpointMethodTypes["repos"]["listReleases"]["response"]["data"][number];
|
||||
|
||||
const fakeReleasesWithPrereleases = () =>
|
||||
fakeReleases(
|
||||
["v1.1.0", false],
|
||||
["v1.2.2", false],
|
||||
["v1.2.3", false],
|
||||
["v1.2.4-beta.1", true],
|
||||
["v1.2.4-beta.2", true],
|
||||
["v1.2.4-beta.3", true],
|
||||
["v1.2.4", false],
|
||||
["v1.3.0-beta.1", true],
|
||||
["v1.3.0", false],
|
||||
["v1.3.1-beta.1", true],
|
||||
);
|
||||
@@ -0,0 +1,36 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import type { IntegrationKindByCategory } from "@homarr/definitions";
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import type { Station, Timetable } from "@homarr/integrations/types";
|
||||
|
||||
import { createCachedIntegrationRequestHandler } from "./lib/cached-integration-request-handler";
|
||||
|
||||
export const timetableSearchStationsRequestHandler = createCachedIntegrationRequestHandler<
|
||||
Station[],
|
||||
IntegrationKindByCategory<"timetable">,
|
||||
{ query: string }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.searchStationsAsync(input.query);
|
||||
},
|
||||
queryKey: "timetableSearchStations",
|
||||
cacheDuration: dayjs.duration(1, "day"),
|
||||
});
|
||||
|
||||
export const timetableGetTimetableRequestHandler = createCachedIntegrationRequestHandler<
|
||||
Timetable,
|
||||
IntegrationKindByCategory<"timetable">,
|
||||
{ stationId: string; limit: number }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getTimetableAsync({
|
||||
stationId: input.stationId,
|
||||
limit: input.limit,
|
||||
});
|
||||
},
|
||||
queryKey: "timetableGetTimetable",
|
||||
cacheDuration: dayjs.duration(1, "minute"),
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import type { TracearrDashboardData } from "@homarr/integrations/types";
|
||||
|
||||
import { createCachedIntegrationRequestHandler } from "./lib/cached-integration-request-handler";
|
||||
|
||||
export const tracearrRequestHandler = createCachedIntegrationRequestHandler<
|
||||
TracearrDashboardData,
|
||||
"tracearr",
|
||||
Record<string, never>
|
||||
>({
|
||||
async requestAsync(integration, _input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getDashboardDataAsync();
|
||||
},
|
||||
cacheDuration: dayjs.duration(5, "seconds"),
|
||||
queryKey: "tracearrDashboard",
|
||||
});
|
||||
@@ -0,0 +1,84 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
import { createIntegrationAsync } from "@homarr/integrations";
|
||||
import type { UmamiEventSeries, UmamiMetricItem, UmamiVisitorStats } from "@homarr/integrations/types";
|
||||
|
||||
import { createCachedIntegrationRequestHandler } from "./lib/cached-integration-request-handler";
|
||||
|
||||
export const umamiRequestHandler = createCachedIntegrationRequestHandler<
|
||||
UmamiVisitorStats,
|
||||
"umami",
|
||||
{ websiteId: string; timeFrame: string; eventName?: string }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getVisitorStatsAsync(input.websiteId, input.timeFrame, input.eventName);
|
||||
},
|
||||
cacheDuration: dayjs.duration(5, "minutes"),
|
||||
queryKey: "umamiVisitorStats",
|
||||
});
|
||||
|
||||
export const umamiEventNamesRequestHandler = createCachedIntegrationRequestHandler<
|
||||
string[],
|
||||
"umami",
|
||||
{ websiteId: string }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getEventNamesAsync(input.websiteId);
|
||||
},
|
||||
cacheDuration: dayjs.duration(5, "minutes"),
|
||||
queryKey: "umamiEventNames",
|
||||
});
|
||||
|
||||
export const umamiTopPagesRequestHandler = createCachedIntegrationRequestHandler<
|
||||
UmamiMetricItem[],
|
||||
"umami",
|
||||
{ websiteId: string; timeFrame: string; limit: number }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getTopPagesAsync(input.websiteId, input.timeFrame, input.limit);
|
||||
},
|
||||
cacheDuration: dayjs.duration(5, "minutes"),
|
||||
queryKey: "umamiTopPages",
|
||||
});
|
||||
|
||||
export const umamiTopReferrersRequestHandler = createCachedIntegrationRequestHandler<
|
||||
UmamiMetricItem[],
|
||||
"umami",
|
||||
{ websiteId: string; timeFrame: string; limit: number }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getTopReferrersAsync(input.websiteId, input.timeFrame, input.limit);
|
||||
},
|
||||
cacheDuration: dayjs.duration(5, "minutes"),
|
||||
queryKey: "umamiTopReferrers",
|
||||
});
|
||||
|
||||
export const umamiMultiEventRequestHandler = createCachedIntegrationRequestHandler<
|
||||
UmamiEventSeries[],
|
||||
"umami",
|
||||
{ websiteId: string; timeFrame: string; eventNames: string[] }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getMultiEventTimeSeriesAsync(input.websiteId, input.timeFrame, input.eventNames);
|
||||
},
|
||||
cacheDuration: dayjs.duration(5, "minutes"),
|
||||
queryKey: "umamiMultiEvent",
|
||||
});
|
||||
|
||||
export const umamiActiveVisitorsRequestHandler = createCachedIntegrationRequestHandler<
|
||||
number,
|
||||
"umami",
|
||||
{ websiteId: string }
|
||||
>({
|
||||
async requestAsync(integration, input) {
|
||||
const integrationInstance = await createIntegrationAsync(integration);
|
||||
return await integrationInstance.getActiveVisitorsAsync(input.websiteId);
|
||||
},
|
||||
cacheDuration: dayjs.duration(30, "seconds"),
|
||||
queryKey: "umamiActiveVisitors",
|
||||
});
|
||||
@@ -1,10 +1,11 @@
|
||||
import dayjs from "dayjs";
|
||||
import { Octokit } from "octokit";
|
||||
import { compareSemVer, isValidSemVer } from "semver-parser";
|
||||
import { compareSemVer, isValidSemVer, parseSemVer } from "semver-parser";
|
||||
|
||||
import { env } from "@homarr/common/env";
|
||||
import { fetchWithTrustedCertificatesAsync } from "@homarr/core/infrastructure/http";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
import { createChannelWithLatestAndEvents } from "@homarr/redis";
|
||||
import { createCachedRequestHandler } from "@homarr/request-handler/lib/cached-request-handler";
|
||||
|
||||
@@ -16,58 +17,101 @@ export const updateCheckerRequestHandler = createCachedRequestHandler({
|
||||
queryKey: "homarr-update-checker",
|
||||
cacheDuration: dayjs.duration(1, "hour"),
|
||||
async requestAsync(_) {
|
||||
if (env.NO_EXTERNAL_CONNECTION)
|
||||
return {
|
||||
availableUpdates: [],
|
||||
};
|
||||
|
||||
const octokit = new Octokit({
|
||||
request: {
|
||||
fetch: fetchWithTrustedCertificatesAsync,
|
||||
},
|
||||
});
|
||||
const releases = await octokit.rest.repos.listReleases({
|
||||
owner: "homarr-labs",
|
||||
repo: "homarr",
|
||||
});
|
||||
|
||||
const currentVersion = (packageJson as { version: string }).version;
|
||||
const availableReleases = [];
|
||||
|
||||
for (const release of releases.data) {
|
||||
if (!isValidSemVer(release.tag_name)) {
|
||||
logger.warn("Unable to parse semantic tag. Update check might not work.", { tagName: release.tag_name });
|
||||
continue;
|
||||
}
|
||||
|
||||
availableReleases.push(release);
|
||||
}
|
||||
|
||||
const availableNewerReleases = availableReleases
|
||||
.filter((release) => compareSemVer(release.tag_name, currentVersion) > 0)
|
||||
.sort((releaseA, releaseB) => compareSemVer(releaseB.tag_name, releaseA.tag_name));
|
||||
if (availableNewerReleases.length > 0) {
|
||||
logger.info(
|
||||
"Update checker found a new available version",
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
{ version: availableReleases[0]!.tag_name, currentVersion },
|
||||
);
|
||||
} else {
|
||||
logger.debug("Update checker did not find any available updates", { currentVersion });
|
||||
}
|
||||
|
||||
return {
|
||||
availableUpdates: availableNewerReleases.map((release) => ({
|
||||
name: release.name,
|
||||
contentHtml: release.body_html,
|
||||
url: release.html_url,
|
||||
tagName: release.tag_name,
|
||||
})),
|
||||
availableUpdates: await getAvailableUpdatesAsync(packageJson.version),
|
||||
};
|
||||
},
|
||||
createRedisChannel() {
|
||||
return createChannelWithLatestAndEvents<{
|
||||
availableUpdates: { name: string | null; contentHtml?: string; url: string; tagName: string }[];
|
||||
availableUpdates: Update[];
|
||||
}>("homarr:update");
|
||||
},
|
||||
});
|
||||
|
||||
interface Update {
|
||||
name: string | null;
|
||||
contentHtml?: string;
|
||||
url: string;
|
||||
tagName: string;
|
||||
isPrerelease: boolean;
|
||||
}
|
||||
|
||||
const isPrereleaseTag = (tagName: string) => {
|
||||
try {
|
||||
const parsed = parseSemVer(tagName);
|
||||
return Boolean(parsed.pre?.length);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export const getAvailableUpdatesAsync = async (currentVersion: string) => {
|
||||
if (env.NO_EXTERNAL_CONNECTION) return [];
|
||||
|
||||
if (!isValidSemVer(currentVersion)) {
|
||||
throw new ErrorWithMetadata("Unable to check for updates due to non semantic current version", {
|
||||
currentVersion,
|
||||
});
|
||||
}
|
||||
|
||||
const octokit = new Octokit({
|
||||
request: {
|
||||
fetch: fetchWithTrustedCertificatesAsync,
|
||||
},
|
||||
});
|
||||
|
||||
const isCurrentPrerelease = isPrereleaseTag(currentVersion);
|
||||
|
||||
const releases = await octokit.rest.repos.listReleases({
|
||||
owner: "homarr-labs",
|
||||
repo: "homarr",
|
||||
});
|
||||
|
||||
const { skippedTags, semanticReleases } = releases.data
|
||||
.map((release) => ({
|
||||
name: release.name,
|
||||
contentHtml: release.body_html,
|
||||
url: release.html_url,
|
||||
tagName: release.tag_name,
|
||||
isPrerelease: release.prerelease,
|
||||
}))
|
||||
.reduce(
|
||||
(prev, curr) => {
|
||||
if (!isValidSemVer(curr.tagName)) {
|
||||
prev.skippedTags.push(curr.tagName);
|
||||
return prev;
|
||||
}
|
||||
|
||||
prev.semanticReleases.push(curr);
|
||||
return prev;
|
||||
},
|
||||
{ semanticReleases: [] as Update[], skippedTags: [] as string[] },
|
||||
);
|
||||
|
||||
if (skippedTags.length > 0) {
|
||||
logger.warn(
|
||||
"Some releases were skipped during the update check because their tag name is not a valid semantic version",
|
||||
{
|
||||
skippedTags: skippedTags.join(","),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const availableUpdates = semanticReleases
|
||||
.filter((release) => isCurrentPrerelease || !release.isPrerelease)
|
||||
.filter((release) => compareSemVer(release.tagName, currentVersion) > 0)
|
||||
.sort((releaseA, releaseB) => compareSemVer(releaseB.tagName, releaseA.tagName));
|
||||
|
||||
if (availableUpdates.length === 0) {
|
||||
logger.debug("No available updates found", { currentVersion });
|
||||
return [];
|
||||
}
|
||||
|
||||
logger.info("Found available updates", {
|
||||
version: availableUpdates[0]?.tagName,
|
||||
count: availableUpdates.length,
|
||||
currentVersion,
|
||||
});
|
||||
|
||||
return availableUpdates;
|
||||
};
|
||||
Reference in new issue
Block a user