perf(start): simplify some init statistic calls

This commit is contained in:
Alexey Kasyanchuk 2019-01-07 16:08:14 +03:00
parent 89a61f1505
commit 57883b0ba4
2 changed files with 45 additions and 30 deletions

View File

@ -117,27 +117,11 @@ module.exports = async ({
if(typeof callback != 'function') if(typeof callback != 'function')
return; return;
sphinx.query('SELECT count(*) AS torrents, sum(size) AS sz FROM `torrents`', function (error, rows, fields) { callback({
if(!rows) { torrents: p2p.info.torrents,
logTE('statistic', error) size: p2p.info.filesSize,
callback(undefined) files: p2p.info.files
return; })
}
let result = {torrents: rows[0].torrents || 0, size: rows[0].sz || 0}
sphinx.query('SELECT sum(files) AS flist FROM `torrents`', function (error, rows, fields) {
if(!rows) {
logTE('statistic', error)
callback(undefined)
return;
}
result.files = (rows[0] && rows[0].flist) || 0
callback(result)
})
});
}); });
const onTorrent = (hash, options, callback) => { const onTorrent = (hash, options, callback) => {

View File

@ -107,16 +107,44 @@ module.exports = function (send, recive, dataDirectory, version, env)
] ]
const sphinxSingle = await single().waitConnection() const sphinxSingle = await single().waitConnection()
torrentsId = (await sphinxSingle.query("SELECT MAX(`id`) as mx from torrents"))[0] let torrentsInfo = await sphinxSingle.query(`
torrentsId = ((torrentsId && torrentsId.mx) || 0) + 1 SELECT
filesId = (await sphinxSingle.query("SELECT MAX(`id`) as mx from files"))[0] MAX(id) as maxid,
filesId = ((filesId && filesId.mx) || 0) + 1 COUNT(*) as torrentscount,
p2p.info.torrents = (await sphinxSingle.query("SELECT COUNT(*) as cnt from torrents"))[0].cnt SUM(files) as numfiles,
p2p.info.files = await sphinxSingle.query("SELECT SUM(files) as cnt from torrents") SUM(size) as filessize
if(p2p.info.files && p2p.info.files.length > 0) FROM torrents
p2p.info.files = p2p.info.files[0].cnt `);
let filesInfo = await sphinxSingle.query(`
SELECT
MAX(id) as maxid
FROM files
`);
if(torrentsInfo && torrentsInfo[0])
{
torrentsInfo = torrentsInfo[0]
torrentsId = (torrentsInfo.maxid || 0) + 1
p2p.info.torrents = torrentsInfo.torrentscount || 0
p2p.info.files = torrentsInfo.numfiles || 0
p2p.info.filesSize = torrentsInfo.filessize || 0
}
else else
p2p.info.files = 0 {
torrentsId = 1;
p2p.info.torrents = 0;
p2p.info.files = 0;
p2p.info.filesSize = 0;
}
if(filesInfo && filesInfo[0])
{
filesInfo = filesInfo[0]
filesId = (filesInfo.maxid || 0) + 1
}
else
{
filesId = 1;
}
const sphinxSingleAlternative = await single().waitConnection() const sphinxSingleAlternative = await single().waitConnection()
@ -610,6 +638,9 @@ module.exports = function (send, recive, dataDirectory, version, env)
}); });
updateTorrentTrackers(torrent.hash); updateTorrentTrackers(torrent.hash);
remoteTrackers.update(torrent) remoteTrackers.update(torrent)
p2p.info.torrents++;
p2p.info.files += torrent.files;
p2p.info.filesSize += torrent.size;
} }
else else
{ {