Repository Public
1// Friendly interval input for schedules: a number and a unit instead of raw seconds.2import type { Schemas } from "@/api/client";34export type IntervalUnit = "minutes" | "hours" | "days";5export const UNIT_SECONDS: Record<IntervalUnit, number> = { minutes: 60, hours: 3600, days: 86400 };6export const MIN_INTERVAL_SECONDS = 60; // contract: every_seconds >= 6078/** Largest unit that divides the interval exactly (3600 → 1 hour, 5400 → 90 minutes). */9export function splitInterval(seconds: number): { value: number; unit: IntervalUnit } {10 for (const unit of ["days", "hours", "minutes"] as const) {11 if (seconds >= UNIT_SECONDS[unit] && seconds % UNIT_SECONDS[unit] === 0) return { value: seconds / UNIT_SECONDS[unit], unit };12 }13 return { value: Math.max(1, Math.round(seconds / 60)), unit: "minutes" };14}1516/** Seconds for a typed value, or null when it is not a whole positive number or is under the contract minimum. */17export function toSeconds(value: string, unit: IntervalUnit): number | null {18 const text = value.trim();19 if (!/^\d+$/.test(text)) return null;20 const seconds = Number(text) * UNIT_SECONDS[unit];21 return Number.isSafeInteger(seconds) && seconds >= MIN_INTERVAL_SECONDS ? seconds : null;22}2324export interface PlacementDraft {25 nodeId: string | null;26 site: string;27 role: string;28 tags: string;29}30export const EMPTY_PLACEMENT: PlacementDraft = { nodeId: null, site: "", role: "", tags: "" };3132export function parseTags(text: string): string[] {33 return [...new Set(text.split(/[,\n]/).map((s) => s.trim()).filter(Boolean))];34}3536/** JobPlacement for the API, or undefined when no constraint was entered (the server then chooses). */37export function toPlacement(d: PlacementDraft): Schemas["JobPlacement"] | undefined {38 const tags = parseTags(d.tags);39 const p: Schemas["JobPlacement"] = {40 ...(d.nodeId ? { node_id: d.nodeId } : {}),41 ...(d.site.trim() ? { site: d.site.trim() } : {}),42 ...(d.role.trim() ? { role: d.role.trim() } : {}),43 ...(tags.length > 0 ? { tags } : {}),44 };45 return Object.keys(p).length > 0 ? p : undefined;46}47export function fromPlacement(p: Schemas["JobPlacement"] | undefined): PlacementDraft {48 return { nodeId: p?.node_id ?? null, site: p?.site ?? "", role: p?.role ?? "", tags: (p?.tags ?? []).join(", ") };49}5051const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;52/** Job ids, one per line or comma-separated. `invalid` lists what is not a UUID. */53export function parseDependencies(text: string): { ids: string[]; invalid: string[] } {54 const all = [...new Set(text.split(/[\s,]+/).map((s) => s.trim()).filter(Boolean))];55 return { ids: all.filter((s) => UUID.test(s)), invalid: all.filter((s) => !UUID.test(s)) };56}57