Skip to content

Commit

Permalink
feat(sync): add dump surveys config for debugging (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
navarroaxel authored Oct 12, 2018
1 parent b1d251b commit b8d0374
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module.exports = {
| :bangbang: | `AUTH_CLIENT_SECRET` | The secret to validate the JWT. | |
| | `SURVEYS_COLLECTION` | The surveys collection name on MongoDB | `surveyAddresses` |
| | `SURVEYS_HISTORY` | Keeps a history of survey changes | `true` |
| | `SURVEYS_DUMP` | Dumps every request to a collection | `false` |
| | `MORGAN_FORMAT` | Log format used by Morgan package | `dev` on `NODE_ENV=development`, `combined` on `NODE_ENV=production` |
| | `DEBUG` | Set to `sivy` to turn on debug logging. | |

Expand Down
25 changes: 19 additions & 6 deletions src/controllers/sync.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const debug = require('debug')('sivy');
const {map} = require('lodash');
const {map, size, isEmpty} = require('lodash');

const {SurveyAddress, surveyAddressState, SyncLog} = require('../model');
const syncHandlers = require('../helpers/syncHandlers');
const {SyncService} = require('../services');

const setSurvey = async (survey, syncLog, user) => {
if (survey.closed) {
syncLog.closed++;
}
if (survey.visits && survey.visits.length) {
if (!isEmpty(survey.visits)) {
syncLog.visited++;
}
if (!survey.synced) {
Expand Down Expand Up @@ -42,6 +44,17 @@ const getSurvey = async surveyAddress => {
};

class SyncController {
static async dumpSurveys(req, res, next) {
try {
debug(`Dumping ${size(req.body.surveys)} surveys from user ${req.user._id}...`);
await SyncService.dumpSurveys(req.user._id, req.body.surveys);
next();
} catch (err) {
debug(`An error dumping the surveys for user ${req.user._id} has occurred.`);
next(err);
}
}

static initSyncLog(req, res, next) {
debug(`Init a syncLog for user ${req.user._id}.`);
req.syncLog = new SyncLog({
Expand All @@ -59,12 +72,12 @@ class SyncController {
static async setSurveys(req, res, next) {
try {
const surveys = req.body.surveys;
const surveysCount = surveys ? surveys.length : 0;
const surveysCount = size(surveys);
debug(`Received ${surveysCount} surveys from user ${req.user._id}.`);
if (surveysCount === 0) {
return next();
}
req.syncLog.received = surveys.length;
req.syncLog.received = surveysCount;
await syncHandlers.receiveSurveys(surveys, req.syncLog);
await Promise.all(map(surveys, survey => setSurvey(survey, req.syncLog, req.user)));
debug(`Saved ${surveysCount} surveys from user ${req.user._id}.`);
Expand All @@ -82,8 +95,8 @@ class SyncController {
user: req.user._id,
surveyAddressState: {$lt: surveyAddressState.CLOSED}
}).populate('address').exec();
debug(`Sending ${surveyAddresses.length} surveys for user ${req.user._id}...`);
req.syncLog.sent = surveyAddresses.length;
req.syncLog.sent = size(surveyAddresses);
debug(`Sending ${req.syncLog.sent} surveys for user ${req.user._id}...`);
await syncHandlers.getSurveys(surveyAddresses, req.syncLog);
res.surveyAddresses = await Promise.all(map(surveyAddresses, getSurvey));
next();
Expand Down
1 change: 1 addition & 0 deletions src/helpers/dotenv.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ process.env.MONGODB_URI = process.env.MONGODB_URI || 'mongodb://localhost';
process.env.RECEIVE_ONLY = process.env.RECEIVE_ONLY || false;
process.env.SURVEYS_COLLECTION = process.env.SURVEYS_COLLECTION || 'surveyAddresses';
process.env.SURVEYS_HISTORY = process.env.SURVEYS_HISTORY || true;
process.env.SURVEYS_DUMP = process.env.SURVEYS_DUMP || false;

if (!process.env.MORGAN_FORMAT) {
process.env.MORGAN_FORMAT = process.env.NODE_ENV === 'development' ? 'dev' : 'combined';
Expand Down
1 change: 1 addition & 0 deletions src/model/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
exports.address = require('./address');
exports.surveyAddressState = require('./surveyAddressState');
exports.SurveyAddress = require('./surveyAddress');
exports.SurveyDump = require('./surveyDump');
exports.SyncLog = require('./syncLog');
9 changes: 9 additions & 0 deletions src/model/surveyDump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const {SURVEYS_COLLECTION} = process.env;
const mongoose = require('mongoose');

const {Mixed, ObjectId, Schema} = mongoose;

module.exports = mongoose.model('SurveyDump', new Schema({
user: {type: ObjectId, required: true, ref: 'User'},
surveys: {type: Mixed}
}, {collection: `${SURVEYS_COLLECTION}_dump`, strict: false, timestamps: true}));
36 changes: 20 additions & 16 deletions src/routes/sync.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {RECEIVE_ONLY} = process.env;
const {RECEIVE_ONLY, SURVEYS_DUMP} = process.env;

const logger = require('../helpers/logger');
const {SyncController} = require('../controllers');
Expand All @@ -7,22 +7,26 @@ module.exports = router => {
const receiveOnly = RECEIVE_ONLY === true.toString();
logger.info(`Sync mode: ${receiveOnly ? 'receive-only' : 'send+receive'}.`);

if (receiveOnly) {
router.post('/',
SyncController.initSyncLog,
SyncController.setSurveys,
SyncController.saveSyncLog,
(req, res) => res.end()
);
} else {
router.post('/',
SyncController.initSyncLog,
SyncController.setSurveys,
SyncController.getSurveys,
SyncController.saveSyncLog,
(req, res) => res.send({surveyAddresses: res.surveyAddresses})
);
const surveysDump = SURVEYS_DUMP === true.toString();
logger.info(`Surveys dump is turned ${surveysDump ? 'on' : 'off'}.`);

const middlewares = [];
if (surveysDump) {
middlewares.push(SyncController.dumpSurveys);
}
middlewares.push(SyncController.initSyncLog, SyncController.setSurveys);

if (!receiveOnly) {
middlewares.push(SyncController.getSurveys);
}
middlewares.push(SyncController.saveSyncLog);

middlewares.push(receiveOnly
? (req, res) => res.end()
: (req, res) => res.send({surveyAddresses: res.surveyAddresses})
);

router.post('/', middlewares);

return router;
};
1 change: 1 addition & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
exports.StatusService = require('./status');
exports.SyncService = require('./sync');
10 changes: 10 additions & 0 deletions src/services/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const {SurveyDump} = require('../model');

class SyncService {
static dumpSurveys(user, surveys) {
const surveyDump = new SurveyDump({user, surveys});
return surveyDump.save();
}
}

module.exports = SyncService;

0 comments on commit b8d0374

Please sign in to comment.