72 lines
2.7 KiB
TypeScript
72 lines
2.7 KiB
TypeScript
|
|
import type { AccountIndex } from './accounts'
|
||
|
|
// Savegames: earlier versions of each account project.
|
||
|
|
//
|
||
|
|
// Before a save replaces a project, its current state is kept as a version,
|
||
|
|
// at most one every 15 minutes (always before a restore and on deletion).
|
||
|
|
// Kept: the newest 30, plus the last version of each day for 90 days.
|
||
|
|
// Versions of a deleted project stay 30 days, so it can be restored.
|
||
|
|
|
||
|
|
import type { Project } from '../../app/lib/logic/types'
|
||
|
|
|
||
|
|
export interface Version {
|
||
|
|
project: Project
|
||
|
|
/** Revision and save time of the state that was kept. */
|
||
|
|
rev: number
|
||
|
|
at: number
|
||
|
|
name: string
|
||
|
|
}
|
||
|
|
|
||
|
|
const DAY = 86_400_000
|
||
|
|
export const SNAPSHOT_EVERY = 15 * 60_000
|
||
|
|
export const KEEP_NEWEST = 30
|
||
|
|
export const KEEP_DAILY_DAYS = 90
|
||
|
|
export const TRASH_DAYS = 30
|
||
|
|
|
||
|
|
const prefix = (uid: string, pid: string) => `${uid}:h:${pid}:`
|
||
|
|
|
||
|
|
/** Which versions (by time) to drop: all but the newest 30 and the last of each day for 90 days. */
|
||
|
|
export function versionsToDrop(times: number[], now: number): number[] {
|
||
|
|
const sorted = [...times].sort((a, b) => b - a)
|
||
|
|
const keep = new Set(sorted.slice(0, KEEP_NEWEST))
|
||
|
|
const days = new Set<string>()
|
||
|
|
for (const t of sorted) {
|
||
|
|
if (now - t > KEEP_DAILY_DAYS * DAY) continue
|
||
|
|
const day = new Date(t).toISOString().slice(0, 10)
|
||
|
|
if (!days.has(day)) {
|
||
|
|
days.add(day)
|
||
|
|
keep.add(t)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return sorted.filter(t => !keep.has(t))
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function versionTimes(uid: string, pid: string): Promise<number[]> {
|
||
|
|
const keys = await accounts().getKeys(prefix(uid, pid))
|
||
|
|
return keys
|
||
|
|
.filter(k => k.startsWith(prefix(uid, pid)))
|
||
|
|
.map(k => Number(k.slice(prefix(uid, pid).length)))
|
||
|
|
.filter(Number.isFinite)
|
||
|
|
.sort((a, b) => b - a)
|
||
|
|
}
|
||
|
|
|
||
|
|
export const readVersion = (uid: string, pid: string, t: number) => accounts().getItem<Version>(`${prefix(uid, pid)}${t}`)
|
||
|
|
|
||
|
|
/** Keeps the project's current state as a version, unless one was kept less than 15 minutes ago. */
|
||
|
|
export async function keepVersion(uid: string, pid: string, current: Version, force = false) {
|
||
|
|
const now = Date.now()
|
||
|
|
const times = await versionTimes(uid, pid)
|
||
|
|
if (!force && times[0] !== undefined && now - times[0] < SNAPSHOT_EVERY) return
|
||
|
|
await accounts().setItem(`${prefix(uid, pid)}${now}`, current)
|
||
|
|
for (const t of versionsToDrop([now, ...times], now)) await accounts().removeItem(`${prefix(uid, pid)}${t}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function dropVersions(uid: string, pid: string) {
|
||
|
|
for (const t of await versionTimes(uid, pid)) await accounts().removeItem(`${prefix(uid, pid)}${t}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
/** Deleted projects older than 30 days lose their versions for good. */
|
||
|
|
export async function emptyOldTrash(uid: string, index: AccountIndex) {
|
||
|
|
const cutoff = Date.now() - TRASH_DAYS * DAY
|
||
|
|
for (const d of index.deleted) if (d.at < cutoff) await dropVersions(uid, d.id)
|
||
|
|
}
|