Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redis changes #1349

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ dump.rdb

# Ignore .DS_Store files created by Mac finder
.DS_Store

tmp
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
force=true
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:8-alpine
FROM node:21-alpine

ENV HOME=/home/refocus
RUN adduser -D -h $HOME refocus
Expand Down
1 change: 1 addition & 0 deletions api/v1/controllers/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
rebuildSampleStore(req, res, next) {
console.log('\n\n rebuildSampleStore ==>>>');
const resultObj = { reqStartTime: req.timestamp };
if (!req.headers.IsAdmin) {
return u.forbidden(next);
Expand Down
9 changes: 9 additions & 0 deletions api/v1/controllers/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,37 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
authenticateUser(req, res, next) {
console.log('authenticateUser', req);
const resultObj = { reqStartTime: req.timestamp };
configuredPassport.authenticate('local-login', (err, user/* , info */) => {
console.log('\n\n\n local login', user);
console.log('local login error', err);
if (err) {
return u.handleError(next, err, resourceName);
}

debugger
if (!user || !user.name) {
const loginErr = new apiErrors.LoginError({
explanation: 'Invalid credentials',
});
return u.handleError(next, loginErr, resourceName);
}

console.log('Before req logIn, req:', user);
req.logIn(user, (_err) => {
console.log('\n\n\n\n req logIn inside', user);
resultObj.dbTime = new Date() - resultObj.reqStartTime;
if (_err) {
return u.handleError(next, _err, resourceName);
}

console.log('After req.logIn, req.user:', req.user);

return user.setLastLogin()
.then(() => Profile.isAdmin(user.profileId))
.then((isAdmin) => { // update in token payload if admin
console.log('\n\n\n i am here in user setLastLogin &&&$$$$$$$$$');
const payloadObj = {
ProfileName: user.profile.name,
IsAdmin: isAdmin,
Expand Down
3 changes: 1 addition & 2 deletions api/v1/controllers/bots.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ module.exports = {
const resultObj = { reqStartTime: req.timestamp };
const url = req.url;

// try to get cached entry
redisCache.get(url, (cacheErr, reply) => {
return redisCache.get(url, (cacheErr, reply) => {
if (reply) {
// reply is responsified bot object as string.
const botObject = JSON.parse(reply);
Expand Down
81 changes: 41 additions & 40 deletions api/v1/controllers/lenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,49 +282,50 @@ module.exports = {
const url = req.url;

// try to get cached entry
redisCache.get(url, (cacheErr, reply) => {
if (reply) {
// reply is responsified lens object as string.
const lensObject = JSON.parse(reply);
u.removeFieldsFromResponse(helper.fieldsToExclude, lensObject);

// add api links to the object and return response.
lensObject.apiLinks = u.getApiLinks(lensObject.id, helper, req.method);
res.status(httpStatus.OK).json(lensObject);
} else {
// if cache error, print error and continue to get lens from db.
if (cacheErr) {
logger.error('api/v1/controllers/lenses.getLens|', cacheErr);
}

// no reply, go to db to get lens object.
let extraAttributes;
if (!req.swagger.params.fields.value) {
extraAttributes = ['lensLibrary'];
}

u.findByKey(helper, req.swagger.params, extraAttributes)
.then((o) => {
resultObj.dbTime = new Date() - resultObj.reqStartTime;
if (o.isPublished === false) {
const eStr = 'Lens is not published. Please contact Refocus admin.';
throw new apiErrors.ResourceNotFoundError({
explanation: eStr,
});
return redisCache.get(url).then((cacheErr, reply) =>{
console.log('reply', reply);
if (reply) {
// reply is responsified lens object as string.
const lensObject = JSON.parse(reply);
u.removeFieldsFromResponse(helper.fieldsToExclude, lensObject);

// add api links to the object and return response.
lensObject.apiLinks = u.getApiLinks(lensObject.id, helper, req.method);
res.status(httpStatus.OK).json(lensObject);
} else {
// if cache error, print error and continue to get lens from db.
if (cacheErr) {
logger.error('api/v1/controllers/lenses.getLens|', cacheErr);
}

return responsify(o, helper, req.method);
})
.then((responseObj) => {
u.logAPI(req, resultObj, responseObj);
res.status(httpStatus.OK).json(responseObj);
// no reply, go to db to get lens object.
let extraAttributes;
if (!req.swagger.params.fields.value) {
extraAttributes = ['lensLibrary'];
}

// cache the lens by id and name.
redisCache.set(url, JSON.stringify(responseObj));
})
.catch((err) => u.handleError(next, err, helper.modelName));
}
});
u.findByKey(helper, req.swagger.params, extraAttributes)
.then((o) => {
resultObj.dbTime = new Date() - resultObj.reqStartTime;
if (o.isPublished === false) {
const eStr = 'Lens is not published. Please contact Refocus admin.';
throw new apiErrors.ResourceNotFoundError({
explanation: eStr,
});
}

return responsify(o, helper, req.method);
})
.then((responseObj) => {
u.logAPI(req, resultObj, responseObj);
res.status(httpStatus.OK).json(responseObj);

// cache the lens by id and name.
redisCache.set(url, JSON.stringify(responseObj));
})
.catch((err) => u.handleError(next, err, helper.modelName));
}
})
},

/**
Expand Down
3 changes: 1 addition & 2 deletions api/v1/controllers/samples.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function doFindSample(req, res, next, resultObj, cacheKey, cacheExpiry) {
const strObj = JSON.stringify(response);
redisCache.setex(cacheKey, cacheExpiry, strObj);
}

u.logAPI(req, resultObj, response); // audit log
res.status(httpStatus.OK).json(response);
})
Expand Down Expand Up @@ -174,7 +173,7 @@ module.exports = {
// Check if Sample Store is on or not
const resultObj = { reqStartTime: req.timestamp }; // for logging
if (helper.cacheEnabled) {
redisCache.get(helper.cacheKey, (cacheErr, reply) => {
return redisCache.get(helper.cacheKey, (cacheErr, reply) => {
if (cacheErr || !reply) {
doFindSample(req, res, next, resultObj, helper.cacheKey,
helper.cacheExpiry);
Expand Down
1 change: 1 addition & 0 deletions api/v1/controllers/subjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ module.exports = {
* @param {Function} next - The next middleware function in the stack
*/
getSubjectHierarchy(req, res, next) {
console.log('getSubjectHierarchy');
const params = req.swagger.params;
const filterParams = ['subjectTags', 'aspectTags', 'aspect', 'status'];

Expand Down
6 changes: 3 additions & 3 deletions api/v1/helpers/verbs/doDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const u = require('./utils');
const publisher = u.publisher;
const event = u.realtimeEvents;
const redisModelSample = require('../../../../cache/models/samples');
const redisCache = require('../../../../cache/redisCache').client.cache;
const redisClient = require('../../../../cache/redisCache').client.cache;
const tracker = require('../../../../realtime/kafkaTracking');

/**
Expand Down Expand Up @@ -80,8 +80,8 @@ function doDelete(req, res, next, props) {
if (props.cacheEnabled) {
const getCacheKey = req.swagger.params.key.value;
const findCacheKey = '{"where":{}}';
redisCache.del(getCacheKey);
redisCache.del(findCacheKey);
redisClient.del(getCacheKey);
redisClient.del(findCacheKey);
}

// when a resource is deleted, delete all its associations too
Expand Down
10 changes: 5 additions & 5 deletions api/v1/helpers/verbs/doFind.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const redisCache = require('../../../../cache/redisCache').client.cache;
* @param {Object} opts - The "options" object to pass into the Sequelize
* find command
*/
function doFindAndCountAll(reqResNext, props, opts, resultObj) {
function doFindAndCountAll(reqResNext, props, opts, resultObj, redisCache) {
return u.getScopedModel(props, opts.attributes)
.findAndCountAll(opts)
.then((o) => {
Expand Down Expand Up @@ -59,15 +59,15 @@ function doFindAndCountAll(reqResNext, props, opts, resultObj) {
* @param {Object} opts - The "options" object to pass into the Sequelize
* find command
*/
function doFindResponse(reqResNext, props, opts, resultObj) {
function doFindResponse(reqResNext, props, opts, resultObj, redisCache) {
if (opts.limit || opts.offset) {
reqResNext.res.links({
prev: reqResNext.req.originalUrl,
next: fu.getNextUrl(reqResNext.req.originalUrl, opts.limit, opts.offset),
});
}

return doFindAndCountAll(reqResNext, props, opts, resultObj)
return doFindAndCountAll(reqResNext, props, opts, resultObj, redisCache)
.then((retVal) => {
u.sortArrayObjectsByField(props, retVal);

Expand Down Expand Up @@ -108,7 +108,7 @@ module.exports = function doFind(req, res, next, props) {
redisCache.get(props.cacheKey, (cacheErr, reply) => {
if (cacheErr || !reply) {
// if err or no reply, get resuls from db and set redis cache
return doFindResponse({ req, res, next }, props, opts, resultObj);
return doFindResponse({ req, res, next }, props, opts, resultObj, redisCache);
}

// get from cache
Expand All @@ -122,6 +122,6 @@ module.exports = function doFind(req, res, next, props) {
}
});
} else {
return doFindResponse({ req, res, next }, props, opts, resultObj);
return doFindResponse({ req, res, next }, props, opts, resultObj, redisCache);
}
}; // exports
48 changes: 24 additions & 24 deletions api/v1/helpers/verbs/doGet.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,32 @@ function doGet(req, res, next, props) {
// only cache requests with no params
if (props.cacheEnabled && !fields) {
const cacheKey = reqParams.key.value;
return redisCache.getAsync(cacheKey)
.then((reply) => {
// get from cache
if (reply) {
res.locals.resultObj.dbTime = new Date() -
res.locals.resultObj.reqStartTime;
res.locals.retVal = u.responsify(JSON.parse(reply), props, req.method);
return Promise.resolve(true);
}
return redisCache.get(cacheKey)
.then((reply) => {
// get from cache
if (reply) {
res.locals.resultObj.dbTime = new Date() -
res.locals.resultObj.reqStartTime;
res.locals.retVal = u.responsify(JSON.parse(reply), props, req.method);
return Promise.resolve(true);
}

throw new Error('no reply');
})
/* if err or no reply, get from db and set redis cache */
.catch((cacheErr) => u.findByKey(props, req.swagger.params, scopes)
.then((o) => {
res.locals.resultObj.dbTime = new Date() -
res.locals.resultObj.reqStartTime;
res.locals.retVal = u.responsify(o, props, req.method);
throw new Error('no reply');
})
/* if err or no reply, get from db and set redis cache */
.catch((cacheErr) => u.findByKey(props, req.swagger.params, scopes)
.then((o) => {
res.locals.resultObj.dbTime = new Date() -
res.locals.resultObj.reqStartTime;
res.locals.retVal = u.responsify(o, props, req.method);

// cache the object by cacheKey. Store the key-value pair in cache
// with an expiry of 1 minute (60s)
const strObj = JSON.stringify(o);
redisCache.setex(cacheKey, cacheExpiry, strObj);
return true;
}))
.catch((err) => u.handleError(next, err, props.modelName));
// cache the object by cacheKey. Store the key-value pair in cache
// with an expiry of 1 minute (60s)
const strObj = JSON.stringify(o);
redisCache.setex(cacheKey, cacheExpiry, strObj);
return true;
}))
.catch((err) => u.handleError(next, err, props.modelName));
} else {
let getPromise;
if (props.modelName === 'Sample') {
Expand Down
24 changes: 12 additions & 12 deletions api/v1/helpers/verbs/heartbeatUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ const NOCHANGE = 0;
*/
function getChangedIds(collectorName) {
return redisClient.multi()
.smembers(getKey(collectorName, ADDED))
.smembers(getKey(collectorName, DELETED))
.smembers(getKey(collectorName, UPDATED))
.execAsync()
.then((replies) => ({
added: replies[0],
deleted: replies[1],
updated: replies[2],
}));
.sMembers(getKey(collectorName, ADDED))
.sMembers(getKey(collectorName, DELETED))
.sMembers(getKey(collectorName, UPDATED))
.exec()
.then((replies) => ({
added: replies[0],
deleted: replies[1],
updated: replies[2],
}));
}

/**
Expand All @@ -48,7 +48,7 @@ function getChangedIds(collectorName) {
function removeFromSet(collectorName, change, genId) {
if (change === ADDED || change === DELETED || change === UPDATED) {
const key = getKey(collectorName, change);
return redisClient.sremAsync(key, genId);
return redisClient.sRem(key, genId);
} else {
return Promise.resolve();
}
Expand All @@ -65,7 +65,7 @@ function removeFromSet(collectorName, change, genId) {
function addToSet(collectorName, change, genId) {
if (change === ADDED || change === DELETED || change === UPDATED) {
const key = getKey(collectorName, change);
return redisClient.saddAsync(key, genId);
return redisClient.sAdd(key, genId);
} else {
return Promise.resolve();
}
Expand All @@ -81,7 +81,7 @@ function resetChanges(collectorName) {
const addedKey = getKey(collectorName, ADDED);
const deletedKey = getKey(collectorName, DELETED);
const updatedKey = getKey(collectorName, UPDATED);
return redisClient.delAsync(addedKey, deletedKey, updatedKey);
return redisClient.del(addedKey, deletedKey, updatedKey);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions api/v1/helpers/verbs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const common = require('../../../../utils/common');
const logAPI = require('../../../../utils/apiLog').logAPI;
const publisher = require('../../../../realtime/redisPublisher');
const realtimeEvents = require('../../../../realtime/constants').events;
const redisCache = require('../../../../cache/redisCache').client.cache;
const redisClient = require('../../../../cache/redisCache').client.cache;
const userProps = require('../nouns/users');
const Op = require('sequelize').Op;
const md5 = require('md5');
Expand Down Expand Up @@ -113,8 +113,8 @@ function handleUpdatePromise(resultObj, req, retVal, props, res) {
if (props.cacheEnabled) {
const getCacheKey = req.swagger.params.key.value;
const findCacheKey = '{"where":{}}';
redisCache.del(getCacheKey);
redisCache.del(findCacheKey);
redisClient.del(getCacheKey);
redisClient.del(findCacheKey);
}

resultObj.dbTime = new Date() - resultObj.reqStartTime;
Expand Down
11 changes: 11 additions & 0 deletions cache/Command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

function Command (command, args, callback, call_on_write) {
this.command = command;
this.args = args;
this.buffer_args = false;
this.callback = callback;
this.call_on_write = call_on_write;
}

module.exports = Command;
Loading