Skip to content

Commit ed0eb05

Browse files
committed
chore: use nunjucks for github reporter
1 parent a2a273b commit ed0eb05

11 files changed

Lines changed: 684 additions & 240 deletions

File tree

.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
POSTGRES_URL=postgres://postgres:123@localhost:5432/hatira_dev
2+
LOG_PATH=/tmp/postgres_logs/postgres.log

.github/workflows/build-action.yaml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@ jobs:
1919
- name: Install Deno
2020
uses: denoland/setup-deno@v2
2121
with:
22-
cache-hash: ${{ hashFiles('deno.json', '**/deno.lock') }}
22+
cache-hash: ${{ hashFiles('**/deno.lock') }}
2323
deno-version: v2.x
2424
- name: Export version
2525
run: |
2626
echo "DENO_VERSION=$(jq -r '.version' deno.json)" >> $GITHUB_ENV
2727
28-
# - name: Build Action
29-
# shell: bash
30-
# working-directory: ${{ github.action_path }}
31-
# run: |
32-
# deno compile --target x86_64-unknown-linux-gnu --allow-run --allow-read --allow-write --allow-env --allow-net -o bin/analyzer_${{ env.DENO_VERSION }}_linux_x86_64 main.ts
33-
# deno compile --target aarch64-unknown-linux-gnu --allow-run --allow-read --allow-write --allow-env --allow-net -o bin/analyzer_${{ env.DENO_VERSION }}_linux_aarch64 main.ts
34-
3528
- name: Upload files to release
3629
uses: softprops/action-gh-release@v2
3730
with:

action.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ runs:
6666
- name: Run Analyzer
6767
shell: bash
6868
working-directory: ${{ github.action_path }}
69-
run: deno run --allow-run --allow-read --allow-write --allow-env --allow-net main.ts
69+
run: deno run start
7070
env:
7171
INPUT_POSTGRES_URL: ${{ inputs.postgres_url }}
7272
INPUT_LOG_PATH: ${{ inputs.log_path }}

