stock-bot/apps/stock/web-api/src/services/experiment-retry.ts

45 lines
1.8 KiB
TypeScript

import type { QueryResultRow } from 'pg';
import {
experimentRetrySchema,
type ExperimentRetryInput,
type ExperimentTrialRetry,
} from '@stock-bot/stock-config/experiments';
import { ExperimentError } from './experiment-input';
export function parseExperimentRetry(input: unknown): ExperimentRetryInput {
const parsed = experimentRetrySchema.safeParse(input);
if (!parsed.success) {
throw new ExperimentError(
400,
'Retry requires the observed nonnegative attempt number and retry count only'
);
}
return parsed.data;
}
/** Format in SQL to preserve PostgreSQL microseconds, including pre-JavaScript failure timestamps. */
const timestamp = (column: string) =>
`to_char(h.${column} AT TIME ZONE 'UTC','YYYY-MM-DD"T"HH24:MI:SS.US"Z"') AS ${column}`;
export const retryColumns = `h.id,h.experiment_id,h.run_id,h.trial_key,h.retry_number,h.previous_attempt_number,h.previous_attempt_id,
h.error,h.progress,h.principal,h.review_id,h.submission_key,h.submission_hash,
${['current_simulation_date', 'started_at', 'completed_at', 'paused_at', 'failure_updated_at', 'recorded_at'].map(timestamp).join(',')}`;
export function mapExperimentRetry(row: QueryResultRow): ExperimentTrialRetry {
return {
id: row.id,
experimentId: row.experiment_id,
runId: row.run_id,
trialKey: row.trial_key,
retryNumber: row.retry_number,
previousAttemptNumber: row.previous_attempt_number,
previousAttemptId: row.previous_attempt_id,
error: row.error,
progress: Number(row.progress),
currentDate: row.current_simulation_date,
startedAt: row.started_at,
completedAt: row.completed_at,
pausedAt: row.paused_at,
failureUpdatedAt: row.failure_updated_at,
recordedAt: row.recorded_at,
principal: row.principal,
reviewId: row.review_id,
};
}