history: prune database + better axis interval for 7 days

2 months kept in the database, and pruned by avg after 7 days
This commit is contained in:
Tanguy Pruvot 2016-04-30 12:58:07 +02:00
parent 6d2ab6efa1
commit b81a3701b4
3 changed files with 54 additions and 3 deletions

View file

@ -33,6 +33,8 @@ class MarketCommand extends CConsoleCommand
echo "Yiimp market command\n"; echo "Yiimp market command\n";
echo "Usage: yiimp market <SYM> list\n"; echo "Usage: yiimp market <SYM> list\n";
echo " yiimp market <SYM> histo <market>\n";
echo " yiimp market <SYM> prune\n";
return 1; return 1;
} else if ($args[1] == 'list') { } else if ($args[1] == 'list') {
@ -48,6 +50,12 @@ class MarketCommand extends CConsoleCommand
$this->queryMarketHistory($symbol, $market); $this->queryMarketHistory($symbol, $market);
return 0;
} else if ($args[1] == 'prune') {
marketHistoryPrune($symbol);
return 0; return 0;
} }
} }

View file

@ -44,8 +44,51 @@ function BackendQuickClean()
dborun("UPDATE blocks SET amount=0 WHERE category='orphan' AND amount>0"); 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() function BackendCleanDatabase()
{ {
marketHistoryPrune();
$delay = time() - 60*24*60*60; $delay = time() - 60*24*60*60;
// dborun("delete from blocks where time<$delay"); // dborun("delete from blocks where time<$delay");
dborun("delete from hashstats where time<$delay"); dborun("delete from hashstats where time<$delay");

View file

@ -96,7 +96,7 @@ function graph_price_data(data)
seriesDefaults: { seriesDefaults: {
xaxis: 'x2axis', xaxis: 'x2axis',
yaxis: 'y2axis', yaxis: 'y2axis',
markerOptions: { style: 'circle', size: 2 } markerOptions: { style: 'circle', size: 0.25 }
}, },
grid: { grid: {
@ -128,7 +128,7 @@ function graph_price_data(data)
// limit visible axis ticks // limit visible axis ticks
var x2ticks = graph.axes.x2axis._ticks; var x2ticks = graph.axes.x2axis._ticks;
graph.axes.xaxis.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; var label, day, lastDay;
for (var i=0; i < x2ticks.length; i++) { for (var i=0; i < x2ticks.length; i++) {
if (i % tickInterval == 0) { if (i % tickInterval == 0) {
@ -207,7 +207,7 @@ function graph_balance_data(data)
// limit visible axis ticks // limit visible axis ticks
var x2ticks = graph.axes.x2axis._ticks; var x2ticks = graph.axes.x2axis._ticks;
graph.axes.xaxis.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; var label, day, lastDay;
for (var i=0; i < x2ticks.length; i++) { for (var i=0; i < x2ticks.length; i++) {
if (i % tickInterval == 0) { if (i % tickInterval == 0) {