From b81a3701b4d603eb3323744f52ab244b3bc96498 Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Sat, 30 Apr 2016 12:58:07 +0200 Subject: [PATCH] history: prune database + better axis interval for 7 days 2 months kept in the database, and pruned by avg after 7 days --- web/yaamp/commands/MarketCommand.php | 8 ++++ web/yaamp/core/backend/system.php | 43 ++++++++++++++++++++ web/yaamp/modules/site/coin_market_graph.php | 6 +-- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/web/yaamp/commands/MarketCommand.php b/web/yaamp/commands/MarketCommand.php index 7234d25..0203d38 100644 --- a/web/yaamp/commands/MarketCommand.php +++ b/web/yaamp/commands/MarketCommand.php @@ -33,6 +33,8 @@ class MarketCommand extends CConsoleCommand echo "Yiimp market command\n"; echo "Usage: yiimp market list\n"; + echo " yiimp market histo \n"; + echo " yiimp market prune\n"; return 1; } else if ($args[1] == 'list') { @@ -48,6 +50,12 @@ class MarketCommand extends CConsoleCommand $this->queryMarketHistory($symbol, $market); + return 0; + + } else if ($args[1] == 'prune') { + + marketHistoryPrune($symbol); + return 0; } } diff --git a/web/yaamp/core/backend/system.php b/web/yaamp/core/backend/system.php index 576d84e..c4d26fd 100644 --- a/web/yaamp/core/backend/system.php +++ b/web/yaamp/core/backend/system.php @@ -44,8 +44,51 @@ function BackendQuickClean() dborun("UPDATE blocks SET amount=0 WHERE category='orphan' AND amount>0"); } +function marketHistoryPrune($symbol="") +{ + $delay2M = time() - 61*24*60*60; // 2 months + dborun("DELETE FROM market_history WHERE time < $delay2M"); + + // Prune records older than 1 week, one max per hour + $delay7D = time() - 7*24*60*60; + $sqlFilter = (!empty($symbol)) ? "AND C.symbol='$symbol'" : ''; + $prune = dbolist("SELECT idcoin, idmarket, + AVG(MH.price) AS price, AVG(MH.price2) AS price2, MAX(MH.balance) AS balance, + MIN(MH.id) AS firstid, COUNT(MH.id) AS nbrecords, ((MH.time + 3500) DIV 3600) AS ival + FROM market_history MH + INNER JOIN coins C ON C.id = MH.idcoin + WHERE MH.time < $delay7D $sqlFilter + GROUP BY MH.idcoin, MH.idmarket, ival + HAVING nbrecords > 1"); + + $nbDel = 0; $nbUpd = 0; + foreach ($prune as $row) { + if (empty($row['idmarket'])) + $sqlFilter = "idcoin=:idcoin AND idmarket IS NULL"; + else + $sqlFilter = "idcoin=:idcoin AND idmarket=".intval($row['idmarket']); + + $nbDel += dborun("DELETE FROM market_history WHERE $sqlFilter AND id != :firstid + AND ((time + 3500) DIV 3600) = :interval", array( + ':idcoin' => $row['idcoin'], + ':interval'=> $row['ival'], + ':firstid' => $row['firstid'], + )); + + $nbUpd += dborun("UPDATE market_history SET balance=:balance, price=:price, price2=:price2 + WHERE id=:firstid", array( + ':balance' => $row['balance'], + ':price' => $row['price'], ':price2' => $row['price2'], + ':firstid' => $row['firstid'], + )); + } + if ($nbDel) debuglog("history: $nbDel records pruned, $nbUpd updated $symbol"); +} + function BackendCleanDatabase() { + marketHistoryPrune(); + $delay = time() - 60*24*60*60; // dborun("delete from blocks where time<$delay"); dborun("delete from hashstats where time<$delay"); diff --git a/web/yaamp/modules/site/coin_market_graph.php b/web/yaamp/modules/site/coin_market_graph.php index b563fd2..8d92544 100644 --- a/web/yaamp/modules/site/coin_market_graph.php +++ b/web/yaamp/modules/site/coin_market_graph.php @@ -96,7 +96,7 @@ function graph_price_data(data) seriesDefaults: { xaxis: 'x2axis', yaxis: 'y2axis', - markerOptions: { style: 'circle', size: 2 } + markerOptions: { style: 'circle', size: 0.25 } }, grid: { @@ -128,7 +128,7 @@ function graph_price_data(data) // limit visible axis ticks var x2ticks = graph.axes.x2axis._ticks; graph.axes.xaxis.ticks = []; - var tickInterval = graph.grid._width > 0 ? Math.round(60*300 / graph.grid._width, 0) : 1; + var tickInterval = graph.grid._width > 0 ? Math.round(90*300 / graph.grid._width, 0) : 1; var label, day, lastDay; for (var i=0; i < x2ticks.length; i++) { if (i % tickInterval == 0) { @@ -207,7 +207,7 @@ function graph_balance_data(data) // limit visible axis ticks var x2ticks = graph.axes.x2axis._ticks; graph.axes.xaxis.ticks = []; - var tickInterval = graph.grid._width > 0 ? Math.round(60*300 / graph.grid._width, 0) : 1; + var tickInterval = graph.grid._width > 0 ? Math.round(90*300 / graph.grid._width, 0) : 1; var label, day, lastDay; for (var i=0; i < x2ticks.length; i++) { if (i % tickInterval == 0) {