@@ -8,12 +8,13 @@ import { Statistics } from "./optimizer/statistics.ts";
88import { IndexOptimizer } from "./optimizer/genalgo.ts" ;
99import process from "node:process" ;
1010import { 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" ;
1512import { preprocessEncodedJson } from "./json.ts" ;
1613import { ExplainedLog } from "./pg_log.ts" ;
14+ import {
15+ deriveIndexStatistics ,
16+ ReportIndexRecommendation ,
17+ } from "./reporters/reporter.ts" ;
1718
1819function 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
209213if ( 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