@voicenter-team/mysql-dynamic-cluster
Version:
Galera cluster with implementation of dynamic choose mysql server for queries, caching, hashing it and metrics
102 lines • 3.64 kB
JavaScript
"use strict";
/**
* Created by Bohdan on Sep, 2021
*/
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validator = void 0;
const Logger_1 = __importDefault(require("../utils/Logger"));
/**
* Pool validator to check if pool can handle queries by validator params
*/
class Validator {
/**
* @param pool pool what check by validators
* @param validators validators params by which to check possibility handle queries
*/
constructor(pool, validators) {
this._poolStatus = pool;
this._validators = validators;
}
/**
* Check pool by validators from result of db global status
* @param result result of db global status
*/
check(result) {
let validateCount = 0;
this._validators.forEach(validator => {
let value;
switch (validator.key) {
// custom validators keys
case 'available_connection_count':
value = this._poolStatus.availableConnectionCount.toString();
break;
case 'query_time':
value = this._poolStatus.queryTime.toString();
break;
case 'active':
value = this._poolStatus.active.toString();
break;
default:
value = result.find(res => res.Variable_name === validator.key).Value;
}
if (Validator.checkValueIsValid(value, validator))
validateCount++;
});
return validateCount === this._validators.length;
}
/**
* Check if value from db is valid with value from validator parameters
* @param value value from result of db global status
* @param validator validator parameters to check value
* @private
*/
static checkValueIsValid(value, validator) {
if (isNaN(+value)) {
// check values if value from db is string
const val = value;
const validatorVal = validator.value;
if (validator.operator === '=') {
if (val === validatorVal) {
return true;
}
}
else if (validator.operator === 'Like') {
if (val.indexOf(validatorVal) >= 0) {
return true;
}
}
else {
Logger_1.default.error('Operator ' + validator.operator + ' doesn\'t support for another type except number');
}
}
else {
// check values if value from db is number
const val = +value;
const validatorVal = +validator.value;
if (isNaN(validatorVal)) {
Logger_1.default.error('Validator value isn\'t a type number like value from database. Check if you write correct data');
return false;
}
switch (validator.operator) {
case "<":
if (val < validatorVal)
return true;
break;
case "=":
if (val === validatorVal)
return true;
break;
case ">":
if (val > validatorVal)
return true;
break;
}
}
return false;
}
}
exports.Validator = Validator;
//# sourceMappingURL=Validator.js.map