This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
maintenance.js
253 lines (242 loc) · 8.03 KB
/
maintenance.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env node
/** Maintenance script **/
const program = require('commander');
const util = require('util');
const request = require('request');
const config = require('./config');
const localconfig = require('./localconfig');
const { logger } = require('./units/logger');
const diff = require('./units/diff');
const {migrate, connection, Map, History} = require("./db/modelsB.js");
program
.version('3.0.0')
.option('-P, --regeneratepreviews', 'Regenerate preview for all maps (can take a long time)')
.option('-R, --regeneratepreview <mapId>', 'Regenerate preview for single map by id')
.option('-T, --testdiff <mapId>', 'Test diff between histories on specified map.id')
.option('-D, --processdiff <mapId>', 'Process diff between on specified map.id')
.option('-E, --checkerrors', 'Check errors in histories table')
.option('-H, --stathistory', 'Read all histories and populate stats map accordingly')
.parse(process.argv);
function regenerateMaps (maps) {
if (maps.length) {
let record = maps.shift();
logger.info("Updating preview for id %d - %s", record.id, record.title);
request({
url: util.format(config.screenshot.hostPattern, localconfig.screenshotServerPort),
method: "PUT",
headers: {
'Accept': 'application/json'
},
json: {mapargs: record.mapargs}
}, async function (error, response, jsonBody) {
if (!error && typeof jsonBody !== "undefined" && typeof jsonBody.path === "string") {
// console.log(jsonBody);
record.screenshot = jsonBody.path;
// logger.debug(record.screenshot);
// logger.debug(record.screenshot);
// update record with the new screenshot
record.save().then(() => {
logger.info("Updated")
regenerateMaps(maps);
})
}
else {
logger.error("Cannot regenerate %d - %s", record.id, record.title)
regenerateMaps(maps);
}
});
}
else {
logger.info("maps previews updated");
// exit without errors
process.exit(0);
}
}
function programProcessdiff(mapId, finalCallback) {
/**
* Read database History for the specified mapId, overwrite postProcess
* with new data, dropping old postProcess.
*
* @param {integer} mapId: map.id of History to regenerate
* @param {callback} finalCallback: callback to call at the very end
* @return nothing, alter records on database
**/
let limit = 2;
let offset = 0;
// specify map.id by command line
let historyWhere = {
mapId: mapId
// diff: true // check even not different elements, maybe wrong with new code
};
function writeDiff() {
logger.info(`limit ${limit}; offset ${offset}`);
History.findAll({
where: historyWhere,
include: [{
model: Map,
where: {
published: true
}
}
],
order: [
['createdAt', 'ASC']
],
limit: limit,
offset: offset
}).then( async hists => {
// must compare 2 elements, not less (last element)
if (hists.length === limit) {
let beforeRecord = hists.shift();
let afterRecord = hists.shift();
// hydrate, remove postProcess, and compare
let before = diff.removePostProcess(JSON.parse(beforeRecord.json));
let after = diff.removePostProcess(JSON.parse(afterRecord.json));
// first record of the set? different by default (to display on map)
afterRecord.diff = diff.isStrictDifferent (before, after);
// change History record
// write new after.data[n].features.postProcess, dehydrate and
// overwrite old record
// set to false to DEBUG cleaning up all postProcess
if (true) {
await diff.postProcess(before, after);
afterRecord.json = JSON.stringify(after);
}
else {
afterRecord.json = JSON.stringify(after);
}
// eg. to compare A, B, C will be compared:
// A vs. B;
// B vs. C (shifting 1 element)
offset += 1;
// next couple by query
afterRecord.save().then(() => {
if (offset > 0) {
// next couple to compare
writeDiff();
}
else {
// first record of History for a single map.id MUST
// 1) be CLEAN (without data[n].features.postProcess)
// 2) marked as different to appear ALWAYS on map
// first record MUST be clean
beforeRecord.json = JSON.stringify(before);
beforeRecord.diff = 1;
beforeRecord.save().then(() => {
// next couple to compare
writeDiff();
});
}
});
}
else {
// quit
finalCallback();
}
});
}
// starts here
writeDiff();
}
if (program.regeneratepreviews) {
// Used for landing page, 3 elements per load, offset passed by url
Map.findAll({
where: {
published: true
}
}).then(maps => {
if (maps) {
// for (record of maps) {
regenerateMaps(maps);
}
else {
logger.info("No maps found on database, cannot update");
}
});
}
if (program.regeneratepreview) {
Map.findOne({
where: {
id: program.regeneratepreview
}
}).then(map => {
if (map) {
regenerateMaps([map]);
}
else {
logger.info("No maps found on database, cannot update");
}
});
}
if (program.testdiff) {
// specify map.id by command line
var historyWhere = {mapId: parseInt(program.testdiff)};
historyWhere['diff'] = true;
History.findAll({
where: historyWhere,
include: [{
model: Map,
where: {
published: true
}
}
],
order: [
['createdAt', 'ASC']
],
// offset: ???,
}).then(hists => {
let elements = [];
for (h of hists) {
// remove our metadata before compare
elements.push(diff.removePostProcess(JSON.parse(h.json)));
}
diff.processDeepDiff(elements, function (results) {
for (r of Object.values(results)) {
logger.info('----------------------------------------------------');
logger.info(JSON.stringify(r, null, 2));
}
process.exit(0);
});
});
}
if (program.processdiff) {
logger.info("TODO: process all records for the provided map and regenerate postProcess field");
let mapId = parseInt(program.processdiff);
programProcessdiff(
mapId,
function () {
// all ok
logger.info("Diff successfully written on History for Map with map.id = %d", mapId);
process.exit(0);
}
);
}
if (program.checkerrors) {
(async () => {
logger.info("Checking errors in histories table");
const hists = await History.findAll({
attributes: ['id']
});
for (const hist of hists) {
const history = await History.findOne({
where: {
id: hist.id
}
});
const jsonValue = JSON.parse(history.json);
if (jsonValue.error) {
logger.error("Error found with id " + hist.id);
history.error = true;
await history.save();
}
}
})();
}
if (program.stathistory) {
(async () => {
const { saveStat } = require('./units/stats');
logger.info("Calc stats for histories table");
await saveStat();
})();
}