Added run state front end support
Updated scholars layout to match dashboard
This commit is contained in:
parent
e49e753023
commit
110e246a1c
10 changed files with 1095 additions and 284 deletions
|
|
@ -3,6 +3,7 @@ import { defineStore } from "pinia";
|
|||
import { ApiRequestError } from "@/lib/api/errors";
|
||||
import { fetchCsrfBootstrap } from "@/lib/api/csrf";
|
||||
import { fetchMe, loginSession, logoutSession, type SessionUser } from "@/lib/auth/session";
|
||||
import { useRunStatusStore } from "@/stores/run_status";
|
||||
import { useUiStore } from "@/stores/ui";
|
||||
import { useUserSettingsStore } from "@/stores/user_settings";
|
||||
|
||||
|
|
@ -55,7 +56,9 @@ export const useAuthStore = defineStore("auth", {
|
|||
this.user = response.data.user;
|
||||
this.csrfToken = response.data.csrf_token;
|
||||
const userSettings = useUserSettingsStore();
|
||||
const runStatus = useRunStatusStore();
|
||||
await userSettings.bootstrap();
|
||||
await runStatus.bootstrap();
|
||||
},
|
||||
async logout(): Promise<void> {
|
||||
await logoutSession();
|
||||
|
|
@ -63,7 +66,9 @@ export const useAuthStore = defineStore("auth", {
|
|||
this.user = null;
|
||||
this.csrfToken = null;
|
||||
const userSettings = useUserSettingsStore();
|
||||
const runStatus = useRunStatusStore();
|
||||
userSettings.reset();
|
||||
runStatus.reset();
|
||||
|
||||
try {
|
||||
const csrf = await fetchCsrfBootstrap();
|
||||
|
|
|
|||
176
frontend/src/stores/run_status.test.ts
Normal file
176
frontend/src/stores/run_status.test.ts
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
|
||||
import { ApiRequestError } from "@/lib/api/errors";
|
||||
|
||||
vi.mock("@/features/runs", () => ({
|
||||
listRuns: vi.fn(),
|
||||
triggerManualRun: vi.fn(),
|
||||
}));
|
||||
|
||||
import { listRuns, triggerManualRun } from "@/features/runs";
|
||||
import {
|
||||
RUN_STATUS_POLL_INTERVAL_MS,
|
||||
RUN_STATUS_STARTING_PHASE_MS,
|
||||
useRunStatusStore,
|
||||
} from "@/stores/run_status";
|
||||
|
||||
function buildRun(overrides: Partial<{
|
||||
id: number;
|
||||
trigger_type: string;
|
||||
status: string;
|
||||
start_dt: string;
|
||||
end_dt: string | null;
|
||||
scholar_count: number;
|
||||
new_publication_count: number;
|
||||
failed_count: number;
|
||||
partial_count: number;
|
||||
}> = {}) {
|
||||
return {
|
||||
id: 1,
|
||||
trigger_type: "manual",
|
||||
status: "success",
|
||||
start_dt: "2026-02-19T12:00:00Z",
|
||||
end_dt: "2026-02-19T12:01:00Z",
|
||||
scholar_count: 3,
|
||||
new_publication_count: 2,
|
||||
failed_count: 0,
|
||||
partial_count: 0,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("run status store", () => {
|
||||
const mockedListRuns = vi.mocked(listRuns);
|
||||
const mockedTriggerManualRun = vi.mocked(triggerManualRun);
|
||||
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
mockedListRuns.mockReset();
|
||||
mockedTriggerManualRun.mockReset();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
useRunStatusStore().reset();
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("bootstraps from latest run and exposes idle start state", async () => {
|
||||
mockedListRuns.mockResolvedValueOnce([buildRun({ id: 11, status: "success" })]);
|
||||
|
||||
const store = useRunStatusStore();
|
||||
await store.bootstrap();
|
||||
|
||||
expect(mockedListRuns).toHaveBeenCalledWith({ limit: 1 });
|
||||
expect(store.latestRun?.id).toBe(11);
|
||||
expect(store.canStart).toBe(true);
|
||||
expect(store.isRunActive).toBe(false);
|
||||
expect(store.isPolling).toBe(false);
|
||||
});
|
||||
|
||||
it("starts manual checks and marks active state", async () => {
|
||||
mockedTriggerManualRun.mockResolvedValueOnce({
|
||||
run_id: 25,
|
||||
status: "running",
|
||||
scholar_count: 0,
|
||||
succeeded_count: 0,
|
||||
failed_count: 0,
|
||||
partial_count: 0,
|
||||
new_publication_count: 0,
|
||||
reused_existing_run: false,
|
||||
idempotency_key: "abc",
|
||||
});
|
||||
mockedListRuns.mockResolvedValueOnce([buildRun({ id: 25, status: "running", end_dt: null })]);
|
||||
|
||||
const store = useRunStatusStore();
|
||||
const result = await store.startManualCheck();
|
||||
|
||||
expect(result).toEqual({
|
||||
kind: "started",
|
||||
runId: 25,
|
||||
reusedExistingRun: false,
|
||||
});
|
||||
expect(store.latestRun?.id).toBe(25);
|
||||
expect(store.isRunActive).toBe(true);
|
||||
expect(store.isPolling).toBe(true);
|
||||
});
|
||||
|
||||
it("normalizes run_in_progress responses into already_running state", async () => {
|
||||
mockedTriggerManualRun.mockRejectedValueOnce(
|
||||
new ApiRequestError({
|
||||
status: 409,
|
||||
code: "run_in_progress",
|
||||
message: "A run is already in progress for this account.",
|
||||
details: { run_id: 42 },
|
||||
requestId: "req_123",
|
||||
}),
|
||||
);
|
||||
mockedListRuns.mockResolvedValueOnce([buildRun({ id: 42, status: "running", end_dt: null })]);
|
||||
|
||||
const store = useRunStatusStore();
|
||||
const result = await store.startManualCheck();
|
||||
|
||||
expect(result).toEqual({
|
||||
kind: "already_running",
|
||||
runId: 42,
|
||||
requestId: "req_123",
|
||||
});
|
||||
expect(store.latestRun?.id).toBe(42);
|
||||
expect(store.isRunActive).toBe(true);
|
||||
expect(store.lastErrorMessage).toBeNull();
|
||||
});
|
||||
|
||||
it("polls while a run is active and stops when it completes", async () => {
|
||||
vi.useFakeTimers();
|
||||
mockedListRuns
|
||||
.mockResolvedValueOnce([buildRun({ id: 90, status: "running", end_dt: null })])
|
||||
.mockResolvedValueOnce([buildRun({ id: 90, status: "success" })]);
|
||||
|
||||
const store = useRunStatusStore();
|
||||
await store.syncLatest();
|
||||
expect(store.isPolling).toBe(true);
|
||||
|
||||
await vi.advanceTimersByTimeAsync(RUN_STATUS_POLL_INTERVAL_MS + 10);
|
||||
|
||||
expect(mockedListRuns).toHaveBeenCalledTimes(2);
|
||||
expect(store.latestRun?.status).toBe("success");
|
||||
expect(store.isPolling).toBe(false);
|
||||
expect(store.isRunActive).toBe(false);
|
||||
});
|
||||
|
||||
it("switches from starting to in-progress when trigger request remains open", async () => {
|
||||
vi.useFakeTimers();
|
||||
mockedTriggerManualRun.mockImplementation(
|
||||
() =>
|
||||
new Promise((resolve) => {
|
||||
setTimeout(() => {
|
||||
resolve({
|
||||
run_id: 77,
|
||||
status: "running",
|
||||
scholar_count: 0,
|
||||
succeeded_count: 0,
|
||||
failed_count: 0,
|
||||
partial_count: 0,
|
||||
new_publication_count: 0,
|
||||
reused_existing_run: false,
|
||||
idempotency_key: "x",
|
||||
});
|
||||
}, RUN_STATUS_STARTING_PHASE_MS + 500);
|
||||
}),
|
||||
);
|
||||
mockedListRuns.mockResolvedValueOnce([]);
|
||||
|
||||
const store = useRunStatusStore();
|
||||
const startPromise = store.startManualCheck();
|
||||
|
||||
expect(store.isSubmitting).toBe(true);
|
||||
expect(store.isLikelyRunning).toBe(false);
|
||||
|
||||
await vi.advanceTimersByTimeAsync(RUN_STATUS_STARTING_PHASE_MS + 20);
|
||||
expect(store.isLikelyRunning).toBe(true);
|
||||
await vi.advanceTimersByTimeAsync(520);
|
||||
await startPromise;
|
||||
expect(store.isRunActive).toBe(true);
|
||||
});
|
||||
});
|
||||
264
frontend/src/stores/run_status.ts
Normal file
264
frontend/src/stores/run_status.ts
Normal file
|
|
@ -0,0 +1,264 @@
|
|||
import { defineStore } from "pinia";
|
||||
|
||||
import { listRuns, triggerManualRun, type RunListItem } from "@/features/runs";
|
||||
import { ApiRequestError } from "@/lib/api/errors";
|
||||
|
||||
export const RUN_STATUS_POLL_INTERVAL_MS = 5000;
|
||||
export const RUN_STATUS_STARTING_PHASE_MS = 1500;
|
||||
|
||||
export type StartManualCheckResult =
|
||||
| {
|
||||
kind: "started";
|
||||
runId: number;
|
||||
reusedExistingRun: boolean;
|
||||
}
|
||||
| {
|
||||
kind: "already_running";
|
||||
runId: number | null;
|
||||
requestId: string | null;
|
||||
}
|
||||
| {
|
||||
kind: "error";
|
||||
message: string;
|
||||
requestId: string | null;
|
||||
};
|
||||
|
||||
let pollTimer: ReturnType<typeof setInterval> | null = null;
|
||||
let syncPromise: Promise<void> | null = null;
|
||||
let submittingPhaseTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
function parseRunId(value: unknown): number | null {
|
||||
if (typeof value === "number" && Number.isFinite(value)) {
|
||||
return value;
|
||||
}
|
||||
if (typeof value === "string" && value.trim().length > 0) {
|
||||
const parsed = Number.parseInt(value, 10);
|
||||
if (Number.isFinite(parsed)) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function extractRunIdFromDetails(details: unknown): number | null {
|
||||
if (!details || typeof details !== "object") {
|
||||
return null;
|
||||
}
|
||||
|
||||
const runIdCandidate = (details as Record<string, unknown>).run_id;
|
||||
return parseRunId(runIdCandidate);
|
||||
}
|
||||
|
||||
function buildPlaceholderRunningRun(runId: number): RunListItem {
|
||||
return {
|
||||
id: runId,
|
||||
trigger_type: "manual",
|
||||
status: "running",
|
||||
start_dt: new Date().toISOString(),
|
||||
end_dt: null,
|
||||
scholar_count: 0,
|
||||
new_publication_count: 0,
|
||||
failed_count: 0,
|
||||
partial_count: 0,
|
||||
};
|
||||
}
|
||||
|
||||
export const useRunStatusStore = defineStore("runStatus", {
|
||||
state: () => ({
|
||||
latestRun: null as RunListItem | null,
|
||||
isSubmitting: false,
|
||||
assumeRunningFromSubmission: false,
|
||||
isPolling: false,
|
||||
lastErrorMessage: null as string | null,
|
||||
lastErrorRequestId: null as string | null,
|
||||
lastSyncAt: null as number | null,
|
||||
}),
|
||||
getters: {
|
||||
isRunActive(state): boolean {
|
||||
return state.isSubmitting || state.latestRun?.status === "running";
|
||||
},
|
||||
isLikelyRunning(state): boolean {
|
||||
return state.latestRun?.status === "running" || state.assumeRunningFromSubmission;
|
||||
},
|
||||
canStart(): boolean {
|
||||
return !this.isRunActive;
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
clearSubmittingPhaseTimer(): void {
|
||||
if (submittingPhaseTimer !== null) {
|
||||
clearTimeout(submittingPhaseTimer);
|
||||
submittingPhaseTimer = null;
|
||||
}
|
||||
},
|
||||
beginSubmittingPhaseTracking(): void {
|
||||
this.assumeRunningFromSubmission = false;
|
||||
this.clearSubmittingPhaseTimer();
|
||||
submittingPhaseTimer = setTimeout(() => {
|
||||
if (!this.isSubmitting) {
|
||||
return;
|
||||
}
|
||||
this.assumeRunningFromSubmission = true;
|
||||
}, RUN_STATUS_STARTING_PHASE_MS);
|
||||
},
|
||||
setLatestRun(run: RunListItem | null): void {
|
||||
this.latestRun = run;
|
||||
this.lastSyncAt = Date.now();
|
||||
this.lastErrorMessage = null;
|
||||
this.lastErrorRequestId = null;
|
||||
if (run?.status === "running") {
|
||||
this.assumeRunningFromSubmission = true;
|
||||
} else if (!this.isSubmitting) {
|
||||
this.assumeRunningFromSubmission = false;
|
||||
}
|
||||
this.updatePolling();
|
||||
},
|
||||
startPolling(): void {
|
||||
if (pollTimer !== null) {
|
||||
this.isPolling = true;
|
||||
return;
|
||||
}
|
||||
|
||||
this.isPolling = true;
|
||||
pollTimer = setInterval(() => {
|
||||
void this.syncLatest();
|
||||
}, RUN_STATUS_POLL_INTERVAL_MS);
|
||||
},
|
||||
stopPolling(): void {
|
||||
if (pollTimer !== null) {
|
||||
clearInterval(pollTimer);
|
||||
pollTimer = null;
|
||||
}
|
||||
this.isPolling = false;
|
||||
},
|
||||
updatePolling(): void {
|
||||
if (this.isRunActive) {
|
||||
this.startPolling();
|
||||
return;
|
||||
}
|
||||
this.stopPolling();
|
||||
},
|
||||
async syncLatest(): Promise<void> {
|
||||
if (syncPromise) {
|
||||
await syncPromise;
|
||||
return;
|
||||
}
|
||||
|
||||
syncPromise = (async () => {
|
||||
try {
|
||||
const runs = await listRuns({ limit: 1 });
|
||||
const latest = runs[0] ?? null;
|
||||
this.latestRun = latest;
|
||||
this.lastSyncAt = Date.now();
|
||||
this.lastErrorMessage = null;
|
||||
this.lastErrorRequestId = null;
|
||||
if (latest?.status === "running") {
|
||||
this.assumeRunningFromSubmission = true;
|
||||
} else if (!this.isSubmitting) {
|
||||
this.assumeRunningFromSubmission = false;
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof ApiRequestError) {
|
||||
this.lastErrorMessage = error.message;
|
||||
this.lastErrorRequestId = error.requestId;
|
||||
} else {
|
||||
this.lastErrorMessage = "Unable to refresh check status.";
|
||||
this.lastErrorRequestId = null;
|
||||
}
|
||||
} finally {
|
||||
syncPromise = null;
|
||||
this.updatePolling();
|
||||
}
|
||||
})();
|
||||
|
||||
await syncPromise;
|
||||
},
|
||||
async bootstrap(): Promise<void> {
|
||||
await this.syncLatest();
|
||||
},
|
||||
async startManualCheck(): Promise<StartManualCheckResult> {
|
||||
if (this.isRunActive) {
|
||||
return {
|
||||
kind: "already_running",
|
||||
runId: this.latestRun?.id ?? null,
|
||||
requestId: null,
|
||||
};
|
||||
}
|
||||
|
||||
this.isSubmitting = true;
|
||||
this.lastErrorMessage = null;
|
||||
this.lastErrorRequestId = null;
|
||||
this.beginSubmittingPhaseTracking();
|
||||
this.updatePolling();
|
||||
|
||||
try {
|
||||
const result = await triggerManualRun();
|
||||
await this.syncLatest();
|
||||
|
||||
if (!this.latestRun || this.latestRun.id !== result.run_id) {
|
||||
this.latestRun = buildPlaceholderRunningRun(result.run_id);
|
||||
this.lastSyncAt = Date.now();
|
||||
this.assumeRunningFromSubmission = true;
|
||||
}
|
||||
|
||||
return {
|
||||
kind: "started",
|
||||
runId: result.run_id,
|
||||
reusedExistingRun: result.reused_existing_run,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof ApiRequestError && error.code === "run_in_progress") {
|
||||
const runId = extractRunIdFromDetails(error.details);
|
||||
if (runId !== null) {
|
||||
this.latestRun = buildPlaceholderRunningRun(runId);
|
||||
this.lastSyncAt = Date.now();
|
||||
this.assumeRunningFromSubmission = true;
|
||||
}
|
||||
await this.syncLatest();
|
||||
|
||||
return {
|
||||
kind: "already_running",
|
||||
runId: this.latestRun?.id ?? runId,
|
||||
requestId: error.requestId,
|
||||
};
|
||||
}
|
||||
|
||||
if (error instanceof ApiRequestError) {
|
||||
this.lastErrorMessage = error.message;
|
||||
this.lastErrorRequestId = error.requestId;
|
||||
return {
|
||||
kind: "error",
|
||||
message: error.message,
|
||||
requestId: error.requestId,
|
||||
};
|
||||
}
|
||||
|
||||
const fallbackMessage = "Unable to start an update check.";
|
||||
this.lastErrorMessage = fallbackMessage;
|
||||
this.lastErrorRequestId = null;
|
||||
return {
|
||||
kind: "error",
|
||||
message: fallbackMessage,
|
||||
requestId: null,
|
||||
};
|
||||
} finally {
|
||||
this.isSubmitting = false;
|
||||
this.clearSubmittingPhaseTimer();
|
||||
if (this.latestRun?.status !== "running") {
|
||||
this.assumeRunningFromSubmission = false;
|
||||
}
|
||||
this.updatePolling();
|
||||
}
|
||||
},
|
||||
reset(): void {
|
||||
this.stopPolling();
|
||||
this.clearSubmittingPhaseTimer();
|
||||
this.latestRun = null;
|
||||
this.isSubmitting = false;
|
||||
this.assumeRunningFromSubmission = false;
|
||||
this.lastErrorMessage = null;
|
||||
this.lastErrorRequestId = null;
|
||||
this.lastSyncAt = null;
|
||||
},
|
||||
},
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue