@supernovaio/cli
Version:
Supernova.io Command Line Interface
146 lines (144 loc) • 6.67 kB
JavaScript
!function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="94d85bd8-e5dc-5f18-a6d4-e16fc655004e")}catch(e){}}();
import { action } from "@oclif/core/ux";
import { isAxiosError } from "axios";
import { sleep } from "./common.js";
const WATCH_INTERVAL_SECONDS = 3;
const WATCH_TIMEOUT_SECONDS = 900;
export async function watchAnalyzeStatus(input) {
const spinner = new AnalyzeStatusSpinner();
const startedAt = Date.now();
const timeoutMs = WATCH_TIMEOUT_SECONDS * 1000;
const watchingSingleRun = Boolean(input.processingRunId);
spinner.start(input.processingRunId ? `Watching processing run ${input.processingRunId}` : "Watching processing runs");
try {
while (true) {
const { runs } = await fetchAnalyzeRuns(input);
const relevantRuns = input.processingRunId
? runs.filter(run => run.processingRunId === input.processingRunId)
: runs;
const activeRuns = relevantRuns.filter(run => run.status === "Processing");
const elapsed = Math.floor((Date.now() - startedAt) / 1000);
spinner.update(watchingSingleRun
? `Processing run ${input.processingRunId} status=${relevantRuns[0]?.status ?? "Unknown"} elapsed=${formatElapsed(elapsed)}`
: `Processing snapshots activeRuns=${activeRuns.length} elapsed=${formatElapsed(elapsed)}`);
if (input.processingRunId && relevantRuns.length === 0) {
spinner.stop();
input.error(`Processing run ${input.processingRunId} was not found.`);
}
const watchedRun = relevantRuns[0];
if (watchingSingleRun && watchedRun?.status === "Failed") {
spinner.stop();
input.log(formatFailedRunSummary(watchedRun));
return {
processingRunId: watchedRun.processingRunId,
status: "failed",
};
}
if (watchingSingleRun && watchedRun?.status === "Processed") {
spinner.stop(`DONE processing run ${input.processingRunId} elapsed=${formatElapsed(elapsed)}`);
return {
processingRunId: watchedRun.processingRunId,
status: "processed",
};
}
if (activeRuns.length === 0) {
const latestRun = relevantRuns[0];
if (!watchingSingleRun && latestRun?.status === "Failed") {
spinner.stop(`FAILED latest run ${latestRun.processingRunId} elapsed=${formatElapsed(elapsed)}`);
input.log(formatFailedRunSummary(latestRun));
return {
processingRunId: latestRun.processingRunId,
status: "failed",
};
}
spinner.stop(`DONE processing activeRuns=0 elapsed=${formatElapsed(elapsed)}`);
return {
processingRunId: latestRun?.processingRunId,
status: "processed",
};
}
if (Date.now() - startedAt >= timeoutMs) {
spinner.stop(input.processingRunId
? `RUNNING processing run ${input.processingRunId} elapsed=${formatElapsed(elapsed)}`
: `RUNNING processing activeRuns=${activeRuns.length} elapsed=${formatElapsed(elapsed)}`);
input.log(input.processingRunId
? `Processing run ${input.processingRunId} is still running after ${WATCH_TIMEOUT_SECONDS}s. Use \`supernova code analyze status\` to continue watching.`
: `Processing is still running after ${WATCH_TIMEOUT_SECONDS}s. Use \`supernova code analyze status\` to continue watching.`);
return {
processingRunId: input.processingRunId,
status: "timed_out",
};
}
await sleep(WATCH_INTERVAL_SECONDS * 1000);
}
}
finally {
spinner.stop();
}
}
async function fetchAnalyzeRuns(input) {
const endpoint = `/code-snapshots/process-status/${encodeURIComponent(input.designSystemId)}`;
try {
const response = await input.apiClient.get(endpoint);
return response.data.result;
}
catch (error) {
if (isAxiosError(error) && error.response?.status === 404) {
input.error("Status endpoint is not deployed on this backend yet. Deploy backend and try again.");
}
throw error;
}
}
function formatElapsed(totalSeconds) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${String(minutes).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
}
function formatFailedRunSummary(run) {
const { startedAt } = run;
const failedComponentsSuffix = run.failedComponentsCount > 0 ? ` Failed components: ${run.failedComponentsCount}.` : "";
return `Processing run ${run.processingRunId} was not successful. Started at ${startedAt}.${failedComponentsSuffix}.`;
}
class AnalyzeStatusSpinner {
spinnerFrames = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
spinnerIndex = 0;
spinnerInterval = null;
spinnerMessage = "";
start(initialMessage) {
this.spinnerMessage = initialMessage;
if (!process.stdout.isTTY) {
action.start(initialMessage);
return;
}
this.spinnerInterval = setInterval(() => {
const frame = this.spinnerFrames[this.spinnerIndex++ % this.spinnerFrames.length];
process.stdout.write(`\r${frame} ${this.spinnerMessage}\u001B[K`);
}, 80);
}
update(message) {
this.spinnerMessage = message;
}
stop(finalMessage) {
if (!process.stdout.isTTY) {
if (finalMessage) {
action.stop(finalMessage);
}
else {
action.stop();
}
return;
}
if (this.spinnerInterval) {
clearInterval(this.spinnerInterval);
this.spinnerInterval = null;
}
if (finalMessage) {
process.stdout.write(`\r${finalMessage}\u001B[K\n`);
}
else {
process.stdout.write("\r\u001B[K");
}
}
}
//# sourceMappingURL=analyze-status.js.map
//# debugId=94d85bd8-e5dc-5f18-a6d4-e16fc655004e