203 lines
8.2 KiB
TypeScript
203 lines
8.2 KiB
TypeScript
import { t } from '../i18n'
|
|
import { CachedInstaller, InstallerKind, InstallerPhase, Locale, Task } from '../types'
|
|
|
|
// Match the finished-task retention period in internal/app/tasks.go.
|
|
export const finishedTaskTTL = 10 * 60 * 1000
|
|
|
|
export function pruneFinishedTasks(tasks: Record<string, Task>, cutoff: number): Record<string, Task> {
|
|
let next = tasks
|
|
for (const task of Object.values(tasks)) {
|
|
if (task.status === 'running' || task.updated_at >= cutoff) continue
|
|
if (next === tasks) next = { ...tasks }
|
|
delete next[task.id]
|
|
}
|
|
return next
|
|
}
|
|
|
|
/** Insert or replace a task by id; events can arrive out of order, keep the newest. */
|
|
export function upsertTask(prev: Record<string, Task>, task: Task): Record<string, Task> {
|
|
const cur = prev[task.id]
|
|
if (cur && cur.updated_at > task.updated_at && cur.status !== 'running') return prev
|
|
return { ...prev, [task.id]: task }
|
|
}
|
|
|
|
/** The task to show in the status bar: active work ahead of queued downloads, newest first. */
|
|
export function currentTask(all: Record<string, Task>): { task: Task | null; running: number } {
|
|
const running = Object.values(all).filter((x) => x.status === 'running')
|
|
running.sort((a, b) => {
|
|
const aq = a.installer_phase === 'queued' ? 1 : 0
|
|
const bq = b.installer_phase === 'queued' ? 1 : 0
|
|
if (aq !== bq) return aq - bq
|
|
return b.updated_at - a.updated_at
|
|
})
|
|
return { task: running[0] ?? null, running: running.length }
|
|
}
|
|
|
|
/** Running vm-create tasks whose VM has not appeared in the VM list yet, oldest first. */
|
|
export function creatingVMTasks(all: Record<string, Task>, existingNames: ReadonlySet<string>): Task[] {
|
|
return Object.values(all)
|
|
.filter((x) => x.status === 'running' && x.kind === 'vm-create' && !!x.subject && !existingNames.has(x.subject))
|
|
.sort((a, b) => a.started_at - b.started_at || a.subject.localeCompare(b.subject))
|
|
}
|
|
|
|
/** Localized title; the upstream step (English) goes in the tooltip. */
|
|
export function taskTitle(locale: Locale, task: Task): string {
|
|
switch (task.kind) {
|
|
case 'installer-download': return t(locale, 'taskDownload', { v: task.subject })
|
|
case 'recovery-download': return t(locale, 'taskRecoveryDownload', { name: task.subject })
|
|
case 'vm-create': return t(locale, 'taskCreate', { name: task.subject })
|
|
case 'runtime-install': return t(locale, 'taskRuntime', { v: task.subject })
|
|
case 'app-update': return t(locale, 'taskAppUpdate', { v: task.subject })
|
|
case 'plugin-download': return t(locale, 'taskPluginDownload', { name: task.subject })
|
|
default: return task.subject
|
|
}
|
|
}
|
|
|
|
/** "8.3 / 18.4 GB · 42 MB/s" or "12 / 50 MB · 512 KB/s" */
|
|
export function taskDetail(task: Task): string {
|
|
const parts: string[] = []
|
|
if (task.total > 0) parts.push(`${bytes(task.done)} / ${bytes(task.total)}`)
|
|
else if (task.done > 0) parts.push(bytes(task.done))
|
|
if (task.rate > 0) parts.push(rate(task.rate))
|
|
return parts.join(' · ')
|
|
}
|
|
|
|
export function isInstallerTask(task: Task): boolean {
|
|
return task.kind === 'installer-download' || task.kind === 'recovery-download' || !!task.installer_kind
|
|
}
|
|
|
|
export function taskInstallerKind(task: Task): InstallerKind | '' {
|
|
if (task.installer_kind === 'offline' || task.installer_kind === 'online') return task.installer_kind
|
|
if (task.kind === 'installer-download') return 'offline'
|
|
if (task.kind === 'recovery-download') return 'online'
|
|
return ''
|
|
}
|
|
|
|
export function taskInstallerVersion(task: Task): string {
|
|
const v = (task.installer_version ?? '').trim()
|
|
if (v) return v
|
|
if (task.kind === 'installer-download') return task.subject.trim()
|
|
if (task.kind === 'recovery-download') return 'latest'
|
|
return ''
|
|
}
|
|
|
|
export function taskInstallerPhase(task: Task): InstallerPhase | '' {
|
|
if (task.status !== 'running') return ''
|
|
if (task.installer_phase === 'queued' || task.installer_phase === 'downloading') return task.installer_phase
|
|
if (task.kind === 'installer-download' || task.kind === 'recovery-download') return 'downloading'
|
|
return ''
|
|
}
|
|
|
|
export function isActiveInstallerTask(task: Task): boolean {
|
|
return taskInstallerPhase(task) !== ''
|
|
}
|
|
|
|
export function taskInstallerKey(task: Task): string {
|
|
const kind = taskInstallerKind(task)
|
|
if (!kind) return ''
|
|
let v = taskInstallerVersion(task).toLowerCase()
|
|
if (kind === 'online' && !v) v = 'latest'
|
|
return `${kind}:${v}`
|
|
}
|
|
|
|
function matchesOnlineInstallerTitle(title: string | undefined, target: string): boolean {
|
|
const want = target.trim().toLowerCase()
|
|
if (!want) return false
|
|
const norm = (title ?? '').trim().toLowerCase().replace(/^macos\s+/, '')
|
|
return norm === want || norm.replace(/\s+/g, '-') === want
|
|
}
|
|
|
|
function matchesCachedInstaller(item: CachedInstaller, task: Task): boolean {
|
|
if (!isActiveInstallerTask(task)) return false
|
|
const kind = taskInstallerKind(task)
|
|
if (!kind || kind !== item.kind) return false
|
|
const v = taskInstallerVersion(task).toLowerCase()
|
|
if (kind === 'offline') {
|
|
if (item.version && item.version.toLowerCase() === v) return true
|
|
return item.partial && !!v && !/^\d/.test(v) && matchesOnlineInstallerTitle(item.title, v)
|
|
}
|
|
if (!v || v === 'latest') return item.partial
|
|
return matchesOnlineInstallerTitle(item.title, v)
|
|
}
|
|
|
|
export interface ActiveInstallerMatch {
|
|
primary: Task
|
|
waitingVMs: string[]
|
|
matchedIDs: string[]
|
|
}
|
|
|
|
function isStandaloneDownload(task: Task): boolean {
|
|
return task.kind === 'installer-download' || task.kind === 'recovery-download'
|
|
}
|
|
|
|
function pickPrimaryInstallerTask(matches: Task[]): ActiveInstallerMatch | null {
|
|
if (matches.length === 0) return null
|
|
const sorted = [...matches].sort((a, b) => {
|
|
const aStandalone = isStandaloneDownload(a) ? 0 : 1
|
|
const bStandalone = isStandaloneDownload(b) ? 0 : 1
|
|
if (aStandalone !== bStandalone) return aStandalone - bStandalone
|
|
const aQueued = taskInstallerPhase(a) === 'queued' ? 1 : 0
|
|
const bQueued = taskInstallerPhase(b) === 'queued' ? 1 : 0
|
|
if (aQueued !== bQueued) return aQueued - bQueued
|
|
return a.started_at - b.started_at
|
|
})
|
|
const primary = sorted[0]
|
|
const waitingVMs: string[] = []
|
|
for (const other of sorted.slice(1)) {
|
|
if (other.kind !== 'vm-create') continue
|
|
const vm = (other.vm || other.subject).trim()
|
|
if (vm && !waitingVMs.includes(vm)) waitingVMs.push(vm)
|
|
}
|
|
return { primary, waitingVMs, matchedIDs: sorted.map((x) => x.id) }
|
|
}
|
|
|
|
export function findActiveOfflineTask(tasks: Task[], version: string): ActiveInstallerMatch | null {
|
|
const want = version.trim().toLowerCase()
|
|
if (!want) return null
|
|
const matches = tasks.filter(
|
|
(x) => isActiveInstallerTask(x) && taskInstallerKind(x) === 'offline' && taskInstallerVersion(x).toLowerCase() === want,
|
|
)
|
|
return pickPrimaryInstallerTask(matches)
|
|
}
|
|
|
|
export function findActiveCachedTask(tasks: Task[], item: CachedInstaller): ActiveInstallerMatch | null {
|
|
const matches = tasks.filter((x) => matchesCachedInstaller(item, x))
|
|
return pickPrimaryInstallerTask(matches)
|
|
}
|
|
|
|
export function groupUnmatchedInstallerTasks(activeTasks: Task[], matchedTaskIDs: Set<string>): ActiveInstallerMatch[] {
|
|
const buckets = new Map<string, Task[]>()
|
|
for (const task of activeTasks) {
|
|
if (matchedTaskIDs.has(task.id)) continue
|
|
const key = taskInstallerKey(task)
|
|
if (!key) continue
|
|
const list = buckets.get(key)
|
|
if (list) list.push(task)
|
|
else buckets.set(key, [task])
|
|
}
|
|
const out: ActiveInstallerMatch[] = []
|
|
for (const group of buckets.values()) {
|
|
const picked = pickPrimaryInstallerTask(group)
|
|
if (picked) out.push(picked)
|
|
}
|
|
return out
|
|
}
|
|
|
|
export function taskStopAction(locale: Locale, task: Task): { kind: 'pause' | 'cancel'; label: string } {
|
|
if (task.kind === 'vm-create') return { kind: 'cancel', label: t(locale, 'cancelCreate') }
|
|
if (task.kind === 'app-update') return { kind: 'cancel', label: t(locale, 'updateCancel') }
|
|
if (taskInstallerPhase(task) === 'queued') return { kind: 'cancel', label: t(locale, 'cancelQueue') }
|
|
return { kind: 'pause', label: t(locale, 'pauseDownload') }
|
|
}
|
|
|
|
function bytes(b: number): string {
|
|
if (b >= 1e9) return `${(b / 1e9).toFixed(1)} GB`
|
|
return `${Math.max(1, Math.ceil(b / 1e6))} MB`
|
|
}
|
|
|
|
function rate(b: number): string {
|
|
if (b >= 1e9) return `${(b / 1e9).toFixed(1)} GB/s`
|
|
if (b >= 1e6) return `${(b / 1e6).toFixed(1)} MB/s`
|
|
return `${Math.max(1, Math.round(b / 1e3))} KB/s`
|
|
}
|