Skip to content
Merged
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
35 changes: 33 additions & 2 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ import {
import { DEFAULT_CONFIG, type AnalyzerConfig } from "./config.ts";
import { env } from "./env.ts";
import { Connectable } from "./sync/connectable.ts";
import { Remote } from "./remote/remote.ts";
import { Remote, StatisticsStrategy } from "./remote/remote.ts";
import { ConnectionManager } from "./sync/connection-manager.ts";
import { RecentQuery } from "./sql/recent-query.ts";
import { QueryHash } from "./sql/recent-query.ts";
import type { OptimizedQuery } from "./sql/recent-query.ts";
import { ExportedStats, StatisticsMode } from "@query-doctor/core";
import { readFile } from "node:fs/promises";

export class Runner {
constructor(
Expand All @@ -44,7 +46,9 @@ export class Runner {
// queries are already sourced from logs
{ disableQueryLoader: true }
);
await remote.syncFrom(options.sourcePostgresUrl);
await remote.syncFrom(options.sourcePostgresUrl,
await Runner.determineStatsMode(options.statisticsPath)
);
await remote.optimizer.finish;
return new Runner(
remote,
Expand All @@ -54,6 +58,33 @@ export class Runner {
);
}

// CI either always pulls data from a file or sets a default. Never pulls from source
static async determineStatsMode(statsPath?: string): Promise<StatisticsStrategy> {
// TODO: grab recent stats from API if they exist
if (statsPath) {
const file = await readFile(statsPath);
const rawStats = JSON.parse(file.toString())
const stats = ExportedStats.array().parse(rawStats);
return {
type: "static",
stats: {
kind: "fromStatisticsExport",
source: { kind: "path", path: statsPath },
stats
}
}
}

return {
type: "static",
stats: {
kind: "fromAssumption",
reltuples: 10_000_000,
relpages: 200_000
}
}
}

async close() {
await this.remote.cleanup();
}
Expand Down
Loading