Skip to content
Snippets Groups Projects
Unverified Commit e88be150 authored by Éloïs's avatar Éloïs
Browse files

[enh] add url csvCalculatorsRank

parent 7b1f06df
No related branches found
No related tags found
1 merge request!62Dev2
Pipeline #
"use strict"; "use strict";
const co = require('co'); const co = require('co');
const os = require('os');
const fs = require('fs');
const webserver = require(__dirname + '/webserver.js'); const webserver = require(__dirname + '/webserver.js');
const timestampToDatetime = require(__dirname + '/timestampToDatetime.js'); const timestampToDatetime = require(__dirname + '/timestampToDatetime.js');
...@@ -15,6 +17,13 @@ module.exports = (duniterServer, host, port, appParente, program) => co(function ...@@ -15,6 +17,13 @@ module.exports = (duniterServer, host, port, appParente, program) => co(function
var offset = -x.getTimezoneOffset(); var offset = -x.getTimezoneOffset();
//timestampToDatetime(1000000, true, offset); //timestampToDatetime(1000000, true, offset);
// Get monitDatasPath
const mdb = program.mdb || "duniter_default";
const monitDatasPath = os.homedir() + "/.config/duniter/" + mdb + "/currency-monit/";
// Create monitDatasPath
if (!fs.existsSync(monitDatasPath)) { fs.mkdirSync(monitDatasPath) }
// Define cache // Define cache
var cache = { var cache = {
// membersCount // membersCount
...@@ -39,7 +48,7 @@ module.exports = (duniterServer, host, port, appParente, program) => co(function ...@@ -39,7 +48,7 @@ module.exports = (duniterServer, host, port, appParente, program) => co(function
console.log("module currency-monit started"); console.log("module currency-monit started");
// Specialized node's UI // Specialized node's UI
let httpServer = webserver(host, port, appParente, duniterServer, offset, cache); let httpServer = webserver(host, port, appParente, duniterServer, monitDatasPath, offset, cache);
yield httpServer.openConnection(); yield httpServer.openConnection();
}) })
......
...@@ -13,7 +13,7 @@ const bodyParser = require('body-parser'); ...@@ -13,7 +13,7 @@ const bodyParser = require('body-parser');
const routes = require(__dirname + '/../routes'); const routes = require(__dirname + '/../routes');
const tpl = require(__dirname + '/tplit.js'); const tpl = require(__dirname + '/tplit.js');
module.exports = (host, port, appParente, duniterServer, offset, cache) => { module.exports = (host, port, appParente, duniterServer, monitDatasPath, offset, cache) => {
var app = express(); var app = express();
...@@ -31,6 +31,7 @@ module.exports = (host, port, appParente, duniterServer, offset, cache) => { ...@@ -31,6 +31,7 @@ module.exports = (host, port, appParente, duniterServer, offset, cache) => {
app.set('view engine', 'html') // register the template engine app.set('view engine', 'html') // register the template engine
app.locals.duniterServer = duniterServer app.locals.duniterServer = duniterServer
app.locals.monitDatasPath = monitDatasPath
app.locals.currencyName = duniterServer.conf.currency app.locals.currencyName = duniterServer.conf.currency
app.locals.offset = offset app.locals.offset = offset
app.locals.cache = cache app.locals.cache = cache
...@@ -40,6 +41,22 @@ module.exports = (host, port, appParente, duniterServer, offset, cache) => { ...@@ -40,6 +41,22 @@ module.exports = (host, port, appParente, duniterServer, offset, cache) => {
app.use( routes ) app.use( routes )
/***************************************
* CSV des membres calculants
***************************************/
app.get('/csvCalculatorsRank', function(req, res) {
let files = fs.readdirSync(monitDatasPath + '/calculators_rank/')
let maxTimestamp = 0
for (let file of files) {
let fileTimestamp = parseInt(file.split('_')[2])
if (fileTimestamp > maxTimestamp) {
maxTimestamp = fileTimestamp
}
}
var file = monitDatasPath + '/calculators_rank/calculators_rank_' + maxTimestamp + '.csv';
res.download(file); // Set disposition and send it.
});
// Si l'on ne dispose pas d'un serveur web parent, lancer notre propre serveur web // Si l'on ne dispose pas d'un serveur web parent, lancer notre propre serveur web
if ( appParente == null ) if ( appParente == null )
{ {
......
"use strict"; "use strict";
const co = require('co') const co = require('co')
const fs = require('fs')
const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime') const timestampToDatetime = require(__dirname + '/../lib/timestampToDatetime')
const colorScale = require(__dirname + '/../lib/colorScale') const colorScale = require(__dirname + '/../lib/colorScale')
const getLang = require(__dirname + '/../lib/getLang') const getLang = require(__dirname + '/../lib/getLang')
...@@ -12,7 +13,7 @@ var previousBlockchainTime= 0; ...@@ -12,7 +13,7 @@ var previousBlockchainTime= 0;
module.exports = (req, res, next) => co(function *() { module.exports = (req, res, next) => co(function *() {
var { duniterServer } = req.app.locals var { duniterServer, monitDatasPath } = req.app.locals
try { try {
// get GET parameters // get GET parameters
...@@ -110,13 +111,32 @@ module.exports = (req, res, next) => co(function *() { ...@@ -110,13 +111,32 @@ module.exports = (req, res, next) => co(function *() {
} }
} }
// Open a write stream into a new calculators_rank file
let pathToCalculatorsRankFile = monitDatasPath + 'calculators_rank/'
let calculatorsRankFilename = 'calculators_rank_' + Date.now() + '.csv'
if (!fs.existsSync(pathToCalculatorsRankFile)) { fs.mkdirSync(pathToCalculatorsRankFile) }
var ws = fs.createWriteStream(pathToCalculatorsRankFile + calculatorsRankFilename, {
flags: 'a',
encoding: 'utf8',
fd: null,
mode: 0o666,
autoClose: true
});
// Calculate the sum of blocks and their nonce and number of core // Calculate the sum of blocks and their nonce and number of core
let calculatorsCount = 1;
for (let b=begin;b<blockchain.length;b++) for (let b=begin;b<blockchain.length;b++)
{ {
for (let m=0;m<tabBlockMembers.length;m++) for (let m=0;m<tabBlockMembers.length;m++)
{ {
if (tabBlockMembers[m].pubkey == blockchain[b].issuer) if (tabBlockMembers[m].pubkey == blockchain[b].issuer)
{ {
if (tabBlockMembers[m].blockCount == 0) {
//console.log("%s, %s, #%s, %s", calculatorsCount, tabBlockMembers[m].uid, b, timestampToDatetime(blockchain[b].medianTime));
ws.write(calculatorsCount + ', ' + tabBlockMembers[m].uid + ', #' + b + ', ' + timestampToDatetime(blockchain[b].medianTime) + '\r\n');
calculatorsCount++;
}
tabBlockMembers[m].blockCount++; tabBlockMembers[m].blockCount++;
let nonce = parseInt((blockchain[b].nonce).toString().substr(3)); let nonce = parseInt((blockchain[b].nonce).toString().substr(3));
if (data == 'meanNonce') { tabBlockMembers[m].data += nonce; } if (data == 'meanNonce') { tabBlockMembers[m].data += nonce; }
...@@ -136,6 +156,24 @@ module.exports = (req, res, next) => co(function *() { ...@@ -136,6 +156,24 @@ module.exports = (req, res, next) => co(function *() {
} }
} }
// Close write stream into calculators_rank file
ws.end();
// Remove oldest calculators_rank file
let files = fs.readdirSync(pathToCalculatorsRankFile);
if (files.length > 10) {
let minTimestamp = parseInt(Date.now())
for (let file of files) {
let fileTimestamp = parseInt(file.split('_')[2])
if (fileTimestamp < minTimestamp) {
minTimestamp = fileTimestamp;
}
}
fs.unlink(pathToCalculatorsRankFile + 'calculators_rank_' + minTimestamp + '.csv',function(err){
if(err) return console.log(err);
});
}
// Delete non-significant nodes // Delete non-significant nodes
// A node is considered as significant if its blockCount represents more than 3 % of the total member blockCount // A node is considered as significant if its blockCount represents more than 3 % of the total member blockCount
var maxSignificantIdNode = 1; var maxSignificantIdNode = 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment