Skip to content

Commit

Permalink
🔧 Add new script and improve others (admin) (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
evans-g-crsj authored Jun 14, 2024
1 parent 2f4ae8e commit b9eb67b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
83 changes: 83 additions & 0 deletions admin/delClinicalIndicesBySuffix.mjs
Original file line number Diff line number Diff line change
@@ -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();
21 changes: 19 additions & 2 deletions admin/findClinicalIndicesUsage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
2 changes: 2 additions & 0 deletions admin/run.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down

0 comments on commit b9eb67b

Please sign in to comment.