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 { ...@@ -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')) () => 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() @MonitorExecutionTime()
getBlockWhereMedianTimeLteAndGt(medianTime: number, previousBlockchainTime: number) { getBlockWhereMedianTimeLteAndGt(medianTime: number, previousBlockchainTime: number) {
return this.getFromCacheOrDB('getBlockWhereMedianTimeLteAndGt', [medianTime, previousBlockchainTime].join('-'), return this.getFromCacheOrDB('getBlockWhereMedianTimeLteAndGt', [medianTime, previousBlockchainTime].join('-'),
......
"use strict"; "use strict";
import {DataFinder} from "./DataFinder";
import {DBBlock} from "duniter/app/lib/db/DBBlock";
const co = require('co'); const co = require('co');
const constants = require(__dirname + '/constants') const constants = require(__dirname + '/constants')
...@@ -7,10 +10,12 @@ const constants = require(__dirname + '/constants') ...@@ -7,10 +10,12 @@ const constants = require(__dirname + '/constants')
* updateCache * updateCache
* *
*/ */
module.exports = (req, res, next) => co(function *() { module.exports = async (req:any, res:any, next:any) => {
var { duniterServer, cache } = req.app.locals var { duniterServer, cache } = req.app.locals
const dataFinder = new DataFinder(duniterServer)
try { try {
// Définition des constantes // Définition des constantes
const conf = duniterServer.conf; const conf = duniterServer.conf;
...@@ -35,7 +40,7 @@ module.exports = (req, res, next) => co(function *() { ...@@ -35,7 +40,7 @@ module.exports = (req, res, next) => co(function *() {
let reinitBdd = false; let reinitBdd = false;
if (cache.endBlock != null) 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) if (cache.blockchain.length > 0 && cache.blockchain[cache.blockchain.length-1].hash != checkBlock[0].hash && upgradeCache)
{ {
// reinitialize cache // reinitialize cache
...@@ -87,27 +92,27 @@ module.exports = (req, res, next) => co(function *() { ...@@ -87,27 +92,27 @@ module.exports = (req, res, next) => co(function *() {
// get endBlock // get endBlock
if ( typeof(req.query.end) == 'undefined' || req.query.end <= 0) 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 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 // Si end >= currentBlock, get currentBlock
if ( typeof(cache.endBlock[0]) == 'undefined' ) 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 // fix begin value
if ( typeof(req.query.begin) == 'undefined' || req.query.begin < 0 ) 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) else if (req.query.begin > cache.endBlock[0].number)
{ {
let beginTime = cache.endBlock[0].medianTime-(parseInt(cache.step)*unitTime*constants.STEP_COUNT_MIN); 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 // Define nbMaxPoints and adaptMaxPoints
if ( typeof(req.query.nbMaxPoints) != 'undefined' && req.query.nbMaxPoints > 0 ) { if ( typeof(req.query.nbMaxPoints) != 'undefined' && req.query.nbMaxPoints > 0 ) {
...@@ -127,13 +132,13 @@ module.exports = (req, res, next) => co(function *() { ...@@ -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 ) 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; 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") { } else if (cache.adaptMaxPoints == "step") {
cache.step = Math.ceil((cache.endBlock[0].medianTime-cache.beginBlock[0].medianTime)/(constants.STEP_COUNT_MAX*unitTime)); cache.step = Math.ceil((cache.endBlock[0].medianTime-cache.beginBlock[0].medianTime)/(constants.STEP_COUNT_MAX*unitTime));
} else { } else {
let newEndTime = cache.beginBlock[0].medianTime+cache.step*cache.nbMaxPoints*unitTime; 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 // Calculate stepTime
...@@ -143,7 +148,7 @@ module.exports = (req, res, next) => co(function *() { ...@@ -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)) 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; // 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 // Initialise newJoiners
let newJoiners = new Array(); let newJoiners = new Array();
...@@ -282,8 +287,9 @@ module.exports = (req, res, next) => co(function *() { ...@@ -282,8 +287,9 @@ module.exports = (req, res, next) => co(function *() {
next() next()
} catch (e) { } catch (e) {
console.error(e.stack || e)
// En cas d'exception, afficher le message // En cas d'exception, afficher le message
res.status(500).send(`<pre>${e.stack || e.message}</pre>`) res.status(500).send(`<pre>${e.stack || e.message}</pre>`)
} }
}) }
.catch((err) => console.error(err.stack || err));
...@@ -94,7 +94,7 @@ app.get('/members', ...@@ -94,7 +94,7 @@ app.get('/members',
* Évolution du nombre de membres * Évolution du nombre de membres
***************************************/ ***************************************/
app.get('/membersCount', 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', { (req, res)=> res.render('Chart.html', {
printMenu, printMenu,
pageName: 'MEMBERS_COUNT', pageName: 'MEMBERS_COUNT',
......
"use strict"; "use strict";
import {DataFinder} from "../lib/DataFinder";
const co = require('co') const co = require('co')
const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime') const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime')
const getLang = require(__dirname + '/../lib/getLang') const getLang = require(__dirname + '/../lib/getLang')
...@@ -7,9 +9,11 @@ const constants = require(__dirname + '/../lib/constants.js') ...@@ -7,9 +9,11 @@ const constants = require(__dirname + '/../lib/constants.js')
//const STEP_COUNT_MAX = 150; //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 var { duniterServer, cache } = req.app.locals
const dataFinder = new DataFinder(duniterServer)
try { try {
// get GET parameters // get GET parameters
...@@ -19,11 +23,8 @@ module.exports = (req, res, next) => co(function *() { ...@@ -19,11 +23,8 @@ module.exports = (req, res, next) => co(function *() {
// get lg file // get lg file
const LANG = getLang(`${__dirname}/../lg/membersCount_${req.query.lg||constants.DEFAULT_LANGUAGE}.txt`); 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 // 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 // Get blockchain timestamp
...@@ -101,8 +102,8 @@ module.exports = (req, res, next) => co(function *() { ...@@ -101,8 +102,8 @@ module.exports = (req, res, next) => co(function *() {
dateTime: dateTime, dateTime: dateTime,
membersCount: blockchain[blockIndex].membersCount, membersCount: blockchain[blockIndex].membersCount,
sentriesCount: cache.blockchain[cacheIndex].sentries, sentriesCount: cache.blockchain[cacheIndex].sentries,
issuersCount: parseInt(stepIssuerCount/bStep), issuersCount: parseInt(String(stepIssuerCount/bStep)),
powMin: parseInt(stepPowMin/bStep) powMin: parseInt(String(stepPowMin/bStep))
}); });
if (cache.stepUnit != "blocks") { nextStepTime += cache.stepTime; } if (cache.stepUnit != "blocks") { nextStepTime += cache.stepTime; }
...@@ -234,4 +235,4 @@ module.exports = (req, res, next) => co(function *() { ...@@ -234,4 +235,4 @@ module.exports = (req, res, next) => co(function *() {
// En cas d'exception, afficher le message // En cas d'exception, afficher le message
res.status(500).send(`<pre>${e.stack || e.message}</pre>`); 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