Skip to content

Commit

Permalink
code cleanup with the right comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shriramshankar committed Aug 16, 2017
1 parent d9f1547 commit 5a43560
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
15 changes: 10 additions & 5 deletions realtime/redisPublisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const rtUtils = require('./utils');
const pub = require('../cache/redisCache').client.pub;
const channelName = require('../config').redis.channelName;
const sampleEvent = require('./constants').events.sample;
const featureToggles = require('feature-toggles');

/**
* When passed an sample object, either a sequelize sample object or
Expand Down Expand Up @@ -89,10 +90,11 @@ function publishObject(inst, event, changedKeys, ignoreAttributes) {
} // publishChange

/**
* [publishPartialSample description]
* @param {[type]} sampleInst [description]
* @param {[type]} event [description]
* @return {[type]} [description]
* Publishes the sample without attaching the related subject and the aspect to
* the redis channel
* @param {Object} sampleInst - The sampel instance to be published
* @param {String} event - The event type that is being published.
* @returns {Object} - the sample object
*/
function publishPartialSample(sampleInst, event) {
const eventType = event || getSampleEventType(sampleInst);
Expand All @@ -116,7 +118,10 @@ function publishPartialSample(sampleInst, event) {
*/
function publishSample(sampleInst, subjectModel, event, aspectModel) {
const eventType = event || getSampleEventType(sampleInst);
return rtUtils.attachAspectSubject(sampleInst, subjectModel, aspectModel)
const useSampleStore =
featureToggles.isFeatureEnabled('enableRedisSampleStore');
return rtUtils.attachAspectSubject(sampleInst, useSampleStore, subjectModel,
aspectModel)
.then((sample) => {
publishObject(sample, eventType);
return sample;
Expand Down
6 changes: 4 additions & 2 deletions realtime/redisSubscriber.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ module.exports = (io) => {
const parsedObj = rtUtils.parseObject(mssgObj[key], key);
if (featureToggles.isFeatureEnabled('publishPartialSample') &&
rtUtils.isThisSample(parsedObj)) {
const useSampleStore =
featureToggles.isFeatureEnabled('enableRedisSampleStore');
/*
* assign the subjectModel to the database model if sampleStore is
* not enabled
*/
const subjectModel =
featureToggles.isFeatureEnabled('enableRedisSampleStore') ? undefined :
useSampleStore ? undefined :
require('../db/index').Subject; // eslint-disable-line global-require
rtUtils.attachAspectSubject(parsedObj, subjectModel)
rtUtils.attachAspectSubject(parsedObj, useSampleStore, subjectModel)
.then((obj) => {
console.log('object to be emitted this is a complete sample', obj);
/*
Expand Down
19 changes: 13 additions & 6 deletions realtime/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const ip = require('ip');
const constants = require('./constants');
const redisClient = require('../cache/redisCache').client.sampleStore;
const redisStore = require('../cache/sampleStore');
const featureToggles = require('feature-toggles');

const eventName = {
add: 'refocus.internal.realtime.subject.add',
Expand Down Expand Up @@ -315,16 +314,24 @@ function isIpWhitelisted(addr, whitelist) {
} // isIpWhitelisted

/**
* [attachAspectSubject description]
* @param {[type]} sampInst [description]
* @return {[type]} [description]
* When passed in a sample, its related subject and aspect is attached to the
* sample. If useSampleStore is set to true, the subject ans aspect is fetched
* for the cache instead of the database.
* @param {Object} sample - The sample instance.
* @param {Boolen} useSampleStore - The sample store flag, the subject and the
* aspect is fetched from the cache if this is set.
* @param {Model} subjectModel - The database subject model.
* @param {Model} aspectModel - The database aspect model.
* @returns {Promise} - which resolves to a complete sample with its subject and
* aspect.
*/
function attachAspectSubject(sample, subjectModel, aspectModel) {
function attachAspectSubject(sample, useSampleStore, subjectModel,
aspectModel) {
const nameParts = sample.name.split('|');
const subName = nameParts[0];
const aspName = nameParts[1];
let promiseArr = [];
if (featureToggles.isFeatureEnabled('enableRedisSampleStore')) {
if (useSampleStore) {
const subKey = redisStore.toKey('subject', subName);
const aspKey = redisStore.toKey('aspect', aspName);
const getAspectPromise = sample.aspect ? Promise.resolve(sample.aspect) :
Expand Down

0 comments on commit 5a43560

Please sign in to comment.