Commit 6b5a1788 authored by Cédric Moreau's avatar Cédric Moreau

[refact] abstracted `membersCount`

parent 587cfb49
Pipeline #8628 passed with stage
in 44 seconds
......@@ -126,6 +126,42 @@ export class DataFinder {
() => this.query('SELECT `issuer`,`membersCount`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` > '+previousBlockchainTime+' ORDER BY `medianTime` ASC'))
}
@MonitorExecutionTime()
getBlockWhereMedianTimeLte(medianTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeLte', [medianTime].join('-'),
() => this.query('SELECT `hash`,`membersCount`,`medianTime`,`number`,`certifications`,`issuersCount`,`powMin` FROM block WHERE `fork`=0 AND `medianTime` <= '+medianTime+' ORDER BY `medianTime` ASC'))
}
@MonitorExecutionTime()
getBlockWhereMedianTimeLteNoLimit(medianTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeLteNoLimit', [medianTime].join('-'),
() => this.query('SELECT `hash`,`membersCount`,`medianTime`,`number`,`certifications`,`issuersCount`,`powMin` FROM block WHERE `fork`=0 AND `medianTime` <= '+medianTime+' ORDER BY `medianTime` ASC'))
}
@MonitorExecutionTime()
getIdentityByWotbid(wotb_id: number): Promise<any> {
return this.getFromCacheOrDB('getIdentityByWotbid', [wotb_id].join('-'),
async () => (await this.duniterServer.dal.idtyDAL.query('SELECT * FROM i_index WHERE wotb_id = ?', [wotb_id]))[0])
}
@MonitorExecutionTime()
getBlockWhereMedianTimeLteAndGtNoLimit(currentBlockTime: number, medianTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeLteAndGtNoLimit', [currentBlockTime, medianTime].join('-'),
() => this.query('SELECT `hash`,`membersCount`,`medianTime`,`number`,`certifications`,`joiners`,`actives`,`revoked` FROM block WHERE `fork`=0 AND `medianTime` > '+currentBlockTime+' AND `medianTime` <= '+medianTime+' ORDER BY `medianTime` ASC'))
}
@MonitorExecutionTime()
getBlockWhereMedianTimeLteAndGte(endMedianTime: number, beginMedianTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeLteAndGte', [endMedianTime, beginMedianTime].join('-'),
() => this.query('SELECT `issuer`,`membersCount`,`monetaryMass`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` <= '+endMedianTime+' AND `medianTime` >= '+beginMedianTime+' ORDER BY `medianTime` ASC'))
}
@MonitorExecutionTime()
getBlockWhereMedianTimeGte(previousBlockchainTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeGte', String(previousBlockchainTime),
() => this.query('SELECT `issuer`,`membersCount`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` >= '+previousBlockchainTime+' ORDER BY `medianTime` ASC'))
}
@MonitorExecutionTime()
getBlockWhereMedianTimeLteAndGt(medianTime: number, previousBlockchainTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeLteAndGt', [medianTime, previousBlockchainTime].join('-'),
......
"use strict";
import {DataFinder} from "./DataFinder";
import {DBBlock} from "duniter/app/lib/db/DBBlock";
const co = require('co');
const constants = require(__dirname + '/constants')
......@@ -7,10 +10,12 @@ const constants = require(__dirname + '/constants')
* updateCache
*
*/
module.exports = (req, res, next) => co(function *() {
module.exports = async (req:any, res:any, next:any) => {
var { duniterServer, cache } = req.app.locals
const dataFinder = new DataFinder(duniterServer)
try {
// Définition des constantes
const conf = duniterServer.conf;
......@@ -35,7 +40,7 @@ module.exports = (req, res, next) => co(function *() {
let reinitBdd = false;
if (cache.endBlock != null)
{
let checkBlock = yield duniterServer.dal.peerDAL.query('SELECT `hash` FROM block WHERE `fork`=0 AND `number`='+(cache.blockchain[cache.blockchain.length-1].number)+' LIMIT 1 ');
let checkBlock = [await dataFinder.getBlock(cache.blockchain[cache.blockchain.length-1].number) as DBBlock];
if (cache.blockchain.length > 0 && cache.blockchain[cache.blockchain.length-1].hash != checkBlock[0].hash && upgradeCache)
{
// reinitialize cache
......@@ -87,27 +92,27 @@ module.exports = (req, res, next) => co(function *() {
// get endBlock
if ( typeof(req.query.end) == 'undefined' || req.query.end <= 0)
{
cache.endBlock = yield duniterServer.dal.peerDAL.query('SELECT `hash`,`medianTime`,`number`,`membersCount` FROM block WHERE `fork`=0 ORDER BY `medianTime` DESC LIMIT 1 ');
cache.endBlock = [await dataFinder.getCurrentBlockOrNull()];
}
else
{
cache.endBlock = yield duniterServer.dal.peerDAL.query('SELECT `hash`,`medianTime`,`number`,`membersCount` FROM block WHERE `fork`=0 AND `number`='+req.query.end+' LIMIT 1 ');
cache.endBlock = [await dataFinder.getBlock(req.query.end)];
// Si end >= currentBlock, get currentBlock
if ( typeof(cache.endBlock[0]) == 'undefined' )
{
cache.endBlock = yield duniterServer.dal.peerDAL.query('SELECT `hash`,`medianTime`,`number`,`membersCount` FROM block WHERE `fork`=0 ORDER BY `medianTime` DESC LIMIT 1 ');
cache.endBlock = [await dataFinder.getCurrentBlockOrNull()];
}
}
// fix begin value
if ( typeof(req.query.begin) == 'undefined' || req.query.begin < 0 )
{ cache.beginBlock = yield duniterServer.dal.peerDAL.query('SELECT `medianTime`,`number` FROM block WHERE `fork`=0 AND `number`=0 LIMIT 1 '); }
{ cache.beginBlock = [await dataFinder.getBlock(0)]; }
else if (req.query.begin > cache.endBlock[0].number)
{
let beginTime = cache.endBlock[0].medianTime-(parseInt(cache.step)*unitTime*constants.STEP_COUNT_MIN);
cache.beginBlock = yield duniterServer.dal.peerDAL.query('SELECT `medianTime`,`number` FROM block WHERE `fork`=0 AND `medianTime` >= \''+beginTime+'\' ORDER BY `medianTime` ASC LIMIT 1 ');
cache.beginBlock = [await dataFinder.getBlockWhereMedianTimeGte(beginTime)];
}
else { cache.beginBlock = yield duniterServer.dal.peerDAL.query('SELECT `medianTime`,`number` FROM block WHERE `fork`=0 AND `number`='+req.query.begin+' LIMIT 1 '); }
else { cache.beginBlock = [await dataFinder.getBlock(req.query.begin)]; }
// Define nbMaxPoints and adaptMaxPoints
if ( typeof(req.query.nbMaxPoints) != 'undefined' && req.query.nbMaxPoints > 0 ) {
......@@ -127,13 +132,13 @@ module.exports = (req, res, next) => co(function *() {
if ( Math.ceil((cache.endBlock[0].medianTime-cache.beginBlock[0].medianTime)/(cache.step*unitTime)) > cache.nbMaxPoints )
{
let newBeginTime = cache.endBlock[0].medianTime-cache.step*cache.nbMaxPoints*unitTime;
cache.beginBlock = yield duniterServer.dal.peerDAL.query('SELECT `medianTime`,`number` FROM block WHERE `fork`=0 AND `medianTime` >= \''+newBeginTime+'\' ORDER BY `medianTime` ASC LIMIT 1 ');
cache.beginBlock = [await dataFinder.getBlockWhereMedianTimeGte(newBeginTime)];
}
} else if (cache.adaptMaxPoints == "step") {
cache.step = Math.ceil((cache.endBlock[0].medianTime-cache.beginBlock[0].medianTime)/(constants.STEP_COUNT_MAX*unitTime));
} else {
let newEndTime = cache.beginBlock[0].medianTime+cache.step*cache.nbMaxPoints*unitTime;
cache.endBlock = yield duniterServer.dal.peerDAL.query('SELECT `medianTime`,`number` FROM block WHERE `fork`=0 AND `medianTime` <= \''+newEndTime+'\' ORDER BY `medianTime` DESC LIMIT 1 ');
cache.endBlock = [await dataFinder.getBlockWhereMedianTimeLte(newEndTime)];
}
// Calculate stepTime
......@@ -143,7 +148,7 @@ module.exports = (req, res, next) => co(function *() {
if ( parseInt(cache.endBlock[0].number) >= cache.currentBlockNumber && Math.floor(Date.now() / 1000) > (cache.lastUptime + constants.MIN_CACHE_UPDATE_FREQ))
{
// let previousCacheTime = (cache.blockchain.length > 0) ? cache.blockchain[cache.blockchain.length-1].medianTime:0;
var newBlocks = yield duniterServer.dal.peerDAL.query('SELECT `hash`,`membersCount`,`medianTime`,`number`,`certifications`,`joiners`,`actives`,`revoked` FROM block WHERE `fork`=0 AND `medianTime` > '+cache.currentBlockTime+' AND `medianTime` <= '+cache.endBlock[0].medianTime+' ORDER BY `medianTime` ASC');
var newBlocks = await dataFinder.getBlockWhereMedianTimeLteAndGtNoLimit(cache.currentBlockTime, cache.endBlock[0].medianTime);
// Initialise newJoiners
let newJoiners = new Array();
......@@ -282,8 +287,9 @@ module.exports = (req, res, next) => co(function *() {
next()
} catch (e) {
console.error(e.stack || e)
// En cas d'exception, afficher le message
res.status(500).send(`<pre>${e.stack || e.message}</pre>`)
}
})
.catch((err) => console.error(err.stack || err));
}
......@@ -94,7 +94,7 @@ app.get('/members',
* Évolution du nombre de membres
***************************************/
app.get('/membersCount',
require(__dirname + '/../lib/updateCache.js'), require(__dirname + '/membersCount.js'),
require(__dirname + '/../lib/updateCache2.js'), require(__dirname + '/membersCount2.js'),
(req, res)=> res.render('Chart.html', {
printMenu,
pageName: 'MEMBERS_COUNT',
......
"use strict";
import {DataFinder} from "../lib/DataFinder";
const co = require('co')
const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime')
const getLang = require(__dirname + '/../lib/getLang')
......@@ -7,9 +9,11 @@ const constants = require(__dirname + '/../lib/constants.js')
//const STEP_COUNT_MAX = 150;
module.exports = (req, res, next) => co(function *() {
module.exports = async (req: any, res: any, next: any) => {
var { duniterServer, cache } = req.app.locals
const dataFinder = new DataFinder(duniterServer)
try {
// get GET parameters
......@@ -19,11 +23,8 @@ module.exports = (req, res, next) => co(function *() {
// get lg file
const LANG = getLang(`${__dirname}/../lg/membersCount_${req.query.lg||constants.DEFAULT_LANGUAGE}.txt`);
// get medianTime of beginBlock
var beginBlock = yield duniterServer.dal.peerDAL.query('SELECT `medianTime`,`hash` FROM block WHERE `fork`=0 AND `number` = '+cache.beginBlock[0].number+' LIMIT 1');
// get blockchain
var blockchain = yield duniterServer.dal.peerDAL.query('SELECT `hash`,`membersCount`,`medianTime`,`number`,`certifications`,`issuersCount`,`powMin` FROM block WHERE `fork`=0 AND `medianTime` <= '+cache.endBlock[0].medianTime+' ORDER BY `medianTime` ASC');
var blockchain = await dataFinder.getBlockWhereMedianTimeLteNoLimit(cache.endBlock[0].medianTime);
// Get blockchain timestamp
......@@ -101,8 +102,8 @@ module.exports = (req, res, next) => co(function *() {
dateTime: dateTime,
membersCount: blockchain[blockIndex].membersCount,
sentriesCount: cache.blockchain[cacheIndex].sentries,
issuersCount: parseInt(stepIssuerCount/bStep),
powMin: parseInt(stepPowMin/bStep)
issuersCount: parseInt(String(stepIssuerCount/bStep)),
powMin: parseInt(String(stepPowMin/bStep))
});
if (cache.stepUnit != "blocks") { nextStepTime += cache.stepTime; }
......@@ -234,4 +235,4 @@ module.exports = (req, res, next) => co(function *() {
// En cas d'exception, afficher le message
res.status(500).send(`<pre>${e.stack || e.message}</pre>`);
}
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment