Skip to content

Commit

Permalink
Merge 4b280f4 into 2eee49a
Browse files Browse the repository at this point in the history
  • Loading branch information
pallavi2209 committed Oct 30, 2017
2 parents 2eee49a + 4b280f4 commit 71b320f
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 2,290 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"homepage": "https://github.com/salesforce/refocus-collector#readme",
"dependencies": {
"@salesforce/refocus-collector-eval": "^1.2.0",
"bluebird": "^3.5.0",
"buffered-queue": "^0.1.4",
"coveralls": "^2.13.1",
Expand Down
33 changes: 0 additions & 33 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,6 @@ errors.create({
parent: errors.CollectorError,
});

errors.create({
name: 'TransformError',
defaultExplanation: 'The sample generator template provided a "transform" ' +
'body which does not return an array of samples.',
defaultResponse: 'Fix the sample generator template. The "transform" ' +
'function must return an array of zero or more samples.',
status: 400,
parent: errors.FunctionBodyError,
});

errors.create({
name: 'ToUrlError',
defaultExplanation: 'The sample generator template provided a "toUrl" ' +
'body which does not return a string.',
defaultResponse: 'Fix the sample generator template. The "toUrl" ' +
'function must return a string.',
status: 400,
parent: errors.FunctionBodyError,
});

errors.create({
name: 'ArgsError',
defaultExplanation: 'Invalid args supplied to the "transform" or "toUrl" ' +
'function.',
defaultResponse: 'Transform args must contain a "ctx" attribute of type ' +
'object, a "res" attribute of type object, and either a "subject" ' +
'attribute (object) or a "subjects" attribute (array). ToUrl args must ' +
'contain a "ctx" attribute of type object, and either a "subject" ' +
'attribute (object) or a "subjects" attribute (array).',
status: 400,
parent: errors.FunctionBodyError,
});

errors.create({
name: 'TemplateVariableSubstitutionError',
defaultExplanation: 'Invalid template for variable substitution.',
Expand Down
6 changes: 2 additions & 4 deletions src/remoteCollection/collect.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@

const debug = require('debug')('refocus-collector:remoteCollection');
const request = require('superagent');
const logger = require('winston');
const evalUtils = require('../utils/evalUtils');
const urlUtils = require('./urlUtils');
const errors = require('../errors');
const configModule = require('../config/config');
const constants = require('../constants');
const RefocusCollectorEval = require('@salesforce/refocus-collector-eval');

/**
* Prepares url of the remote datasource either by expanding the url or by
Expand All @@ -42,7 +40,7 @@ function prepareUrl(generator) {
subjects: generator.subjects,
};
const fbody = Array.isArray(toUrl) ? toUrl.join('\n') : toUrl;
url = evalUtils.safeToUrl(fbody, args);
url = RefocusCollectorEval.safeToUrl(fbody, args);
} else {
throw new errors.ValidationError('The generator template must provide ' +
'either a connection.url attribute or a "toUrl" attribute.');
Expand Down
33 changes: 28 additions & 5 deletions src/remoteCollection/handleCollectResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
* src/remoteCollection/handleCollectResponse.js
*/
const debug = require('debug')('refocus-collector:handleCollectResponse');
const evalUtils = require('../utils/evalUtils');
const errors = require('../errors');
const errorSamples = require('./errorSamples');
const logger = require('winston');
const queueUtils = require('../utils/queueUtils');
const httpStatus = require('../constants').httpStatus;
const bulkUpsertSampleQueue = require('../constants').bulkUpsertSampleQueue;
const commonUtils = require('../utils/commonUtils');
const RefocusCollectorEval = require('@salesforce/refocus-collector-eval');

/**
* Validates the response from the collect function. Confirms that it is an
Expand Down Expand Up @@ -62,9 +62,31 @@ function validateCollectResponse(cr) {
throw new errors.ValidationError(`Invalid response from ${cr.preparedUrl}: `
+ `invalid HTTP status code "${cr.res.statusCode}"`);
}

} // validateCollectResponse

/**
* Prepare arguments to be passed to the transform function
* @param {Object} generator - Generator object
* @throws {TransformError} - if transform function does not return an array
* of zero or more samples
* @throws {ValidationError} - if any of the above mentioned check fails
*/
function prepareTransformArgs(generator) {
const args = {};

args.ctx = generator.ctx;
args.res = generator.res;
args.aspects = generator.aspects;

if (commonUtils.isBulk(generator)) {
args.subjects = generator.subjects;
} else {
args.subject = generator.subjects[0];
}

return args;
}

/**
* Handles the response from the remote data source by calling the transform
* function. It also calls the sample bulk upsert api to send the data to the
Expand All @@ -89,9 +111,9 @@ function handleCollectResponse(collectResponse) {
* status codes.
*/
const tr = collectRes.generatorTemplate.transform;
const args = evalUtils.prepareTransformArgs(collectRes);
const args = prepareTransformArgs(collectRes);
if (typeof tr === 'string') { // match all status codes
const samplesToEnqueue = evalUtils.safeTransform(tr, args);
const samplesToEnqueue = RefocusCollectorEval.safeTransform(tr, args);
logger.info(`{
generator: ${collectRes.name},
url: ${collectRes.preparedUrl},
Expand Down Expand Up @@ -127,7 +149,7 @@ function handleCollectResponse(collectResponse) {
}

if (func) {
const samplesToEnqueue = evalUtils.safeTransform(func, args);
const samplesToEnqueue = RefocusCollectorEval.safeTransform(func, args);
logger.info(`{
generator: ${collectRes.name},
url: ${collectRes.preparedUrl},
Expand Down Expand Up @@ -165,4 +187,5 @@ function handleCollectResponse(collectResponse) {
module.exports = {
handleCollectResponse,
validateCollectResponse, // export for testing only
prepareTransformArgs, // export for testing only
};

0 comments on commit 71b320f

Please sign in to comment.