|
| 1 | +#!/usr/bin/env node |
| 2 | +/* |
| 3 | + Compare search results between two Finlex services. |
| 4 | + Usage: |
| 5 | + node scripts/compareSearch.js --q tupakka --language fin |
| 6 | + node scripts/compareSearch.js --q tupakka --language fin --prod https://... --staging https://... |
| 7 | + node scripts/compareSearch.js --file words.txt --language fin |
| 8 | +*/ |
| 9 | + |
| 10 | +import axios from 'axios'; |
| 11 | +import { readFileSync } from 'fs'; |
| 12 | + |
| 13 | +function parseArgs() { |
| 14 | + const args = process.argv.slice(2); |
| 15 | + const out = { q: null, file: null, language: 'fin', prod: 'https://finlex.ext.ocp-prod-0.k8s.it.helsinki.fi', staging: 'https://finlex-lukija-ohtuprojekti-staging.ext.ocp-prod-0.k8s.it.helsinki.fi' }; |
| 16 | + for (let i = 0; i < args.length; i++) { |
| 17 | + const a = args[i]; |
| 18 | + if (a === '--q') out.q = args[++i]; |
| 19 | + else if (a === '--file') out.file = args[++i]; |
| 20 | + else if (a === '--language') out.language = args[++i]; |
| 21 | + else if (a === '--prod') out.prod = args[++i]; |
| 22 | + else if (a === '--staging') out.staging = args[++i]; |
| 23 | + } |
| 24 | + if (!out.q && !out.file) { |
| 25 | + console.error('Missing --q query parameter or --file path'); |
| 26 | + process.exit(1); |
| 27 | + } |
| 28 | + return out; |
| 29 | +} |
| 30 | + |
| 31 | +function buildUrl(base, q, language) { |
| 32 | + const params = new URLSearchParams({ q, language }); |
| 33 | + return `${base}/api/statute/search?${params.toString()}`; |
| 34 | +} |
| 35 | + |
| 36 | +function asArray(data) { |
| 37 | + if (Array.isArray(data)) return data; |
| 38 | + if (data && Array.isArray(data.content)) return data.content; // Finlex API wraps in {type, content} |
| 39 | + if (data && Array.isArray(data.results)) return data.results; // fallback |
| 40 | + return []; |
| 41 | +} |
| 42 | + |
| 43 | +function indexById(list) { |
| 44 | + const map = new Map(); |
| 45 | + for (const item of list) { |
| 46 | + // API returns docYear, docNumber, docTitle (not year, number, title) |
| 47 | + const key = item.id || `${item.docYear || ''}:${item.docNumber || ''}:${item.docTitle || ''}`; |
| 48 | + map.set(key, item); |
| 49 | + } |
| 50 | + return map; |
| 51 | +} |
| 52 | + |
| 53 | +function diffLists(aList, bList) { |
| 54 | + const aMap = indexById(aList); |
| 55 | + const bMap = indexById(bList); |
| 56 | + const onlyA = []; |
| 57 | + const onlyB = []; |
| 58 | + |
| 59 | + for (const [id, item] of aMap.entries()) { |
| 60 | + if (!bMap.has(id)) onlyA.push(item); |
| 61 | + } |
| 62 | + for (const [id, item] of bMap.entries()) { |
| 63 | + if (!aMap.has(id)) onlyB.push(item); |
| 64 | + } |
| 65 | + return { onlyA, onlyB }; |
| 66 | +} |
| 67 | + |
| 68 | +async function compareQuery(q, language, prod, staging) { |
| 69 | + const prodUrl = buildUrl(prod, q, language); |
| 70 | + const stagingUrl = buildUrl(staging, q, language); |
| 71 | + |
| 72 | + try { |
| 73 | + const [prodResp, stagingResp] = await Promise.all([ |
| 74 | + axios.get(prodUrl, { headers: { Accept: 'application/json' } }), |
| 75 | + axios.get(stagingUrl, { headers: { Accept: 'application/json' } }), |
| 76 | + ]); |
| 77 | + |
| 78 | + const prodList = asArray(prodResp.data); |
| 79 | + const stagingList = asArray(stagingResp.data); |
| 80 | + |
| 81 | + const { onlyA: onlyProd, onlyB: onlyStaging } = diffLists(prodList, stagingList); |
| 82 | + |
| 83 | + return { |
| 84 | + q, |
| 85 | + prodCount: prodList.length, |
| 86 | + stagingCount: stagingList.length, |
| 87 | + onlyProd, |
| 88 | + onlyStaging, |
| 89 | + match: onlyProd.length === 0 && onlyStaging.length === 0 |
| 90 | + }; |
| 91 | + } catch (err) { |
| 92 | + return { |
| 93 | + q, |
| 94 | + error: err.response ? `${err.response.status} ${err.config?.url}` : err.message |
| 95 | + }; |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +async function main() { |
| 100 | + const { q, file, language, prod, staging } = parseArgs(); |
| 101 | + |
| 102 | + let queries = []; |
| 103 | + if (file) { |
| 104 | + const content = readFileSync(file, 'utf-8'); |
| 105 | + queries = content.split('\n').map(line => line.trim()).filter(line => line && !line.startsWith('#')); |
| 106 | + } else { |
| 107 | + queries = [q]; |
| 108 | + } |
| 109 | + |
| 110 | + console.log(`Testing ${queries.length} queries against prod and staging (language=${language})\n`); |
| 111 | + |
| 112 | + const results = []; |
| 113 | + for (const query of queries) { |
| 114 | + process.stdout.write(`Testing "${query}"... `); |
| 115 | + const result = await compareQuery(query, language, prod, staging); |
| 116 | + results.push(result); |
| 117 | + |
| 118 | + if (result.error) { |
| 119 | + console.log(`ERROR: ${result.error}`); |
| 120 | + } else if (result.match) { |
| 121 | + console.log(`✓ Match (prod: ${result.prodCount}, staging: ${result.stagingCount})`); |
| 122 | + } else { |
| 123 | + console.log(`✗ Diff (prod: ${result.prodCount}, staging: ${result.stagingCount}, only-prod: ${result.onlyProd.length}, only-staging: ${result.onlyStaging.length})`); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + console.log('\n=== Summary ==='); |
| 128 | + const matches = results.filter(r => !r.error && r.match).length; |
| 129 | + const diffs = results.filter(r => !r.error && !r.match).length; |
| 130 | + const errors = results.filter(r => r.error).length; |
| 131 | + console.log(`Total: ${results.length}, Matches: ${matches}, Diffs: ${diffs}, Errors: ${errors}`); |
| 132 | + |
| 133 | + const problemResults = results.filter(r => r.error || !r.match); |
| 134 | + if (problemResults.length > 0) { |
| 135 | + console.log('\n=== Details for non-matching queries ==='); |
| 136 | + for (const result of problemResults) { |
| 137 | + if (result.error) { |
| 138 | + console.log(`\n"${result.q}": ERROR - ${result.error}`); |
| 139 | + } else { |
| 140 | + console.log(`\n"${result.q}": prod=${result.prodCount}, staging=${result.stagingCount}`); |
| 141 | + if (result.onlyProd.length > 0) { |
| 142 | + console.log(` Only in prod (${result.onlyProd.length}):`, result.onlyProd.map(i => `${i.docYear}/${i.docNumber}`).join(', ')); |
| 143 | + } |
| 144 | + if (result.onlyStaging.length > 0) { |
| 145 | + console.log(` Only in staging (${result.onlyStaging.length}):`, result.onlyStaging.map(i => `${i.docYear}/${i.docNumber}`).join(', ')); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +main(); |
0 commit comments