From b9eb67bc419dfc932d8f5e7b25e8d36b5ede3451 Mon Sep 17 00:00:00 2001 From: Evans Girard <54366437+evans-g-crsj@users.noreply.github.com> Date: Fri, 14 Jun 2024 09:03:30 -0400 Subject: [PATCH] :wrench: Add new script and improve others (admin) (#154) --- admin/delClinicalIndicesBySuffix.mjs | 83 ++++++++++++++++++++++++++++ admin/findClinicalIndicesUsage.mjs | 21 ++++++- admin/run.mjs | 2 + 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 admin/delClinicalIndicesBySuffix.mjs diff --git a/admin/delClinicalIndicesBySuffix.mjs b/admin/delClinicalIndicesBySuffix.mjs new file mode 100644 index 0000000..6d85a65 --- /dev/null +++ b/admin/delClinicalIndicesBySuffix.mjs @@ -0,0 +1,83 @@ +//FIX: DUPLICATED CODE +/** + * node ... delClinicalIndicesBySuffix.mjs -- suffix:xyz + * + * */ +import assert from 'node:assert/strict'; +import readline from 'readline'; +import { Client } from '@elastic/elasticsearch'; +import { esHost } from '../dist/src/env.js'; +import { cbKeepClinicalIndicesOnly } from './utils.mjs'; + +const args = process.argv.slice(2); +const rawSuffix = args.find(a => a.startsWith('suffix:')) ?? ''; +const suffix = (rawSuffix.split(':')[1] || '').trim(); +assert(!!suffix, 'You must instruct the suffix of an index name. For instance, "suffix:sd_bhjxbdqk_20240611_1"'); + +const userReadline = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); + +const displayDoYouWantToProceed = () => + new Promise(resolve => { + userReadline.question(`You are about to delete indices. Do you want to proceed y/n? > `, answer => + resolve(answer === 'y'), + ); + }); + +const wannaProceed = await displayDoYouWantToProceed(); + +if (!wannaProceed) { + userReadline.close(); + process.exit(0); +} + +const client = new Client({ node: esHost }); + +const catIndicesResponse = await client.cat.indices({ + index: `*${suffix}`, + h: 'index', + format: 'json', +}); + +if (catIndicesResponse.statusCode !== 200) { + console.error('Received bad response', catIndicesResponse, ' Terminating.'); + process.exit(1); +} + +const clinicalIndices = catIndicesResponse.body.filter(cbKeepClinicalIndicesOnly).map(x => x.index); +assert(Array.isArray(clinicalIndices) && clinicalIndices.length > 0, 'No index found. Terminating'); + +const rAllAliases = await client.cat.aliases({ + h: 'alias,index', + format: 'json', +}); +assert(rAllAliases.statusCode === 200); + +const allAliases = rAllAliases.body; +const clinicalAliases = allAliases.filter(cbKeepClinicalIndicesOnly); + +const displayDoYouWantToDeleteIndices = () => + new Promise(resolve => { + console.log('found', clinicalIndices.length, 'indices.'); + console.log( + JSON.stringify( + clinicalIndices.sort().map(x => { + return clinicalAliases.some(a => a.index === x) ? ['Aliased', x] : x; + }), + null, + 2, + ), + ); + userReadline.question(`Do you want to delete those indices y/n? > `, answer => resolve(answer === 'y')); + }); + +const okDelete = await displayDoYouWantToDeleteIndices(); +if (okDelete) { + const deleteResponse = await client.indices.delete({ + index: clinicalIndices, + }); + console.log(deleteResponse.body); +} +userReadline.close(); diff --git a/admin/findClinicalIndicesUsage.mjs b/admin/findClinicalIndicesUsage.mjs index a9238e1..063ac2f 100644 --- a/admin/findClinicalIndicesUsage.mjs +++ b/admin/findClinicalIndicesUsage.mjs @@ -44,15 +44,32 @@ const unaliasedClinicalIndicesWithCreationDate = makeReleaseToCreationDate(clini const unaliasedReleases = unaliasedClinicalIndicesWithCreationDate.map(x => x.release); assert(clinicalAliases.every(x => !unaliasedReleases.includes(`re_${x.index.split('re_')[1]}`))); -console.log(`===== Not Aliased`); +console.log(`===== Not Aliased (pattern:re_*)`); unaliasedClinicalIndicesWithCreationDate.length ? console.table(unaliasedClinicalIndicesWithCreationDate) : console.log('None'); -console.log(`===== Aliased`); +console.log(`===== Aliased (pattern:re_*)`); const clinicalIndicesAliased = clinicalIndices.filter(x => clinicalAliases.some(a => a.index === x.index)); const aliasedClinicalIndicesWithCreationDate = makeReleaseToCreationDate(clinicalIndicesAliased); aliasedClinicalIndicesWithCreationDate.length ? console.table(aliasedClinicalIndicesWithCreationDate) : console.log('None'); + +console.log(`===== Others (Clinical)`); +const othersClinical = clinicalIndices + .filter(x => !x.index.includes('_re_')) + .sort((a, b) => b['creation.date'] - a['creation.date']) + .map(x => { + const release = x.index + .split('sd_')[1] + .split('_') + .slice(1) + .join('_'); + return [release, x['creation.date.string']]; + }) + .reduce((xs, x) => (xs.some(y => y[0] === x[0]) ? xs : [...xs, x]), []) + .map(x => ({ release: x[0], creation_date: x[1] })); + +othersClinical.length ? console.table(othersClinical) : console.log('None'); diff --git a/admin/run.mjs b/admin/run.mjs index 0e8767e..fbf0dd2 100644 --- a/admin/run.mjs +++ b/admin/run.mjs @@ -200,7 +200,9 @@ if (updateConditions.every(b => b)) { ); if (nOfDocsInProjectMetadata === projectConf.indices.length) { console.debug(`admin-project-script - Applying extended mapping mutations.`); + console.time("fixExtendedMapping") await ArrangerApi.fixExtendedMapping(client, projectConf.extendedMappingMutations); + console.timeEnd("fixExtendedMapping") } }