11import { setPool , dbIsReady , fillDb , createTables , dbIsUpToDate , setupTestDatabase , addStatusRow , clearStatusRows } from "./db/db.js" ;
2- import { stopFinlexLimiterLogging , startFinlexLimiterLogging } from "./db/load.js" ;
2+ import { stopFinlexLimiterLogging } from "./db/load.js" ;
33import * as Sentry from '@sentry/node' ;
44import './util/config.js' ;
55import { exit } from 'process' ;
6- import { syncStatutes , deleteCollection , syncJudgments } from "./search.js" ;
6+ import { syncStatutes , deleteCollection , syncJudgments , SyncResult } from "./search.js" ;
77import { yearFrom , yearTo } from "./util/config.js" ;
88
99setPool ( process . env . PG_URI ?? '' ) ;
@@ -41,11 +41,80 @@ async function initDatabase(startYear?: number) {
4141 }
4242}
4343
44- export const runSetup = async ( startYear ?: number ) => {
45- const setupStartTime = Date . now ( ) ;
44+ function printSyncSummary ( results : SyncResult [ ] ) {
45+ console . log ( '\n' + '=' . repeat ( 80 ) ) ;
46+ console . log ( 'TYPESENSE INDEXING SUMMARY' ) ;
47+ console . log ( '=' . repeat ( 80 ) ) ;
48+
49+ let totalProcessed = 0 ;
50+ let totalSuccess = 0 ;
51+ let totalFailures = 0 ;
52+ let hasAnyFailures = false ;
53+
54+ // Group by type
55+ const statuteResults = results . filter ( r => r . type === 'statutes' ) ;
56+ const judgmentResults = results . filter ( r => r . type === 'judgments' ) ;
57+
58+ // Print Statutes Summary
59+ console . log ( '\n📚 STATUTES:' ) ;
60+ console . log ( '-' . repeat ( 80 ) ) ;
61+ statuteResults . forEach ( result => {
62+ const status = result . failureCount === 0 ? '✓' : '⚠️' ;
63+ const statusText = result . failureCount === 0 ? 'SUCCESS' : 'PARTIAL' ;
64+ console . log ( ` ${ status } ${ result . language . toUpperCase ( ) } : ${ statusText } ` ) ;
65+ console . log ( ` Total: ${ result . totalProcessed } | Success: ${ result . successCount } | Failed: ${ result . failureCount } ` ) ;
66+
67+ totalProcessed += result . totalProcessed ;
68+ totalSuccess += result . successCount ;
69+ totalFailures += result . failureCount ;
70+
71+ if ( result . failureCount > 0 ) {
72+ hasAnyFailures = true ;
73+ }
74+ } ) ;
75+
76+ // Print Judgments Summary
77+ console . log ( '\n⚖️ JUDGMENTS:' ) ;
78+ console . log ( '-' . repeat ( 80 ) ) ;
79+ judgmentResults . forEach ( result => {
80+ const status = result . failureCount === 0 ? '✓' : '⚠️' ;
81+ const statusText = result . failureCount === 0 ? 'SUCCESS' : 'PARTIAL' ;
82+ console . log ( ` ${ status } ${ result . language . toUpperCase ( ) } : ${ statusText } ` ) ;
83+ console . log ( ` Total: ${ result . totalProcessed } | Success: ${ result . successCount } | Failed: ${ result . failureCount } ` ) ;
84+
85+ totalProcessed += result . totalProcessed ;
86+ totalSuccess += result . successCount ;
87+ totalFailures += result . failureCount ;
88+
89+ if ( result . failureCount > 0 ) {
90+ hasAnyFailures = true ;
91+ }
92+ } ) ;
93+
94+ // Print Overall Summary
95+ console . log ( '\n' + '=' . repeat ( 80 ) ) ;
96+ console . log ( 'OVERALL TOTALS:' ) ;
97+ console . log ( ` Total Documents Processed: ${ totalProcessed } ` ) ;
98+ console . log ( ` Successfully Indexed: ${ totalSuccess } ` ) ;
99+ console . log ( ` Failed to Index: ${ totalFailures } ` ) ;
46100
47- startFinlexLimiterLogging ( ) ;
101+ const successRate = totalProcessed > 0 ? ( ( totalSuccess / totalProcessed ) * 100 ) . toFixed ( 2 ) : '0.00' ;
102+ console . log ( ` Success Rate: ${ successRate } %` ) ;
48103
104+ console . log ( '=' . repeat ( 80 ) ) ;
105+
106+ if ( hasAnyFailures ) {
107+ console . log ( '\n⚠️ WARNING: Some documents failed to index. Review the detailed logs above.' ) ;
108+ console . log ( 'Failed documents have been logged with full details for investigation.\n' ) ;
109+ } else {
110+ console . log ( '\n✓ ALL DOCUMENTS SUCCESSFULLY INDEXED!\n' ) ;
111+ }
112+ }
113+
114+ export const runSetup = async ( startYear ?: number ) => {
115+ const setupStartTime = Date . now ( ) ;
116+ const syncResults : SyncResult [ ] = [ ] ;
117+
49118 try {
50119 if ( process . env . NODE_ENV === 'test' ) {
51120 console . log ( '[SETUP] Running in test mode...' ) ;
@@ -68,33 +137,41 @@ export const runSetup = async (startYear?: number) => {
68137
69138 const step3Start = Date . now ( ) ;
70139 console . log ( '[SETUP] Step 3: Sync statutes to Typesense...' ) ;
71- await syncStatutes ( 'fin' ) ;
72- await syncStatutes ( 'swe' ) ;
140+ const statuteFinResult = await syncStatutes ( 'fin' ) ;
141+ syncResults . push ( statuteFinResult ) ;
142+ const statuteSweResult = await syncStatutes ( 'swe' ) ;
143+ syncResults . push ( statuteSweResult ) ;
73144 console . log ( `[SETUP] Step 3 completed in ${ Date . now ( ) - step3Start } ms` ) ;
74145
75146 const step4Start = Date . now ( ) ;
76147 console . log ( '[SETUP] Step 4: Sync judgments to Typesense...' ) ;
77- await syncJudgments ( 'fin' ) ;
78- await syncJudgments ( 'swe' ) ;
148+ const judgmentFinResult = await syncJudgments ( 'fin' ) ;
149+ syncResults . push ( judgmentFinResult ) ;
150+ const judgmentSweResult = await syncJudgments ( 'swe' ) ;
151+ syncResults . push ( judgmentSweResult ) ;
79152 console . log ( `[SETUP] Step 4 completed in ${ Date . now ( ) - step4Start } ms` ) ;
80153
81154 const step5Start = Date . now ( ) ;
82155 console . log ( '[SETUP] Step 5: Write completion status...' ) ;
83156 const from = startYear ?? yearFrom ( ) ;
84157 const to = yearTo ( ) ;
85- await addStatusRow ( { message : 'updated' , from, to, timestamp : new Date ( ) . toISOString ( ) } , false ) ;
158+ await addStatusRow ( { message : 'updated' , from, to } , false ) ;
86159 console . log ( `[SETUP] Step 5 completed in ${ Date . now ( ) - step5Start } ms` ) ;
160+
161+ // Print comprehensive summary
162+ printSyncSummary ( syncResults ) ;
163+ }
164+
165+ const totalTime = Date . now ( ) - setupStartTime ;
166+ console . log ( `[SETUP] Total setup time: ${ totalTime } ms (${ ( totalTime / 1000 / 60 ) . toFixed ( 2 ) } minutes)` ) ;
167+
168+ if ( process . env . NODE_ENV !== 'test' ) {
169+ stopFinlexLimiterLogging ( ) ;
170+ exit ( 0 ) ;
87171 }
88- const totalDuration = Date . now ( ) - setupStartTime ;
89- const minutes = Math . floor ( totalDuration / 60000 ) ;
90- const seconds = Math . floor ( ( totalDuration % 60000 ) / 1000 ) ;
91- console . log ( `[SETUP] Database setup completed in ${ minutes } m ${ seconds } s (${ totalDuration } ms)` ) ;
92172 } catch ( error ) {
93- const errorDuration = Date . now ( ) - setupStartTime ;
173+ console . error ( '[SETUP] Setup failed:' , error ) ;
94174 Sentry . captureException ( error ) ;
95- console . error ( `[SETUP] Setup failed after ${ errorDuration } ms:` , error ) ;
96- throw error ;
97- } finally {
98- stopFinlexLimiterLogging ( ) ;
175+ exit ( 1 ) ;
99176 }
100- }
177+ } ;
0 commit comments