Say what the last Duplicati report was, so a machine with no jobs explains itself
Each machine's card shows whether the last report was a backup, another operation that was set aside, or one that could not be read (with the reason), and counts of each since it was added. Administrators can see the start of an unreadable report. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
64961b1141
commit
e517de35af
11 files changed
+9248
-9
No files matched your search
@@ -180,6 +180,66 @@ export async function sourceForToken(token: string): Promise<SourceRow | null> {
|
||||
return (await db.select().from(duplicatiSources).where(eq(duplicatiSources.tokenHash, hash(token))).limit(1))[0] ?? null;
|
||||
}
|
||||
|
||||
/** What the last report on an address turned out to be, so the page can say why a machine that is being heard has no jobs. */
|
||||
export interface SourceActivity {
|
||||
/** When it arrived (ISO). */
|
||||
at: string;
|
||||
/** "recorded": a backup, written down. "set_aside": a report about something other than a backup. "unreadable": not a report this app can use. */
|
||||
outcome: "recorded" | "set_aside" | "unreadable";
|
||||
/** The operation it was about (Backup, Compact, Repair …), when it said. */
|
||||
operation: string | null;
|
||||
/** How it ended (Success, Warning …), when it said. */
|
||||
result: string | null;
|
||||
/** The job it was about, when it said. */
|
||||
job: string | null;
|
||||
/** For an unreadable one: what was wrong with it, in a sentence. */
|
||||
reason: string | null;
|
||||
/** For an unreadable one: the start of what was sent, to see its form (shown to administrators only). */
|
||||
sample: string | null;
|
||||
/** How many reports of each kind have come since the machine was added. */
|
||||
counts: { recorded: number; setAside: number; unreadable: number };
|
||||
}
|
||||
|
||||
export const MAX_SAMPLE = 600;
|
||||
|
||||
/** Pure: why a body is not a report this app can use, in a sentence, and the start of it. Read for the same two forms parseReport reads. */
|
||||
export function explainUnreadable(body: unknown): { reason: string; sample: string | null } {
|
||||
const text = typeof body === "string" ? body.trim() : "";
|
||||
if (text === "") return { reason: "the body was empty (or not text) — Duplicati should post its result as text or JSON", sample: null };
|
||||
const sample = text.slice(0, MAX_SAMPLE);
|
||||
if (text.startsWith("{")) {
|
||||
let payload: Record<string, unknown> | null = null;
|
||||
try {
|
||||
payload = obj(JSON.parse(text));
|
||||
} catch {
|
||||
return { reason: "it looks like JSON but is not valid JSON (cut short, perhaps)", sample };
|
||||
}
|
||||
const data = Object.keys(obj(payload.Data)).length > 0 ? obj(payload.Data) : payload;
|
||||
const keys = Object.keys(data).slice(0, 8).join(", ");
|
||||
return { reason: `JSON without a ParsedResult field (it has: ${keys || "nothing"})`, sample };
|
||||
}
|
||||
return { reason: "not JSON, and no \"ParsedResult:\" line in it — set send-http-result-output-format=Json (or leave the default text, which has that line)", sample };
|
||||
}
|
||||
|
||||
/** Pure: the activity after one more report. */
|
||||
export function nextActivity(previous: SourceActivity | null, at: string, report: Pick<SourceActivity, "outcome" | "operation" | "result" | "job" | "reason" | "sample">): SourceActivity {
|
||||
const counts = { recorded: previous?.counts.recorded ?? 0, setAside: previous?.counts.setAside ?? 0, unreadable: previous?.counts.unreadable ?? 0 };
|
||||
if (report.outcome === "recorded") counts.recorded++;
|
||||
else if (report.outcome === "set_aside") counts.setAside++;
|
||||
else counts.unreadable++;
|
||||
return { at, ...report, counts };
|
||||
}
|
||||
|
||||
export function unpackActivity(stored: string | null): SourceActivity | null {
|
||||
if (!stored) return null;
|
||||
try {
|
||||
const v = JSON.parse(stored) as SourceActivity;
|
||||
return v && typeof v === "object" && typeof v.outcome === "string" && v.counts ? v : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export type RecordOutcome = { recorded: true; backupId: number; result: string } | { recorded: false; reason: "unknown_token" | "unreadable" | "not_a_backup" };
|
||||
|
||||
/** Reads a report that arrived on a secret address and writes it down. Other operations than a backup are said to have been seen and set aside. */
|
||||
@@ -187,10 +247,22 @@ export async function recordReport(token: string, body: unknown, now: Date = new
|
||||
const source = await sourceForToken(token);
|
||||
if (!source) return { recorded: false, reason: "unknown_token" };
|
||||
const nowIso = now.toISOString();
|
||||
await db.update(duplicatiSources).set({ lastReportAt: nowIso }).where(eq(duplicatiSources.id, source.id));
|
||||
const previous = unpackActivity(source.lastReport);
|
||||
/** Writes down that a report came, and what it was. */
|
||||
const note = async (what: Parameters<typeof nextActivity>[2]) => {
|
||||
await db.update(duplicatiSources).set({ lastReportAt: nowIso, lastReport: JSON.stringify(nextActivity(previous, nowIso, what)) }).where(eq(duplicatiSources.id, source.id));
|
||||
};
|
||||
const report = parseReport(body);
|
||||
if (!report) return { recorded: false, reason: "unreadable" };
|
||||
if (!/^backup$/i.test(report.operation)) return { recorded: false, reason: "not_a_backup" };
|
||||
if (!report) {
|
||||
const why = explainUnreadable(body);
|
||||
await note({ outcome: "unreadable", operation: null, result: null, job: null, reason: why.reason, sample: why.sample });
|
||||
return { recorded: false, reason: "unreadable" };
|
||||
}
|
||||
if (!/^backup$/i.test(report.operation)) {
|
||||
await note({ outcome: "set_aside", operation: report.operation.slice(0, 60), result: report.result.slice(0, 30), job: report.backupName, reason: null, sample: null });
|
||||
return { recorded: false, reason: "not_a_backup" };
|
||||
}
|
||||
await note({ outcome: "recorded", operation: "Backup", result: report.result.slice(0, 30), job: report.backupName, reason: null, sample: null });
|
||||
|
||||
const [before] = await db.select().from(duplicatiBackups).where(and(eq(duplicatiBackups.sourceId, source.id), eq(duplicatiBackups.name, report.backupName))).limit(1);
|
||||
const good = isGood(report.result);
|
||||
@@ -293,6 +365,7 @@ export async function currentProblems(nowMs: number = Date.now()): Promise<Dupli
|
||||
}
|
||||
|
||||
/** A source as lists show it: everything but the hash. */
|
||||
export function publicSource(row: SourceRow) {
|
||||
return { id: row.id, name: row.name, tokenPrefix: row.tokenPrefix, serverId: row.serverId, createdBy: row.createdBy, createdAt: row.createdAt, lastReportAt: row.lastReportAt };
|
||||
export function publicSource(row: SourceRow, showSample = false) {
|
||||
const activity = unpackActivity(row.lastReport);
|
||||
return { id: row.id, name: row.name, tokenPrefix: row.tokenPrefix, serverId: row.serverId, createdBy: row.createdBy, createdAt: row.createdAt, lastReportAt: row.lastReportAt, lastReport: activity && { ...activity, sample: showSample ? activity.sample : null } };
|
||||
}
|
||||
Reference in new issue
Block a user