From d8239466ce6ba592b471bde40c50b3a077107e44 Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Tue, 3 Jan 2017 06:14:23 +0300 Subject: [PATCH] =?UTF-8?q?=D1=83=D0=B2=D0=B5=D0=BB=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D0=BA=D0=BE=D1=80=D0=BE=D1=81=D1=82=D1=8C?= =?UTF-8?q?=20=D0=BF=D0=BE=D0=BB=D1=83=D1=87=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=BE=20=D1=80=D0=B0=D0=B7=D0=BC=D0=B5=D1=80=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- btsearch.sql | 25 ++++++++++++++++++++++++- index.js | 35 ++++++++++++++++++++++++++++------- src/recent-torrents.js | 5 ++++- src/search.js | 8 +++++++- 4 files changed, 63 insertions(+), 10 deletions(-) diff --git a/btsearch.sql b/btsearch.sql index dbf435e..5dd9755 100644 --- a/btsearch.sql +++ b/btsearch.sql @@ -40,6 +40,29 @@ LOCK TABLES `files` WRITE; /*!40000 ALTER TABLE `files` ENABLE KEYS */; UNLOCK TABLES; +-- +-- Table structure for table `statistic` +-- + +DROP TABLE IF EXISTS `statistic`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `statistic` ( + `size` bigint(20) unsigned DEFAULT NULL, + `files` bigint(20) unsigned DEFAULT NULL, + `torrents` int(10) unsigned DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `statistic` +-- + +LOCK TABLES `statistic` WRITE; +/*!40000 ALTER TABLE `statistic` DISABLE KEYS */; +/*!40000 ALTER TABLE `statistic` ENABLE KEYS */; +UNLOCK TABLES; + -- -- Table structure for table `torrents` -- @@ -79,4 +102,4 @@ UNLOCK TABLES; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2017-01-02 11:33:56 +-- Dump completed on 2017-01-03 6:03:58 diff --git a/index.js b/index.js index de4bb8c..6e1d2e9 100644 --- a/index.js +++ b/index.js @@ -74,6 +74,32 @@ app.get('*', function(req, res) res.sendfile(__dirname + '/build/index.html'); }); + +// start + +// обновление статистики +setInterval(() => { + let stats = {}; + socketMysql.query('SELECT COUNT(*) as tornum FROM `torrents`', function (error, rows, fields) { + stats.torrents = rows[0].tornum; + socketMysql.query('SELECT COUNT(*) as filesnum, SUM(`size`) as filesizes FROM `files`', function (error, rows, fields) { + stats.files = rows[0].filesnum; + stats.size = rows[0].filesizes; + io.sockets.emit('newStatistic', stats); + socketMysql.query('DELETE FROM `statistic`', function (err, result) { + if(!result) { + console.error(err); + } + socketMysql.query('INSERT INTO `statistic` SET ?', stats, function(err, result) { + if(!result) { + console.error(err); + } + }); + }) + }); + }); +}, 60 * 1000) + io.on('connection', function(socket) { function baseRowData(row) @@ -109,13 +135,8 @@ io.on('connection', function(socket) if(typeof callback != 'function') return; - let stats = {}; - socketMysql.query('SELECT COUNT(*) as tornum FROM `torrents`', function (error, rows, fields) { - stats.torrents = rows[0].tornum; - socketMysql.query('SELECT COUNT(*) as filesnum FROM `files`', function (error, rows, fields) { - stats.files = rows[0].filesnum; - callback(stats) - }); + socketMysql.query('SELECT * FROM `statistic`', function (error, rows, fields) { + callback(rows[0]) }); }); diff --git a/src/recent-torrents.js b/src/recent-torrents.js index 4560e1c..442a253 100644 --- a/src/recent-torrents.js +++ b/src/recent-torrents.js @@ -73,6 +73,9 @@ export default class RecentTorrents extends Component { }); } render() { + if(!this.torrents || this.torrents.length == 0) + return null; + return ( Most recent torrents @@ -82,7 +85,7 @@ export default class RecentTorrents extends Component { return ; }) } - + ); } } diff --git a/src/search.js b/src/search.js index e88bfdf..bd9a773 100644 --- a/src/search.js +++ b/src/search.js @@ -5,6 +5,8 @@ import TextField from 'material-ui/TextField'; import RaisedButton from 'material-ui/RaisedButton'; import RefreshIndicator from 'material-ui/RefreshIndicator'; +import formatBytes from './format-bytes' + export default class Search extends Component { constructor(props) { @@ -28,6 +30,10 @@ export default class Search extends Component { this.stats = statistic; this.forceUpdate(); }); + window.torrentSocket.on('newStatistic', (statistic) => { + this.stats = statistic; + this.forceUpdate(); + }); } render() { const style = { @@ -59,7 +65,7 @@ export default class Search extends Component { { this.stats ? -
we have {this.stats.torrents} torrents and around {this.stats.files} files here
+
we have information about {this.stats.torrents} torrents and around {this.stats.files} files and { formatBytes(this.stats.size, 1) } of data
: null }