-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.ts
More file actions
234 lines (215 loc) · 8.27 KB
/
Copy pathrunner.ts
File metadata and controls
234 lines (215 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { recommendationClearsFloor } from "./gate/cost-floor.ts";
import * as core from "@actions/core";
import { PgbadgerSource } from "./sql/pgbadger.ts";
import type { RecentQuerySource } from "./sql/recent-query.ts";
import { GithubReporter } from "./reporters/github/github.ts";
import {
deriveIndexStatistics,
type ReportContext,
type ReportIndexRecommendation,
type ReportQueryCostWarning,
} from "./reporters/reporter.ts";
import { DEFAULT_CONFIG, type AnalyzerConfig } from "./config.ts";
import { env } from "./env.ts";
import { Connectable } from "./sync/connectable.ts";
import { Remote, StatisticsStrategy } from "./remote/remote.ts";
import { ConnectionManager } from "./sync/connection-manager.ts";
import type { OptimizedQuery } from "./sql/recent-query.ts";
import { ExportedStats, Statistics } from "@query-doctor/core";
import { readFile } from "node:fs/promises";
import { buildQueries } from "./reporters/site-api.ts";
export class Runner {
constructor(
private readonly remote: Remote,
private readonly source: RecentQuerySource,
private readonly maxCost?: number,
private readonly ignoredQueryHashes: Set<string> = new Set(),
) { }
static async build(options: {
targetPostgresUrl: Connectable;
sourcePostgresUrl: Connectable;
statisticsPath?: string;
maxCost?: number;
source: RecentQuerySource;
ignoredQueryHashes?: string[];
remote?: Remote;
// Real production statistics pulled from the Site API. When present, queries
// are costed against true prod cardinality instead of synthetic assumptions.
productionStats?: ExportedStats[];
}) {
const remote = options.remote ?? new Remote(
options.targetPostgresUrl,
ConnectionManager.forLocalDatabase(),
ConnectionManager.forRemoteDatabase(),
{ disableQueryLoader: true }
);
await remote.syncFrom(options.sourcePostgresUrl,
await Runner.determineStatsMode(options.statisticsPath, options.productionStats)
);
await remote.optimizer.finish;
return new Runner(
remote,
options.source,
options.maxCost,
new Set(options.ignoredQueryHashes ?? []),
);
}
// Stats-mode precedence for CI: real production stats pulled from the Site API
// win, then an explicit stats file, then synthetic assumptions. CI never dumps
// stats from the ephemeral target database itself.
static async determineStatsMode(
statsPath?: string,
productionStats?: ExportedStats[],
): Promise<StatisticsStrategy> {
if (productionStats && productionStats.length > 0) {
return {
type: "static",
stats: Statistics.statsModeFromExport(productionStats),
};
}
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,
}
}
}
async close() {
await this.remote.cleanup();
}
async run(config: AnalyzerConfig = DEFAULT_CONFIG) {
const startDate = new Date();
console.time("total");
const recentQueries = await this.source.getRecentQueries();
const error = this.source instanceof PgbadgerSource
? this.source.streamError
: undefined;
const totalRows = this.source instanceof PgbadgerSource
? this.source.totalRows
: recentQueries.length;
await this.remote.optimizer.addQueries(recentQueries);
await this.remote.optimizer.finish;
const optimizedQueries = this.remote.optimizer.getQueries();
const existingIndexes = this.remote.optimizer.getExistingIndexes();
const resolveIndexNames = (names: string[]) =>
names.map((name) => {
const idx = existingIndexes.find((e) => e.indexName.toString() === name);
return idx
? `${idx.schemaName}.${idx.tableName}(${idx.keyColumns.map((c) => `"${c.name}" ${c.order ?? "ASC"}`).join(", ")})`
: name;
});
console.log(
`Matched ${this.remote.optimizer.validQueriesProcessed} unique queries out of ${totalRows} entries`,
);
const recommendations: ReportIndexRecommendation[] = [];
const queriesPastThreshold: ReportQueryCostWarning[] = [];
const allResults: OptimizedQuery[] = [];
for (const q of optimizedQueries) {
if (this.ignoredQueryHashes.has(q.hash)) {
continue;
}
const { optimization } = q;
if (
optimization.state === "improvements_available" ||
optimization.state === "no_improvement_found"
) {
optimization.indexesUsed = resolveIndexNames(optimization.indexesUsed);
}
allResults.push(q);
if (optimization.state === "improvements_available") {
recommendations.push({
fingerprint: q.hash,
formattedQuery: q.formattedQuery,
baseCost: optimization.cost,
baseExplainPlan: optimization.explainPlan,
optimizedCost: optimization.optimizedCost,
existingIndexes: optimization.indexesUsed,
proposedIndexes: optimization.indexRecommendations.map((r) => r.definition),
explainPlan: optimization.optimizedExplainPlan,
});
} else if (
optimization.state === "no_improvement_found" &&
typeof this.maxCost === "number" &&
optimization.cost > this.maxCost
) {
queriesPastThreshold.push({
fingerprint: q.hash,
formattedQuery: q.formattedQuery,
baseCost: optimization.cost,
explainPlan: optimization.explainPlan,
maxCost: this.maxCost,
});
}
}
const filteredRecommendations =
config.minimumCost > 0
? recommendations.filter((r) => recommendationClearsFloor(r.baseCost, config.minimumCost))
: recommendations;
// Kept, not discarded: still shown in the comment (marked below-threshold) so
// a below-floor query isn't mislabeled "no index suggestion", but excluded
// from gating and index statistics.
const belowThresholdRecommendations =
config.minimumCost > 0
? recommendations.filter((r) => !recommendationClearsFloor(r.baseCost, config.minimumCost))
: [];
const filteredThresholdWarnings =
config.minimumCost > 0
? queriesPastThreshold.filter((w) => w.baseCost > config.minimumCost)
: queriesPastThreshold;
if (config.minimumCost > 0) {
const shownNotGated = belowThresholdRecommendations.length;
const filteredWarnings =
queriesPastThreshold.length - filteredThresholdWarnings.length;
if (shownNotGated > 0 || filteredWarnings > 0) {
console.log(
`minimumCost=${config.minimumCost}: ${shownNotGated} recommendation(s) shown below threshold (not gated), ${filteredWarnings} cost warning(s) filtered from the PR comment`,
);
}
}
const analyzed = buildQueries(allResults, config).length;
const statistics = deriveIndexStatistics(filteredRecommendations);
const timeElapsed = Date.now() - startDate.getTime();
const reportContext: ReportContext = {
statisticsMode: this.remote.optimizer.statisticsMode,
computedStats: this.remote.optimizer.computedStats,
modeledTables: this.remote.optimizer.syntheticTables,
recommendations: filteredRecommendations,
belowThresholdRecommendations,
queriesPastThreshold: filteredThresholdWarnings,
queryStats: Object.freeze({
analyzed,
matched: this.remote.optimizer.validQueriesProcessed,
optimized: filteredRecommendations.length,
errored: optimizedQueries.filter((q) => q.optimization.state === "error").length,
}),
statistics,
error,
metadata: {
logSize: this.source instanceof PgbadgerSource ? this.source.logSize : -1,
timeElapsed,
},
};
console.timeEnd("total");
return { reportContext, allResults };
}
async report(reportContext: ReportContext) {
const reporter = new GithubReporter(env.GITHUB_TOKEN);
console.log(`Generating report (${reporter.provider()})`);
await reporter.report(reportContext);
}
}
export type QueryProcessResult = OptimizedQuery;