-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloadInventory.js
265 lines (247 loc) · 8.26 KB
/
loadInventory.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
254
255
256
257
258
259
260
261
262
263
264
265
/*
This script will stream instance, holdings, or item json objects from large collections.
options:
-s start record
-b batch size
-u upsert (true or false)
*/
const fs = require('fs');
const superagent = require('superagent');
const winston = require('winston');
const readline = require('readline');
const argv = require('minimist')(process.argv.slice(2));
const { getAuthToken } = require('./lib/login');
let inFile = argv._[0];
let startRec = 1;
let upsert = '';
if (argv.s) {
startRec = parseInt(argv.s, 10);
console.log(`Starting at ${startRec}`);
}
if (argv.u === 'true') {
upsert = '?upsert=true';
}
let collSize = 1000;
if (argv.b) {
collSize = parseInt(argv.b, 10);
}
const wait = (ms) => {
console.log(`(Waiting ${ms}ms...)`);
return new Promise((resolve) => setTimeout(resolve, ms));
};
(async () => {
try {
const start = new Date().valueOf();
let inData;
if (!inFile) {
throw new Error('Usage: node loadInventory.js [options -s start, -b batch size (default 1000) -u upsert (true|false)] <file>');
} else if (!fs.existsSync(inFile)) {
throw new Error('Can\'t find input file');
}
let config = await getAuthToken(superagent, true);
var logger;
const lpath = config.logpath;
const lname = inFile.replace(/.+\//, '');
if (config.logpath) {
logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [
new winston.transports.File({ filename: `${lpath}/${lname}.log` })
]
});
} else {
logger = console;
}
const userId = (config.res && config.res.user) ? config.res.user.id : '';
const now = new Date().toISOString();
const metadata = {
createdDate: now,
updatedDate: now,
};
if (userId) {
metadata.createdByUserId = userId,
metadata.updatedByUserId = userId
}
let success = 0;
let fail = 0;
const runRequest = (data, count, end) => {
return new Promise(async (resolve, reject) => {
let lDate = new Date();
if (config.expiry && config.expiry <= lDate.valueOf()) {
config = await getAuthToken(superagent, true);
}
let date = lDate.toISOString();
let endpoint = null;
let root = null;
if (data.holdingsRecords) {
endpoint = '/holdings-storage/batch/synchronous' + upsert;
root = 'holdingsRecords'
} else if (data.items) {
endpoint = '/item-storage/batch/synchronous' + upsert;
root = 'items';
} else {
endpoint = '/instance-storage/batch/synchronous' + upsert;
root = 'instances';
}
const actionUrl = config.okapi + endpoint;
const range = `${count}-${end}`;
const slice = `${data[root][0].id} - ${data[root][data[root].length - 1].id}`;
console.log(`# ${range} Loading section ${slice}`);
const ldata = { [root]: data[root] };
try {
await superagent
.post(actionUrl)
.send(data)
.set('x-okapi-token', config.token)
.set('content-type', 'application/json')
.set('accept', 'text/plain')
.set('connection', 'keep-alive');
logger.info(`${date} [${range}] Successfully added record ${slice}`);
success++;
if (config.delay) await wait(config.delay);
resolve();
} catch (e) {
logger.error(`${date} [${range}] (${slice}): ${e.response.text}`);
if (lpath) {
let rfname = lname.replace(/\.json$/, '');
let efile = `${lpath}/${rfname}_${range}_err.jsonl`;
if (fs.existsSync(efile)) fs.unlinkSync(efile);
for (let x = 0; x < ldata[root].length; x++) {
let erec = ldata[root][x];
fs.writeFileSync(`${lpath}/${rfname}_${range}_err.jsonl`, JSON.stringify(erec) + '\n', { flag: 'a'});
}
}
fail++;
if (config.delay) await wait(config.delay);
reject(e.response.text);
}
});
}
const fileStream = fs.createReadStream(inFile, { encoding: "utf8" });
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity
});
let ttl = 0
let endRec = 0;
let startNum = startRec;
let coll = {};
for await (const line of rl) {
ttl++;
let json = JSON.parse(line);
if (json.__) delete json.__;
if (json.jobExecutionId || json.snapshotId) {
let ep = (json.jobExecutionId) ? 'source-storage/snapshots' : 'source-storage/records';
let xid = (json.jobExecutionId) ? json.jobExecutionId : json.id;
let url = `${config.okapi}/${ep}`;
console.log(`POST ${url}`);
try {
await superagent
.post(url)
.send(json)
.set('x-okapi-token', config.token)
.set('content-type', 'application/json')
.set('accept', 'text/plain')
.set('connection', 'keep-alive');
console.log(`INFO Successfully loaded ${xid}`);
collSize = 1;
success++;
} catch (e) {
console.log(e);
fail++;
}
} else if (ttl >= startRec) {
endRec++;
if (!json.metadata) {
json.metadata = metadata;
}
if (json.holdingsRecordId) {
if (!coll.items) coll.items = [];
coll.items.push(json);
} else if (json.instanceId) {
if (!coll.holdingsRecords) coll.holdingsRecords = [];
coll.holdingsRecords.push(json);
} else {
if (!coll.instances) coll.instances = [];
// lets do some last minute fixing of records...
if (!json._version) json._version = 1;
if (json.contributors) {
for (let i = 0; i < json.contributors.length; i++) {
if (!json.contributors[i].name || !json.contributors[i].contributorNameTypeId) {
json.contributors.splice(i, 1);
i--;
} else if (json.contributors[i].authorityId) {
delete json.contributors[i].authorityId;
}
}
}
if (json.subjects) {
for (let i = 0; i < json.subjects.length; i++) {
if (json.subjects[i].authorityId) {
delete json.subjects[i].authorityId;
}
}
}
if (json.alternativeTitles) {
for (let i = 0; i < json.alternativeTitles.length; i++) {
if (json.alternativeTitles[i].authorityId) {
delete json.alternativeTitles[i].authorityId;
}
}
}
if (json.series) {
for (let i = 0; i < json.series.length; i++) {
if (json.series[i].authorityId) {
delete json.series[i].authorityId;
}
}
}
if (json.classifications) {
for (let i = 0; i < json.classifications.length; i++) {
if (!json.classifications[i].classificationNumber || !json.classifications[i].classificationTypeId) {
json.classifications.splice(i, 1);
i--;
}
};
}
if (json.identifiers) {
for (let i = 0; i < json.identifiers.length; i++) {
if (!json.identifiers[i].value || !json.identifiers[i].identifierTypeId) {
json.identifiers.splice(i, 1);
i--;
}
};
}
coll.instances.push(json);
}
if (endRec%collSize === 0) {
try {
await runRequest(coll, startNum, ttl);
} catch (e) {
}
startNum = ttl + 1;
coll = {};
}
}
}
if (Object.keys(coll).length > 0) {
try {
await runRequest(coll, startNum, ttl);
} catch (e) {
}
}
const showStats = () => {
const end = new Date().valueOf();
const ms = end - start;
const time = Math.floor(ms / 1000);
logger.info(`\nTime: ${time} sec`);
logger.info(`Batches added: ${success} (${collSize} recs per batch)`);
logger.info(`Failures: ${fail}\n`);
}
showStats();
} catch (e) {
console.error(e.message);
}
})();