Skip to content

Instantly share code, notes, and snippets.

@swvitaliy
Created February 8, 2019 08:48
Show Gist options
  • Save swvitaliy/d9489dfc44f2c4a13eaf67713e380430 to your computer and use it in GitHub Desktop.
Save swvitaliy/d9489dfc44f2c4a13eaf67713e380430 to your computer and use it in GitHub Desktop.
/*
Check max_free index consistency.
Run command for check:
NODE_ENV=mis_integration node test/bin/max_free_check.js b 4000000004017 > /tmp/max_free_check.log
GREP errors:
grep 'wrong max_free' /tmp/max_free_check.log
Check in common logs what happens:
grep 5ac3202fca16773b555b1823 /var/www/.forever/mis_medexis_sched_4017.log | grep -v raw | grep crac-vectors | grep 9124348
*/
const logger = require('./../../lib/logger')();
const sms = require('../../lib/sms');
const Business = require('../../app/models/business').Business;
const Appointment = require('./../../app/models/appointment').Appointment;
const redis = require('redis');
const moment = require('moment');
const Q = require('q');
const redis_port = 6379;
const redis_host = 'prod3.srv.gbooking.ru';
const redis_db = 15;
//redis.debug_mode = true;
const redisClient = redis.createClient(redis_port, redis_host);
redisClient.on("error", function (err) {
smsLogger.error("Redis Error " + err);
});
redisClient.select(redis_db, (err, status) => {
if (err) return console.error('REDIS ERROR ' + err);
else if (!status) console.error('REDIS SELECT ERROR');
else main();
});
function getBusinesses() {
const criteria = {
"integrationData.medexis.active": true,
"integrationData.medexis.useEEBSchedule": true
};
return Business.find(criteria, {resources: 1, internalID:1}).lean().execQ();
}
function getBusiness(id) {
return Business.findOne({internalID: id}, {resources: 1, internalID:1}).lean().execQ();
}
let days = [];
// generate dates during one month
((d) => {
for (let i = 0; i < 30; ++i) {
days.push(d.format('YYYY/M/D'));
d.add(1, 'day');
}
})(moment.utc());
console.info('Days for checking: %j', days);
function checkResource(r, taxonomies) {
let job = Q();
// found maximal duration per each day by each taxonomy
let resultMap = {};
let resultTax = {};
taxonomies.forEach((t) => {
job = job.then(() => {
return Q.nfcall(redisClient.hgetall.bind(redisClient), 'max_free:' + t + '_' + r);
}).then((taxMap) => {
console.info('RESOURCE_TAXONOMY %s %s %j', r, t, taxMap);
if (taxMap === null) return;
for (let i = 0; i < days.length; ++i) {
let dur = Number(taxMap[days[i]] || 0);
if (typeof resultMap[days[i]] === 'undefined') {
resultMap[days[i]] = dur;
resultTax[days[i]] = t;
} else if (dur > resultMap[days[i]]) {
resultMap[days[i]] = dur;
resultTax[days[i]] = t;
}
}
});
});
// compare each day calculated index with stored
job = job.then(() => {
return Q.nfcall(redisClient.hgetall.bind(redisClient), 'max_free:' + r);
}).then((resMap) => {
console.info('RESOURCE %s %j', r, resMap);
for (let i = 0; i < days.length; ++i) {
const resDur = Number(resMap[days[i]] || 0);
const dur = Number(resultMap[days[i]] || 0);
if (resDur !== dur) {
console.error('wrong max_free res:' + r + ' day:' + days[i] + ' tax:' + resultTax[days[i]] +
' actual:' + resDur + ' expected:' + dur);
}
}
});
// catch error of resource
job = job.catch((err) => {
console.error('REDIS ERROR ' + err);
});
job.done();
}
function checkBusiness(b) {
console.info('BUSINESS ' + b.internalID);
b.resources.forEach((r) => checkResource(r.internalID, r.taxonomies));
}
function main() {
if (process.argv[2] === 'a')
getBusinesses().then((bs) => bs.forEach(checkBusiness));
else if (process.argv[2] === 'b')
getBusiness(Number(process.argv[3])).then(checkBusiness);
else
console.error('wrong arguments');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment