making project ids 1 based
This commit is contained in:
@@ -28,7 +28,7 @@ cp .env.example .env # simulated mode, admin login admin / change-me
|
|||||||
npm run dev # http://localhost:5288
|
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 |
|
| Command | What it does |
|
||||||
| ----------------------- | ------------------------------------------------------------------------------------------------------------------- |
|
| ----------------------- | ------------------------------------------------------------------------------------------------------------------- |
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
CREATE TABLE `items` (
|
CREATE TABLE `items` (
|
||||||
`project_id` text NOT NULL,
|
`project_id` integer NOT NULL,
|
||||||
`source_id` text NOT NULL,
|
`source_id` text NOT NULL,
|
||||||
`parent_source_id` text NOT NULL,
|
`parent_source_id` text NOT NULL,
|
||||||
`name` text NOT NULL,
|
`name` text NOT NULL,
|
||||||
@@ -24,7 +24,7 @@ CREATE TABLE `items` (
|
|||||||
--> statement-breakpoint
|
--> statement-breakpoint
|
||||||
CREATE INDEX `items_project_status` ON `items` (`project_id`,`status`);--> statement-breakpoint
|
CREATE INDEX `items_project_status` ON `items` (`project_id`,`status`);--> statement-breakpoint
|
||||||
CREATE TABLE `projects` (
|
CREATE TABLE `projects` (
|
||||||
`id` text PRIMARY KEY NOT NULL,
|
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
`name` text NOT NULL,
|
`name` text NOT NULL,
|
||||||
`enabled` integer DEFAULT true NOT NULL,
|
`enabled` integer DEFAULT true NOT NULL,
|
||||||
`paused_reason` text,
|
`paused_reason` text,
|
||||||
@@ -44,8 +44,8 @@ CREATE TABLE `projects` (
|
|||||||
--> statement-breakpoint
|
--> statement-breakpoint
|
||||||
CREATE TABLE `run_events` (
|
CREATE TABLE `run_events` (
|
||||||
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
`run_id` text NOT NULL,
|
`run_id` integer NOT NULL,
|
||||||
`project_id` text NOT NULL,
|
`project_id` integer NOT NULL,
|
||||||
`source_id` text NOT NULL,
|
`source_id` text NOT NULL,
|
||||||
`path` text NOT NULL,
|
`path` text NOT NULL,
|
||||||
`action` text NOT NULL,
|
`action` text NOT NULL,
|
||||||
@@ -56,8 +56,8 @@ CREATE TABLE `run_events` (
|
|||||||
--> statement-breakpoint
|
--> statement-breakpoint
|
||||||
CREATE INDEX `run_events_run` ON `run_events` (`run_id`);--> statement-breakpoint
|
CREATE INDEX `run_events_run` ON `run_events` (`run_id`);--> statement-breakpoint
|
||||||
CREATE TABLE `runs` (
|
CREATE TABLE `runs` (
|
||||||
`id` text PRIMARY KEY NOT NULL,
|
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
`project_id` text NOT NULL,
|
`project_id` integer NOT NULL,
|
||||||
`trigger` text NOT NULL,
|
`trigger` text NOT NULL,
|
||||||
`started_at` integer NOT NULL,
|
`started_at` integer NOT NULL,
|
||||||
`finished_at` integer,
|
`finished_at` integer,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"version": "6",
|
"version": "6",
|
||||||
"dialect": "sqlite",
|
"dialect": "sqlite",
|
||||||
"id": "a7309db7-6811-48f5-bb28-822a92adf5d5",
|
"id": "e1cc8fc1-3ac7-41c4-915b-3d703eccd229",
|
||||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||||
"tables": {
|
"tables": {
|
||||||
"items": {
|
"items": {
|
||||||
@@ -9,7 +9,7 @@
|
|||||||
"columns": {
|
"columns": {
|
||||||
"project_id": {
|
"project_id": {
|
||||||
"name": "project_id",
|
"name": "project_id",
|
||||||
"type": "text",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true,
|
||||||
"autoincrement": false
|
"autoincrement": false
|
||||||
@@ -184,10 +184,10 @@
|
|||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"type": "text",
|
"type": "integer",
|
||||||
"primaryKey": true,
|
"primaryKey": true,
|
||||||
"notNull": true,
|
"notNull": true,
|
||||||
"autoincrement": false
|
"autoincrement": true
|
||||||
},
|
},
|
||||||
"name": {
|
"name": {
|
||||||
"name": "name",
|
"name": "name",
|
||||||
@@ -314,14 +314,14 @@
|
|||||||
},
|
},
|
||||||
"run_id": {
|
"run_id": {
|
||||||
"name": "run_id",
|
"name": "run_id",
|
||||||
"type": "text",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true,
|
||||||
"autoincrement": false
|
"autoincrement": false
|
||||||
},
|
},
|
||||||
"project_id": {
|
"project_id": {
|
||||||
"name": "project_id",
|
"name": "project_id",
|
||||||
"type": "text",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true,
|
||||||
"autoincrement": false
|
"autoincrement": false
|
||||||
@@ -395,14 +395,14 @@
|
|||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
"name": "id",
|
"name": "id",
|
||||||
"type": "text",
|
"type": "integer",
|
||||||
"primaryKey": true,
|
"primaryKey": true,
|
||||||
"notNull": true,
|
"notNull": true,
|
||||||
"autoincrement": false
|
"autoincrement": true
|
||||||
},
|
},
|
||||||
"project_id": {
|
"project_id": {
|
||||||
"name": "project_id",
|
"name": "project_id",
|
||||||
"type": "text",
|
"type": "integer",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true,
|
"notNull": true,
|
||||||
"autoincrement": false
|
"autoincrement": false
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
{
|
{
|
||||||
"idx": 0,
|
"idx": 0,
|
||||||
"version": "6",
|
"version": "6",
|
||||||
"when": 1792725790862,
|
"when": 1792772657596,
|
||||||
"tag": "0000_init",
|
"tag": "0000_init",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,10 @@
|
|||||||
for (const node of list) {
|
for (const node of list) {
|
||||||
if (attentionOnly && !needsAttention(node)) continue;
|
if (attentionOnly && !needsAttention(node)) continue;
|
||||||
result.push({ node, depth });
|
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);
|
walk(nodes, 0);
|
||||||
@@ -72,7 +75,7 @@
|
|||||||
<tr class:folder={node.isFolder}>
|
<tr class:folder={node.isFolder}>
|
||||||
<td class="name" style:--depth={depth}>
|
<td class="name" style:--depth={depth}>
|
||||||
<div class="name-line">
|
<div class="name-line">
|
||||||
{#if node.isFolder}
|
{#if node.isFolder && !attentionOnly}
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
class="twisty"
|
class="twisty"
|
||||||
@@ -82,15 +85,16 @@
|
|||||||
>
|
>
|
||||||
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M6 4l4 4-4 4" /></svg>
|
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M6 4l4 4-4 4" /></svg>
|
||||||
</button>
|
</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}
|
{:else}
|
||||||
<span class="twisty-spacer"></span>
|
<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}
|
{/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>
|
<span title={node.error ?? undefined}>{node.name}</span>
|
||||||
{#if node.problems > 0}
|
{#if node.problems > 0}
|
||||||
<span class="problems">{node.problems} failed inside</span>
|
<span class="problems">{node.problems} failed inside</span>
|
||||||
|
|||||||
@@ -8,14 +8,19 @@ import type {
|
|||||||
import { FakeAcc } from './fake-acc.ts';
|
import { FakeAcc } from './fake-acc.ts';
|
||||||
import { FakeDrive } from './fake-drive.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';
|
export const REJECTED_FILE = 'Corrupt model.rvt';
|
||||||
|
|
||||||
const SAMPLE_TREE: Record<string, string[]> = {
|
const SAMPLE_TREE: Record<string, string[]> = {
|
||||||
Drawings: ['A-101 Floor Plan.pdf', 'A-201 Elevations.pdf', 'S-101 Foundations.dwg'],
|
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'],
|
Specifications: ['Division 08 - Openings.docx', 'Division 26 - Electrical.docx'],
|
||||||
'Submittals/Approved': ['Door hardware.pdf'],
|
'Submittals/Approved': ['Door hardware.pdf'],
|
||||||
'': ['Project Directory.xlsx', REJECTED_FILE, '~$Project Directory.xlsx']
|
'': ['Project Directory.xlsx', '~$Project Directory.xlsx']
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type Tx = Parameters<Parameters<Db['transaction']>[0]>[0];
|
|||||||
export class SqliteItemRepository implements ItemRepository {
|
export class SqliteItemRepository implements ItemRepository {
|
||||||
constructor(private readonly db: Db) {}
|
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
|
const row = this.db
|
||||||
.select()
|
.select()
|
||||||
.from(items)
|
.from(items)
|
||||||
@@ -23,11 +23,11 @@ export class SqliteItemRepository implements ItemRepository {
|
|||||||
return row ? toItem(row) : null;
|
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);
|
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
|
const rows = this.db
|
||||||
.select({ status: items.status, total: count() })
|
.select({ status: items.status, total: count() })
|
||||||
.from(items)
|
.from(items)
|
||||||
@@ -39,19 +39,19 @@ export class SqliteItemRepository implements ItemRepository {
|
|||||||
return counts;
|
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) => {
|
this.db.transaction((tx) => {
|
||||||
upsert(tx, projectId, states);
|
upsert(tx, projectId, states);
|
||||||
tx.update(projects).set({ deltaCursor: cursor }).where(eq(projects.id, projectId)).run();
|
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));
|
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) {
|
for (const state of states) {
|
||||||
const row = toRow(projectId, state);
|
const row = toRow(projectId, state);
|
||||||
tx.insert(items)
|
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 {
|
return {
|
||||||
projectId,
|
projectId,
|
||||||
sourceId,
|
sourceId,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ export class SqliteProjectRepository implements ProjectRepository {
|
|||||||
private readonly clock: Clock
|
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();
|
const row = this.db.select().from(projects).where(eq(projects.id, id)).get();
|
||||||
return row ? toProject(row) : null;
|
return row ? toProject(row) : null;
|
||||||
}
|
}
|
||||||
@@ -26,41 +26,37 @@ export class SqliteProjectRepository implements ProjectRepository {
|
|||||||
const row = this.db
|
const row = this.db
|
||||||
.insert(projects)
|
.insert(projects)
|
||||||
// A NewProject supplies every editable field, so toColumns returns them all.
|
// A NewProject supplies every editable field, so toColumns returns them all.
|
||||||
.values({
|
.values({ ...toColumns(project), createdAt: this.clock() } as ProjectInsert)
|
||||||
...toColumns(project),
|
|
||||||
id: crypto.randomUUID(),
|
|
||||||
createdAt: this.clock()
|
|
||||||
} as ProjectInsert)
|
|
||||||
.returning()
|
.returning()
|
||||||
.get();
|
.get();
|
||||||
return toProject(row);
|
return toProject(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(id: string, changes: ProjectChanges): Promise<void> {
|
async update(id: number, changes: ProjectChanges): Promise<void> {
|
||||||
this.set(id, toColumns(changes));
|
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();
|
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 });
|
this.set(id, { pausedReason: reason });
|
||||||
}
|
}
|
||||||
|
|
||||||
async resume(id: string): Promise<void> {
|
async resume(id: number): Promise<void> {
|
||||||
this.set(id, { pausedReason: null });
|
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 });
|
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 });
|
this.set(id, { lastRunAt: at });
|
||||||
}
|
}
|
||||||
|
|
||||||
async reset(id: string): Promise<void> {
|
async reset(id: number): Promise<void> {
|
||||||
this.db.transaction((tx) => {
|
this.db.transaction((tx) => {
|
||||||
tx.delete(items).where(eq(items.projectId, id)).run();
|
tx.delete(items).where(eq(items.projectId, id)).run();
|
||||||
tx.update(projects)
|
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);
|
const defined = Object.entries(values).filter(([, value]) => value !== undefined);
|
||||||
if (defined.length === 0) return;
|
if (defined.length === 0) return;
|
||||||
this.db.update(projects).set(Object.fromEntries(defined)).where(eq(projects.id, id)).run();
|
this.db.update(projects).set(Object.fromEntries(defined)).where(eq(projects.id, id)).run();
|
||||||
|
|||||||
@@ -2,6 +2,14 @@ import { describe, expect, it } from 'vitest';
|
|||||||
import { createHarness } from '#lib/server/sync/testing/harness.ts';
|
import { createHarness } from '#lib/server/sync/testing/harness.ts';
|
||||||
|
|
||||||
describe('SqliteRunRepository', () => {
|
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 () => {
|
it('prunes old runs together with their events', async () => {
|
||||||
const h = await createHarness();
|
const h = await createHarness();
|
||||||
h.drive.addFile(h.rootId, 'a.txt');
|
h.drive.addFile(h.rootId, 'a.txt');
|
||||||
|
|||||||
@@ -15,14 +15,17 @@ type RunRow = typeof runs.$inferSelect;
|
|||||||
export class SqliteRunRepository implements RunRepository {
|
export class SqliteRunRepository implements RunRepository {
|
||||||
constructor(private readonly db: Db) {}
|
constructor(private readonly db: Db) {}
|
||||||
|
|
||||||
async start(projectId: string, trigger: RunTrigger, at: Date): Promise<string> {
|
async start(projectId: number, trigger: RunTrigger, at: Date): Promise<number> {
|
||||||
const id = crypto.randomUUID();
|
const { id } = this.db
|
||||||
this.db.insert(runs).values({ id, projectId, trigger, startedAt: at }).run();
|
.insert(runs)
|
||||||
|
.values({ projectId, trigger, startedAt: at })
|
||||||
|
.returning({ id: runs.id })
|
||||||
|
.get();
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
async finish(
|
async finish(
|
||||||
runId: string,
|
runId: number,
|
||||||
result: { finishedAt: Date; outcome: RunOutcome; error: string | null; counts: RunCounts }
|
result: { finishedAt: Date; outcome: RunOutcome; error: string | null; counts: RunCounts }
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { finishedAt, outcome, error, counts } = result;
|
const { finishedAt, outcome, error, counts } = result;
|
||||||
@@ -37,7 +40,7 @@ export class SqliteRunRepository implements RunRepository {
|
|||||||
this.db.insert(runEvents).values(event).run();
|
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
|
const row = this.db
|
||||||
.select()
|
.select()
|
||||||
.from(runs)
|
.from(runs)
|
||||||
@@ -48,7 +51,7 @@ export class SqliteRunRepository implements RunRepository {
|
|||||||
return row ? toRunRecord(row) : null;
|
return row ? toRunRecord(row) : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async listSince(projectId: string, since: Date): Promise<RunRecord[]> {
|
async listSince(projectId: number, since: Date): Promise<RunRecord[]> {
|
||||||
return this.db
|
return this.db
|
||||||
.select()
|
.select()
|
||||||
.from(runs)
|
.from(runs)
|
||||||
@@ -58,7 +61,7 @@ export class SqliteRunRepository implements RunRepository {
|
|||||||
.map(toRunRecord);
|
.map(toRunRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
async listEvents(runId: string): Promise<RunEvent[]> {
|
async listEvents(runId: number): Promise<RunEvent[]> {
|
||||||
return this.db
|
return this.db
|
||||||
.select()
|
.select()
|
||||||
.from(runEvents)
|
.from(runEvents)
|
||||||
|
|||||||
@@ -5,8 +5,12 @@ const timestamp = (name: string) => integer(name, { mode: 'timestamp_ms' });
|
|||||||
const flag = (name: string) => integer(name, { mode: 'boolean' });
|
const flag = (name: string) => integer(name, { mode: 'boolean' });
|
||||||
const count = (name: string) => integer(name).notNull().default(0);
|
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', {
|
export const projects = sqliteTable('projects', {
|
||||||
id: text('id').primaryKey(),
|
id: id(),
|
||||||
name: text('name').notNull(),
|
name: text('name').notNull(),
|
||||||
enabled: flag('enabled').notNull().default(true),
|
enabled: flag('enabled').notNull().default(true),
|
||||||
pausedReason: text('paused_reason'),
|
pausedReason: text('paused_reason'),
|
||||||
@@ -27,7 +31,7 @@ export const projects = sqliteTable('projects', {
|
|||||||
export const items = sqliteTable(
|
export const items = sqliteTable(
|
||||||
'items',
|
'items',
|
||||||
{
|
{
|
||||||
projectId: text('project_id')
|
projectId: integer('project_id')
|
||||||
.notNull()
|
.notNull()
|
||||||
.references(() => projects.id, { onDelete: 'cascade' }),
|
.references(() => projects.id, { onDelete: 'cascade' }),
|
||||||
sourceId: text('source_id').notNull(),
|
sourceId: text('source_id').notNull(),
|
||||||
@@ -61,8 +65,8 @@ export const items = sqliteTable(
|
|||||||
export const runs = sqliteTable(
|
export const runs = sqliteTable(
|
||||||
'runs',
|
'runs',
|
||||||
{
|
{
|
||||||
id: text('id').primaryKey(),
|
id: id(),
|
||||||
projectId: text('project_id')
|
projectId: integer('project_id')
|
||||||
.notNull()
|
.notNull()
|
||||||
.references(() => projects.id, { onDelete: 'cascade' }),
|
.references(() => projects.id, { onDelete: 'cascade' }),
|
||||||
trigger: text('trigger', { enum: ['schedule', 'manual'] }).notNull(),
|
trigger: text('trigger', { enum: ['schedule', 'manual'] }).notNull(),
|
||||||
@@ -84,11 +88,11 @@ export const runs = sqliteTable(
|
|||||||
export const runEvents = sqliteTable(
|
export const runEvents = sqliteTable(
|
||||||
'run_events',
|
'run_events',
|
||||||
{
|
{
|
||||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
id: id(),
|
||||||
runId: text('run_id')
|
runId: integer('run_id')
|
||||||
.notNull()
|
.notNull()
|
||||||
.references(() => runs.id, { onDelete: 'cascade' }),
|
.references(() => runs.id, { onDelete: 'cascade' }),
|
||||||
projectId: text('project_id').notNull(),
|
projectId: integer('project_id').notNull(),
|
||||||
sourceId: text('source_id').notNull(),
|
sourceId: text('source_id').notNull(),
|
||||||
path: text('path').notNull(),
|
path: text('path').notNull(),
|
||||||
action: text('action', { enum: EVENT_ACTIONS }).notNull(),
|
action: text('action', { enum: EVENT_ACTIONS }).notNull(),
|
||||||
|
|||||||
8
src/lib/server/params.ts
Normal file
8
src/lib/server/params.ts
Normal 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;
|
||||||
|
}
|
||||||
@@ -65,6 +65,6 @@ describe('StatusService', () => {
|
|||||||
const run = await h.sync();
|
const run = await h.sync();
|
||||||
|
|
||||||
expect(await h.status.runEvents(h.project.id, run.runId)).toHaveLength(1);
|
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([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export interface StatusServiceDeps {
|
|||||||
projects: ProjectRepository;
|
projects: ProjectRepository;
|
||||||
items: ItemRepository;
|
items: ItemRepository;
|
||||||
runs: RunRepository;
|
runs: RunRepository;
|
||||||
activity: { isRunning(projectId: string): boolean };
|
activity: { isRunning(projectId: number): boolean };
|
||||||
clock: Clock;
|
clock: Clock;
|
||||||
retentionDays: number;
|
retentionDays: number;
|
||||||
}
|
}
|
||||||
@@ -52,12 +52,12 @@ export class StatusService {
|
|||||||
return Promise.all(projects.map((project) => this.summarize(project)));
|
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);
|
const project = await this.deps.projects.get(id);
|
||||||
return project ? this.summarize(project) : null;
|
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);
|
const project = await this.deps.projects.get(id);
|
||||||
if (!project) return null;
|
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);
|
const since = new Date(this.deps.clock().getTime() - this.deps.retentionDays * 86_400_000);
|
||||||
return this.deps.runs.listSince(projectId, since);
|
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);
|
const events = await this.deps.runs.listEvents(runId);
|
||||||
return events.filter((event) => event.projectId === projectId);
|
return events.filter((event) => event.projectId === projectId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,20 +5,20 @@ import type { ProjectRepository } from './core/ports.ts';
|
|||||||
import { SyncCoordinator } from './coordinator.ts';
|
import { SyncCoordinator } from './coordinator.ts';
|
||||||
import { silentLogger } from './testing/harness.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;
|
return { id, enabled: true, pausedReason: null, ...overrides } as Project;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** A runner whose runs stay in flight until `finish` is called. */
|
/** A runner whose runs stay in flight until `finish` is called. */
|
||||||
class ControlledRunner implements ProjectSyncRunner {
|
class ControlledRunner implements ProjectSyncRunner {
|
||||||
readonly started: Array<[string, RunTrigger]> = [];
|
readonly started: Array<[number, RunTrigger]> = [];
|
||||||
private readonly pending: Array<() => void> = [];
|
private readonly pending: Array<() => void> = [];
|
||||||
|
|
||||||
runProject(projectId: string, trigger: RunTrigger) {
|
runProject(projectId: number, trigger: RunTrigger) {
|
||||||
this.started.push([projectId, trigger]);
|
this.started.push([projectId, trigger]);
|
||||||
return new Promise<never>((resolve) => {
|
return new Promise<never>((resolve) => {
|
||||||
this.pending.push(() =>
|
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;
|
let now = 0;
|
||||||
const runner = new ControlledRunner();
|
const runner = new ControlledRunner();
|
||||||
const repo = {
|
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
|
list: async () => projects
|
||||||
} as ProjectRepository;
|
} as ProjectRepository;
|
||||||
const coordinator = new SyncCoordinator({
|
const coordinator = new SyncCoordinator({
|
||||||
@@ -47,67 +47,69 @@ function setup(projects: Project[]) {
|
|||||||
|
|
||||||
describe('SyncCoordinator', () => {
|
describe('SyncCoordinator', () => {
|
||||||
it('starts a manual run and reports it as already running on a second click', async () => {
|
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(1)).toEqual({ status: 'started' });
|
||||||
expect(await coordinator.requestManualRun('p1')).toEqual({ status: 'already-running' });
|
expect(await coordinator.requestManualRun(1)).toEqual({ status: 'already-running' });
|
||||||
expect(runner.started).toEqual([['p1', 'manual']]);
|
expect(runner.started).toEqual([[1, 'manual']]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('enforces the manual cooldown after a run finishes', async () => {
|
it('enforces the manual cooldown after a run finishes', async () => {
|
||||||
const { runner, coordinator, advance } = setup([project('p1')]);
|
const { runner, coordinator, advance } = setup([project(1)]);
|
||||||
await coordinator.requestManualRun('p1');
|
await coordinator.requestManualRun(1);
|
||||||
runner.finishAll();
|
runner.finishAll();
|
||||||
await coordinator.drain();
|
await coordinator.drain();
|
||||||
|
|
||||||
advance(10_000);
|
advance(10_000);
|
||||||
expect(await coordinator.requestManualRun('p1')).toEqual({
|
expect(await coordinator.requestManualRun(1)).toEqual({
|
||||||
status: 'cooldown',
|
status: 'cooldown',
|
||||||
retryAfterSeconds: 20
|
retryAfterSeconds: 20
|
||||||
});
|
});
|
||||||
|
|
||||||
advance(20_000);
|
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 () => {
|
it('keeps cooldowns per project', async () => {
|
||||||
const { coordinator } = setup([project('p1'), project('p2')]);
|
const { coordinator } = setup([project(1), project(2)]);
|
||||||
await coordinator.requestManualRun('p1');
|
await coordinator.requestManualRun(1);
|
||||||
expect(await coordinator.requestManualRun('p2')).toEqual({ status: 'started' });
|
expect(await coordinator.requestManualRun(2)).toEqual({ status: 'started' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('refuses manual runs for disabled, paused or unknown projects', async () => {
|
it('refuses manual runs for disabled, paused or unknown projects', async () => {
|
||||||
|
const [off, paused, unknown] = [1, 2, 99];
|
||||||
const { coordinator } = setup([
|
const { coordinator } = setup([
|
||||||
project('off', { enabled: false }),
|
project(off, { enabled: false }),
|
||||||
project('paused', { pausedReason: 'Root deleted' })
|
project(paused, { pausedReason: 'Root deleted' })
|
||||||
]);
|
]);
|
||||||
|
|
||||||
expect(await coordinator.requestManualRun('off')).toMatchObject({ status: 'unavailable' });
|
expect(await coordinator.requestManualRun(off)).toMatchObject({ status: 'unavailable' });
|
||||||
expect(await coordinator.requestManualRun('paused')).toEqual({
|
expect(await coordinator.requestManualRun(paused)).toEqual({
|
||||||
status: 'unavailable',
|
status: 'unavailable',
|
||||||
reason: 'Root deleted'
|
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 () => {
|
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([
|
const { runner, coordinator } = setup([
|
||||||
project('busy'),
|
project(busy),
|
||||||
project('off', { enabled: false }),
|
project(off, { enabled: false }),
|
||||||
project('paused', { pausedReason: 'x' }),
|
project(paused, { pausedReason: 'x' }),
|
||||||
project('a'),
|
project(a),
|
||||||
project('b')
|
project(b)
|
||||||
]);
|
]);
|
||||||
await coordinator.requestManualRun('busy');
|
await coordinator.requestManualRun(busy);
|
||||||
|
|
||||||
const pass = coordinator.runScheduledPass();
|
const pass = coordinator.runScheduledPass();
|
||||||
await Promise.resolve();
|
await Promise.resolve();
|
||||||
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();
|
runner.finishAll();
|
||||||
await new Promise((r) => setTimeout(r));
|
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();
|
runner.finishAll();
|
||||||
await pass;
|
await pass;
|
||||||
|
|||||||
@@ -22,16 +22,16 @@ export interface SyncCoordinatorDeps {
|
|||||||
* time and rate-limits manual "Run now" requests per project.
|
* time and rate-limits manual "Run now" requests per project.
|
||||||
*/
|
*/
|
||||||
export class SyncCoordinator {
|
export class SyncCoordinator {
|
||||||
private readonly active = new Map<string, Promise<void>>();
|
private readonly active = new Map<number, Promise<void>>();
|
||||||
private readonly lastManualRequest = new Map<string, number>();
|
private readonly lastManualRequest = new Map<number, number>();
|
||||||
|
|
||||||
constructor(private readonly deps: SyncCoordinatorDeps) {}
|
constructor(private readonly deps: SyncCoordinatorDeps) {}
|
||||||
|
|
||||||
isRunning(projectId: string): boolean {
|
isRunning(projectId: number): boolean {
|
||||||
return this.active.has(projectId);
|
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);
|
const project = await this.deps.projects.get(projectId);
|
||||||
if (!project) return { status: 'unavailable', reason: 'Project not found.' };
|
if (!project) return { status: 'unavailable', reason: 'Project not found.' };
|
||||||
if (!project.enabled) return { status: 'unavailable', reason: 'Project is disabled.' };
|
if (!project.enabled) return { status: 'unavailable', reason: 'Project is disabled.' };
|
||||||
@@ -65,7 +65,7 @@ export class SyncCoordinator {
|
|||||||
await Promise.all(this.active.values());
|
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
|
const run = this.deps.runner
|
||||||
.runProject(projectId, trigger)
|
.runProject(projectId, trigger)
|
||||||
.then(() => undefined)
|
.then(() => undefined)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import type {
|
|||||||
import type { Reconciler } from './reconciler.ts';
|
import type { Reconciler } from './reconciler.ts';
|
||||||
|
|
||||||
export interface ProjectSyncRunner {
|
export interface ProjectSyncRunner {
|
||||||
runProject(projectId: string, trigger: RunTrigger): Promise<RunResult>;
|
runProject(projectId: number, trigger: RunTrigger): Promise<RunResult>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface SyncEngineDeps {
|
export interface SyncEngineDeps {
|
||||||
@@ -39,7 +39,7 @@ export interface SyncEngineDeps {
|
|||||||
export class SyncEngine implements ProjectSyncRunner {
|
export class SyncEngine implements ProjectSyncRunner {
|
||||||
constructor(private readonly deps: SyncEngineDeps) {}
|
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 { projects, items, runs, clock, logger } = this.deps;
|
||||||
const project = await projects.get(projectId);
|
const project = await projects.get(projectId);
|
||||||
if (!project) throw new Error(`Project ${projectId} not found.`);
|
if (!project) throw new Error(`Project ${projectId} not found.`);
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export interface TargetRoot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface Project {
|
export interface Project {
|
||||||
id: string;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
enabled: boolean;
|
enabled: boolean;
|
||||||
/** Set when the sync refuses to continue (e.g. the SharePoint root vanished). */
|
/** Set when the sync refuses to continue (e.g. the SharePoint root vanished). */
|
||||||
@@ -137,7 +137,7 @@ export function emptyStatusCounts(): StatusCounts {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface RunResult {
|
export interface RunResult {
|
||||||
runId: string;
|
runId: number;
|
||||||
outcome: RunOutcome;
|
outcome: RunOutcome;
|
||||||
error: string | null;
|
error: string | null;
|
||||||
counts: RunCounts;
|
counts: RunCounts;
|
||||||
@@ -146,15 +146,15 @@ export interface RunResult {
|
|||||||
export interface RunRecord extends Omit<RunResult, 'outcome'> {
|
export interface RunRecord extends Omit<RunResult, 'outcome'> {
|
||||||
/** Null while the run is still in progress. */
|
/** Null while the run is still in progress. */
|
||||||
outcome: RunOutcome | null;
|
outcome: RunOutcome | null;
|
||||||
projectId: string;
|
projectId: number;
|
||||||
trigger: RunTrigger;
|
trigger: RunTrigger;
|
||||||
startedAt: Date;
|
startedAt: Date;
|
||||||
finishedAt: Date | null;
|
finishedAt: Date | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RunEvent {
|
export interface RunEvent {
|
||||||
runId: string;
|
runId: number;
|
||||||
projectId: string;
|
projectId: number;
|
||||||
sourceId: string;
|
sourceId: string;
|
||||||
path: string;
|
path: string;
|
||||||
action: EventAction;
|
action: EventAction;
|
||||||
|
|||||||
@@ -73,39 +73,41 @@ export interface TargetRootResolver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface ProjectRepository {
|
export interface ProjectRepository {
|
||||||
get(id: string): Promise<Project | null>;
|
get(id: number): Promise<Project | null>;
|
||||||
list(): Promise<Project[]>;
|
list(): Promise<Project[]>;
|
||||||
|
/** Assigns the next ID: 1, 2, 3... */
|
||||||
create(project: NewProject): Promise<Project>;
|
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. */
|
/** Removes the project with all its items and history. */
|
||||||
delete(id: string): Promise<void>;
|
delete(id: number): Promise<void>;
|
||||||
pause(id: string, reason: string): Promise<void>;
|
pause(id: number, reason: string): Promise<void>;
|
||||||
resume(id: string): Promise<void>;
|
resume(id: number): Promise<void>;
|
||||||
markFirstSynced(id: string, at: Date): Promise<void>;
|
markFirstSynced(id: number, at: Date): Promise<void>;
|
||||||
setLastRunAt(id: string, at: Date): Promise<void>;
|
setLastRunAt(id: number, at: Date): Promise<void>;
|
||||||
/** Forget all sync state so the project starts over from a full listing. */
|
/** 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 {
|
export interface ItemRepository {
|
||||||
get(projectId: string, sourceId: string): Promise<ItemState | null>;
|
get(projectId: number, sourceId: string): Promise<ItemState | null>;
|
||||||
listByProject(projectId: string): Promise<ItemState[]>;
|
listByProject(projectId: number): Promise<ItemState[]>;
|
||||||
countByStatus(projectId: string): Promise<StatusCounts>;
|
countByStatus(projectId: number): Promise<StatusCounts>;
|
||||||
/** Atomically store ingested changes and the cursor they were read up to. */
|
/** Atomically store ingested changes and the cursor they were read up to. */
|
||||||
commitIngest(projectId: string, items: ItemState[], cursor: string): Promise<void>;
|
commitIngest(projectId: number, items: ItemState[], cursor: string): Promise<void>;
|
||||||
save(projectId: string, items: ItemState[]): Promise<void>;
|
save(projectId: number, items: ItemState[]): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RunRepository {
|
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(
|
finish(
|
||||||
runId: string,
|
runId: number,
|
||||||
result: { finishedAt: Date; outcome: RunOutcome; error: string | null; counts: RunCounts }
|
result: { finishedAt: Date; outcome: RunOutcome; error: string | null; counts: RunCounts }
|
||||||
): Promise<void>;
|
): Promise<void>;
|
||||||
addEvent(event: RunEvent): Promise<void>;
|
addEvent(event: RunEvent): Promise<void>;
|
||||||
latest(projectId: string): Promise<RunRecord | null>;
|
latest(projectId: number): Promise<RunRecord | null>;
|
||||||
listSince(projectId: string, since: Date): Promise<RunRecord[]>;
|
listSince(projectId: number, since: Date): Promise<RunRecord[]>;
|
||||||
listEvents(runId: string): Promise<RunEvent[]>;
|
listEvents(runId: number): Promise<RunEvent[]>;
|
||||||
/** Delete runs (and their events) started before `cutoff`; returns how many runs were removed. */
|
/** Delete runs (and their events) started before `cutoff`; returns how many runs were removed. */
|
||||||
pruneBefore(cutoff: Date): Promise<number>;
|
pruneBefore(cutoff: Date): Promise<number>;
|
||||||
/** Close every run left unfinished (e.g. by a restart) as failed; returns how many were closed. */
|
/** Close every run left unfinished (e.g. by a restart) as failed; returns how many were closed. */
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ export class Reconciler {
|
|||||||
|
|
||||||
async reconcile(
|
async reconcile(
|
||||||
project: Project,
|
project: Project,
|
||||||
runId: string,
|
runId: number,
|
||||||
items: Map<string, ItemState>
|
items: Map<string, ItemState>
|
||||||
): Promise<ReconcileResult> {
|
): Promise<ReconcileResult> {
|
||||||
const { maxAttempts } = this.deps;
|
const { maxAttempts } = this.deps;
|
||||||
|
|||||||
@@ -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 () => {
|
it('rejects blank names and locations', async () => {
|
||||||
const h = await createHarness();
|
const h = await createHarness();
|
||||||
await expect(
|
await expect(
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ export interface ProjectServiceDeps {
|
|||||||
items: ItemRepository;
|
items: ItemRepository;
|
||||||
sourceResolver: SourceRootResolver;
|
sourceResolver: SourceRootResolver;
|
||||||
targetResolver: TargetRootResolver;
|
targetResolver: TargetRootResolver;
|
||||||
activity: { isRunning(projectId: string): boolean };
|
activity: { isRunning(projectId: number): boolean };
|
||||||
maxAttempts: number;
|
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 project = await this.require(id);
|
||||||
const changes: ProjectChanges = { name: validName(edit.name), enabled: edit.enabled };
|
const changes: ProjectChanges = { name: validName(edit.name), enabled: edit.enabled };
|
||||||
const sourceInput = edit.sourceInput?.trim();
|
const sourceInput = edit.sourceInput?.trim();
|
||||||
@@ -73,24 +73,24 @@ export class ProjectService {
|
|||||||
await this.deps.projects.update(id, changes);
|
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.require(id);
|
||||||
await this.deps.projects.resume(id);
|
await this.deps.projects.resume(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Forget all sync state; the next run starts from a full listing and roots unlock. */
|
/** 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.requireIdle(id);
|
||||||
await this.deps.projects.reset(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.requireIdle(id);
|
||||||
await this.deps.projects.delete(id);
|
await this.deps.projects.delete(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Give a failed item a fresh set of attempts on the next run. */
|
/** 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);
|
await this.requireIdle(projectId);
|
||||||
const item = await this.deps.items.get(projectId, sourceId);
|
const item = await this.deps.items.get(projectId, sourceId);
|
||||||
if (!item) throw new InvalidRequestError('That file is no longer tracked.');
|
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]);
|
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);
|
const project = await this.deps.projects.get(id);
|
||||||
if (!project) throw new InvalidRequestError('Project not found.');
|
if (!project) throw new InvalidRequestError('Project not found.');
|
||||||
return project;
|
return project;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async requireIdle(id: string): Promise<Project> {
|
private async requireIdle(id: number): Promise<Project> {
|
||||||
const project = await this.require(id);
|
const project = await this.require(id);
|
||||||
if (this.deps.activity.isRunning(id)) throw new InvalidRequestError(BUSY);
|
if (this.deps.activity.isRunning(id)) throw new InvalidRequestError(BUSY);
|
||||||
return project;
|
return project;
|
||||||
|
|||||||
@@ -60,8 +60,8 @@ export async function createHarness({ maxAttempts = 3 } = {}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
/** Stands in for the coordinator: tests add project IDs to mark them as running. */
|
/** Stands in for the coordinator: tests add project IDs to mark them as running. */
|
||||||
const running = new Set<string>();
|
const running = new Set<number>();
|
||||||
const activity = { isRunning: (id: string) => running.has(id) };
|
const activity = { isRunning: (id: number) => running.has(id) };
|
||||||
|
|
||||||
/** Resolves any input to a new folder of that name on each side. */
|
/** Resolves any input to a new folder of that name on each side. */
|
||||||
const resolver: SourceRootResolver & TargetRootResolver = {
|
const resolver: SourceRootResolver & TargetRootResolver = {
|
||||||
|
|||||||
@@ -42,7 +42,7 @@
|
|||||||
{@const { project, counts } = summary}
|
{@const { project, counts } = summary}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<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
|
>{project.name}</a
|
||||||
>
|
>
|
||||||
<div class="small muted">{project.sourceDisplayPath}</div>
|
<div class="small muted">{project.sourceDisplayPath}</div>
|
||||||
|
|||||||
@@ -41,7 +41,9 @@
|
|||||||
{#each data.projects as { project, health } (project.id)}
|
{#each data.projects as { project, health } (project.id)}
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<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>
|
||||||
<td class="small">{project.sourceDisplayPath}</td>
|
<td class="small">{project.sourceDisplayPath}</td>
|
||||||
<td class="small">{project.targetDisplayPath}</td>
|
<td class="small">{project.targetDisplayPath}</td>
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
import { error, redirect } from '@sveltejs/kit';
|
import { error, redirect } from '@sveltejs/kit';
|
||||||
import { services } from '#lib/server/app.ts';
|
import { services } from '#lib/server/app.ts';
|
||||||
import { attempt, field } from '#lib/server/forms.ts';
|
import { attempt, field } from '#lib/server/forms.ts';
|
||||||
|
import { idParam } from '#lib/server/params.ts';
|
||||||
import type { Actions, PageServerLoad } from './$types';
|
import type { Actions, PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params }) => {
|
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');
|
if (!summary) error(404, 'Project not found');
|
||||||
return { summary };
|
return { summary };
|
||||||
};
|
};
|
||||||
@@ -20,7 +21,7 @@ export const actions: Actions = {
|
|||||||
};
|
};
|
||||||
return attempt(
|
return attempt(
|
||||||
async () => {
|
async () => {
|
||||||
await services().projectService.update(params.id, values);
|
await services().projectService.update(idParam(params.id), values);
|
||||||
return { saved: true };
|
return { saved: true };
|
||||||
},
|
},
|
||||||
{ values }
|
{ values }
|
||||||
@@ -29,19 +30,19 @@ export const actions: Actions = {
|
|||||||
|
|
||||||
resume: ({ params }) =>
|
resume: ({ params }) =>
|
||||||
attempt(async () => {
|
attempt(async () => {
|
||||||
await services().projectService.resume(params.id);
|
await services().projectService.resume(idParam(params.id));
|
||||||
return { resumed: true };
|
return { resumed: true };
|
||||||
}),
|
}),
|
||||||
|
|
||||||
reset: ({ params }) =>
|
reset: ({ params }) =>
|
||||||
attempt(async () => {
|
attempt(async () => {
|
||||||
await services().projectService.reset(params.id);
|
await services().projectService.reset(idParam(params.id));
|
||||||
return { reset: true };
|
return { reset: true };
|
||||||
}),
|
}),
|
||||||
|
|
||||||
delete: ({ params }) =>
|
delete: ({ params }) =>
|
||||||
attempt(async () => {
|
attempt(async () => {
|
||||||
await services().projectService.delete(params.id);
|
await services().projectService.delete(idParam(params.id));
|
||||||
redirect(303, '/admin');
|
redirect(303, '/admin');
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -28,7 +28,9 @@
|
|||||||
<div class="section">
|
<div class="section">
|
||||||
<a class="small" href={resolve('/admin')}>Back to projects</a>
|
<a class="small" href={resolve('/admin')}>Back to projects</a>
|
||||||
<h1>{project.name}</h1>
|
<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>
|
</div>
|
||||||
|
|
||||||
{#if form?.error}
|
{#if form?.error}
|
||||||
|
|||||||
@@ -1,24 +1,25 @@
|
|||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
import { services } from '#lib/server/app.ts';
|
import { services } from '#lib/server/app.ts';
|
||||||
import { attempt, field } from '#lib/server/forms.ts';
|
import { attempt, field } from '#lib/server/forms.ts';
|
||||||
|
import { idParam } from '#lib/server/params.ts';
|
||||||
import type { Actions, PageServerLoad } from './$types';
|
import type { Actions, PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params }) => {
|
export const load: PageServerLoad = async ({ params }) => {
|
||||||
const { status, config } = services();
|
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');
|
if (!detail) error(404, 'Project not found');
|
||||||
return { detail, cooldownSeconds: config.manualRunCooldownSeconds };
|
return { detail, cooldownSeconds: config.manualRunCooldownSeconds };
|
||||||
};
|
};
|
||||||
|
|
||||||
export const actions: Actions = {
|
export const actions: Actions = {
|
||||||
run: async ({ params }) => ({
|
run: async ({ params }) => ({
|
||||||
run: await services().coordinator.requestManualRun(params.id)
|
run: await services().coordinator.requestManualRun(idParam(params.id))
|
||||||
}),
|
}),
|
||||||
|
|
||||||
retry: async ({ params, request }) => {
|
retry: async ({ params, request }) => {
|
||||||
const sourceId = field(await request.formData(), 'sourceId');
|
const sourceId = field(await request.formData(), 'sourceId');
|
||||||
return attempt(async () => {
|
return attempt(async () => {
|
||||||
await services().projectService.retryItem(params.id, sourceId);
|
await services().projectService.retryItem(idParam(params.id), sourceId);
|
||||||
return { retried: sourceId };
|
return { retried: sourceId };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,7 +57,7 @@
|
|||||||
{:else}
|
{:else}
|
||||||
<span class="muted">Not synced yet</span>
|
<span class="muted">Not synced yet</span>
|
||||||
{/if}
|
{/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
|
>See the last 7 days</a
|
||||||
>
|
>
|
||||||
</dd>
|
</dd>
|
||||||
|
|||||||
@@ -1,15 +1,17 @@
|
|||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
import { services } from '#lib/server/app.ts';
|
import { services } from '#lib/server/app.ts';
|
||||||
|
import { idParam } from '#lib/server/params.ts';
|
||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params }) => {
|
export const load: PageServerLoad = async ({ params }) => {
|
||||||
const { status, config } = services();
|
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');
|
if (!summary) error(404, 'Project not found');
|
||||||
return {
|
return {
|
||||||
project: summary.project,
|
project: summary.project,
|
||||||
running: summary.running,
|
running: summary.running,
|
||||||
runs: await status.history(params.id),
|
runs: await status.history(id),
|
||||||
retentionDays: config.historyRetentionDays
|
retentionDays: config.historyRetentionDays
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
<div class="section">
|
<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}
|
Back to {data.project.name}
|
||||||
</a>
|
</a>
|
||||||
<h1>Sync history</h1>
|
<h1>Sync history</h1>
|
||||||
@@ -47,8 +47,8 @@
|
|||||||
<td>
|
<td>
|
||||||
<a
|
<a
|
||||||
href={resolve('/projects/[id]/history/[runId]', {
|
href={resolve('/projects/[id]/history/[runId]', {
|
||||||
id: data.project.id,
|
id: String(data.project.id),
|
||||||
runId: run.runId
|
runId: String(run.runId)
|
||||||
})}
|
})}
|
||||||
>
|
>
|
||||||
{formatDateTime(run.startedAt)}
|
{formatDateTime(run.startedAt)}
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
import { services } from '#lib/server/app.ts';
|
import { services } from '#lib/server/app.ts';
|
||||||
|
import { idParam } from '#lib/server/params.ts';
|
||||||
import type { PageServerLoad } from './$types';
|
import type { PageServerLoad } from './$types';
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ params }) => {
|
export const load: PageServerLoad = async ({ params }) => {
|
||||||
const { status } = services();
|
const { status } = services();
|
||||||
|
const id = idParam(params.id);
|
||||||
|
const runId = idParam(params.runId);
|
||||||
const [summary, runs, events] = await Promise.all([
|
const [summary, runs, events] = await Promise.all([
|
||||||
status.getSummary(params.id),
|
status.getSummary(id),
|
||||||
status.history(params.id),
|
status.history(id),
|
||||||
status.runEvents(params.id, params.runId)
|
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.');
|
if (!summary || !run) error(404, 'Sync run not found. It may be older than the history window.');
|
||||||
return { project: summary.project, run, events };
|
return { project: summary.project, run, events };
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
|
|
||||||
<div class="page">
|
<div class="page">
|
||||||
<div class="section">
|
<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
|
Back to sync history
|
||||||
</a>
|
</a>
|
||||||
<h1>Sync on {formatDateTime(data.run.startedAt)}</h1>
|
<h1>Sync on {formatDateTime(data.run.startedAt)}</h1>
|
||||||
|
|||||||
Reference in New Issue
Block a user