From 32d746852879f04e982bf976da8c2338aeeede4a Mon Sep 17 00:00:00 2001 From: cgeek <cem.moreau@gmail.com> Date: Sun, 5 Apr 2020 17:20:38 +0200 Subject: [PATCH] [refact] constants.js ==> constants.ts --- lib/{constants.js => constants2.ts} | 6 ++---- lib/updateCache2.ts | 12 ++++++------ routes/blockCount2.ts | 4 ++-- routes/gaussianWotQuality2.ts | 22 +++++++++------------- routes/index.js | 2 +- routes/members2.ts | 18 +++++++++--------- routes/membersCount2.ts | 4 ++-- routes/monetaryMass2.ts | 8 +++----- routes/tools/membersQuality.js | 2 +- routes/willMembers2.ts | 8 ++++---- routes/wotex2.ts | 6 ++---- views/printMenu.js | 2 +- 12 files changed, 42 insertions(+), 52 deletions(-) rename lib/{constants.js => constants2.ts} (91%) diff --git a/lib/constants.js b/lib/constants2.ts similarity index 91% rename from lib/constants.js rename to lib/constants2.ts index 8bf5736..80530f2 100755 --- a/lib/constants.js +++ b/lib/constants2.ts @@ -1,6 +1,4 @@ -"use strict"; - -module.exports = { +export const MonitConstants = { USE_WOTB6: false, DEFAULT_LANGUAGE: "fr", MIN_WILLMEMBERS_UPDATE_FREQ: 180, @@ -16,4 +14,4 @@ module.exports = { GET_SENTRIES_COUNT: 3, GET_D_SEN: 3 } -}; \ No newline at end of file +}; diff --git a/lib/updateCache2.ts b/lib/updateCache2.ts index 91f6d50..9d7bc72 100755 --- a/lib/updateCache2.ts +++ b/lib/updateCache2.ts @@ -2,9 +2,9 @@ import {DataFinder} from "./DataFinder"; import {DBBlock} from "duniter/app/lib/db/DBBlock"; +import {MonitConstants} from "./constants2"; const co = require('co'); -const constants = require(__dirname + '/constants') /** * updateCache @@ -24,7 +24,7 @@ module.exports = async (req:any, res:any, next:any) => { let upgradeCache = false; // Cacluler s'il faut mettre à jour le cache ou pas - upgradeCache = (Math.floor(Date.now() / 1000) > (cache.lastUptime + constants.MIN_CACHE_UPDATE_FREQ)); + upgradeCache = (Math.floor(Date.now() / 1000) > (cache.lastUptime + MonitConstants.MIN_CACHE_UPDATE_FREQ)); // Si le cache membersCount est dévérouillé, le vérouiller, sinon ne pas réinitialiser le cache if (upgradeCache && !cache.lockMembersCount) @@ -109,7 +109,7 @@ module.exports = async (req:any, res:any, next:any) => { { 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); + let beginTime = cache.endBlock[0].medianTime-(parseInt(cache.step)*unitTime*MonitConstants.STEP_COUNT_MIN); cache.beginBlock = [await dataFinder.getBlockWhereMedianTimeGte(beginTime)]; } else { cache.beginBlock = [await dataFinder.getBlock(req.query.begin)]; } @@ -118,7 +118,7 @@ module.exports = async (req:any, res:any, next:any) => { if ( typeof(req.query.nbMaxPoints) != 'undefined' && req.query.nbMaxPoints > 0 ) { cache.nbMaxPoints = req.query.nbMaxPoints; } else { - cache.nbMaxPoints = constants.STEP_COUNT_MAX; + cache.nbMaxPoints = MonitConstants.STEP_COUNT_MAX; } if ( typeof(req.query.adaptMaxPoints) != 'undefined' && (req.query.adaptMaxPoints == "step" || req.query.adaptMaxPoints == "end")) { cache.adaptMaxPoints = req.query.adaptMaxPoints; @@ -135,7 +135,7 @@ module.exports = async (req:any, res:any, next:any) => { 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)); + cache.step = Math.ceil((cache.endBlock[0].medianTime-cache.beginBlock[0].medianTime)/(MonitConstants.STEP_COUNT_MAX*unitTime)); } else { let newEndTime = cache.beginBlock[0].medianTime+cache.step*cache.nbMaxPoints*unitTime; cache.endBlock = [await dataFinder.getBlockWhereMedianTimeLte(newEndTime)]; @@ -145,7 +145,7 @@ module.exports = async (req:any, res:any, next:any) => { cache.stepTime = parseInt(cache.step)*unitTime; // if new blocks and MIN_CACHE_UPDATE_FREQ pass, update cache - 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 + MonitConstants.MIN_CACHE_UPDATE_FREQ)) { // let previousCacheTime = (cache.blockchain.length > 0) ? cache.blockchain[cache.blockchain.length-1].medianTime:0; var newBlocks = await dataFinder.getBlockWhereMedianTimeLteAndGtNoLimit(cache.currentBlockTime, cache.endBlock[0].medianTime); diff --git a/routes/blockCount2.ts b/routes/blockCount2.ts index a92abb5..97fa18a 100755 --- a/routes/blockCount2.ts +++ b/routes/blockCount2.ts @@ -1,12 +1,12 @@ import {DBBlock} from 'duniter/app/lib/db/DBBlock' import {DataFinder} from '../lib/DataFinder' import {showExecutionTimes} from '../lib/MonitorExecutionTime' +import {MonitConstants} from "../lib/constants2"; const fs = require('fs') const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime') const colorScale = require(__dirname + '/../lib/colorScale') const getLang = require(__dirname + '/../lib/getLang') -const constants = require(__dirname + '/../lib/constants') // Garder l'index des blocs en mémoire vive var blockchain: DBBlock[] = []; @@ -30,7 +30,7 @@ module.exports = async (req: any, res: any, next: any) => { var significantPercent = req.query.significantPercent || 3; // get lg file - const LANG = getLang(`${__dirname}/../lg/blockCount_${req.query.lg||constants.DEFAULT_LANGUAGE}.txt`); + const LANG = getLang(`${__dirname}/../lg/blockCount_${req.query.lg||MonitConstants.DEFAULT_LANGUAGE}.txt`); // detect fork if ( blockchain.length > 0 ) diff --git a/routes/gaussianWotQuality2.ts b/routes/gaussianWotQuality2.ts index 25513f9..7c46509 100644 --- a/routes/gaussianWotQuality2.ts +++ b/routes/gaussianWotQuality2.ts @@ -1,10 +1,6 @@ -"use strict"; - import {DataFinder} from "../lib/DataFinder"; +import {MonitConstants} from "../lib/constants2"; -const co = require('co') - -const constants = require(__dirname + '/../lib/constants') const membersQuality = require(__dirname + '/tools/membersQuality') const getLang = require(__dirname + '/../lib/getLang') @@ -25,14 +21,14 @@ module.exports = async (req:any, res:any, next:any) => { const nextYn = (req.query.nextYn=="yes") ? "yes":"no"; // get lg file - const LANG = getLang(`${__dirname}/../lg/gaussianWotQuality_${req.query.lg||constants.DEFAULT_LANGUAGE}.txt`); + const LANG = getLang(`${__dirname}/../lg/gaussianWotQuality_${req.query.lg||MonitConstants.DEFAULT_LANGUAGE}.txt`); // Définition des contantes const conf = duniterServer.conf; const qualityMax = (1/conf.xpercent); // Définition des variables - let lastUpgradeTimeDatas = membersQuality(constants.QUALITY_CACHE_ACTION.INIT); + let lastUpgradeTimeDatas = membersQuality(MonitConstants.QUALITY_CACHE_ACTION.INIT); let tabUidIndex = []; let tabMembersQuality= []; let tabMembersQualitySorted = []; @@ -44,7 +40,7 @@ module.exports = async (req:any, res:any, next:any) => { let membersList = await dataFinder.getMembers(); // Si les données de qualité n'ont jamais été calculés, le faire - if (lastUpgradeTimeDatas == 0 || (lastUpgradeTimeDatas+constants.MIN_WOT_QUALITY_CACHE_UPDATE_FREQ) < (Math.floor(Date.now() / 1000)) || (previousNextYn != nextYn)) + if (lastUpgradeTimeDatas == 0 || (lastUpgradeTimeDatas+MonitConstants.MIN_WOT_QUALITY_CACHE_UPDATE_FREQ) < (Math.floor(Date.now() / 1000)) || (previousNextYn != nextYn)) { // Calculer dSen var dSen = Math.ceil(Math.pow(membersList.length, 1 / conf.stepMax)); @@ -54,14 +50,14 @@ module.exports = async (req:any, res:any, next:any) => { const wot = duniterServer.dal.wotb; // Initialiser le cache des données de qualité - membersQuality(constants.QUALITY_CACHE_ACTION.INIT, 0, dSen, conf.stepMax, conf.xpercent, wot.memCopy()); + membersQuality(MonitConstants.QUALITY_CACHE_ACTION.INIT, 0, dSen, conf.stepMax, conf.xpercent, wot.memCopy()); } // Mettre a jour previousNextYn previousNextYn = (nextYn=="yes") ? "yes":"no"; // Calculer nbSentries, limit1 and label - const nbSentries = (sentries=="no") ? membersList.length:membersQuality(constants.QUALITY_CACHE_ACTION.GET_SENTRIES_COUNT); + const nbSentries = (sentries=="no") ? membersList.length:membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_SENTRIES_COUNT); let limit1 = 1; let label = LANG['QUALITY']; switch (unit) @@ -80,15 +76,15 @@ module.exports = async (req:any, res:any, next:any) => { // Récupérer le tableau de qualité des membres tabMembersQuality= []; - for (let i=0;membersQuality(constants.QUALITY_CACHE_ACTION.GET_QUALITY, i) >= 0;i++) + for (let i=0;membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_QUALITY, i) >= 0;i++) { if (sentries == "no") { - tabMembersQuality[i] = membersQuality(constants.QUALITY_CACHE_ACTION.GET_QUALITY, i, -1); + tabMembersQuality[i] = membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_QUALITY, i, -1); } else { - tabMembersQuality[i] = membersQuality(constants.QUALITY_CACHE_ACTION.GET_QUALITY, i); + tabMembersQuality[i] = membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_QUALITY, i); } } diff --git a/routes/index.js b/routes/index.js index f7efc13..d49a20d 100755 --- a/routes/index.js +++ b/routes/index.js @@ -1,7 +1,7 @@ const fs = require('fs') const express = require('express') -const constants = require(__dirname + '/../lib/constants') +const constants = require(__dirname + '/../lib/constants2').MonitConstants const getLang = require(__dirname + '/../lib/getLang') const printMenu = require(__dirname + '/../views/printMenu') diff --git a/routes/members2.ts b/routes/members2.ts index 5e9c783..d290cd2 100755 --- a/routes/members2.ts +++ b/routes/members2.ts @@ -1,6 +1,6 @@ import {DataFinder} from '../lib/DataFinder' +import {MonitConstants} from "../lib/constants2"; -const constants = require(__dirname + '/../lib/constants') const randomInt = require(__dirname + '/../lib/randomInt') const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime') const membersQuality = require(__dirname + '/tools/membersQuality') @@ -74,15 +74,15 @@ module.exports = async (req: any, res: any, next: any) => { let numberOfRandomMembers = req.query.randomCounts || 10 // Vérifier la valeur de nextYn dans le cache - let lastUpgradeTimeDatas = membersQuality(constants.QUALITY_CACHE_ACTION.INIT); - let dSenCache = membersQuality(constants.QUALITY_CACHE_ACTION.GET_D_SEN); + let lastUpgradeTimeDatas = membersQuality(MonitConstants.QUALITY_CACHE_ACTION.INIT); + let dSenCache = membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_D_SEN); if (lastUpgradeTimeDatas > 0 && dSenCache > dSen) { previousNextYn == "yes"; } // Alimenter wotb avec la toile actuelle const wotbInstance = duniterServer.dal.wotb; // Vérifier si le cache doit être Réinitialiser - let reinitCache = (Math.floor(Date.now() / 1000) > (membersLastUptime + constants.MIN_MEMBERS_UPDATE_FREQ)); + let reinitCache = (Math.floor(Date.now() / 1000) > (membersLastUptime + MonitConstants.MIN_MEMBERS_UPDATE_FREQ)); // Si changement de conditions, alors forcer le rechargement du cache s'il n'est pas vérouillé, sinon forcer les conditions à celles en mémoire if (previousMode != mode || previousCentrality != centrality || previousNextYn != nextYn || previousRandomList != randomList || numberOfRandomMembers != previousRandomCounts) @@ -145,7 +145,7 @@ module.exports = async (req: any, res: any, next: any) => { if (nextYn=="yes") { dSen++; } // réinitialiser le cache des données de qualité - membersQuality(constants.QUALITY_CACHE_ACTION.INIT, 0, dSen, conf.stepMax, conf.xpercent, wotbInstance.memCopy()); + membersQuality(MonitConstants.QUALITY_CACHE_ACTION.INIT, 0, dSen, conf.stepMax, conf.xpercent, wotbInstance.memCopy()); // Réinitialiser le cache des données de centralité if (centrality=='yes') @@ -229,14 +229,14 @@ module.exports = async (req: any, res: any, next: any) => { membersNbSentriesUnreached[membersList[m].uid] = parseInt(detailedDistance.nbSentries) - parseInt(detailedDistance.nbSuccess); // Calculer la qualité du membre courant - if (membersQuality(constants.QUALITY_CACHE_ACTION.GET_QUALITY, membersList[m].wotb_id, (currentMemberIsSentry) ? 1 : 0) >= 1.0) { + if (membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_QUALITY, membersList[m].wotb_id, (currentMemberIsSentry) ? 1 : 0) >= 1.0) { proportionMembersWithQualityUpper1++; } // Calculer la qualité du membre courant s'il n'y avait pas de référents (autrement di si tout les membres était référents) //let membersQualityIfNoSentries = ((detailedDistanceQualityExt.nbReached/membersList.length)/conf.xpercent).toFixed(2); //console.log("membersQualityIfNoSentries[%s] = %s", membersList[m].uid, membersQualityIfNoSentries); - if (membersQuality(constants.QUALITY_CACHE_ACTION.GET_QUALITY, membersList[m].wotb_id, -1) >= 1.0) { + if (membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_QUALITY, membersList[m].wotb_id, -1) >= 1.0) { proportionMembersWithQualityUpper1IfNoSentries++; } @@ -474,7 +474,7 @@ module.exports = async (req: any, res: any, next: any) => { { for (const member of membersList) { - tabSort.push(membersQuality(constants.QUALITY_CACHE_ACTION.GET_QUALITY, member.wotb_id)); + tabSort.push(membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_QUALITY, member.wotb_id)); } } else if (sort_by == "sigCount") @@ -574,7 +574,7 @@ module.exports = async (req: any, res: any, next: any) => { // Sinon, printer le tableau html else { - let meansMembersQuality = membersQuality(constants.QUALITY_CACHE_ACTION.GET_MEANS); + let meansMembersQuality = membersQuality(MonitConstants.QUALITY_CACHE_ACTION.GET_MEANS); res.locals = { host: req.headers.host.toString(), diff --git a/routes/membersCount2.ts b/routes/membersCount2.ts index 33f6dbd..bb3458f 100755 --- a/routes/membersCount2.ts +++ b/routes/membersCount2.ts @@ -1,11 +1,11 @@ "use strict"; import {DataFinder} from "../lib/DataFinder"; +import {MonitConstants} from "../lib/constants2"; const co = require('co') const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime') const getLang = require(__dirname + '/../lib/getLang') -const constants = require(__dirname + '/../lib/constants.js') //const STEP_COUNT_MAX = 150; @@ -21,7 +21,7 @@ module.exports = async (req: any, res: any, next: any) => { var pow = req.query.pow || 'no'; // 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||MonitConstants.DEFAULT_LANGUAGE}.txt`); // get blockchain var blockchain = await dataFinder.getBlockWhereMedianTimeLteNoLimit(cache.endBlock[0].medianTime); diff --git a/routes/monetaryMass2.ts b/routes/monetaryMass2.ts index 12a1115..b17ab59 100755 --- a/routes/monetaryMass2.ts +++ b/routes/monetaryMass2.ts @@ -1,11 +1,9 @@ -"use strict"; - import {DataFinder} from "../lib/DataFinder"; +import {MonitConstants} from "../lib/constants2"; const co = require('co') const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime') const getLang = require(__dirname + '/../lib/getLang') -const constants = require(__dirname + '/../lib/constants.js') module.exports = async (req:any, res:any, next:any) => { @@ -21,14 +19,14 @@ module.exports = async (req:any, res:any, next:any) => { var format = req.query.format || 'HTML'; // get lg file - const LANG = getLang(`${__dirname}/../lg/monetaryMass_${req.query.lg||constants.DEFAULT_LANGUAGE}.txt`); + const LANG = getLang(`${__dirname}/../lg/monetaryMass_${req.query.lg||MonitConstants.DEFAULT_LANGUAGE}.txt`); // calculate meanMonetaryMassAtFullCurrency const meanMonetaryMassAtFullCurrency = Math.ceil((1/duniterServer.conf.c)*(duniterServer.conf.dtReeval / duniterServer.conf.dt)); // get beginBlock and endBlock var beginBlock = [await dataFinder.getBlock(begin)]; - var endBlock = null; + var endBlock: any = null; if (end > 0) { endBlock = [await dataFinder.getBlock(end)]; diff --git a/routes/tools/membersQuality.js b/routes/tools/membersQuality.js index bc65860..fd8fdff 100644 --- a/routes/tools/membersQuality.js +++ b/routes/tools/membersQuality.js @@ -1,4 +1,4 @@ -const constants = require(__dirname + '/../../lib/constants') +const constants = require(__dirname + '/../../lib/constants2').MonitConstants // membersQuality cache var lastUpgradeTime = 0; diff --git a/routes/willMembers2.ts b/routes/willMembers2.ts index 350c15a..c1da687 100755 --- a/routes/willMembers2.ts +++ b/routes/willMembers2.ts @@ -3,8 +3,8 @@ import {DBMembership} from 'duniter/app/lib/dal/sqliteDAL/MembershipDAL' import {DBIdentity} from 'duniter/app/lib/dal/sqliteDAL/IdentityDAL' import {showExecutionTimes} from '../lib/MonitorExecutionTime' import {DataFinder} from '../lib/DataFinder' +import {MonitConstants} from "../lib/constants2"; -const constants = require(__dirname + '/../lib/constants') const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime') // Préserver les résultats en cache @@ -31,7 +31,7 @@ module.exports = async (req: any, res: any, next: any) => { try { // get blockchain timestamp - let resultQueryCurrentBlock = await dataFinder.getCurrentBlockOrNull(); + let resultQueryCurrentBlock: any = await dataFinder.getCurrentBlockOrNull(); const currentBlockchainTimestamp = resultQueryCurrentBlock.medianTime; const currentMembersCount = resultQueryCurrentBlock.membersCount; const currentBlockNumber = resultQueryCurrentBlock.number; @@ -60,7 +60,7 @@ module.exports = async (req: any, res: any, next: any) => { // Vérifier si le cache doit être Réinitialiser - let reinitCache = (Math.floor(Date.now() / 1000) > (willMembersLastUptime + constants.MIN_WILLMEMBERS_UPDATE_FREQ)); + let reinitCache = (Math.floor(Date.now() / 1000) > (willMembersLastUptime + MonitConstants.MIN_WILLMEMBERS_UPDATE_FREQ)); // Si le cache willMembers est dévérouillé, le vérouiller, sinon ne pas réinitialiser le cache if (reinitCache && !lockWillMembers) { @@ -360,7 +360,7 @@ module.exports = async (req: any, res: any, next: any) => { { // Tester la présence de l'adhésion let membership: DBMembership|null = null - const pendingMembershipsOfIdty: DBMembership[] = await duniterServer.dal.msDAL.getPendingINOfTarget(identitiesList[idMax].hash); + const pendingMembershipsOfIdty: DBMembership[] = await duniterServer.dal.msDAL.getPendingINOfTarget(identitiesList[idMax].hash as string); for (const ms of pendingMembershipsOfIdty) { if (!membership && ms.expires_on > currentBlockchainTimestamp) diff --git a/routes/wotex2.ts b/routes/wotex2.ts index 75fbe08..a18aa75 100755 --- a/routes/wotex2.ts +++ b/routes/wotex2.ts @@ -1,10 +1,8 @@ -"use strict"; - import {DataFinder} from "../lib/DataFinder"; +import {MonitConstants} from "../lib/constants2"; const _ = require('underscore') const getLang = require(__dirname + '/../lib/getLang') -const constants = require(__dirname + '/../lib/constants.js') const MAX_STEP_LOOK = 7 @@ -20,7 +18,7 @@ module.exports = async (req:any, res:any, next:any) => { var help = req.query.help || 'yes'; // get lg file - const LANG = getLang(`${__dirname}/../lg/wotex_${req.query.lg||constants.DEFAULT_LANGUAGE}.txt`); + const LANG = getLang(`${__dirname}/../lg/wotex_${req.query.lg||MonitConstants.DEFAULT_LANGUAGE}.txt`); // Trouve les points de contrôle efficacement grâce au module C (nommé "wotb") const wotb = duniterServer.dal.wotb.memCopy(); diff --git a/views/printMenu.js b/views/printMenu.js index 82854f6..d0bc7de 100644 --- a/views/printMenu.js +++ b/views/printMenu.js @@ -1,4 +1,4 @@ -const constants = require(__dirname + '/../lib/constants') +const constants = require(__dirname + '/../lib/constants2').MonitConstants module.exports = function printMenu(lang, help, location) { let htmlMenu = '<ul class="menu">';//'<table align="center" width="100%"><tr>'; -- GitLab