Skip to content

Instantly share code, notes, and snippets.

@dfranciosi
Last active October 3, 2017 16:19
Show Gist options
  • Save dfranciosi/a0f3e1c13f74d54a0014b3cf8e89a742 to your computer and use it in GitHub Desktop.
Save dfranciosi/a0f3e1c13f74d54a0014b3cf8e89a742 to your computer and use it in GitHub Desktop.
const pipelineState = require('state').pipeline;
const request = require('superagent');
const config = require('config');
const log = require('log');
const _ = require('lodash');
const async = require('async');
const CHUNK_SIZE = 100;
const INTERVAL = 100;
module.exports = (done) => {
const managementSystem = pipelineState.get('managementSystem');
const parsedRegistry = pipelineState.get('registry.parsed');
console.log('[STEP] saving registry length:', parsedRegistry.length);
const status = ['In Service', 'Not in service'];
if (!managementSystem) return done(Error('Management System not defined'));
if (!parsedRegistry) {
log.warning('[STEP][NMS] Parsed registry not found, will use the old one later.');
return done();
}
const id = managementSystem.managementSystemId;
//const registryURL = `${config.policyManagerBaseURL}/networkManagementSystems/${id}/registries`;
const registryURL = `http://0.0.0.0:3000/networkManagementSystems/${id}/registries`;
const registryId = pipelineState.get('registryId') || 0;
const payload = {
data: parsedRegistry.map((el) => {
return {
registryId: el.registryId || registryId,
networkElementTypeName: el.networkElementTypeName,
networkElementName: el.networkElementName,
networkElementActivity: el.networkElementActivity,
networkElementStatus: status[el.networkElementStatus],
backupStatus: el.backupStatus || 'RUNNING',
backupDate: el.backupDate || null,
statusMessage: el.statusMessage || null,
filePath: el.filePath || null,
filename: el.filename || null,
backupSize: el.backupSize || null,
schedulingId: el.schedulingId || null,
};
}),
}
console.log('[STEP] save registry URL:', registryURL);
console.log('PAYLOAD items count', payload.data.length);
function saveChunk(chunk, cb) {
setTimeout(() => {
request
.put(registryURL)
.send({data: chunk})
.end((err, res) => {
if (err) return cb(err);
console.log('SAVE RESPONSE', res.body);
const it = res.body.shift();
const registryId = it['REGISTRY_ID'];
pipelineState.set('registryId', registryId);
return cb(null, registryId);
});
}, INTERVAL);
}
const firstElement = payload.data.shift();
saveChunk([firstElement], (err, registryId) => {
if (err) return done(err);
const chunkedPayload = _.chunk(payload.data.map((it) => Object.assign(it, { registryId })), CHUNK_SIZE);
console.log('Chunk Size: ', chunkedPayload.length);
async.eachSeries(chunkedPayload, saveChunk, (err, res) => {
if (err) {
log.error(`[STEP][NMS][UPDATE REGISTRY] Error while saving registry to backend. Err: ${err.message}`);
return done(err);
}
log.info('[STEP][NMS][UPDATE REGISTRY] Registry updated.');
return done();
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment