/var/www/myprog.com.ua/server/node/db
Edit: /var/www/myprog.com.ua/server/node/db/db_context_values.js (5472B)
const Value = require('../objects/value');
const ValueHistory = require('../objects/value_history');
const log = require('../helpers/log');
module.exports = (DbContext) => {
DbContext.prototype.get_value = async function (host_id, value_n) {
//let t1 = new Date().microtime(true);
let _self = this;
let value = null;
try {
let sql = `SELECT * FROM \`values\` WHERE host_id='${host_id}' AND n='${value_n}' LIMIT 1`;
//log.debug('DbContext', 'get_value()', `SQL: ${sql}`);
let result = await _self.query(sql);
if(result[0]!=undefined){
if (result[0].id > 0) {
value = new Value(result[0]);
}
}
} catch (e) {
log.error('DbContext', 'get_value()', e);
}
//let t2 = new Date().microtime(true);
//log.debug('DbContext', 'get_value()', `complete ${t2 - t1}`);
return value;
}
DbContext.prototype.get_user_values = async function (user_id) {
let _self = this;
let t1 = new Date().microtime(true);
let sql = `SELECT va.* FROM \`values\` va
INNER JOIN users_hosts uh ON uh.host_id=va.host_id
WHERE uh.user_id='${user_id}' ORDER BY va.k, va.n LIMIT 100`;
//log.debug('DbContext', 'get_user_devices()', `SQL: ${sql}`);
let values = [];
try {
let rows = await _self.query(sql);
for (row of rows) {
values.push(new Value(row));
}
} catch (e) {
log.error('DbContext', 'get_user_values()', e);
}
let t2 = new Date().microtime(true);
//log.debug('DbContext', 'get_user_values()', `complete ${t2 - t1}`);
return values;
}
DbContext.prototype.get_user_values_history = async function (user_id) {
let _self = this;
let t1 = new Date().microtime(true);
let sql = "SELECT eh.* FROM `values_history` eh \
INNER JOIN `users_hosts` uh ON uh.user_id="+user_id+" \
INNER JOIN `values` d ON d.host_id=uh.host_id \
WHERE eh.value_id=d.id ORDER BY eh.id DESC\
LIMIT 100;";
//WHERE uh.user_id='${user_id}' LIMIT 100`;
//log.debug('DbContext', 'get_user_events()', `SQL: ${sql}`);
let events = [];
try {
let rows = await _self.query(sql);
for (row of rows) {
events.push(new ValueHistory(row));
}
} catch (e) {
log.error('DbContext', 'get_user_values_history()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_user_values_history()', `complete ${t2 - t1}`);
return events;
}
DbContext.prototype.get_values_history = async function (value_id,period) {
let _self = this;
let t1 = new Date().microtime(true);
let sql = "SELECT eh.* FROM `values_history` eh \
WHERE eh.value_id="+value_id+" ORDER BY eh.id DESC\
LIMIT 250";
//WHERE uh.user_id='${user_id}' LIMIT 100`;
//log.debug('DbContext', 'get_user_events()', `SQL: ${sql}`);
let events = [];
try {
let rows = await _self.query(sql);
for (row of rows) {
events.push(new ValueHistory(row));
}
} catch (e) {
log.error('DbContext', 'get_values_history()', e);
}
let t2 = new Date().microtime(true);
log.debug('DbContext', 'get_values_history()', `complete ${t2 - t1} events:`+events.length);
return events.reverse();
}
DbContext.prototype.update_value = async function (value) {
//let t1 = new Date().microtime(true);
let _self = this;
//await this.query('START TRANSACTION');
try {
let sql = _self.sql_insert_or_update(value, 'values');
//log.debug('DbContext', 'update_value()',`SQL: ${sql}`);
let result = await _self.query(sql);
if (result.insertId > 0) {
value.id = result.insertId;
}
//await _self.query('COMMIT');
//let t2 = new Date().microtime(true);
//log.debug('DbContext', 'update_value()',`complete ${t2 - t1}`);
return value;
} catch (e) {
log.error('DbContext', 'update_value()',e);
//await _self.query('ROLLBACK');
return null;
}
}
DbContext.prototype.update_value_history = async function (value) {
// let t1 = new Date().microtime(true);
let _self = this;
//await this.query('START TRANSACTION');
try {
let sql = _self.sql_insert_or_update(value, 'values_history');
//log.debug('DbContext', 'update_value_history()',`SQL: ${sql}`);
let result = await _self.query(sql);
if (result.insertId > 0) {
value.id = result.insertId;
}
//await _self.query('COMMIT');
// let t2 = new Date().microtime(true);
// log.debug('DbContext', 'update_value()',`complete ${t2 - t1}`);
return value;
} catch (e) {
log.error('DbContext', 'update_value()',e);
//await _self.query('ROLLBACK');
return null;
}
}
}