-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathclean-ke-snapshots.js
105 lines (89 loc) · 3.31 KB
/
clean-ke-snapshots.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pick from 'lodash/pick.js';
import { knex } from '../../db/knex-database-connection.js';
import { Script } from '../../src/shared/application/scripts/script.js';
import { ScriptRunner } from '../../src/shared/application/scripts/script-runner.js';
const DEFAULT_CHUNK_SIZE = 10000;
const DEFAULT_PAUSE_DURATION = 2000;
const pause = async (duration) => {
return new Promise((resolve) => {
setTimeout(resolve, duration);
});
};
function getKnowlegdeElementSnapshotsQuery() {
return knex('knowledge-element-snapshots').whereRaw("(snapshot->0->>'answerId') is not null");
}
function getKnowlegdeElementSnapshotLimit(firstId, limit = DEFAULT_CHUNK_SIZE) {
return getKnowlegdeElementSnapshotsQuery()
.select('id', 'snapshot')
.limit(limit)
.where('id', '>=', firstId)
.orderBy('id', 'asc');
}
function getKnowlegdeElementSnapshotCount() {
return getKnowlegdeElementSnapshotsQuery().count({ count: 1 }).first();
}
UPDATE products
SET details = jsonb_set(details, '{specs,storage}', '"1TB SSD"')
WHERE name = 'Laptop';
// Définition du script
export class CleanKeSnapshotScript extends Script {
constructor() {
super({
description: 'This script will remove unused properties from the column knowledge-element-snapshots.snapshot',
permanent: false,
options: {
chunkSize: {
type: 'number',
default: DEFAULT_CHUNK_SIZE,
description: 'number of records to update in one update',
},
pauseDuration: {
type: 'number',
default: DEFAULT_PAUSE_DURATION,
description: 'Time in ms between each chunk processing',
},
},
});
}
async handle({
options = { chunkSize: DEFAULT_CHUNK_SIZE, pauseDuration: DEFAULT_PAUSE_DURATION },
logger,
dependencies = { pause },
}) {
const logInfo = (message) => logger.info({ event: 'CleanKeSnapshotScript' }, message);
const snapshotToClean = await getKnowlegdeElementSnapshotCount();
const nbChunk = Math.ceil(snapshotToClean.count / (options.chunkSize || DEFAULT_CHUNK_SIZE));
logInfo(`Start cleaning ${snapshotToClean.count} (${nbChunk} batch) knowledge-element-snapshots to clean.`);
let snapshots = await getKnowlegdeElementSnapshotLimit(0, options.chunkSize);
let chunkDone = 0;
while (snapshots.length > 0) {
await knex.transaction(async (trx) => {
for (const { id, snapshot } of snapshots) {
const cleanedSnapshot = pick(snapshot, [
'source',
'status',
'skillId',
'createdAt',
'earnedPix',
'competenceId',
]);
await trx('knowledge-element-snapshots')
.where('id', id)
.update({
// we keep only these property from snapshot
snapshot: JSON.stringify(cleanedSnapshot),
});
}
});
const lastSnapshotRow = snapshots[snapshots.length - 1];
snapshots = await getKnowlegdeElementSnapshotLimit(lastSnapshotRow.id + 1, options.chunkSize);
if (snapshots.length > 0 && options.pauseDuration > 0) {
await dependencies.pause(options.pauseDuration);
}
chunkDone += 1;
logInfo(`${chunkDone}/${nbChunk} chunks done !`);
}
}
}
// Exécution du script
await ScriptRunner.execute(import.meta.url, CleanKeSnapshotScript);