Skip to content
Snippets Groups Projects
Commit 587cfb49 authored by Cédric Moreau's avatar Cédric Moreau
Browse files

[refact] abstracted `wotex`

parent c644ebea
No related branches found
No related tags found
No related merge requests found
...@@ -143,4 +143,11 @@ export class DataFinder { ...@@ -143,4 +143,11 @@ export class DataFinder {
return this.getFromCacheOrDB('getBlockWhereMedianTimeGtNoLimit', String(beginMedianTime), return this.getFromCacheOrDB('getBlockWhereMedianTimeGtNoLimit', String(beginMedianTime),
() => this.query('SELECT `issuer`,`membersCount`,`monetaryMass`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` >= '+beginMedianTime+' ORDER BY `medianTime` ASC')) () => this.query('SELECT `issuer`,`membersCount`,`monetaryMass`,`medianTime`,`dividend`,`number`,`nonce` FROM block WHERE `fork`=0 AND `medianTime` >= '+beginMedianTime+' ORDER BY `medianTime` ASC'))
} }
searchIdentities(search: string) { // TODO: refactor duniterServer in all this class
return this.duniterServer.dal.idtyDAL.query('' +
'SELECT uid, pub, wotb_id FROM i_index WHERE (uid = ? or pub = ?) ' +
'UNION ALL ' +
'SELECT uid, pubkey as pub, (SELECT NULL) AS wotb_id FROM idty WHERE (uid = ? or pubkey = ?)', [search, search, search, search])
}
} }
...@@ -108,7 +108,7 @@ app.get('/membersCount', ...@@ -108,7 +108,7 @@ app.get('/membersCount',
* Wotex * Wotex
***************************************/ ***************************************/
app.get('/wotex', app.get('/wotex',
require(__dirname + '/wotex.js'), require(__dirname + '/wotex2.js'),
(req, res)=> res.render('wotex.html', { (req, res)=> res.render('wotex.html', {
printMenu, printMenu,
help: req.query.help, help: req.query.help,
......
"use strict"; "use strict";
import {DataFinder} from "../lib/DataFinder";
const _ = require('underscore') const _ = require('underscore')
const co = require('co')
const getLang = require(__dirname + '/../lib/getLang') const getLang = require(__dirname + '/../lib/getLang')
const constants = require(__dirname + '/../lib/constants.js')
const MAX_STEP_LOOK = 7 const MAX_STEP_LOOK = 7
module.exports = (req, res, next) => co(function *() { module.exports = async (req:any, res:any, next:any) => {
var { duniterServer } = req.app.locals var { duniterServer } = req.app.locals
const dataFinder = new DataFinder(duniterServer)
try { try {
// get GET parameters // get GET parameters
var format = req.query.format || 'HTML'; var format = req.query.format || 'HTML';
...@@ -21,20 +25,20 @@ module.exports = (req, res, next) => co(function *() { ...@@ -21,20 +25,20 @@ module.exports = (req, res, next) => co(function *() {
// Trouve les points de contrôle efficacement grâce au module C (nommé "wotb") // Trouve les points de contrôle efficacement grâce au module C (nommé "wotb")
const wotb = duniterServer.dal.wotb.memCopy(); const wotb = duniterServer.dal.wotb.memCopy();
wotb.setMaxCert(100); wotb.setMaxCert(100);
const head = yield duniterServer.dal.getCurrentBlockOrNull(); const head = await duniterServer.dal.getCurrentBlockOrNull();
const membersCount = head ? head.membersCount : 0; const membersCount = head ? head.membersCount : 0;
let dSen = Math.ceil(Math.pow(membersCount, 1 / duniterServer.conf.stepMax)); let dSen = Math.ceil(Math.pow(membersCount, 1 / duniterServer.conf.stepMax));
const dicoIdentites = {}; const dicoIdentites: any = {};
const pointsDeControle = wotb.getSentries(dSen); const pointsDeControle = wotb.getSentries(dSen);
const sentries = yield pointsDeControle.map((wotb_id) => co(function*() { const sentries = await Promise.all(pointsDeControle.map(async (wotb_id: number) => {
const identite = (yield duniterServer.dal.idtyDAL.query('SELECT * FROM i_index WHERE wotb_id = ?', [wotb_id]))[0]; const identite = await dataFinder.getIdentityByWotbid(wotb_id);
identite.statusClass = 'isSentry'; identite.statusClass = 'isSentry';
dicoIdentites[identite.wotb_id] = identite; dicoIdentites[identite.wotb_id] = identite;
return identite; return identite;
})); }));
let searchResult = ''; let searchResult = '';
let lignes = []; let lignes:any = [];
if (req.query.to) { if (req.query.to) {
let idty; let idty;
let pos = 0, search = req.query.to; let pos = 0, search = req.query.to;
...@@ -43,10 +47,7 @@ module.exports = (req, res, next) => co(function *() { ...@@ -43,10 +47,7 @@ module.exports = (req, res, next) => co(function *() {
search = match[1]; search = match[1];
pos = parseInt(match[2].replace(/(\[|\])/g, '')); pos = parseInt(match[2].replace(/(\[|\])/g, ''));
} }
let idties = yield duniterServer.dal.idtyDAL.query('' + let idties = await dataFinder.searchIdentities(search);
'SELECT uid, pub, wotb_id FROM i_index WHERE (uid = ? or pub = ?) ' +
'UNION ALL ' +
'SELECT uid, pubkey as pub, (SELECT NULL) AS wotb_id FROM idty WHERE (uid = ? or pubkey = ?)', [search, search, search, search]);
idty = idties[pos]; idty = idties[pos];
if (!idty) { if (!idty) {
searchResult = ` searchResult = `
...@@ -58,9 +59,9 @@ module.exports = (req, res, next) => co(function *() { ...@@ -58,9 +59,9 @@ module.exports = (req, res, next) => co(function *() {
`; `;
} else { } else {
let membres = yield prepareMembresInitiaux(wotb, dSen, sentries, dicoIdentites, duniterServer); let membres = await prepareMembresInitiaux(dataFinder, wotb, dSen, sentries, dicoIdentites, duniterServer);
const res = yield prepareMembres(req, wotb, duniterServer, membres, idty, dicoIdentites); const res = await prepareMembres(req, wotb, duniterServer, membres, idty, dicoIdentites);
membres = res.membres; membres = res.membres;
idty = res.idty; idty = res.idty;
const mapPendingCerts = res.mapPendingCerts; const mapPendingCerts = res.mapPendingCerts;
...@@ -87,7 +88,7 @@ module.exports = (req, res, next) => co(function *() { ...@@ -87,7 +88,7 @@ module.exports = (req, res, next) => co(function *() {
{ {
// write sentriesHTML // write sentriesHTML
let sentriesHTML = sentries let sentriesHTML = sentries
.map((sentry) => ` .map((sentry:any) => `
<div class="sentry isSentry"><a href="wotex?lg=${LANG['LG']}&to=${sentry.uid}">${sentry.uid}</a></div> <div class="sentry isSentry"><a href="wotex?lg=${LANG['LG']}&to=${sentry.uid}">${sentry.uid}</a></div>
`) `)
.join(''); .join('');
...@@ -105,10 +106,10 @@ module.exports = (req, res, next) => co(function *() { ...@@ -105,10 +106,10 @@ module.exports = (req, res, next) => co(function *() {
res.status(500).send('<pre>' + (e.stack || e.message) + '</pre>'); res.status(500).send('<pre>' + (e.stack || e.message) + '</pre>');
} }
}); };
function traduitCheminEnIdentites(chemins, dicoIdentites) { function traduitCheminEnIdentites(chemins:any, dicoIdentites:any): any[] {
const cheminsTries = chemins.sort((cheminA, cheminB) => { const cheminsTries = chemins.sort((cheminA:any, cheminB:any) => {
if (cheminA.length < cheminB.length) { if (cheminA.length < cheminB.length) {
return -1; return -1;
} }
...@@ -118,7 +119,7 @@ function traduitCheminEnIdentites(chemins, dicoIdentites) { ...@@ -118,7 +119,7 @@ function traduitCheminEnIdentites(chemins, dicoIdentites) {
return 0; return 0;
}); });
if (cheminsTries[0]) { if (cheminsTries[0]) {
return cheminsTries[0].slice().map((wotb_id) => { return cheminsTries[0].slice().map((wotb_id: number) => {
return { return {
uid: dicoIdentites[wotb_id].uid, uid: dicoIdentites[wotb_id].uid,
pub: dicoIdentites[wotb_id].pub, pub: dicoIdentites[wotb_id].pub,
...@@ -131,36 +132,33 @@ function traduitCheminEnIdentites(chemins, dicoIdentites) { ...@@ -131,36 +132,33 @@ function traduitCheminEnIdentites(chemins, dicoIdentites) {
} }
} }
function prepareMembresInitiaux(wotb, dSen, sentries, dicoIdentites, duniterServer) { async function prepareMembresInitiaux(dataFinder: DataFinder, wotb:any, dSen:any, sentries:any, dicoIdentites:any, duniterServer:any) {
return co(function*() {
// Ajout des membres non-sentries // Ajout des membres non-sentries
const pointsNormaux = wotb.getNonSentries(dSen); const pointsNormaux = wotb.getNonSentries(dSen);
const nonSentries = yield pointsNormaux.map((wotb_id) => co(function*() { const nonSentries = await Promise.all(pointsNormaux.map(async (wotb_id:number) => {
const identite = (yield duniterServer.dal.idtyDAL.query('SELECT * FROM i_index WHERE wotb_id = ?', [wotb_id]))[0]; const identite = await dataFinder.getIdentityByWotbid(wotb_id);
identite.statusClass = 'isMember'; identite.statusClass = 'isMember';
dicoIdentites[identite.wotb_id] = identite; dicoIdentites[identite.wotb_id] = identite;
return identite; return identite;
})); }));
const nonMembres = wotb.getDisabled(); const nonMembres = wotb.getDisabled();
const disabled = yield nonMembres.map((wotb_id) => co(function*() { const disabled = await Promise.all(nonMembres.map(async (wotb_id:number) => {
const identite = (yield duniterServer.dal.idtyDAL.query('SELECT * FROM i_index WHERE wotb_id = ?', [wotb_id]))[0]; const identite = await dataFinder.getIdentityByWotbid(wotb_id);
identite.statusClass = 'isNonMember'; identite.statusClass = 'isNonMember';
dicoIdentites[identite.wotb_id] = identite; dicoIdentites[identite.wotb_id] = identite;
return identite; return identite;
})); }));
return sentries.concat(nonSentries).concat(disabled); return sentries.concat(nonSentries).concat(disabled);
});
} }
function prepareMembres(req, wotb, duniterServer, membres, idty, dicoIdentites) { async function prepareMembres(req:any, wotb:any, duniterServer:any, membres:any, idty:any, dicoIdentites:any) {
return co(function*() { const mapPendingCerts:any = {};
const mapPendingCerts = {}; const mapPendingIdties:any = {};
const mapPendingIdties = {}; const mapSiblings:any = {};
const mapSiblings = {};
if (req.query.pending) { if (req.query.pending) {
// Recherche les identités en attente // Recherche les identités en attente
const pendingIdties = yield duniterServer.dal.idtyDAL.sqlListAll(); const pendingIdties = await duniterServer.dal.idtyDAL.sqlListAll();
for (const theIdty of pendingIdties) { for (const theIdty of pendingIdties) {
// Add it to the temp wot // Add it to the temp wot
theIdty.wotb_id = wotb.addNode(); theIdty.wotb_id = wotb.addNode();
...@@ -187,7 +185,7 @@ function prepareMembres(req, wotb, duniterServer, membres, idty, dicoIdentites) ...@@ -187,7 +185,7 @@ function prepareMembres(req, wotb, duniterServer, membres, idty, dicoIdentites)
membres = membres.concat(Object.values(mapPendingIdties)); membres = membres.concat(Object.values(mapPendingIdties));
// Recherche les certifications en attente // Recherche les certifications en attente
const pendingCerts = yield duniterServer.dal.certDAL.sqlListAll(); const pendingCerts = await duniterServer.dal.certDAL.sqlListAll();
for (const cert of pendingCerts) { for (const cert of pendingCerts) {
const from = _.findWhere(membres, { pub: cert.from }); const from = _.findWhere(membres, { pub: cert.from });
const target = _.findWhere(membres, { hash: cert.target }); const target = _.findWhere(membres, { hash: cert.target });
...@@ -198,10 +196,9 @@ function prepareMembres(req, wotb, duniterServer, membres, idty, dicoIdentites) ...@@ -198,10 +196,9 @@ function prepareMembres(req, wotb, duniterServer, membres, idty, dicoIdentites)
} }
} }
return { idty, membres, mapPendingCerts }; return { idty, membres, mapPendingCerts };
});
} }
function alimenteLignes(wotb, source, cible, lignes, dicoIdentites, mapPendingCerts) { function alimenteLignes(wotb:any, source:any, cible:any, lignes:any, dicoIdentites:any, mapPendingCerts:any) {
const plusCourtsCheminsPossibles = wotb.getPaths(source.wotb_id, cible.wotb_id, MAX_STEP_LOOK); const plusCourtsCheminsPossibles = wotb.getPaths(source.wotb_id, cible.wotb_id, MAX_STEP_LOOK);
if (plusCourtsCheminsPossibles.length) { if (plusCourtsCheminsPossibles.length) {
const ligne = traduitCheminEnIdentites(plusCourtsCheminsPossibles, dicoIdentites); const ligne = traduitCheminEnIdentites(plusCourtsCheminsPossibles, dicoIdentites);
...@@ -228,8 +225,8 @@ function alimenteLignes(wotb, source, cible, lignes, dicoIdentites, mapPendingCe ...@@ -228,8 +225,8 @@ function alimenteLignes(wotb, source, cible, lignes, dicoIdentites, mapPendingCe
} }
} }
function genereHTMLdeRecherche(lignes, LANG, help) { function genereHTMLdeRecherche(lignes: any, LANG: any, help: any) {
lignes.sort((ligneA, ligneB) => { lignes.sort((ligneA:any, ligneB:any) => {
if (ligneA.length > ligneB.length) return -1; if (ligneA.length > ligneB.length) return -1;
if (ligneB.length > ligneA.length) return 1; if (ligneB.length > ligneA.length) return 1;
if ((ligneA[1] && ligneA[1] == '?') && (!ligneB[1] || ligneB[1] != '?')) { if ((ligneA[1] && ligneA[1] == '?') && (!ligneB[1] || ligneB[1] != '?')) {
...@@ -241,7 +238,7 @@ function genereHTMLdeRecherche(lignes, LANG, help) { ...@@ -241,7 +238,7 @@ function genereHTMLdeRecherche(lignes, LANG, help) {
return 0; return 0;
}); });
lignes.reverse(); lignes.reverse();
const chemins = lignes.map((colonnes) => { const chemins = lignes.map((colonnes: any) => {
return ` return `
<tr> <tr>
<td class="${ colonnes[0] && colonnes[0].statusClass }"><a href="wotex?lg=${LANG['LG']}&help=${help}&to=${ (colonnes[0] && colonnes[0].uid) || ''}">${ (colonnes[0] && colonnes[0].uid) || ''}</td> <td class="${ colonnes[0] && colonnes[0].statusClass }"><a href="wotex?lg=${LANG['LG']}&help=${help}&to=${ (colonnes[0] && colonnes[0].uid) || ''}">${ (colonnes[0] && colonnes[0].uid) || ''}</td>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment