-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathpopulate-campaign-participation-id-in-knowledge-element-snapshot.js
159 lines (142 loc) · 5.81 KB
/
populate-campaign-participation-id-in-knowledge-element-snapshot.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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 getEmptyParticipationKnowlegdeElementSnapshotIds(firstId, size = DEFAULT_CHUNK_SIZE) {
return knex('knowledge-element-snapshots')
.whereNull('campaignParticipationId')
.where('id', '>=', firstId)
.orderBy('id', 'asc')
.pluck('id')
.limit(size);
}
// Définition du script
export class PopulateCampaignParticipationIdScript extends Script {
constructor() {
super({
description:
'This script will populate the column knowledge-element-snapshots.campaignParticipationId with campaign-participations.id',
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, logger, dependencies = { pause } }) {
const result = await knex('knowledge-element-snapshots').count().whereNull('campaignParticipationId').first();
if (result.count === 0) {
logger.info(
{ event: 'PopulateCampaignParticipationIdScript' },
`There is no knowledge-element-snapshot with missing campaignParticipationId. Job done !`,
);
return;
} else {
logger.info(
{ event: 'PopulateCampaignParticipationIdScript' },
`Try to populate ${result.count} missing campaignParticipationId`,
);
}
let ids = await getEmptyParticipationKnowlegdeElementSnapshotIds(0, options.chunkSize);
let totalUddatedRows = 0;
while (ids.length > 0) {
const updatedRows = await knex('knowledge-element-snapshots')
.whereNull('campaignParticipationId')
.updateFrom('campaign-participations')
.update({
campaignParticipationId: knex.ref('campaign-participations.id'),
})
.where('knowledge-element-snapshots.snappedAt', knex.ref('campaign-participations.sharedAt'))
.where('knowledge-element-snapshots.userId', knex.ref('campaign-participations.userId'))
.whereIn('knowledge-element-snapshots.id', ids);
totalUddatedRows += updatedRows;
logger.info(
{ event: 'PopulateCampaignParticipationIdScript' },
`loop: ${updatedRows} rows updated from "knwoledge-element-snapshots"`,
);
ids = await getEmptyParticipationKnowlegdeElementSnapshotIds(ids[ids.length - 1] + 1, options.chunkSize);
if (ids.length > 0 && options.pauseDuration > 0) {
await dependencies.pause(options.pauseDuration);
}
}
logger.info(
{ event: 'PopulateCampaignParticipationIdScript' },
`${totalUddatedRows} rows updated from "knowledge-element-snapshots"`,
);
let emptyRowResult = await knex('knowledge-element-snapshots').count().whereNull('campaignParticipationId').first();
if (emptyRowResult.count === 0) {
logger.info(
{ event: 'PopulateCampaignParticipationIdScript' },
`No row with empty campaignParticipationId to update. Job done !`,
);
return;
} else {
logger.info(`${emptyRowResult.count} rows with empty campaignParticipationId to update`);
}
const anonymisedParticipations = await knex('campaign-participations')
.select(['campaign-participations.id', 'sharedAt'])
.join('campaigns', function () {
this.on('campaigns.id', 'campaign-participations.campaignId');
})
.whereNull('userId')
.whereNotNull('sharedAt');
const emptyKeSnapshotParticipations = await knex('knowledge-element-snapshots')
.whereNull('campaignParticipationId')
.select(['id', 'snappedAt']);
logger.info(
{ event: 'PopulateCampaignParticipationIdScript' },
`Try to populate ${emptyKeSnapshotParticipations.length} keSnapshot from ${anonymisedParticipations.length} anonymised participations`,
);
const matchingSnapshotAndParticipations = emptyKeSnapshotParticipations.flatMap(({ id, snappedAt }) => {
const participations = anonymisedParticipations.filter(
({ sharedAt }) => snappedAt.toISOString() === sharedAt.toISOString(),
);
if (participations.length !== 1) {
return [];
}
return [{ keSnapshotId: id, campaignParticipationId: participations[0].id }];
});
await knex.transaction(async (trx) => {
for (const row of matchingSnapshotAndParticipations) {
await trx('knowledge-element-snapshots')
.update({ campaignParticipationId: row.campaignParticipationId })
.where({ id: row.keSnapshotId });
}
});
logger.info(
{ event: 'PopulateCampaignParticipationIdScript' },
`Populate ${matchingSnapshotAndParticipations.length} anonymised participations`,
);
emptyRowResult = await knex('knowledge-element-snapshots')
.select('id')
.whereNull('campaignParticipationId')
.pluck('id');
if (emptyRowResult.length === 0) {
logger.info(
{ event: 'PopulateCampaignParticipationIdScript' },
`No row with empty campaignParticipationId to update. Job done !`,
);
} else {
logger.info(
{ event: 'PopulateCampaignParticipationIdScript', ids: emptyRowResult },
`${emptyRowResult.length} rows with empty campaignParticipationId to update`,
);
}
}
}
// Exécution du script
await ScriptRunner.execute(import.meta.url, PopulateCampaignParticipationIdScript);