Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface ResourceValueInputProps {
presets: readonly number[];
value: number;
onChange: (value: number) => void;
/** Preset label override; defaults to the value with its unit. */
formatValue?: (value: number) => string;
}

/**
Expand All @@ -39,6 +41,7 @@ export function ResourceValueInput({
presets,
value,
onChange,
formatValue,
}: ResourceValueInputProps) {
const [isCustom, setIsCustom] = useState(() => !presets.includes(value));

Expand All @@ -50,7 +53,7 @@ export function ResourceValueInput({
const options = [
...presets.map((preset) => ({
value: String(preset),
label: formatPreset(preset, unit),
label: formatValue ? formatValue(preset) : formatPreset(preset, unit),
})),
{ value: CUSTOM_OPTION, label: 'Custom…' },
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Save } from 'lucide-react';
import { api, ApiError } from '@/lib/api';
import { keys, queries } from '@/lib/queries';
import { useSession } from '@/lib/auth';
import { RESOURCE_DEFAULTS, formatIdleTime } from '@/lib/resources';
import type { EffectiveResources, ResourceRequest } from '@/lib/types';
import { ResourceValueInput } from './ResourceValueInput';

Expand Down Expand Up @@ -44,9 +45,21 @@ const RESOURCE_OPTIONS = [
min: 5,
presets: [5, 10, 20, 50, 100, 200, 500],
},
{
key: 'idleTime',
label: 'Idle shutdown',
unit: 'hours',
step: 1,
min: 0,
presets: [0, 1, 4, 8, 24, 72, 168, 336, 720],
},
] as const;

const DEFAULTS: EffectiveResources = { memory: 8192, swap: 0, cpus: 4, rootfs: 50 };
/** Display value for a resource row; idleTime reads as "Never" / "3 days". */
function formatResource(key: keyof EffectiveResources, value: number, unit: string): string {
if (key === 'idleTime') return formatIdleTime(value);
return unit ? `${value} ${unit}` : `${value}`;
}

