Skip to content

Commit

Permalink
Merge branch 'master' into profile-add-column-sql
Browse files Browse the repository at this point in the history
  • Loading branch information
shriramshankar committed Aug 14, 2017
2 parents c03bbc8 + 184b5c7 commit c479d68
Show file tree
Hide file tree
Showing 30 changed files with 151 additions and 211 deletions.
6 changes: 3 additions & 3 deletions api/v1/controllers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = {
authUtils.isAdmin(req)
.then((ok) => {
if (!ok) {
u.forbidden(next);
return u.forbidden(next);
}

const enabled = featureToggles
Expand All @@ -51,8 +51,8 @@ module.exports = {
}

return sampleStoreInit.eradicate()
.then(() => sampleStoreInit.populate())
.then(() => res.status(httpStatus.NO_CONTENT).json());
.then(() => sampleStoreInit.populate())
.then(() => res.status(httpStatus.NO_CONTENT).json());
})
.catch(() => u.forbidden(next));
},
Expand Down
80 changes: 60 additions & 20 deletions cache/sampleStoreInit.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const samsto = require('./sampleStore');
const log = require('winston');
const samstoPersist = require('./sampleStorePersist');
const constants = samsto.constants;
const infoLoggingEnabled =
featureToggles.isFeatureEnabled('enableSampleStoreInfoLogging');

