-
Notifications
You must be signed in to change notification settings - Fork 0
/
rehydrate-entities-worker.js
161 lines (155 loc) · 5.56 KB
/
rehydrate-entities-worker.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
160
161
const Goblin = require('xcraft-core-goblin');
const T = require('goblin-nabu');
const workshopConfig = require('xcraft-core-etc')().load('goblin-workshop');
const entityStorage = workshopConfig.entityStorageProvider.replace(
'goblin-',
''
);
exports.xcraftCommands = function () {
return Goblin.buildQueueWorker('rehydrate-entities', {
workQuest: function* (quest, desktopId, userDesktopId, data, next) {
const desktop = quest.getAPI(userDesktopId).noThrow();
const r = quest.getStorage(entityStorage);
const tables = data.selectedTables.join(', ');
const tablesNumber = data.selectedTables.length;
yield desktop.addNotification({
notificationId: `notification@${quest.uuidV4()}`,
glyph: 'solid/download',
color: 'blue',
message: T(
`Recupération des entités {length, plural, one {de la table {tables}} other {des tables: {tables}s}}`,
null,
{
length: tablesNumber,
tables,
}
),
});
const statuses = ['Published', 'Draft', 'Archived']
.filter((status) => !!data[`status${status}`])
.map((status) => status.toLocaleLowerCase());
const promises = [];
for (const table of data.selectedTables) {
const getInfo = (r, table, statuses) => {
let q = r.table(table).getAll(r.args(statuses), {index: 'status'});
return q
.pluck('id', {
meta: ['rootAggregateId', 'rootAggregatePath', 'type'],
})
.map(function (doc) {
return {
id: doc('id'),
root: doc('meta')('rootAggregateId'),
path: doc('meta')('rootAggregatePath'),
type: doc('meta')('type'),
};
});
};
const query = getInfo.toString();
const args = [table, statuses];
promises.push(r.query({query, args}));
}
const forRehydrate = yield Promise.all(promises);
const hydrateClassifier = forRehydrate.reduce(
(state, entities) => {
const roots = entities.filter((entity) => entity.path.length === 0);
const leefs = entities.filter((entity) => entity.path.length > 0);
state[0] = state[0].concat(roots);
leefs.reduce((state, leef) => {
const lvl = leef.path.length;
if (!state[lvl]) {
state[lvl] = [];
}
state[lvl].push(leef);
return state;
}, state);
return state;
},
{0: []}
);
let totalLength = 0;
const orderedHydratation = Object.keys(hydrateClassifier).reduce(
(order, index) => {
const entities = hydrateClassifier[index];
totalLength += entities.length;
order.push(entities);
return order;
},
[]
);
const reverseHydratation = orderedHydratation.reverse();
let count = 1;
const batchSize = 100;
const progressNotificationId = `notification@${quest.uuidV4()}`;
yield desktop.addNotification({
notificationId: `notification@${quest.uuidV4()}`,
glyph: 'solid/play',
color: 'blue',
message: T(
`Début de l'hydratation {length, plural, one {de la table {tables}} other {des tables: {tables}s}}`,
null,
{
length: tablesNumber,
tables,
}
),
});
for (const [key, entities] of reverseHydratation.entries()) {
// const current = key + 1;
// const total = reverseHydratation.length;
if (entities.length > 0) {
for (const entity of entities) {
const requestId = quest.uuidV4();
quest.evt('<hydrate-entity-requested>', {
desktopId: quest.getDesktop(),
requestId,
entityId: entity.id,
rootAggregateId: entity.root,
rootAggregatePath: entity.path,
muteChanged: true,
muteHydrated: data.emitHydrated === false,
notify: false,
force: true,
options: {
rebuildValueCache: data.mustRebuild === true,
buildSummaries: data.mustBuildSummaries === true,
buildViews: data.mustBuildViews === true,
buildAlerts: data.mustBuildAlerts === true,
buildProps: data.mustBuildProps === true,
compute: data.mustCompute === true,
index: data.mustIndex === true,
},
});
if (count % batchSize === 0 || totalLength < batchSize) {
const progress = (count / totalLength) * 100;
yield quest.sub.wait(`*::*.${requestId}-hydrate.done`);
yield desktop.addNotification({
notificationId: progressNotificationId,
glyph: 'solid/leaf',
color: 'blue',
//- message: `(${current}/${total}) ${progress.toFixed(0)} %`,
current: progress,
total: 100,
});
}
count++;
}
}
}
yield desktop.addNotification({
notificationId: progressNotificationId,
glyph: 'solid/beer',
//- color: 'blue',
//- message: T(
//- `100 % {length, plural, one {de la table hydratée} other {des tables hydratées}}`,
//- null,
//- {
//- length: tablesNumber,
//- }
//- ),
color: 'green',
message: T('Réhydratation terminée'),
});
},
});
};