interface ResourcesSectionProps {
siteId: string;
Expand Down Expand Up @@ -121,7 +134,7 @@ export function ResourcesSection({
onError: (err: ApiError) => toast.error(err.message),
});

const resources = effective || DEFAULTS;
const resources = effective || RESOURCE_DEFAULTS;

if (!hostname) {
return (
Expand Down Expand Up @@ -162,6 +175,11 @@ export function ResourcesSection({
<p className="text-xs text-muted-foreground">
Resource allocations for this container. Changes above the default require admin approval.
</p>
<p className="text-xs text-muted-foreground">
Idle shutdown stops the container after that long without traffic through the proxy
(SSH and direct connections don&apos;t count). Shortening it applies immediately; longer
or Never requires admin approval.
Comment on lines +180 to +181
</p>
{!isOwner && (
<p className="rounded-md border border-border bg-muted/40 px-3 py-2 text-xs text-muted-foreground">
Only owners can make resource requests on their containers.
Expand All @@ -174,9 +192,9 @@ export function ResourcesSection({
)}
<div className="grid gap-3">
{RESOURCE_OPTIONS.map((opt) => {
const currentValue = resources[opt.key as keyof EffectiveResources];
const currentValue = resources[opt.key];
const isEditing = editingResource === opt.key;
const isDefault = currentValue === DEFAULTS[opt.key as keyof EffectiveResources];
const isDefault = currentValue === RESOURCE_DEFAULTS[opt.key];

return (
<div
Expand All @@ -194,8 +212,7 @@ export function ResourcesSection({
</div>
{!isEditing && (
<span className="text-sm text-muted-foreground">
{currentValue}
{opt.unit ? ` ${opt.unit}` : ''}
{formatResource(opt.key, currentValue, opt.unit)}
</span>
)}
</div>
Expand All @@ -209,6 +226,7 @@ export function ResourcesSection({
presets={opt.presets}
value={editValue}
onChange={setEditValue}
formatValue={opt.key === 'idleTime' ? formatIdleTime : undefined}
/>
<Input
placeholder="Reason (optional)"
Expand Down
41 changes: 41 additions & 0 deletions create-a-container/client/src/lib/resources.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { EffectiveResources, ResourceType } from './types';

/** Mirrors ResourceRequest.RESOURCE_DEFAULTS on the server. */
export const RESOURCE_DEFAULTS: EffectiveResources = {
memory: 8192,
swap: 0,
cpus: 4,
rootfs: 50,
idleTime: 72,
};

export const RESOURCE_LABELS: Record<ResourceType, string> = {
memory: 'RAM',
swap: 'Swap',
cpus: 'CPU',
rootfs: 'Storage',
idleTime: 'Idle shutdown',
};

/** idleTime value that exempts a container from idle shutdown. */
export const IDLE_TIME_NEVER = 0;

/** Idle-shutdown threshold in hours -> "Never", "8 hours", "3 days". */
export function formatIdleTime(hours: number): string {
if (hours === IDLE_TIME_NEVER) return 'Never';
if (hours % 24 === 0) {
const days = hours / 24;
return `${days} ${days === 1 ? 'day' : 'days'}`;
}
return `${hours} ${hours === 1 ? 'hour' : 'hours'}`;
}

/** Compact value for request tables, e.g. "8.0GB", "50GB", "3 days". */
export function formatResourceValue(type: string, value: number): string {
if (type === 'memory' || type === 'swap') {
return value >= 1024 ? `${(value / 1024).toFixed(1)}GB` : `${value}MB`;
}
if (type === 'rootfs') return `${value}GB`;
if (type === 'idleTime') return formatIdleTime(value);
return `${value}`;
}
4 changes: 3 additions & 1 deletion create-a-container/client/src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ export interface AppSettings {
mailMessageSizeLimitMb: string;
}

export type ResourceType = 'memory' | 'swap' | 'cpus' | 'rootfs';
export type ResourceType = 'memory' | 'swap' | 'cpus' | 'rootfs' | 'idleTime';

export interface ResourceRequest {
id: number;
Expand All @@ -506,4 +506,6 @@ export interface EffectiveResources {
swap: number;
cpus: number;
rootfs: number;
/** Hours without proxied access before an idle stop; 0 = never. */
idleTime: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,8 @@ import {
import { ClipboardList } from 'lucide-react';
import { keys, queries } from '@/lib/queries';
import { FormPageHeader } from '@/components/FormPageHeader';

const RESOURCE_LABELS: Record<string, string> = {
memory: 'RAM',
swap: 'Swap',
cpus: 'CPU',
rootfs: 'Storage',
};

const RESOURCE_DEFAULTS: Record<string, number> = {
memory: 8192,
swap: 0,
cpus: 4,
rootfs: 50,
};

function formatValue(type: string, value: number) {
if (type === 'memory' || type === 'swap') {
return value >= 1024 ? `${(value / 1024).toFixed(1)}GB` : `${value}MB`;
}
if (type === 'rootfs') return `${value}GB`;
return `${value}`;
}
import { RESOURCE_DEFAULTS, RESOURCE_LABELS, formatResourceValue } from '@/lib/resources';
import type { ResourceType } from '@/lib/types';

function statusBadgeVariant(status: string) {
if (status === 'approved') return 'success' as const;
Expand Down Expand Up @@ -120,7 +100,7 @@ export function MyRequestsPage() {
interface Request {
id: number;
hostname: string;
resourceType: string;
resourceType: ResourceType;
value: number;
status: string;
comment: string | null;
Expand Down Expand Up @@ -172,17 +152,17 @@ function RequestTable({
<tr key={req.id} className="border-b border-border last:border-0">
<td className="px-5 py-4 font-mono text-xs">{req.hostname}</td>
<td className="px-5 py-4">
<Badge variant="outline">
<Badge variant="outline" className="whitespace-nowrap">
{RESOURCE_LABELS[req.resourceType] || req.resourceType}
</Badge>
</td>
<td className="px-5 py-4 font-mono text-xs">
<td className="px-5 py-4 font-mono text-xs whitespace-nowrap">
<span className="text-muted-foreground">
{formatValue(req.resourceType, prevValue)}
{formatResourceValue(req.resourceType, prevValue)}
</span>
<span className="mx-1.5">→</span>
<span className="font-semibold">
{formatValue(req.resourceType, req.value)}
{formatResourceValue(req.resourceType, req.value)}
</span>
</td>
<td className="max-w-[200px] px-5 py-4 text-muted-foreground">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,9 @@ import { CheckCircle, XCircle, ClipboardList } from 'lucide-react';
import { api, ApiError } from '@/lib/api';
import { keys, queries } from '@/lib/queries';
import { FormPageHeader } from '@/components/FormPageHeader';
import { RESOURCE_DEFAULTS, RESOURCE_LABELS, formatResourceValue } from '@/lib/resources';
import type { ResourceRequest } from '@/lib/types';

const RESOURCE_LABELS: Record<string, string> = {
memory: 'RAM',
swap: 'Swap',
cpus: 'CPU',
rootfs: 'Storage',
};

const RESOURCE_DEFAULTS: Record<string, number> = {
memory: 8192,
swap: 0,
cpus: 4,
rootfs: 50,
};

function formatValue(type: string, value: number) {
if (type === 'memory' || type === 'swap') {
return value >= 1024 ? `${(value / 1024).toFixed(1)}GB` : `${value}MB`;
}
if (type === 'rootfs') return `${value}GB`;
return `${value}`;
}

export function ResourceRequestsPage() {
const qc = useQueryClient();
const toast = useToast();
Expand Down Expand Up @@ -142,17 +121,17 @@ export function ResourceRequestsPage() {
<td className="px-5 py-4 font-mono text-xs">{req.hostname}</td>
<td className="px-5 py-4">{req.username}</td>
<td className="px-5 py-4">
<Badge variant="outline">
<Badge variant="outline" className="whitespace-nowrap">
{RESOURCE_LABELS[req.resourceType] || req.resourceType}
</Badge>
</td>
<td className="px-5 py-4 font-mono text-xs">
<td className="px-5 py-4 font-mono text-xs whitespace-nowrap">
<span className="text-muted-foreground">
{formatValue(req.resourceType, prevValue)}
{formatResourceValue(req.resourceType, prevValue)}
</span>
<span className="mx-1.5">→</span>
<span className="font-semibold">
{formatValue(req.resourceType, req.value)}
{formatResourceValue(req.resourceType, req.value)}
</span>
</td>
<td className="max-w-[200px] px-5 py-4 text-muted-foreground">
Expand Down Expand Up @@ -260,17 +239,17 @@ export function ResourceRequestsPage() {
<td className="px-5 py-4 font-mono text-xs">{req.hostname}</td>
<td className="px-5 py-4">{req.username}</td>
<td className="px-5 py-4">
<Badge variant="outline">
<Badge variant="outline" className="whitespace-nowrap">
{RESOURCE_LABELS[req.resourceType] || req.resourceType}
</Badge>
</td>
<td className="px-5 py-4 font-mono text-xs">
<td className="px-5 py-4 font-mono text-xs whitespace-nowrap">
<span className="text-muted-foreground">
{formatValue(req.resourceType, prevValue)}
{formatResourceValue(req.resourceType, prevValue)}
</span>
<span className="mx-1.5">→</span>
<span className="font-semibold">
{formatValue(req.resourceType, req.value)}
{formatResourceValue(req.resourceType, req.value)}
</span>
</td>
<td className="max-w-[200px] px-5 py-4 text-muted-foreground">
Expand Down
7 changes: 4 additions & 3 deletions create-a-container/models/resourcerequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
const { Model } = require('sequelize');

/**
* Default resource values for containers.
* Requests at or below these values are auto-approved for non-admin users.
* Default resource values for containers, keyed by resource type. Per-type
* approval and apply rules live in resources/resource-requests/service.js.
*/
const RESOURCE_DEFAULTS = {
memory: 8192, // MB
swap: 0, // MB
cpus: 4,
rootfs: 50, // GB
idleTime: 72, // hours without proxied access before an idle stop; 0 = never
};

const VALID_RESOURCE_TYPES = ['memory', 'swap', 'cpus', 'rootfs'];
const VALID_RESOURCE_TYPES = Object.keys(RESOURCE_DEFAULTS);

module.exports = (sequelize, DataTypes) => {
class ResourceRequest extends Model {
Expand Down
Loading
Loading