Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions frontend/src/api/async-job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
export type AsyncJobStatus = "ONGOING" | "SUCCEEDED" | "FAILED";

export interface AsyncJob<TPayload = unknown> {
id: string;
status: AsyncJobStatus;
payload?: TPayload;
}

const DEFAULT_POLL_INTERVAL_MS = 10 * 1_000;
Comment thread
hilnius marked this conversation as resolved.
const DEFAULT_TIMEOUT_MS = 5 * 60 * 1_000;

export async function pollUntilSucceeded<TPayload>(
fetchJob: () => Promise<AsyncJob<TPayload>>,
options: { intervalMs?: number; timeoutMs?: number } = {},
): Promise<TPayload> {
const intervalMs = options.intervalMs ?? DEFAULT_POLL_INTERVAL_MS;
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const deadline = Date.now() + timeoutMs;

while (true) {
Comment thread
hilnius marked this conversation as resolved.
await sleep(intervalMs);

if (Date.now() >= deadline) {
throw new Error(`Did not get results within ${timeoutMs}ms. Aborting polling.`);
}

const job = await fetchJob();

if (job.status === "SUCCEEDED") {
if (job.payload === undefined) {
throw new Error("Async job succeeded but returned no payload.");
}
return job.payload;
}

if (job.status === "FAILED") {
throw new Error(`Async job ${job.id} failed.`);
}
}
}

function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
22 changes: 15 additions & 7 deletions frontend/src/api/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { type AsyncJob, pollUntilSucceeded } from "./async-job.js";
import { nablaFetch } from "../transport/client.js";
import type { ClinicalNote } from "./note.js";

Expand All @@ -18,11 +19,18 @@ export interface NormalizedData {
export async function generateNormalizedData(
note: ClinicalNote,
): Promise<NormalizedData> {
const response = await nablaFetch("/v1/core/user/generate-normalized-data", {
method: "POST",
body: JSON.stringify({
note,
}),
});
return response.json() as Promise<NormalizedData>;
const submitResponse = await nablaFetch(
"/v1/core/user/generate-normalized-data-async",
{
method: "POST",
body: JSON.stringify({ note }),
},
);
const { id } = (await submitResponse.json()) as AsyncJob;

return pollUntilSucceeded<NormalizedData>(() =>
nablaFetch(`/v1/core/user/generate-normalized-data-async/${id}`).then(
(response) => response.json() as Promise<AsyncJob<NormalizedData>>,
),
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function markup(): string {
<div class="bg-white rounded-xl border border-grey-200 p-5">
<div class="flex items-center gap-2 mb-1">
<h3 class="font-semibold text-grey-400">Normalize Data</h3>
<a href="${DOCUMENTATION_LINKS.generateNormalizedData}" target="_blank" rel="noopener" class="text-xs font-mono text-grey-250 hover:text-primary-600 bg-grey-100 hover:bg-primary-50 px-2 py-0.5 rounded transition-colors">POST /generate-normalized-data ↗</a>
<a href="${DOCUMENTATION_LINKS.generateNormalizedData}" target="_blank" rel="noopener" class="text-xs font-mono text-grey-250 hover:text-primary-600 bg-grey-100 hover:bg-primary-50 px-2 py-0.5 rounded transition-colors">POST /generate-normalized-data-async ↗</a>
</div>
<p class="text-xs text-grey-300 mb-4">Extract ICD-10 / LOINC codes in FHIR format from the note.</p>
<button id="generate-normalized-btn" disabled class="bg-primary-600 hover:bg-primary-700 text-white text-sm font-medium px-4 py-2 rounded-lg transition-colors disabled:opacity-40 disabled:cursor-not-allowed">
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/shared/documentationLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export const DOCUMENTATION_LINKS = {
transcribeWs: "https://docs.nabla.com/user/transcribe-ws",
generateNote: "https://docs.nabla.com/user/generate-note",
generateNormalizedData: "https://docs.nabla.com/user/generate-normalized-data",
generateNormalizedData:
"https://docs.nabla.com/user/generate-normalized-data-async",
generatePatientInstructions: "https://docs.nabla.com/user/generate-patient-instructions",
} as const;