/**
* Deletes the previousStatuskey (that stores the previous value of the
Expand Down Expand Up @@ -88,8 +90,13 @@ function eradicate() {
}));
return deletePreviousStatus()
.then(() => Promise.all(promises))
.then(() => log.info('Sample Store eradicated from cache :D'))
.then(() => true);
.then(() => {
if (infoLoggingEnabled) {
log.info('Sample Store eradicated from cache :D');
}

return true;
});
} // eradicate

/**
Expand All @@ -106,8 +113,11 @@ function populateAspects() {
},
})
.then((allAspects) => {
const msg = `Starting to load ${allAspects.length} aspects to cache :|`;
log.info(msg);
if (infoLoggingEnabled) {
const msg = `Starting to load ${allAspects.length} aspects to cache :|`;
log.info(msg);
}

aspects = allAspects;
const getWritersPromises = [];

Expand Down Expand Up @@ -135,8 +145,13 @@ function populateAspects() {

cmds.push(['sadd', constants.indexKey.aspect, aspectIdx]);
return redisClient.batch(cmds).execAsync()
.then(() => log.info('Done loading aspects to cache :D'))
.then(() => true);
.then(() => {
if (infoLoggingEnabled) {
log.info('Done loading aspects to cache :D');
}

return true;
});
})
.catch(console.error); // eslint-disable-line no-console
} // populateAspects
Expand All @@ -149,8 +164,11 @@ function populateAspects() {
function populateSubjects() {
return Subject.findAll({ where: { isPublished: true } })
.then((subjects) => {
const msg = `Starting to load ${subjects.length} subjects to cache :|`;
log.info(msg);
if (infoLoggingEnabled) {
const msg = `Starting to load ${subjects.length} subjects to cache :|`;
log.info(msg);
}

const cmds = [];
subjects.forEach((s) => {
const key = samsto.toKey(constants.objectType.subject, s.absolutePath);
Expand All @@ -163,8 +181,13 @@ function populateSubjects() {
});

return redisClient.batch(cmds).execAsync()
.then(() => log.info('Done loading subjects to cache :D'))
.then(() => true);
.then(() => {
if (infoLoggingEnabled) {
log.info('Done loading subjects to cache :D');
}

return true;
});
})
.catch(console.error); // eslint-disable-line no-console
} // populateSubjects
Expand All @@ -177,8 +200,11 @@ function populateSubjects() {
function populateSamples() {
return Sample.findAll()
.then((samples) => {
const msg = `Starting to load ${samples.length} samples to cache :|`;
log.info(msg);
if (infoLoggingEnabled) {
const msg = `Starting to load ${samples.length} samples to cache :|`;
log.info(msg);
}

const sampleIdx = new Set();
const subjectSets = {};
const sampleHashes = {};
Expand Down Expand Up @@ -227,8 +253,13 @@ function populateSamples() {

// Return once all batches have completed.
return Promise.all(batchPromises)
.then(() => log.info('Done loading samples to cache :D'))
.then(() => true);
.then(() => {
if (infoLoggingEnabled) {
log.info('Done loading samples to cache :D');
}

return true;
});
})
.catch(console.error); // eslint-disable-line no-console
} // populateSamples
Expand All @@ -240,8 +271,11 @@ function populateSamples() {
* false if the feature is not enabled.
*/
function populate() {
const msg = 'Populating redis sample store from db started :|';
log.info(msg);
if (infoLoggingEnabled) {
const msg = 'Populating redis sample store from db started :|';
log.info(msg);
}

let resp;
const promises = [populateSubjects(), populateAspects()];
return Promise.all(promises)
Expand Down Expand Up @@ -286,14 +320,20 @@ function storeSampleToCacheOrDb() {
* "enableRedisSampleStore" flag has been changed from true to false
*/
if (currentStatus) {
log.info('"enableRedisSampleStore" flag was switched to true,' +
' so populating the cache from db');
if (infoLoggingEnabled) {
log.info('"enableRedisSampleStore" flag was switched to true, so ' +
'populating the cache from db');
}

return populate();
}

log.info('"enableRedisSampleStore" flag was switched to false' +
' so persisting to db from cache. The cache will be eradicated ' +
if (infoLoggingEnabled) {
log.info('"enableRedisSampleStore" flag was switched to false so ' +
'so persisting to db from cache. The cache will be eradicated ' +
'after the samples are persisted to db');
}

return samstoPersist.storeSampleToDb() // populate the sample table
.then(() => eradicate()); // eradicate the cache
}
Expand Down
31 changes: 24 additions & 7 deletions cache/sampleStorePersist.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const redisClient = require('./redisCache').client.sampleStore;
const samsto = require('./sampleStore');
const constants = samsto.constants;
const log = require('winston');
const infoLoggingEnabled =
featureToggles.isFeatureEnabled('enableSampleStoreInfoLogging');

/**
* Truncate the sample table in the DB and persist all the samples from redis
Expand All @@ -26,18 +28,27 @@ const log = require('winston');
* @returns {Promise} - which resolves to number of samples persisted to db
*/
function storeSampleToDb() {
log.info('Persist to db started :|. This will start by truncating the ' +
'sample table followed by persisting the sample to db');
if (infoLoggingEnabled) {
log.info('Persist to db started :|. This will start by truncating the ' +
'sample table followed by persisting the sample to db');
}

return Sample.destroy({ truncate: true, force: true })
.then(() => {
log.info('truncated the sample table :|');
if (infoLoggingEnabled) {
log.info('truncated the sample table :|');
}

return redisClient.smembersAsync(constants.indexKey.sample);
})
.then((keys) => keys.map((key) => ['hgetall', key]))
.then((cmds) => redisClient.batch(cmds).execAsync())
.then((res) => {
log.info('Preparing list of samples to persist...');
log.info(`Checking ${res.length} samples...`);
if (infoLoggingEnabled) {
log.info('Preparing list of samples to persist...');
log.info(`Checking ${res.length} samples...`);
}

const samplesToCreate = res.map((sample) => {
sample.relatedLinks = JSON.parse(sample.relatedLinks);
return sample;
Expand All @@ -51,11 +62,17 @@ function storeSampleToDb() {

return true;
});
log.info(`Bulk creating ${samplesToCreate.length} samples...`);
if (infoLoggingEnabled) {
log.info(`Bulk creating ${samplesToCreate.length} samples...`);
}

return Sample.bulkCreate(samplesToCreate);
})
.then((retval) => {
log.info('persisted redis sample store to db :D');
if (infoLoggingEnabled) {
log.info('persisted redis sample store to db :D');
}

return retval.length;
});
} // storeSampleToDb
Expand Down
4 changes: 4 additions & 0 deletions config/toggles.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ const longTermToggles = {
// Enable Rooms functionality
enableRooms: environmentVariableTrue(pe, 'ENABLE_ROOMS'),

// Enable sample store info logging
enableSampleStoreInfoLogging: environmentVariableTrue(pe,
'ENABLE_SAMPLE_STORE_INFO_LOGGING'),

// Enable worker activity logging
enableWorkerActivityLogs:
environmentVariableTrue(pe, 'ENABLE_WORKER_ACTIVITY_LOGS'),
Expand Down
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const WORKERS = process.env.WEB_CONCURRENCY || DEFAULT_WEB_CONCURRENCY;
const sampleStore = require('./cache/sampleStoreInit');

/**
* Entry point for each newly clustered process
* Entry point for each clustered process.
*/
function start() { // eslint-disable-line max-statements
const featureToggles = require('feature-toggles');
Expand Down Expand Up @@ -65,11 +65,9 @@ function start() { // eslint-disable-line max-statements
});

/*
* Compress(gzip) all the api responses and all the static files.
* Since this is called before the static pages and the API routes, this will
* ensure that both the static pages and the API response are compressed.
* Call this *before* the static pages and the API routes so that both the
* static pages *and* the API responses are compressed (gzip).
*/

app.use(compress());

const httpServer = require('http').Server(app);
Expand Down Expand Up @@ -177,13 +175,15 @@ function start() { // eslint-disable-line max-statements
// Keep browsers from sniffing mimetypes
app.use(helmet.noSniff());

// Redirect '/' to '/v1'.
/*
* Redirect '/' to the application landing page, which right now is the
* default perspective (or the first perspective in alphabetical order if
* no perspective is defined as the default).
*/
app.get('/', (req, res) => res.redirect('/perspectives'));

// set json payload limit
app.use(bodyParser.json(
{ limit: conf.payloadLimit }
));
// Set the JSON payload limit.
app.use(bodyParser.json({ limit: conf.payloadLimit }));

/*
* Interpret Swagger resources and attach metadata to request - must be
Expand Down Expand Up @@ -236,6 +236,7 @@ function start() { // eslint-disable-line max-statements

// create app routes
require('./view/loadView')(app, passportModule, '/v1');

module.exports = { app, passportModule };
}

Expand Down
2 changes: 1 addition & 1 deletion tests/enforceToken/collector/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe(`api: POST ${path}`, () => {
const deRegisterPath = `/v1/collectors/${res.body.id}/deregister`;
return Collector.destroy({
where: {},
truncate: true
truncate: true,
})
.then(() => {
api.post(deRegisterPath)
Expand Down
18 changes: 3 additions & 15 deletions tests/enforceToken/createToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('api: createToken', () => {
.send(u.fakeUserCredentials)
.end((err, res) => {
if (err) {
done(err);
return done(err);
}

defaultToken = res.body.token;
Expand All @@ -40,13 +40,7 @@ describe('api: createToken', () => {
.send({ name: 'newToken' })
.expect(constants.httpStatus.FORBIDDEN)
.expect(/No authorization token was found/)
.end((err) => {
if (err) {
return done(err);
}

done();
});
.end(done);
});

it('error if wrong token found provided', (done) => {
Expand All @@ -55,13 +49,7 @@ describe('api: createToken', () => {
.send({ name: 'newToken' })
.expect(constants.httpStatus.FORBIDDEN)
.expect(/Invalid Token/)
.end((err) => {
if (err) {
return done(err);
}

done();
});
.end(done);
});

it('sucessful authentication, create token for user', (done) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/enforceToken/revoke.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('enforceToken: revoke:', () => {
.send(u.fakeUserCredentials)
.end((err, res) => {
if (err) {
done(err);
return done(err);
}

defaultToken = res.body.token;
Expand Down
17 changes: 2 additions & 15 deletions tests/enforceToken/sampleUpsertBulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
* tests/tokenReq/sampleUpsertBulk.js
*/
'use strict';

const expect = require('chai').expect;
const supertest = require('supertest');
const api = supertest(require('../../index').app);
Expand Down Expand Up @@ -81,13 +80,7 @@ describe('api: POST ' + path, () => {
])
.expect(constants.httpStatus.FORBIDDEN)
.expect(/ForbiddenError/)
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
.end(done);
});

it('all succeed', (done) => {
Expand All @@ -103,12 +96,6 @@ describe('api: POST ' + path, () => {
},
])
.expect(constants.httpStatus.OK)
.end((err /* , res */) => {
if (err) {
return done(err);
}

done();
});
.end(done);
});
});

0 comments on commit c479d68

Please sign in to comment.