deno.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"$schema": "https://raw.githubusercontent.com/denoland/deno/refs/heads/main/cli/schemas/config-file.v1.json",
33
"version": "0.0.1",
44
"tasks": {
5-
"dev": "deno run -A --watch main.ts"
5+
"start": "deno run --env-file=.env -A --unstable-raw-imports main.ts",
6+
"dev": "deno run --env-file=.env -A --unstable-raw-imports --watch main.ts"
67
},
78
"imports": {
89
"@actions/core": "npm:@actions/core@^1.11.1",
@@ -12,8 +13,10 @@
1213
"@std/assert": "jsr:@std/assert@1",
1314
"@std/data-structures": "jsr:@std/data-structures@^1.0.8",
1415
"@std/fmt": "jsr:@std/fmt@^1.0.8",
16+
"@types/nunjucks": "npm:@types/nunjucks@^3.2.6",
1517
"dedent": "npm:dedent@^1.6.0",
1618
"fast-csv": "npm:fast-csv@^5.0.2",
19+
"nunjucks": "npm:nunjucks@^3.2.4",
1720
"pgsql-deparser": "npm:pgsql-deparser@^17.8.1",
1821
"sql-formatter": "npm:sql-formatter@^15.6.5",
1922
"sql-highlight": "npm:sql-highlight@^6.1.0",

deno.lock

Lines changed: 238 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.ts

Lines changed: 70 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ import { Statistics } from "./optimizer/statistics.ts";
88
import { IndexOptimizer } from "./optimizer/genalgo.ts";
99
import process from "node:process";
1010
import { fingerprint } from "@libpg-query/parser";
11-
import {
12-
GithubReporter,
13-
ReportIndexRecommendation,
14-
} from "./reporters/github.ts";
11+
import { GithubReporter } from "./reporters/github/github.ts.tsx";
1512
import { preprocessEncodedJson } from "./json.ts";
1613
import { ExplainedLog } from "./pg_log.ts";
14+
import {
15+
deriveIndexStatistics,
16+
ReportIndexRecommendation,
17+
} from "./reporters/reporter.ts";
1718

1819
function formatQuery(query: string) {
1920
return format(query, {
@@ -132,7 +133,7 @@ async function main() {
132133
console.log(
133134
"Skipping query that selects from catalog tables",
134135
selectsCatalog,
135-
fingerprintNum
136+
fingerprintNum,
136137
);
137138
}
138139
continue;
@@ -151,30 +152,31 @@ async function main() {
151152
} catch (err) {
152153
console.error(err);
153154
console.error(
154-
`Something went wrong while running this query. Skipping`
155+
`Something went wrong while running this query. Skipping`,
155156
);
156157
return;
157158
}
158-
if (out.newIndexes.size > 0) {
159+
if (out.kind === "ok" && out.newIndexes.size > 0) {
159160
const newIndexes = Array.from(out.newIndexes)
160161
.map((n) => out.triedIndexes.get(n)?.definition)
161162
.filter((n) => n !== undefined);
162163
const existingIndexesForQuery = Array.from(out.existingIndexes)
163164
.map((index) => {
164165
const existing = existingIndexes.find(
165-
(e) => e.index_name === index
166+
(e) => e.index_name === index,
166167
);
167168
if (existing) {
168-
return `${existing.schema_name}.${
169-
existing.table_name
170-
}(${existing.index_columns
171-
.map((c) => `"${c.name}" ${c.order}`)
172-
.join(", ")})`;
169+
return `${existing.schema_name}.${existing.table_name}(${
170+
existing.index_columns
171+
.map((c) => `"${c.name}" ${c.order}`)
172+
.join(", ")
173+
})`;
173174
}
174175
})
175176
.filter((i) => i !== undefined);
176177
console.log(`New indexes: ${newIndexes.join(", ")}`);
177178
recommendations.push({
179+
fingerprint: fingerprintNum,
178180
formattedQuery: formatQuery(query),
179181
baseCost: out.baseCost,
180182
optimizedCost: out.finalCost,
@@ -192,10 +194,12 @@ async function main() {
192194
await output.status;
193195
console.log(`Matched ${matching} queries out of ${allQueries}`);
194196
const reporter = new GithubReporter(process.env.GITHUB_TOKEN);
197+
const statistics = deriveIndexStatistics(recommendations);
195198
await reporter.report({
196199
recommendations,
197-
queriesLookedAt: matching,
198-
totalQueries: allQueries,
200+
queriesMatched: matching,
201+
queriesSeen: allQueries,
202+
statistics,
199203
error,
200204
metadata: {
201205
logSize: fileSize,
@@ -207,6 +211,57 @@ async function main() {
207211
}
208212

209213
if (import.meta.main) {
214+
// const reporter = new GithubReporter(process.env.GITHUB_TOKEN);
215+
// reporter.report({
216+
// metadata: {
217+
// logSize: 100,
218+
// timeElapsed: 100,
219+
// },
220+
// queriesMatched: 2,
221+
// queriesSeen: 100,
222+
// recommendations: [
223+
// {
224+
// fingerprint: "1234567890",
225+
// formattedQuery: `select
226+
// "id",
227+
// "ladder_id",
228+
// "challenger_id",
229+
// "opponent_id",
230+
// "match_date",
231+
// "outcome",
232+
// "rounds_won",
233+
// "rounds_lost",
234+
// "created_at",
235+
// "updated_at",
236+
// "deleted_at"
237+
// from
238+
// "matches"
239+
// where
240+
// (
241+
// (
242+
// "matches"."challenger_id" = $1
243+
// or "matches"."opponent_id" = $2
244+
// )
245+
// and "matches"."deleted_at" is null
246+
// )`,
247+
// baseCost: 100,
248+
// optimizedCost: 50,
249+
// existingIndexes: [],
250+
// proposedIndexes: ["assets(id, name)"],
251+
// explainPlan: {},
252+
// // isQueryLong: true
253+
// },
254+
// {
255+
// fingerprint: "999999",
256+
// formattedQuery: "SELECT * FROM users where aa",
257+
// baseCost: 1000,
258+
// optimizedCost: 50,
259+
// existingIndexes: [],
260+
// proposedIndexes: ["assets(id, name)", "guests(gaming, ez)"],
261+
// explainPlan: { hello: "world" },
262+
// },
263+
// ],
264+
// });
210265
await main();
211266
}
212267

0 commit comments

Comments
 (0)