function parse_table($tablesource, $database)
{
    $q = new nginx_stats($database);
    $sql = "SELECT SUM(size) as tsize,COUNT(zmd5) as hits,\n\t\t\tDATE_FORMAT( zDate, '%Y-%m-%d' ) as zDate,\n\t\t\tHOUR(zDate) as hour,ipaddr,hostname,useragent,country,uri,proto,httpcode\n\t\t\tFROM {$tablesource}\n\t\t\tGROUP BY hour,ipaddr,hostname,useragent,country,uri,proto,httpcode,zDate\n\t\t\t";
    $results = $q->QUERY_SQL($sql);
    events("{$database}/{$tablesource} add " . mysql_num_rows($results) . " elements", __FUNCTION__, __LINE__);
    if (!$q->ok) {
        events($q->mysql_error, __FUNCTION__, __LINE__);
        return false;
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $date = $ligne["zDate"];
        $time = strtotime("{$date} 00:00:00");
        $tablename = "day_" . date("Ymd", $time);
        $zmd5 = md5(serialize($ligne));
        $hour = $ligne["hour"];
        $ipaddr = $ligne["ipaddr"];
        $hostname = mysql_escape_string2($ligne["hostname"]);
        $useragent = mysql_escape_string2($ligne["useragent"]);
        $country = mysql_escape_string2($ligne["country"]);
        $uri = mysql_escape_string2($ligne["uri"]);
        $size = $ligne["tsize"];
        $hits = $ligne["hits"];
        $proto = $ligne["proto"];
        $httpcode = $ligne["httpcode"];
        $line = "('{$zmd5}','{$date}','{$hour}','{$ipaddr}','{$hostname}','{$useragent}','{$country}','{$uri}','{$size}','{$hits}','{$proto}','{$httpcode}')";
        $f[$tablename][] = $line;
        if (count($f[$tablename]) > 500) {
            if (!parse_table_array($f, $database)) {
                return false;
            }
            $f = array();
        }
    }
    if (count($f) > 0) {
        if (!parse_table_array($f, $database)) {
            return false;
        }
    }
    return true;
}
function parse_table($tablesource, $database)
{
    $q = new nginx_stats($database);
    $sql = "SELECT SUM(size) as tsize,SUM(hits) as hits,\n\t\t\tDATE_FORMAT( zDate, '%d' ) as zDay,zDate,ipaddr,hostname,country\n\t\t\tFROM {$tablesource}\n\t\t\tGROUP BY zDay,zDate,ipaddr,hostname,country\n\t\t\t";
    $results = $q->QUERY_SQL($sql);
    events("{$database}/{$tablesource} add " . mysql_num_rows($results) . " elements", __FUNCTION__, __LINE__);
    if (!$q->ok) {
        events($q->mysql_error, __FUNCTION__, __LINE__);
        return false;
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $zDay = $ligne["zDay"];
        $time = strtotime("{$ligne["zDate"]} 00:00:00");
        $tablename = "month_" . date("Ym", $time);
        $zmd5 = md5(serialize($ligne));
        $ipaddr = $ligne["ipaddr"];
        $hostname = mysql_escape_string2($ligne["hostname"]);
        $country = mysql_escape_string2($ligne["country"]);
        $size = $ligne["size"];
        $hits = $ligne["hits"];
        $line = "('{$zmd5}','{$zDay}','{$ipaddr}','{$hostname}','{$country}','{$size}','{$hits}')";
        $f[$tablename][] = $line;
        if (count($f[$tablename]) > 500) {
            if (!parse_table_array($f, $database)) {
                return false;
            }
            $f = array();
        }
    }
    if (count($f) > 0) {
        if (!parse_table_array($f, $database)) {
            return false;
        }
    }
    return true;
}