making project ids 1 based

This commit is contained in:
C-West8
2026-10-23 11:32:30 -05:00
parent a9e31acaa4
commit fbd81b0b45
33 changed files with 215 additions and 160 deletions

View File

@@ -28,7 +28,7 @@ cp .env.example .env # simulated mode, admin login admin / change-me
npm run dev # http://localhost:5288
```
In simulated mode, SharePoint and ACC are in-memory stand-ins. A demo project is created at startup, filled with sample files, and edited at random every 45 seconds so there's something to watch. One file (`Corrupt model.rvt`) is always rejected, so the issues list has something in it. Nothing persists: a restart starts over.
In simulated mode, SharePoint and ACC are in-memory stand-ins. A demo project is created at startup, filled with sample files, and edited at random every 45 seconds so there's something to watch. One file (`Models/Architectural/Corrupt model.rvt`) is always rejected, so the issues list has something in it. Nothing persists: a restart starts over.
| Command | What it does |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------- |

View File

@@ -1,5 +1,5 @@
CREATE TABLE `items` (
`project_id` text NOT NULL,
`project_id` integer NOT NULL,
`source_id` text NOT NULL,
`parent_source_id` text NOT NULL,
`name` text NOT NULL,
@@ -24,7 +24,7 @@ CREATE TABLE `items` (
--> statement-breakpoint
CREATE INDEX `items_project_status` ON `items` (`project_id`,`status`);--> statement-breakpoint
CREATE TABLE `projects` (
`id` text PRIMARY KEY NOT NULL,
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`enabled` integer DEFAULT true NOT NULL,
`paused_reason` text,
@@ -44,8 +44,8 @@ CREATE TABLE `projects` (
--> statement-breakpoint
CREATE TABLE `run_events` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`run_id` text NOT NULL,
`project_id` text NOT NULL,
`run_id` integer NOT NULL,
`project_id` integer NOT NULL,
`source_id` text NOT NULL,
`path` text NOT NULL,
`action` text NOT NULL,
@@ -56,8 +56,8 @@ CREATE TABLE `run_events` (
--> statement-breakpoint
CREATE INDEX `run_events_run` ON `run_events` (`run_id`);--> statement-breakpoint
CREATE TABLE `runs` (
`id` text PRIMARY KEY NOT NULL,
`project_id` text NOT NULL,
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`project_id` integer NOT NULL,
`trigger` text NOT NULL,
`started_at` integer NOT NULL,
`finished_at` integer,

View File

@@ -1,7 +1,7 @@
{
"version": "6",
"dialect": "sqlite",
"id": "a7309db7-6811-48f5-bb28-822a92adf5d5",
"id": "e1cc8fc1-3ac7-41c4-915b-3d703eccd229",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"items": {
@@ -9,7 +9,7 @@
"columns": {
"project_id": {
"name": "project_id",
"type": "text",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
@@ -184,10 +184,10 @@
"columns": {
"id": {
"name": "id",
"type": "text",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": false
"autoincrement": true
},
"name": {
"name": "name",
@@ -314,14 +314,14 @@
},
"run_id": {
"name": "run_id",
"type": "text",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"project_id": {
"name": "project_id",
"type": "text",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
@@ -395,14 +395,14 @@
"columns": {
"id": {
"name": "id",
"type": "text",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": false
"autoincrement": true
},
"project_id": {
"name": "project_id",
"type": "text",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false

View File

@@ -5,7 +5,7 @@
{
"idx": 0,
"version": "6",
"when": 1792725790862,
"when": 1792772657596,
"tag": "0000_init",
"breakpoints": true
}

View File

@@ -24,7 +24,10 @@
for (const node of list) {
if (attentionOnly && !needsAttention(node)) continue;
result.push({ node, depth });
if (node.isFolder && !collapsed.has(node.sourceId)) walk(node.children, depth + 1);
// When showing only problems, every folder on the way stays open so each problem
// shows where it lives.
const open = attentionOnly || !collapsed.has(node.sourceId);
if (node.isFolder && open) walk(node.children, depth + 1);
}
};
walk(nodes, 0);
@@ -72,7 +75,7 @@
<tr class:folder={node.isFolder}>
<td class="name" style:--depth={depth}>
<div class="name-line">
{#if node.isFolder}
{#if node.isFolder && !attentionOnly}
<button
type="button"
class="twisty"
@@ -82,15 +85,16 @@
>
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M6 4l4 4-4 4" /></svg>
</button>
<svg class="icon" viewBox="0 0 16 16" aria-hidden="true">
<path d="M1.5 3.5h5l1.5 1.5h6.5v8h-13z" />
</svg>
{:else}
<span class="twisty-spacer"></span>
<svg class="icon" viewBox="0 0 16 16" aria-hidden="true">
<path d="M3.5 1.5h6l3 3v10h-9z M9.5 1.5v3h3" />
</svg>
{/if}
<svg class="icon" viewBox="0 0 16 16" aria-hidden="true">
{#if node.isFolder}
<path d="M1.5 3.5h5l1.5 1.5h6.5v8h-13z" />
{:else}
<path d="M3.5 1.5h6l3 3v10h-9z M9.5 1.5v3h3" />
{/if}
</svg>
<span title={node.error ?? undefined}>{node.name}</span>
{#if node.problems > 0}
<span class="problems">{node.problems} failed inside</span>

View File

@@ -8,14 +8,19 @@ import type {
import { FakeAcc } from './fake-acc.ts';
import { FakeDrive } from './fake-drive.ts';
/** A file the simulated ACC always rejects, so the issues list has something in it. */
/**
* A file the simulated ACC always rejects, so the issues list has something in it. It sits
* two folders down, next to files that sync fine, to show where problems are in the tree.
*/
export const REJECTED_FILE = 'Corrupt model.rvt';
const SAMPLE_TREE: Record<string, string[]> = {
Drawings: ['A-101 Floor Plan.pdf', 'A-201 Elevations.pdf', 'S-101 Foundations.dwg'],
'Models/Architectural': ['Architectural model.rvt', REJECTED_FILE],
'Models/Structural': ['Structural model.rvt'],
Specifications: ['Division 08 - Openings.docx', 'Division 26 - Electrical.docx'],
'Submittals/Approved': ['Door hardware.pdf'],
'': ['Project Directory.xlsx', REJECTED_FILE, '~$Project Directory.xlsx']
'': ['Project Directory.xlsx', '~$Project Directory.xlsx']
};
/**

View File

@@ -14,7 +14,7 @@ type Tx = Parameters<Parameters<Db['transaction']>[0]>[0];
export class SqliteItemRepository implements ItemRepository {
constructor(private readonly db: Db) {}
async get(projectId: string, sourceId: string): Promise<ItemState | null> {
async get(projectId: number, sourceId: string): Promise<ItemState | null> {
const row = this.db
.select()
.from(items)
@@ -23,11 +23,11 @@ export class SqliteItemRepository implements ItemRepository {
return row ? toItem(row) : null;
}
async listByProject(projectId: string): Promise<ItemState[]> {
async listByProject(projectId: number): Promise<ItemState[]> {
return this.db.select().from(items).where(eq(items.projectId, projectId)).all().map(toItem);
}
async countByStatus(projectId: string): Promise<StatusCounts> {
async countByStatus(projectId: number): Promise<StatusCounts> {
const rows = this.db
.select({ status: items.status, total: count() })
.from(items)
@@ -39,19 +39,19 @@ export class SqliteItemRepository implements ItemRepository {
return counts;
}
async commitIngest(projectId: string, states: ItemState[], cursor: string): Promise<void> {
async commitIngest(projectId: number, states: ItemState[], cursor: string): Promise<void> {
this.db.transaction((tx) => {
upsert(tx, projectId, states);
tx.update(projects).set({ deltaCursor: cursor }).where(eq(projects.id, projectId)).run();
});
}
async save(projectId: string, states: ItemState[]): Promise<void> {
async save(projectId: number, states: ItemState[]): Promise<void> {
this.db.transaction((tx) => upsert(tx, projectId, states));
}
}
function upsert(tx: Tx, projectId: string, states: ItemState[]): void {
function upsert(tx: Tx, projectId: number, states: ItemState[]): void {
for (const state of states) {
const row = toRow(projectId, state);
tx.insert(items)
@@ -61,7 +61,7 @@ function upsert(tx: Tx, projectId: string, states: ItemState[]): void {
}
}
function toRow(projectId: string, { sourceId, desired, synced, ...rest }: ItemState): ItemRow {
function toRow(projectId: number, { sourceId, desired, synced, ...rest }: ItemState): ItemRow {
return {
projectId,
sourceId,

View File

@@ -13,7 +13,7 @@ export class SqliteProjectRepository implements ProjectRepository {
private readonly clock: Clock
) {}
async get(id: string): Promise<Project | null> {
async get(id: number): Promise<Project | null> {
const row = this.db.select().from(projects).where(eq(projects.id, id)).get();
return row ? toProject(row) : null;
}
@@ -26,41 +26,37 @@ export class SqliteProjectRepository implements ProjectRepository {
const row = this.db
.insert(projects)
// A NewProject supplies every editable field, so toColumns returns them all.
.values({
...toColumns(project),
id: crypto.randomUUID(),
createdAt: this.clock()
} as ProjectInsert)
.values({ ...toColumns(project), createdAt: this.clock() } as ProjectInsert)
.returning()
.get();
return toProject(row);
}
async update(id: string, changes: ProjectChanges): Promise<void> {
async update(id: number, changes: ProjectChanges): Promise<void> {
this.set(id, toColumns(changes));
}
async delete(id: string): Promise<void> {
async delete(id: number): Promise<void> {
this.db.delete(projects).where(eq(projects.id, id)).run();
}
async pause(id: string, reason: string): Promise<void> {
async pause(id: number, reason: string): Promise<void> {
this.set(id, { pausedReason: reason });
}
async resume(id: string): Promise<void> {
async resume(id: number): Promise<void> {
this.set(id, { pausedReason: null });
}
async markFirstSynced(id: string, at: Date): Promise<void> {
async markFirstSynced(id: number, at: Date): Promise<void> {
this.set(id, { firstSyncedAt: at });
}
async setLastRunAt(id: string, at: Date): Promise<void> {
async setLastRunAt(id: number, at: Date): Promise<void> {
this.set(id, { lastRunAt: at });
}
async reset(id: string): Promise<void> {
async reset(id: number): Promise<void> {
this.db.transaction((tx) => {
tx.delete(items).where(eq(items.projectId, id)).run();
tx.update(projects)
@@ -70,7 +66,7 @@ export class SqliteProjectRepository implements ProjectRepository {
});
}
private set(id: string, values: Partial<ProjectRow>): void {
private set(id: number, values: Partial<ProjectRow>): void {
const defined = Object.entries(values).filter(([, value]) => value !== undefined);
if (defined.length === 0) return;
this.db.update(projects).set(Object.fromEntries(defined)).where(eq(projects.id, id)).run();

View File

@@ -2,6 +2,14 @@ import { describe, expect, it } from 'vitest';
import { createHarness } from '#lib/server/sync/testing/harness.ts';
describe('SqliteRunRepository', () => {
it('numbers runs from 1', async () => {
const h = await createHarness();
const runs = [await h.sync(), await h.sync()];
expect(runs.map((run) => run.runId)).toEqual([1, 2]);
});
it('prunes old runs together with their events', async () => {
const h = await createHarness();
h.drive.addFile(h.rootId, 'a.txt');

View File

@@ -15,14 +15,17 @@ type RunRow = typeof runs.$inferSelect;
export class SqliteRunRepository implements RunRepository {
constructor(private readonly db: Db) {}
async start(projectId: string, trigger: RunTrigger, at: Date): Promise<string> {
const id = crypto.randomUUID();
this.db.insert(runs).values({ id, projectId, trigger, startedAt: at }).run();
async start(projectId: number, trigger: RunTrigger, at: Date): Promise<number> {
const { id } = this.db
.insert(runs)
.values({ projectId, trigger, startedAt: at })
.returning({ id: runs.id })
.get();
return id;
}
async finish(
runId: string,
runId: number,
result: { finishedAt: Date; outcome: RunOutcome; error: string | null; counts: RunCounts }
): Promise<void> {
const { finishedAt, outcome, error, counts } = result;
@@ -37,7 +40,7 @@ export class SqliteRunRepository implements RunRepository {
this.db.insert(runEvents).values(event).run();
}
async latest(projectId: string): Promise<RunRecord | null> {
async latest(projectId: number): Promise<RunRecord | null> {
const row = this.db
.select()
.from(runs)
@@ -48,7 +51,7 @@ export class SqliteRunRepository implements RunRepository {
return row ? toRunRecord(row) : null;
}
async listSince(projectId: string, since: Date): Promise<RunRecord[]> {
async listSince(projectId: number, since: Date): Promise<RunRecord[]> {
return this.db
.select()
.from(runs)
@@ -58,7 +61,7 @@ export class SqliteRunRepository implements RunRepository {
.map(toRunRecord);
}
async listEvents(runId: string): Promise<RunEvent[]> {
async listEvents(runId: number): Promise<RunEvent[]> {
return this.db
.select()
.from(runEvents)

View File

@@ -5,8 +5,12 @@ const timestamp = (name: string) => integer(name, { mode: 'timestamp_ms' });
const flag = (name: string) => integer(name, { mode: 'boolean' });
const count = (name: string) => integer(name).notNull().default(0);
// IDs count up from 1 and are never reused, even after a delete, so old links never point
// at a different project or run.
const id = () => integer('id').primaryKey({ autoIncrement: true });
export const projects = sqliteTable('projects', {
id: text('id').primaryKey(),
id: id(),
name: text('name').notNull(),
enabled: flag('enabled').notNull().default(true),
pausedReason: text('paused_reason'),
@@ -27,7 +31,7 @@ export const projects = sqliteTable('projects', {
export const items = sqliteTable(
'items',
{
projectId: text('project_id')
projectId: integer('project_id')
.notNull()
.references(() => projects.id, { onDelete: 'cascade' }),
sourceId: text('source_id').notNull(),
@@ -61,8 +65,8 @@ export const items = sqliteTable(
export const runs = sqliteTable(
'runs',
{
id: text('id').primaryKey(),
projectId: text('project_id')
id: id(),
projectId: integer('project_id')
.notNull()
.references(() => projects.id, { onDelete: 'cascade' }),
trigger: text('trigger', { enum: ['schedule', 'manual'] }).notNull(),
@@ -84,11 +88,11 @@ export const runs = sqliteTable(
export const runEvents = sqliteTable(
'run_events',
{
id: integer('id').primaryKey({ autoIncrement: true }),
runId: text('run_id')
id: id(),
runId: integer('run_id')
.notNull()
.references(() => runs.id, { onDelete: 'cascade' }),
projectId: text('project_id').notNull(),
projectId: integer('project_id').notNull(),
sourceId: text('source_id').notNull(),
path: text('path').notNull(),
action: text('action', { enum: EVENT_ACTIONS }).notNull(),

8
src/lib/server/params.ts Normal file
View File

@@ -0,0 +1,8 @@
import { error } from '@sveltejs/kit';
/** An ID from the URL. Anything that isn't a whole number from 1 up is treated as not found. */
export function idParam(value: string): number {
const id = Number(value);
if (!/^[1-9]\d*$/.test(value) || !Number.isSafeInteger(id)) error(404, 'Not found');
return id;
}

View File

@@ -65,6 +65,6 @@ describe('StatusService', () => {
const run = await h.sync();
expect(await h.status.runEvents(h.project.id, run.runId)).toHaveLength(1);
expect(await h.status.runEvents('other-project', run.runId)).toEqual([]);
expect(await h.status.runEvents(h.project.id + 1, run.runId)).toEqual([]);
});
});

View File

@@ -38,7 +38,7 @@ export interface StatusServiceDeps {
projects: ProjectRepository;
items: ItemRepository;
runs: RunRepository;
activity: { isRunning(projectId: string): boolean };
activity: { isRunning(projectId: number): boolean };
clock: Clock;
retentionDays: number;
}
@@ -52,12 +52,12 @@ export class StatusService {
return Promise.all(projects.map((project) => this.summarize(project)));
}
async getSummary(id: string): Promise<ProjectSummary | null> {
async getSummary(id: number): Promise<ProjectSummary | null> {
const project = await this.deps.projects.get(id);
return project ? this.summarize(project) : null;
}
async getProject(id: string): Promise<ProjectDetail | null> {
async getProject(id: number): Promise<ProjectDetail | null> {
const project = await this.deps.projects.get(id);
if (!project) return null;
@@ -85,12 +85,12 @@ export class StatusService {
};
}
async history(projectId: string): Promise<RunRecord[]> {
async history(projectId: number): Promise<RunRecord[]> {
const since = new Date(this.deps.clock().getTime() - this.deps.retentionDays * 86_400_000);
return this.deps.runs.listSince(projectId, since);
}
async runEvents(projectId: string, runId: string): Promise<RunEvent[]> {
async runEvents(projectId: number, runId: number): Promise<RunEvent[]> {
const events = await this.deps.runs.listEvents(runId);
return events.filter((event) => event.projectId === projectId);
}

View File

@@ -5,20 +5,20 @@ import type { ProjectRepository } from './core/ports.ts';
import { SyncCoordinator } from './coordinator.ts';
import { silentLogger } from './testing/harness.ts';
function project(id: string, overrides: Partial<Project> = {}): Project {
function project(id: number, overrides: Partial<Project> = {}): Project {
return { id, enabled: true, pausedReason: null, ...overrides } as Project;
}
/** A runner whose runs stay in flight until `finish` is called. */
class ControlledRunner implements ProjectSyncRunner {
readonly started: Array<[string, RunTrigger]> = [];
readonly started: Array<[number, RunTrigger]> = [];
private readonly pending: Array<() => void> = [];
runProject(projectId: string, trigger: RunTrigger) {
runProject(projectId: number, trigger: RunTrigger) {
this.started.push([projectId, trigger]);
return new Promise<never>((resolve) => {
this.pending.push(() =>
resolve({ runId: 'r', outcome: 'success', error: null, counts: emptyCounts() } as never)
resolve({ runId: 1, outcome: 'success', error: null, counts: emptyCounts() } as never)
);
});
}
@@ -32,7 +32,7 @@ function setup(projects: Project[]) {
let now = 0;
const runner = new ControlledRunner();
const repo = {
get: async (id: string) => projects.find((p) => p.id === id) ?? null,
get: async (id: number) => projects.find((p) => p.id === id) ?? null,
list: async () => projects
} as ProjectRepository;
const coordinator = new SyncCoordinator({
@@ -47,67 +47,69 @@ function setup(projects: Project[]) {
describe('SyncCoordinator', () => {
it('starts a manual run and reports it as already running on a second click', async () => {
const { runner, coordinator } = setup([project('p1')]);
const { runner, coordinator } = setup([project(1)]);
expect(await coordinator.requestManualRun('p1')).toEqual({ status: 'started' });
expect(await coordinator.requestManualRun('p1')).toEqual({ status: 'already-running' });
expect(runner.started).toEqual([['p1', 'manual']]);
expect(await coordinator.requestManualRun(1)).toEqual({ status: 'started' });
expect(await coordinator.requestManualRun(1)).toEqual({ status: 'already-running' });
expect(runner.started).toEqual([[1, 'manual']]);
});
it('enforces the manual cooldown after a run finishes', async () => {
const { runner, coordinator, advance } = setup([project('p1')]);
await coordinator.requestManualRun('p1');
const { runner, coordinator, advance } = setup([project(1)]);
await coordinator.requestManualRun(1);
runner.finishAll();
await coordinator.drain();
advance(10_000);
expect(await coordinator.requestManualRun('p1')).toEqual({
expect(await coordinator.requestManualRun(1)).toEqual({
status: 'cooldown',
retryAfterSeconds: 20
});
advance(20_000);
expect(await coordinator.requestManualRun('p1')).toEqual({ status: 'started' });
expect(await coordinator.requestManualRun(1)).toEqual({ status: 'started' });
});
it('keeps cooldowns per project', async () => {
const { coordinator } = setup([project('p1'), project('p2')]);
await coordinator.requestManualRun('p1');
expect(await coordinator.requestManualRun('p2')).toEqual({ status: 'started' });
const { coordinator } = setup([project(1), project(2)]);
await coordinator.requestManualRun(1);
expect(await coordinator.requestManualRun(2)).toEqual({ status: 'started' });
});
it('refuses manual runs for disabled, paused or unknown projects', async () => {
const [off, paused, unknown] = [1, 2, 99];
const { coordinator } = setup([
project('off', { enabled: false }),
project('paused', { pausedReason: 'Root deleted' })
project(off, { enabled: false }),
project(paused, { pausedReason: 'Root deleted' })
]);
expect(await coordinator.requestManualRun('off')).toMatchObject({ status: 'unavailable' });
expect(await coordinator.requestManualRun('paused')).toEqual({
expect(await coordinator.requestManualRun(off)).toMatchObject({ status: 'unavailable' });
expect(await coordinator.requestManualRun(paused)).toEqual({
status: 'unavailable',
reason: 'Root deleted'
});
expect(await coordinator.requestManualRun('nope')).toMatchObject({ status: 'unavailable' });
expect(await coordinator.requestManualRun(unknown)).toMatchObject({ status: 'unavailable' });
});
it('runs scheduled passes sequentially, skipping busy, disabled and paused projects', async () => {
const [busy, off, paused, a, b] = [1, 2, 3, 4, 5];
const { runner, coordinator } = setup([
project('busy'),
project('off', { enabled: false }),
project('paused', { pausedReason: 'x' }),
project('a'),
project('b')
project(busy),
project(off, { enabled: false }),
project(paused, { pausedReason: 'x' }),
project(a),
project(b)
]);
await coordinator.requestManualRun('busy');
await coordinator.requestManualRun(busy);
const pass = coordinator.runScheduledPass();
await Promise.resolve();
await Promise.resolve();
expect(runner.started.map(([id]) => id)).toEqual(['busy', 'a']);
expect(runner.started.map(([id]) => id)).toEqual([busy, a]);
runner.finishAll();
await new Promise((r) => setTimeout(r));
expect(runner.started.map(([id]) => id)).toEqual(['busy', 'a', 'b']);
expect(runner.started.map(([id]) => id)).toEqual([busy, a, b]);
runner.finishAll();
await pass;

View File

@@ -22,16 +22,16 @@ export interface SyncCoordinatorDeps {
* time and rate-limits manual "Run now" requests per project.
*/
export class SyncCoordinator {
private readonly active = new Map<string, Promise<void>>();
private readonly lastManualRequest = new Map<string, number>();
private readonly active = new Map<number, Promise<void>>();
private readonly lastManualRequest = new Map<number, number>();
constructor(private readonly deps: SyncCoordinatorDeps) {}
isRunning(projectId: string): boolean {
isRunning(projectId: number): boolean {
return this.active.has(projectId);
}
async requestManualRun(projectId: string): Promise<RunRequestResult> {
async requestManualRun(projectId: number): Promise<RunRequestResult> {
const project = await this.deps.projects.get(projectId);
if (!project) return { status: 'unavailable', reason: 'Project not found.' };
if (!project.enabled) return { status: 'unavailable', reason: 'Project is disabled.' };
@@ -65,7 +65,7 @@ export class SyncCoordinator {
await Promise.all(this.active.values());
}
private launch(projectId: string, trigger: RunTrigger): Promise<void> {
private launch(projectId: number, trigger: RunTrigger): Promise<void> {
const run = this.deps.runner
.runProject(projectId, trigger)
.then(() => undefined)

View File

@@ -20,7 +20,7 @@ import type {
import type { Reconciler } from './reconciler.ts';
export interface ProjectSyncRunner {
runProject(projectId: string, trigger: RunTrigger): Promise<RunResult>;
runProject(projectId: number, trigger: RunTrigger): Promise<RunResult>;
}
export interface SyncEngineDeps {
@@ -39,7 +39,7 @@ export interface SyncEngineDeps {
export class SyncEngine implements ProjectSyncRunner {
constructor(private readonly deps: SyncEngineDeps) {}
async runProject(projectId: string, trigger: RunTrigger): Promise<RunResult> {
async runProject(projectId: number, trigger: RunTrigger): Promise<RunResult> {
const { projects, items, runs, clock, logger } = this.deps;
const project = await projects.get(projectId);
if (!project) throw new Error(`Project ${projectId} not found.`);

View File

@@ -14,7 +14,7 @@ export interface TargetRoot {
}
export interface Project {
id: string;
id: number;
name: string;
enabled: boolean;
/** Set when the sync refuses to continue (e.g. the SharePoint root vanished). */
@@ -137,7 +137,7 @@ export function emptyStatusCounts(): StatusCounts {
}
export interface RunResult {
runId: string;
runId: number;
outcome: RunOutcome;
error: string | null;
counts: RunCounts;
@@ -146,15 +146,15 @@ export interface RunResult {
export interface RunRecord extends Omit<RunResult, 'outcome'> {
/** Null while the run is still in progress. */
outcome: RunOutcome | null;
projectId: string;
projectId: number;
trigger: RunTrigger;
startedAt: Date;
finishedAt: Date | null;
}
export interface RunEvent {
runId: string;
projectId: string;
runId: number;
projectId: number;
sourceId: string;
path: string;
action: EventAction;

View File

@@ -73,39 +73,41 @@ export interface TargetRootResolver {
}
export interface ProjectRepository {
get(id: string): Promise<Project | null>;
get(id: number): Promise<Project | null>;
list(): Promise<Project[]>;
/** Assigns the next ID: 1, 2, 3... */
create(project: NewProject): Promise<Project>;
update(id: string, changes: ProjectChanges): Promise<void>;
update(id: number, changes: ProjectChanges): Promise<void>;
/** Removes the project with all its items and history. */
delete(id: string): Promise<void>;
pause(id: string, reason: string): Promise<void>;
resume(id: string): Promise<void>;
markFirstSynced(id: string, at: Date): Promise<void>;
setLastRunAt(id: string, at: Date): Promise<void>;
delete(id: number): Promise<void>;
pause(id: number, reason: string): Promise<void>;
resume(id: number): Promise<void>;
markFirstSynced(id: number, at: Date): Promise<void>;
setLastRunAt(id: number, at: Date): Promise<void>;
/** Forget all sync state so the project starts over from a full listing. */
reset(id: string): Promise<void>;
reset(id: number): Promise<void>;
}
export interface ItemRepository {
get(projectId: string, sourceId: string): Promise<ItemState | null>;
listByProject(projectId: string): Promise<ItemState[]>;
countByStatus(projectId: string): Promise<StatusCounts>;
get(projectId: number, sourceId: string): Promise<ItemState | null>;
listByProject(projectId: number): Promise<ItemState[]>;
countByStatus(projectId: number): Promise<StatusCounts>;
/** Atomically store ingested changes and the cursor they were read up to. */
commitIngest(projectId: string, items: ItemState[], cursor: string): Promise<void>;
save(projectId: string, items: ItemState[]): Promise<void>;
commitIngest(projectId: number, items: ItemState[], cursor: string): Promise<void>;
save(projectId: number, items: ItemState[]): Promise<void>;
}
export interface RunRepository {
start(projectId: string, trigger: RunTrigger, at: Date): Promise<string>;
/** Records a new run and returns its ID (1, 2, 3... across all projects). */
start(projectId: number, trigger: RunTrigger, at: Date): Promise<number>;
finish(
runId: string,
runId: number,
result: { finishedAt: Date; outcome: RunOutcome; error: string | null; counts: RunCounts }
): Promise<void>;
addEvent(event: RunEvent): Promise<void>;
latest(projectId: string): Promise<RunRecord | null>;
listSince(projectId: string, since: Date): Promise<RunRecord[]>;
listEvents(runId: string): Promise<RunEvent[]>;
latest(projectId: number): Promise<RunRecord | null>;
listSince(projectId: number, since: Date): Promise<RunRecord[]>;
listEvents(runId: number): Promise<RunEvent[]>;
/** Delete runs (and their events) started before `cutoff`; returns how many runs were removed. */
pruneBefore(cutoff: Date): Promise<number>;
/** Close every run left unfinished (e.g. by a restart) as failed; returns how many were closed. */

View File

@@ -44,7 +44,7 @@ export class Reconciler {
async reconcile(
project: Project,
runId: string,
runId: number,
items: Map<string, ItemState>
): Promise<ReconcileResult> {
const { maxAttempts } = this.deps;

View File

@@ -20,6 +20,18 @@ describe('ProjectService', () => {
});
});
it('numbers projects from 1 and never reuses a deleted project number', async () => {
const h = await createHarness();
const input = { name: 'Tower B', sourceInput: 'Tower B', targetInput: 'Tower B Docs' };
expect(h.project.id).toBe(1);
const second = await h.projectService.create(input);
await h.projectService.delete(second.id);
const third = await h.projectService.create(input);
expect([second.id, third.id]).toEqual([2, 3]);
});
it('rejects blank names and locations', async () => {
const h = await createHarness();
await expect(

View File

@@ -31,7 +31,7 @@ export interface ProjectServiceDeps {
items: ItemRepository;
sourceResolver: SourceRootResolver;
targetResolver: TargetRootResolver;
activity: { isRunning(projectId: string): boolean };
activity: { isRunning(projectId: number): boolean };
maxAttempts: number;
}
@@ -53,7 +53,7 @@ export class ProjectService {
});
}
async update(id: string, edit: ProjectEdit): Promise<void> {
async update(id: number, edit: ProjectEdit): Promise<void> {
const project = await this.require(id);
const changes: ProjectChanges = { name: validName(edit.name), enabled: edit.enabled };
const sourceInput = edit.sourceInput?.trim();
@@ -73,24 +73,24 @@ export class ProjectService {
await this.deps.projects.update(id, changes);
}
async resume(id: string): Promise<void> {
async resume(id: number): Promise<void> {
await this.require(id);
await this.deps.projects.resume(id);
}
/** Forget all sync state; the next run starts from a full listing and roots unlock. */
async reset(id: string): Promise<void> {
async reset(id: number): Promise<void> {
await this.requireIdle(id);
await this.deps.projects.reset(id);
}
async delete(id: string): Promise<void> {
async delete(id: number): Promise<void> {
await this.requireIdle(id);
await this.deps.projects.delete(id);
}
/** Give a failed item a fresh set of attempts on the next run. */
async retryItem(projectId: string, sourceId: string): Promise<void> {
async retryItem(projectId: number, sourceId: string): Promise<void> {
await this.requireIdle(projectId);
const item = await this.deps.items.get(projectId, sourceId);
if (!item) throw new InvalidRequestError('That file is no longer tracked.');
@@ -99,13 +99,13 @@ export class ProjectService {
await this.deps.items.save(projectId, [fresh]);
}
private async require(id: string): Promise<Project> {
private async require(id: number): Promise<Project> {
const project = await this.deps.projects.get(id);
if (!project) throw new InvalidRequestError('Project not found.');
return project;
}
private async requireIdle(id: string): Promise<Project> {
private async requireIdle(id: number): Promise<Project> {
const project = await this.require(id);
if (this.deps.activity.isRunning(id)) throw new InvalidRequestError(BUSY);
return project;

View File

@@ -60,8 +60,8 @@ export async function createHarness({ maxAttempts = 3 } = {}) {
});
/** Stands in for the coordinator: tests add project IDs to mark them as running. */
const running = new Set<string>();
const activity = { isRunning: (id: string) => running.has(id) };
const running = new Set<number>();
const activity = { isRunning: (id: number) => running.has(id) };
/** Resolves any input to a new folder of that name on each side. */
const resolver: SourceRootResolver & TargetRootResolver = {

View File

@@ -42,7 +42,7 @@
{@const { project, counts } = summary}
<tr>
<td>
<a class="project" href={resolve('/projects/[id]', { id: project.id })}
<a class="project" href={resolve('/projects/[id]', { id: String(project.id) })}
>{project.name}</a
>
<div class="small muted">{project.sourceDisplayPath}</div>

View File

@@ -41,7 +41,9 @@
{#each data.projects as { project, health } (project.id)}
<tr>
<td>
<a href={resolve('/admin/projects/[id]', { id: project.id })}>{project.name}</a>
<a href={resolve('/admin/projects/[id]', { id: String(project.id) })}
>{project.name}</a
>
</td>
<td class="small">{project.sourceDisplayPath}</td>
<td class="small">{project.targetDisplayPath}</td>

View File

@@ -1,10 +1,11 @@
import { error, redirect } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { attempt, field } from '#lib/server/forms.ts';
import { idParam } from '#lib/server/params.ts';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const summary = await services().status.getSummary(params.id);
const summary = await services().status.getSummary(idParam(params.id));
if (!summary) error(404, 'Project not found');
return { summary };
};
@@ -20,7 +21,7 @@ export const actions: Actions = {
};
return attempt(
async () => {
await services().projectService.update(params.id, values);
await services().projectService.update(idParam(params.id), values);
return { saved: true };
},
{ values }
@@ -29,19 +30,19 @@ export const actions: Actions = {
resume: ({ params }) =>
attempt(async () => {
await services().projectService.resume(params.id);
await services().projectService.resume(idParam(params.id));
return { resumed: true };
}),
reset: ({ params }) =>
attempt(async () => {
await services().projectService.reset(params.id);
await services().projectService.reset(idParam(params.id));
return { reset: true };
}),
delete: ({ params }) =>
attempt(async () => {
await services().projectService.delete(params.id);
await services().projectService.delete(idParam(params.id));
redirect(303, '/admin');
})
};

View File

@@ -28,7 +28,9 @@
<div class="section">
<a class="small" href={resolve('/admin')}>Back to projects</a>
<h1>{project.name}</h1>
<a class="small" href={resolve('/projects/[id]', { id: project.id })}>View sync status</a>
<a class="small" href={resolve('/projects/[id]', { id: String(project.id) })}
>View sync status</a
>
</div>
{#if form?.error}

View File

@@ -1,24 +1,25 @@
import { error } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { attempt, field } from '#lib/server/forms.ts';
import { idParam } from '#lib/server/params.ts';
import type { Actions, PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const { status, config } = services();
const detail = await status.getProject(params.id);
const detail = await status.getProject(idParam(params.id));
if (!detail) error(404, 'Project not found');
return { detail, cooldownSeconds: config.manualRunCooldownSeconds };
};
export const actions: Actions = {
run: async ({ params }) => ({
run: await services().coordinator.requestManualRun(params.id)
run: await services().coordinator.requestManualRun(idParam(params.id))
}),
retry: async ({ params, request }) => {
const sourceId = field(await request.formData(), 'sourceId');
return attempt(async () => {
await services().projectService.retryItem(params.id, sourceId);
await services().projectService.retryItem(idParam(params.id), sourceId);
return { retried: sourceId };
});
}

View File

@@ -57,7 +57,7 @@
{:else}
<span class="muted">Not synced yet</span>
{/if}
<a class="small" href={resolve('/projects/[id]/history', { id: project.id })}
<a class="small" href={resolve('/projects/[id]/history', { id: String(project.id) })}
>See the last 7 days</a
>
</dd>

View File

@@ -1,15 +1,17 @@
import { error } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { idParam } from '#lib/server/params.ts';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const { status, config } = services();
const summary = await status.getSummary(params.id);
const id = idParam(params.id);
const summary = await status.getSummary(id);
if (!summary) error(404, 'Project not found');
return {
project: summary.project,
running: summary.running,
runs: await status.history(params.id),
runs: await status.history(id),
retentionDays: config.historyRetentionDays
};
};

View File

@@ -17,7 +17,7 @@
<div class="page">
<div class="section">
<a class="small" href={resolve('/projects/[id]', { id: data.project.id })}>
<a class="small" href={resolve('/projects/[id]', { id: String(data.project.id) })}>
Back to {data.project.name}
</a>
<h1>Sync history</h1>
@@ -47,8 +47,8 @@
<td>
<a
href={resolve('/projects/[id]/history/[runId]', {
id: data.project.id,
runId: run.runId
id: String(data.project.id),
runId: String(run.runId)
})}
>
{formatDateTime(run.startedAt)}

View File

@@ -1,15 +1,18 @@
import { error } from '@sveltejs/kit';
import { services } from '#lib/server/app.ts';
import { idParam } from '#lib/server/params.ts';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = async ({ params }) => {
const { status } = services();
const id = idParam(params.id);
const runId = idParam(params.runId);
const [summary, runs, events] = await Promise.all([
status.getSummary(params.id),
status.history(params.id),
status.runEvents(params.id, params.runId)
status.getSummary(id),
status.history(id),
status.runEvents(id, runId)
]);
const run = runs.find((r) => r.runId === params.runId);
const run = runs.find((r) => r.runId === runId);
if (!summary || !run) error(404, 'Sync run not found. It may be older than the history window.');
return { project: summary.project, run, events };
};

View File

@@ -14,7 +14,7 @@
<div class="page">
<div class="section">
<a class="small" href={resolve('/projects/[id]/history', { id: data.project.id })}>
<a class="small" href={resolve('/projects/[id]/history', { id: String(data.project.id) })}>
Back to sync history
</a>
<h1>Sync on {formatDateTime(data.run.startedAt)}</h1>