refactoring(bw-limit): stylistic refactoring

This commit is contained in:
Nicolas Meienberger 2026-01-15 17:57:38 +01:00
parent cb72e6c073
commit 1622e34b0d
9 changed files with 1657 additions and 470 deletions

2
.gitignore vendored
View file

@ -31,3 +31,5 @@ node_modules/
playwright/.auth playwright/.auth
playwright/temp playwright/temp
.idea/

View file

@ -10,13 +10,7 @@ import {
import { Input } from "../../../../components/ui/input"; import { Input } from "../../../../components/ui/input";
import { Textarea } from "../../../../components/ui/textarea"; import { Textarea } from "../../../../components/ui/textarea";
import { Checkbox } from "../../../../components/ui/checkbox"; import { Checkbox } from "../../../../components/ui/checkbox";
import { import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../../../../components/ui/select";
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue
} from "../../../../components/ui/select";
import { Tooltip, TooltipContent, TooltipTrigger } from "../../../../components/ui/tooltip"; import { Tooltip, TooltipContent, TooltipTrigger } from "../../../../components/ui/tooltip";
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../../../../components/ui/collapsible"; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "../../../../components/ui/collapsible";
import type { RepositoryFormValues } from "../create-repository-form"; import type { RepositoryFormValues } from "../create-repository-form";
@ -30,26 +24,15 @@ type Props = {
export const AdvancedForm = ({ form }: Props) => { export const AdvancedForm = ({ form }: Props) => {
const insecureTls = form.watch("insecureTls"); const insecureTls = form.watch("insecureTls");
const cacert = form.watch("cacert"); const cacert = form.watch("cacert");
const uploadEnabled = form.watch("uploadLimit.enabled"); const uploadLimitEnabled = form.watch("uploadLimit.enabled");
const downloadEnabled = form.watch("downloadLimit.enabled"); const downloadLimitEnabled = form.watch("downloadLimit.enabled");
return ( return (
<Collapsible> <Collapsible>
<CollapsibleTrigger className="">Advanced Settings</CollapsibleTrigger> <CollapsibleTrigger className="">Advanced Settings</CollapsibleTrigger>
<CollapsibleContent className="pb-4 space-y-4"> <CollapsibleContent className="pb-4 space-y-4">
{/* Bandwidth Limit Controls */} <div className="space-y-4 mt-4">
<div className="space-y-6 rounded-lg border bg-card p-6"> <div className="grid gap-6">
<div className="flex items-center gap-3">
<div>
<h3 className="text-base font-semibold">Bandwidth Limits</h3>
<p className="text-sm text-muted-foreground">
Control upload and download speeds to prevent saturating network bandwidth
</p>
</div>
</div>
<div className="grid gap-6 sm:grid-cols-2">
{/* Upload Limit */}
<div className="space-y-4 rounded-lg border bg-background/50 p-4"> <div className="space-y-4 rounded-lg border bg-background/50 p-4">
<FormField <FormField
control={form.control} control={form.control}
@ -57,23 +40,15 @@ export const AdvancedForm = ({ form }: Props) => {
render={({ field }) => ( render={({ field }) => (
<FormItem className="flex flex-row items-start space-x-3 space-y-0"> <FormItem className="flex flex-row items-start space-x-3 space-y-0">
<FormControl> <FormControl>
<Checkbox <Checkbox checked={field.value ?? false} onCheckedChange={field.onChange} />
checked={field.value ?? false}
onCheckedChange={field.onChange}
/>
</FormControl> </FormControl>
<div className="space-y-1 leading-none"> <div className="space-y-1">
<FormLabel>Enable upload speed limit</FormLabel> <FormLabel>Enable upload speed limit</FormLabel>
<FormDescription className="text-xs"> <FormDescription className="text-xs">Limit upload speed to the repository</FormDescription>
Limit upload speed to the repository
</FormDescription>
</div> </div>
</FormItem> </FormItem>
)} )}
/> />
{uploadEnabled && (
<div className="space-y-3 pt-2">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<FormField <FormField
control={form.control} control={form.control}
@ -83,20 +58,22 @@ export const AdvancedForm = ({ form }: Props) => {
<FormControl> <FormControl>
<div className="relative"> <div className="relative">
<Input <Input
disabled={!uploadLimitEnabled}
type="number" type="number"
min={1}
step={1}
max={999999}
placeholder="10" placeholder="10"
min="0"
step="0.1"
className="pr-12" className="pr-12"
{...field} {...field}
onChange={(e) => field.onChange(parseFloat(e.target.value) || 0)} onChange={(e) => field.onChange(parseInt(e.target.value, 10) || 1)}
/> />
<div className="absolute inset-y-0 right-0 flex items-center pr-3"> <div className="absolute inset-y-0 right-0 flex items-center pr-3">
<div className="h-4 w-px bg-border" /> <div className="h-4 w-px bg-border" />
</div> </div>
<FormMessage />
</div> </div>
</FormControl> </FormControl>
<FormMessage />
</FormItem> </FormItem>
)} )}
/> />
@ -105,7 +82,12 @@ export const AdvancedForm = ({ form }: Props) => {
name="uploadLimit.unit" name="uploadLimit.unit"
render={({ field }) => ( render={({ field }) => (
<FormItem className="w-24"> <FormItem className="w-24">
<Select onValueChange={field.onChange} defaultValue={field.value || "Mbps"} value={field.value || "Mbps"}> <Select
onValueChange={field.onChange}
defaultValue={field.value || "Mbps"}
value={field.value || "Mbps"}
disabled={!uploadLimitEnabled}
>
<FormControl> <FormControl>
<SelectTrigger className="text-xs"> <SelectTrigger className="text-xs">
<SelectValue /> <SelectValue />
@ -125,34 +107,25 @@ export const AdvancedForm = ({ form }: Props) => {
/> />
</div> </div>
</div> </div>
)}
</div> </div>
{/* Download Limit */} <div className="rounded-lg border bg-background/50 p-4">
<div className="space-y-4 rounded-lg border bg-background/50 p-4">
<FormField <FormField
control={form.control} control={form.control}
name="downloadLimit.enabled" name="downloadLimit.enabled"
render={({ field }) => ( render={({ field }) => (
<FormItem className="flex flex-row items-start space-x-3 space-y-0"> <FormItem className="flex flex-row items-start space-x-3 space-y-0">
<FormControl> <FormControl>
<Checkbox <Checkbox checked={field.value ?? false} onCheckedChange={field.onChange} />
checked={field.value ?? false}
onCheckedChange={field.onChange}
/>
</FormControl> </FormControl>
<div className="space-y-1 leading-none"> <div className="space-y-1">
<FormLabel>Enable download speed limit</FormLabel> <FormLabel>Enable download speed limit</FormLabel>
<FormDescription className="text-xs"> <FormDescription className="text-xs">Limit download speed from the repository</FormDescription>
Limit download speed from the repository
</FormDescription>
</div> </div>
</FormItem> </FormItem>
)} )}
/> />
{downloadEnabled && (
<div className="space-y-3 pt-2"> <div className="space-y-3 pt-2">
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<FormField <FormField
@ -163,13 +136,15 @@ export const AdvancedForm = ({ form }: Props) => {
<FormControl> <FormControl>
<div className="relative"> <div className="relative">
<Input <Input
type="number"
placeholder="10" placeholder="10"
min="0" type="number"
step="0.1" min={1}
step={1}
max={999999}
disabled={!downloadLimitEnabled}
className="pr-12" className="pr-12"
{...field} {...field}
onChange={(e) => field.onChange(parseFloat(e.target.value) || 0)} onChange={(e) => field.onChange(parseInt(e.target.value, 10) || 1)}
/> />
<div className="absolute inset-y-0 right-0 flex items-center pr-3"> <div className="absolute inset-y-0 right-0 flex items-center pr-3">
<div className="h-4 w-px bg-border" /> <div className="h-4 w-px bg-border" />
@ -185,7 +160,11 @@ export const AdvancedForm = ({ form }: Props) => {
name="downloadLimit.unit" name="downloadLimit.unit"
render={({ field }) => ( render={({ field }) => (
<FormItem className="w-24"> <FormItem className="w-24">
<Select onValueChange={field.onChange} defaultValue={field.value || "Mbps"} value={field.value || "Mbps"}> <Select
onValueChange={field.onChange}
defaultValue={field.value || "Mbps"}
value={field.value || "Mbps"}
>
<FormControl> <FormControl>
<SelectTrigger className="text-xs"> <SelectTrigger className="text-xs">
<SelectValue /> <SelectValue />
@ -205,8 +184,6 @@ export const AdvancedForm = ({ form }: Props) => {
/> />
</div> </div>
</div> </div>
)}
</div>
</div> </div>
</div> </div>

View file

@ -1,6 +1,6 @@
ALTER TABLE `repositories_table` ADD `upload_limit_enabled` integer DEFAULT false NOT NULL;--> statement-breakpoint ALTER TABLE `repositories_table` ADD `upload_limit_enabled` integer DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `repositories_table` ADD `upload_limit_value` integer DEFAULT 0 NOT NULL;--> statement-breakpoint ALTER TABLE `repositories_table` ADD `upload_limit_value` real DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE `repositories_table` ADD `upload_limit_unit` text DEFAULT 'MB/s' NOT NULL;--> statement-breakpoint ALTER TABLE `repositories_table` ADD `upload_limit_unit` text DEFAULT 'Mbps' NOT NULL;--> statement-breakpoint
ALTER TABLE `repositories_table` ADD `download_limit_enabled` integer DEFAULT false NOT NULL;--> statement-breakpoint ALTER TABLE `repositories_table` ADD `download_limit_enabled` integer DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `repositories_table` ADD `download_limit_value` integer DEFAULT 0 NOT NULL;--> statement-breakpoint ALTER TABLE `repositories_table` ADD `download_limit_value` real DEFAULT 0 NOT NULL;--> statement-breakpoint
ALTER TABLE `repositories_table` ADD `download_limit_unit` text DEFAULT 'MB/s' NOT NULL; ALTER TABLE `repositories_table` ADD `download_limit_unit` text DEFAULT 'Mbps' NOT NULL;

File diff suppressed because it is too large Load diff

View file

@ -232,6 +232,13 @@
"when": 1768136755563, "when": 1768136755563,
"tag": "0032_lowercase-email", "tag": "0032_lowercase-email",
"breakpoints": true "breakpoints": true
},
{
"idx": 33,
"version": "6",
"when": 1768490085610,
"tag": "0033_curved_warhawk",
"breakpoints": true
} }
] ]
} }

View file

@ -14,17 +14,16 @@ export const REPOSITORY_BACKENDS = {
export type RepositoryBackend = keyof typeof REPOSITORY_BACKENDS; export type RepositoryBackend = keyof typeof REPOSITORY_BACKENDS;
export const BANDWIDTH_UNITS = { export const BANDWIDTH_UNITS = {
"Kbps": "Kbps", Kbps: "Kbps",
"Mbps": "Mbps", Mbps: "Mbps",
"Gbps": "Gbps", Gbps: "Gbps",
} as const; } as const;
export type BandwidthUnit = keyof typeof BANDWIDTH_UNITS; export type BandwidthUnit = keyof typeof BANDWIDTH_UNITS;
// Bandwidth limit configuration
export const bandwidthLimitSchema = type({ export const bandwidthLimitSchema = type({
enabled: "boolean = false", enabled: "boolean = false",
value: "number >= 0 = 0", value: "number > 0 = 1",
unit: type.valueOf(BANDWIDTH_UNITS).default("Mbps"), unit: type.valueOf(BANDWIDTH_UNITS).default("Mbps"),
}); });

View file

@ -1,6 +1,12 @@
import { relations, sql } from "drizzle-orm"; import { relations, sql } from "drizzle-orm";
import { index, int, integer, sqliteTable, text, real, primaryKey, unique } from "drizzle-orm/sqlite-core"; import { index, int, integer, sqliteTable, text, real, primaryKey, unique } from "drizzle-orm/sqlite-core";
import type { CompressionMode, RepositoryBackend, repositoryConfigSchema, RepositoryStatus, BandwidthUnit } from "~/schemas/restic"; import type {
CompressionMode,
RepositoryBackend,
repositoryConfigSchema,
RepositoryStatus,
BandwidthUnit,
} from "~/schemas/restic";
import type { BackendStatus, BackendType, volumeConfigSchema } from "~/schemas/volumes"; import type { BackendStatus, BackendType, volumeConfigSchema } from "~/schemas/volumes";
import type { NotificationType, notificationConfigSchema } from "~/schemas/notifications"; import type { NotificationType, notificationConfigSchema } from "~/schemas/notifications";
@ -159,7 +165,6 @@ export const repositoriesTable = sqliteTable("repositories_table", {
status: text().$type<RepositoryStatus>().default("unknown"), status: text().$type<RepositoryStatus>().default("unknown"),
lastChecked: int("last_checked", { mode: "number" }), lastChecked: int("last_checked", { mode: "number" }),
lastError: text("last_error"), lastError: text("last_error"),
// Bandwidth limit fields
uploadLimitEnabled: int("upload_limit_enabled", { mode: "boolean" }).notNull().default(false), uploadLimitEnabled: int("upload_limit_enabled", { mode: "boolean" }).notNull().default(false),
uploadLimitValue: real("upload_limit_value").notNull().default(0), uploadLimitValue: real("upload_limit_value").notNull().default(0),
uploadLimitUnit: text("upload_limit_unit").$type<BandwidthUnit>().notNull().default("Mbps"), uploadLimitUnit: text("upload_limit_unit").$type<BandwidthUnit>().notNull().default("Mbps"),

View file

@ -846,31 +846,14 @@ const copy = async (
args.push("latest"); args.push("latest");
} }
// Apply common args without automatic bandwidth limit injection to prevent duplication
addCommonArgs(args, env, destConfig, { skipBandwidth: true }); addCommonArgs(args, env, destConfig, { skipBandwidth: true });
// Manually handle bandwidth limits with correct copy semantics:
// --limit-download uses sourceConfig.downloadLimit (limiting downloads from source repo)
// --limit-upload uses destConfig.uploadLimit (limiting uploads to destination repo)
const sourceDownloadLimit = formatBandwidthLimit(sourceConfig.downloadLimit); const sourceDownloadLimit = formatBandwidthLimit(sourceConfig.downloadLimit);
const destUploadLimit = formatBandwidthLimit(destConfig.uploadLimit); const destUploadLimit = formatBandwidthLimit(destConfig.uploadLimit);
if (sourceConfig.backend === "rclone") {
// For rclone source backends, use rclone.from.bwlimit with source download limit only
let effectiveSourceLimit = "";
if (sourceDownloadLimit) {
effectiveSourceLimit = sourceDownloadLimit;
}
if (effectiveSourceLimit) {
args.push("-o", `rclone.from.bwlimit=${effectiveSourceLimit}`);
}
} else {
// For restic source backends, apply download limit from source
if (sourceDownloadLimit) { if (sourceDownloadLimit) {
args.push("--limit-download", sourceDownloadLimit); args.push("--limit-download", sourceDownloadLimit);
} }
}
// Apply upload limit to destination for all backends // Apply upload limit to destination for all backends
if (destUploadLimit) { if (destUploadLimit) {
@ -899,13 +882,11 @@ const copy = async (
}; };
}; };
// Helper function to convert bandwidth limit to restic/rclone format
const formatBandwidthLimit = (limit?: BandwidthLimit): string => { const formatBandwidthLimit = (limit?: BandwidthLimit): string => {
if (!limit || !limit.enabled || limit.value <= 0) { if (!limit || !limit.enabled || limit.value <= 0) {
return ""; return "";
} }
// Convert to KiB/s for restic compatibility, or use suffixed format for rclone
let kibibytesPerSecond: number; let kibibytesPerSecond: number;
switch (limit.unit) { switch (limit.unit) {
case "Kbps": case "Kbps":
@ -924,11 +905,15 @@ const formatBandwidthLimit = (limit?: BandwidthLimit): string => {
return ""; return "";
} }
// Return as integer KiB/s for restic
return `${Math.floor(kibibytesPerSecond)}`; return `${Math.floor(kibibytesPerSecond)}`;
}; };
export const addCommonArgs = (args: string[], env: Record<string, string>, config?: RepositoryConfig, options?: { skipBandwidth?: boolean }) => { export const addCommonArgs = (
args: string[],
env: Record<string, string>,
config?: RepositoryConfig,
options?: { skipBandwidth?: boolean },
) => {
args.push("--json"); args.push("--json");
if (env._SFTP_SSH_ARGS) { if (env._SFTP_SSH_ARGS) {
@ -943,28 +928,7 @@ export const addCommonArgs = (args: string[], env: Record<string, string>, confi
args.push("--cacert", env.RESTIC_CACERT); args.push("--cacert", env.RESTIC_CACERT);
} }
// Add bandwidth limits if configuration is provided and not skipped
if (config && !options?.skipBandwidth) { if (config && !options?.skipBandwidth) {
if (config.backend === "rclone") {
// For rclone backends, consolidate both upload and download limits into one bwlimit
const uploadLimit = formatBandwidthLimit(config.uploadLimit);
const downloadLimit = formatBandwidthLimit(config.downloadLimit);
// Determine effective limit (choose more restrictive when both exist)
let effectiveLimit = "";
if (uploadLimit && downloadLimit) {
effectiveLimit = parseInt(uploadLimit, 10) < parseInt(downloadLimit, 10) ? uploadLimit : downloadLimit;
} else if (uploadLimit) {
effectiveLimit = uploadLimit;
} else if (downloadLimit) {
effectiveLimit = downloadLimit;
}
if (effectiveLimit) {
args.push("-o", `rclone.bwlimit=${effectiveLimit}`);
}
} else {
// For restic backends, handle upload and download limits separately
const uploadLimit = formatBandwidthLimit(config.uploadLimit); const uploadLimit = formatBandwidthLimit(config.uploadLimit);
if (uploadLimit) { if (uploadLimit) {
args.push("--limit-upload", uploadLimit); args.push("--limit-upload", uploadLimit);
@ -975,7 +939,6 @@ export const addCommonArgs = (args: string[], env: Record<string, string>, confi
args.push("--limit-download", downloadLimit); args.push("--limit-download", downloadLimit);
} }
} }
}
}; };
export const restic = { export const restic = {
@ -995,26 +958,17 @@ export const restic = {
copy, copy,
}; };
// Helper function to clean up temporary files
export const cleanupTemporaryKeys = async (env: Record<string, string>) => { export const cleanupTemporaryKeys = async (env: Record<string, string>) => {
const keysToClean = ["_SFTP_KEY_PATH", "_SFTP_KNOWN_HOSTS_PATH", "RESTIC_CACERT", "GOOGLE_APPLICATION_CREDENTIALS"]; const keysToClean = ["_SFTP_KEY_PATH", "_SFTP_KNOWN_HOSTS_PATH", "RESTIC_CACERT", "GOOGLE_APPLICATION_CREDENTIALS"];
for (const key of keysToClean) { for (const key of keysToClean) {
if (env[key]) { if (env[key]) {
try { await fs.unlink(env[key]).catch(() => {});
await fs.unlink(env[key]);
} catch (_error) {
// Ignore errors when cleaning up temporary files
}
} }
} }
// Clean up custom password files // Clean up custom password files
if (env.RESTIC_PASSWORD_FILE && env.RESTIC_PASSWORD_FILE !== RESTIC_PASS_FILE) { if (env.RESTIC_PASSWORD_FILE && env.RESTIC_PASSWORD_FILE !== RESTIC_PASS_FILE) {
try { await fs.unlink(env.RESTIC_PASSWORD_FILE).catch(() => {});
await fs.unlink(env.RESTIC_PASSWORD_FILE);
} catch (_error) {
// Ignore errors when cleaning up temporary files
}
} }
}; };