-
Notifications
You must be signed in to change notification settings - Fork 2
/
loadInventoryBNCF.js
161 lines (145 loc) · 4.89 KB
/
loadInventoryBNCF.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
/*
This script will stream instance, holdings, or item json objects from large collections.
options:
-s start record
-r root node (an array of folio records)
-m method (post|put) defaults to post
Logs are committed to postgres database
*/
const fs = require('fs');
const superagent = require('superagent');
const winston = require('winston');
const PostgreSQL = require('./lib/winston-cl-pg');
const JSONStream = require('JSONStream');
const es = require('event-stream');
const argv = require('minimist')(process.argv.slice(2));
const { getAuthToken } = require('./lib/login');
let inFile = argv._[0];
let root = (argv.r) ? argv.r + '.*' : '*';
let method = (argv.m && argv.m.match(/put/i)) ? 'put' : 'post';
let startRec = 0;
if (argv.s) {
startRec = parseInt(argv.s, 10);
}
const wait = (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
(async () => {
try {
const start = new Date().valueOf();
let inData;
if (!inFile) {
throw new Error('Usage: node loadInstances.js [options -s start, -r root, -m method] <file>');
} else if (!fs.existsSync(inFile)) {
throw new Error('Can\'t find input file');
}
const config = (fs.existsSync('./config.js')) ? require('./config.js') : require('./config.default.js');
var logger;
const lpath = config.logpath;
const lname = inFile.replace(/.+\//, '');
if (config.logpath && config.logpath.match(/postgres:/)) {
logger = winston.createLogger({
defaultMeta: { filename: inFile },
transports: [
new PostgreSQL({
connectionString: config.logpath,
tableName: 'winston_logs',
})]
});
} else 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;
}
logger.info('Logging in');
const authToken = await getAuthToken(superagent, config.okapi, config.tenant, config.authpath, config.username, config.password);
let updated = 0;
let success = 0;
let fail = 0;
let count = startRec;
let failedRecs = [];
const runRequest = async (data, es) => {
let date = new Date().toISOString();
let endpoint = null;
if (data.instanceId) {
endpoint = '/holdings-storage/holdings';
} else if (data.holdingsRecordId) {
endpoint = '/item-storage/items';
} else if (data.succeedingInstanceId || data.precedingInstanceId) {
endpoint = '/preceding-succeeding-titles';
} else {
endpoint = '/instance-storage/instances';
}
const actionUrl = config.okapi + endpoint;
console.log(`# ${count} Loading ${data.id}`);
method = (data.hrid && data.hrid.match(/\S/)) ? 'put' : 'post';
count++;
try {
if (method === 'put') {
await superagent
.put(actionUrl + '/' + data.id)
.send(data)
.set('x-okapi-token', authToken)
.set('content-type', 'application/json')
.set('accept', 'text/plain')
.set('connection', 'keep-alive');
logger.info(`${date} [${count}] Successfully updated record ${data.id}`);
} else {
await superagent
.post(actionUrl)
.send(data)
.set('x-okapi-token', authToken)
.set('content-type', 'application/json')
.set('accept', 'application/json')
.set('connection', 'keep-alive');
logger.info(`${date} [${count}] Successfully added record ${data.id}`);
}
success++;
} catch (e) {
logger.error(`${date} [${count}] (${data.id}): ${e.response.text}`);
failedRecs.push(data);
fail++;
}
await wait(config.delay);
es.resume();
}
const stream = fs.createReadStream(inFile, { encoding: "utf8" });
let streamCount = 0;
stream
.pipe(JSONStream.parse(root))
.pipe(es.through(function write(data) {
if (streamCount >= startRec) {
runRequest(data, this);
this.pause();
}
streamCount++;
},
function end() {
showStats();
this.emit('end')
})
);
const showStats = () => {
const end = new Date().valueOf();
const ms = end - start;
const time = Math.floor(ms / 1000);
logger.info(`\nTime: ${time} sec`);
logger.info(`Records updated: ${updated}`);
logger.info(`Records added: ${success}`);
logger.info(`Failures: ${fail}\n`);
if (config.logpath && failedRecs[0]) {
let rfname = lname.replace(/\.json$/, '');
fs.writeFileSync(`${lpath}/${rfname}_err.json`, JSON.stringify(failedRecs, null, 2));
}
}
} catch (e) {
console.error(e);
}
})();