mysql and redis suff of php
<?php
$timeunits = array(
'_1m' => 60,
'_3m' => 60 * 3,
'_5m' => 60 * 5,
'_15m' => 60 * 15,
'_30m' => 60 * 30,
'_1h' => 60 * 60,
'_2h' => 60 * 60 * 2,
'_4h' => 60 * 60 * 4,
'_6h' => 60 * 60 * 6,
'_12h' => 60 * 60 * 12,
'_1d' => 60 * 60 * 24,
'_3d' => 60 * 60 * 24 * 3,
'_1w' => 60 * 60 * 24 * 7
);
$allMarkets = array('cnybtc', 'cnyltc', 'btcltc', 'usdbtc');
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
function printUsage(){
echo "Usage: \n";
}
if($argc != 4){
printUsage();
exit;
}
$market = $argv[1];
$timeunit = $argv[2];
if(!in_array($timeunit, array_keys($timeunits))){
printUsage();
exit;
}
if(!in_array($market, $allMarkets)){
printUsage();
exit;
}
$start = $argv[3];
$firstStartPoint = $redis->zRange('chartdata:'.$market.':'.$timeunit, 0, 0);
$firstStartPoint = $firstStartPoint[0];
echo "first start point";
var_dump($firstStartPoint);
$dbcon = mysql_connect("ds5.cfdsf45pttva.ap-northeast-1.rds.amazonaws.com","marketapi","1p8iKXh0WPwXY3U", false, MYSQL_CLIENT_SSL);
date_default_timezone_set('Asia/Shanghai');
if (!$dbcon)
{
die('Could not connect: ' . mysql_error());
}
function getModTime($time, $mod){
//echo "mod: ".$mod."\n";
return $time - $time % $mod;
}
function checkData($redis, $market, $timeunit, $startPoint){
global $timeunits;
global $dbcon;
global $firstStartPoint;
$startPoint = getModTime($startPoint, $timeunits[$timeunit]);
echo "----------------------------------------\n";
echo "start point: ".date('Y-n-j G:i:s', $startPoint)."\n";
if($startPoint < $firstStartPoint){
echo "earlier than first start point.\n";
return;
}
$endPoint = $startPoint + $timeunits[$timeunit];
echo "end point: ".date('Y-n-j G:i:s', $endPoint)."\n";
$currentTime = time();
if($currentTime <= $endPoint){
echo "current time slot is not ended yet\n";
return;
}
switch ($market) {
case 'cnybtc':
$tablename = 'trade';
break;
case 'cnyltc':
$tablename = 'trade_cnyltc';
break;
case 'btcltc':
$tablename = 'trade_btcltc';
break;
case 'usdbtc':
$tablename = 'trade_usdbtc';
break;
}
$key = 'chartdata:'.$market.':'.$timeunit.':'.$startPoint;
echo "key: ".$key."\n";
$redisdata = $redis->hGetAll($key);
echo "redis data: \n";
var_dump($redisdata);
mysql_select_db("btcchina_mk", $dbcon);
//get high low volume
$query = sprintf("SELECT max(price) as high, min(price) as low, sum(amount) as vol FROM `%s` WHERE dateline >= %s AND dateline < %s", $tablename, $startPoint, $endPoint);
$result = mysql_query($query);
$dbdata = array();
while($row = mysql_fetch_array($result))
{
$dbdata['high'] = $row['high'];
$dbdata['low'] = $row['low'];
$dbdata['vol'] = $row['vol'];
}
//get open close
$query = sprintf("SELECT price as open FROM `%s` WHERE dateline >= %s AND dateline < %s order by trade_id limit 1", $tablename, $startPoint, $endPoint);
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$dbdata['open'] = $row['open'];
}
$query = sprintf("SELECT price as close FROM `%s` WHERE dateline >= %s AND dateline < %s order by trade_id desc limit 1", $tablename, $startPoint, $endPoint);
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$dbdata['close'] = $row['close'];
}
echo "dbdata: \n";
var_dump($dbdata);
if($redisdata['high'] != $dbdata['high']){
echo "high mismatch\n";
$redis->hSet($key, 'high', $dbdata['high']);
}else if ($redisdata['low'] != $dbdata['low']){
echo "low mismatch\n";
$redis->hSet($key, 'low', $dbdata['low']);
}else if($redisdata['vol'] != $dbdata['vol']){
echo "vol mismatch\n";
$redis->hSet($key, 'vol', $dbdata['vol']);
}else if($redisdata['open'] != $dbdata['open']){
echo "open mismatch\n";
$redis->hSet($key, 'open', $dbdata['open']);
}else if($redisdata['close'] != $dbdata['close']){
echo "close mismatch\n";
$redis->hSet($key, 'close', $dbdata['close']);
}else{
echo "match\n";
}
}
$startPoint = getModTime($start, $timeunits[$timeunit]);
$currentTime = time();
while($startPoint < $currentTime){
checkData($redis, $market, $timeunit, $startPoint);
$startPoint = $startPoint + $timeunits[$timeunit];
}
$redis->close();
mysql_close($dbcon);