Skip to content

Commit

Permalink
perf(start): simplify some init statistic calls
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jan 8, 2019
1 parent e4db846 commit c788569
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
26 changes: 5 additions & 21 deletions src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,11 @@ module.exports = async ({
if(typeof callback != 'function')
return;

sphinx.query('SELECT count(*) AS torrents, sum(size) AS sz FROM `torrents`', function (error, rows, fields) {
if(!rows) {
logTE('statistic', error)
callback(undefined)
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)
})
});
callback({
torrents: p2p.info.torrents,
size: p2p.info.filesSize,
files: p2p.info.files
})
});

const onTorrent = (hash, options, callback) => {
Expand Down
49 changes: 40 additions & 9 deletions src/background/spider.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,44 @@ module.exports = function (send, recive, dataDirectory, version, env)
]

const sphinxSingle = await single().waitConnection()
torrentsId = (await sphinxSingle.query("SELECT MAX(`id`) as mx from torrents"))[0]
torrentsId = ((torrentsId && torrentsId.mx) || 0) + 1
filesId = (await sphinxSingle.query("SELECT MAX(`id`) as mx from files"))[0]
filesId = ((filesId && filesId.mx) || 0) + 1
p2p.info.torrents = (await sphinxSingle.query("SELECT COUNT(*) as cnt from torrents"))[0].cnt
p2p.info.files = await sphinxSingle.query("SELECT SUM(files) as cnt from torrents")
if(p2p.info.files && p2p.info.files.length > 0)
p2p.info.files = p2p.info.files[0].cnt
let torrentsInfo = await sphinxSingle.query(`
SELECT
MAX(id) as maxid,
COUNT(*) as torrentscount,
SUM(files) as numfiles,
SUM(size) as filessize
FROM torrents
`);
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
{
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
p2p.info.files = 0
{
filesId = 1;
}
const sphinxSingleAlternative = await single().waitConnection()


Expand Down Expand Up @@ -610,6 +638,9 @@ module.exports = function (send, recive, dataDirectory, version, env)
});
updateTorrentTrackers(torrent.hash);
remoteTrackers.update(torrent)
p2p.info.torrents++;
p2p.info.files += torrent.files;
p2p.info.filesSize += torrent.size;
}
else
{
Expand Down

0 comments on commit c788569

Please sign in to comment.