function BUILD_REPORT($md5)
{
    build_progress("{building_query}", 5);
    $unix = new unix();
    $q = new mysql_squid_builder();
    $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT * FROM reports_cache WHERE `zmd5`='{$md5}'"));
    $params = unserialize($ligne["params"]);
    $influx = new influx();
    $from = InfluxQueryFromUTC($params["FROM"]);
    $to = InfluxQueryFromUTC($params["TO"]);
    $interval = $params["INTERVAL"];
    $md5_table = $md5;
    $searchsites_sql = null;
    $USER_FIELD = $params["USER"];
    $SEARCH = $params["SEARCH"];
    if ($SEARCH != null) {
        $searchuser_sql = str_replace("*", "%", $SEARCH);
        if (strpos(" {$SEARCH}", "%") > 0) {
            $searchuser_sql = " HAVING {$USER_FIELD} LIKE '{$SEARCH}'";
        } else {
            $searchuser_sql = " HAVING `{$USER_FIELD}`='{$SEARCH}'";
        }
    }
    if (!GRAB_DATAS($ligne, $md5)) {
        build_progress("{unable_to_query_to_bigdata}", 110);
        return;
    }
    $q = new mysql_squid_builder();
    //zDate,familysite,user,size,hits
    $sql = "SELECT SUM(SIZE) as SIZE, SUM(RQS) as RQS,{$USER_FIELD},`ZDATE` FROM `tmp_{$md5}user` \n\tGROUP BY {$USER_FIELD},`ZDATE` {$searchuser_sql} ORDER BY `ZDATE`";
    echo "********** SQL **********\n";
    echo $sql . "\n";
    echo "********** SQL **********\n";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo "{$sql}\n{$q->mysql_error}\n";
        build_progress("MySQL error [" . __LINE__ . "]", 110);
        return;
    }
    $c = 0;
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $time = $ligne["ZDATE"];
        $c++;
        if (intval($ligne["SIZE"]) == 0) {
            continue;
        }
        $size = $ligne["SIZE"] / 1024;
        $size = round($size / 1024, 2);
        $xdata[] = $ligne["ZDATE"];
        $ydata[] = $size;
    }
    echo "Building chronology\n";
    $sql = "SELECT SUM(SIZE) as SIZE, SUM(RQS) as RQS,`ZDATE`,FAMILYSITE,{$USER_FIELD}\n\tFROM `tmp_{$md5}user` GROUP BY FAMILYSITE,{$USER_FIELD},ZDATE {$searchuser_sql} ORDER BY ZDATE";
    echo "********** SQL **********\n";
    echo $sql . "\n";
    echo "********** SQL **********\n";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo "{$sql}\n{$q->mysql_error}\n";
        build_progress("MySQL error [" . __LINE__ . "]", 110);
        return;
    }
    $c = 0;
    $MAIN["CSV"][] = array("time", "bytes", "site", "hits");
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $c++;
        $time = $ligne["ZDATE"];
        if ($GLOBALS["VERBOSE"]) {
            echo "CHRONOS: {$ligne["FAMILYSITE"]} {$time}'\n";
        }
        $MAIN["CRONOS"][] = array("TIME" => $time, "BYTES" => $ligne["SIZE"], "SITE" => $ligne["FAMILYSITE"], "RQS" => $ligne["RQS"]);
        $MAIN["CSV"][] = array("{$time}", "{$ligne["SIZE"]}", "{$ligne["FAMILYSITE"]}", "{$ligne["RQS"]}");
    }
    $MAIN["GRAPH1"]["xdata"] = $xdata;
    $MAIN["GRAPH1"]["ydata"] = $ydata;
    if ($c == 0) {
        build_progress("{chronology}: {failed} C == 0", 110);
        return;
    }
    //----------------------------------------------------------------------------------------------------------
    $results = $q->QUERY_SQL("SELECT USERID as uid,IPADDR as ipaddr,MAC as mac FROM `tmp_{$md5}user` GROUP BY uid,ipaddr,mac");
    build_progress("{parsing_data} (2)", 25);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $IDENT[] = array("IPADDR" => $ligne["ipaddr"], "USERID" => $ligne["uid"], "MAC" => $ligne["mac"]);
    }
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(SIZE) as SIZE,FAMILYSITE,{$USER_FIELD} FROM `tmp_{$md5}user` \n\tGROUP BY FAMILYSITE,{$USER_FIELD} {$searchuser_sql} ORDER BY SIZE DESC";
    echo "********** SQL **********\n";
    echo $sql . "\n";
    echo "********** SQL **********\n";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data} (2)", 30);
    if (!$q->ok) {
        echo "{$sql}\n{$q->mysql_error}\n";
        build_progress("MySQL error [" . __LINE__ . "]", 110);
        return;
    }
    $tt = 0;
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $size = $ligne["SIZE"];
        $FAMILYSITE = $ligne["FAMILYSITE"];
        $size = $size / 1024;
        $PieData2[$FAMILYSITE] = $size;
        if ($GLOBALS["VERBOSE"]) {
            echo "PIE DATA: {$FAMILYSITE}' -> {$size}\n";
        }
        if ($tt < 11) {
            $MAIN["GRAPH2"]["PIEDATA"][$FAMILYSITE] = $size;
            $MAIN["GRAPH2"]["TABLE"][$FAMILYSITE] = $size;
        }
        $tt++;
        $MAIN["FAMS"][$FAMILYSITE] = $ligne["SIZE"];
    }
    //----------------------------------------------------------------------------------------------------------
    echo "MD5:{$GLOBALS["zMD5"]}\n";
    REMOVE_TABLES($GLOBALS["zMD5"]);
    $encoded_data = base64_encode(serialize($MAIN));
    $datasize = strlen($encoded_data);
    echo "Saving " . strlen($encoded_data) . " bytes...\n";
    $q->QUERY_SQL("UPDATE reports_cache SET `builded`=1,`values`='{$encoded_data}',`values_size`='{$datasize}' WHERE `zmd5`='{$GLOBALS["zMD5"]}'");
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("MySQL {failed}", 110);
        return;
    }
    build_progress("{success}", 100);
}
function BUILD_REPORT($md5)
{
    build_progress("{building_query}", 5);
    $unix = new unix();
    $q = new mysql_squid_builder();
    $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT * FROM reports_cache WHERE `zmd5`='{$md5}'"));
    $params = unserialize($ligne["params"]);
    $influx = new influx();
    $to = InfluxQueryFromUTC($params["TO"]);
    $from = InfluxQueryFromUTC($params["FROM"]);
    $interval = $params["INTERVAL"];
    $user = $params["USER"];
    $md5_table = $md5;
    if (!GRAB_DATAS($ligne, $md5)) {
        build_progress("{unable_to_query_to_bigdata}", 110);
    }
    $q = new mysql_squid_builder();
    $per["10m"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1h"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1d"] = "DATE_FORMAT(zDate,'%m-%d') as tdate";
    $per["1w"] = "DATE_FORMAT(zDate,'%U') as tdate";
    $per["30d"] = "DATE_FORMAT(zDate,'%m') as tdate";
    $datformat = $per[$interval];
    $results = $q->QUERY_SQL("SELECT user,size FROM `{$md5}user` ORDER BY size DESC");
    build_progress("{parsing_data} (2)", 25);
    $c = 0;
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $USER = $ligne["user"];
        $SIZE = $ligne["size"];
        $BIGDATA[$USER] = $SIZE;
        $c++;
    }
    build_progress("{$c} {rows}", 8);
    echo "{$c} rows....\n";
    REMOVE_TABLES($md5);
    $encoded_data = base64_encode(serialize($BIGDATA));
    $datasize = strlen($encoded_data);
    echo "Saving " . strlen($encoded_data) . " bytes...\n";
    $q->QUERY_SQL("UPDATE reports_cache SET `builded`=1,`values`='{$encoded_data}',`values_size`='{$datasize}' WHERE `zmd5`='{$GLOBALS["zMD5"]}'");
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("MySQL {failed}", 110);
        return;
    }
    build_progress("{success}", 100);
}
function xsFLOW($md5)
{
    build_progress("{building_query}", 5);
    $unix = new unix();
    $q = new mysql_squid_builder();
    $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT * FROM reports_cache WHERE `zmd5`='{$md5}'"));
    $params = unserialize($ligne["params"]);
    $influx = new influx();
    $from = InfluxQueryFromUTC($params["FROM"]);
    $to = InfluxQueryFromUTC($params["TO"]);
    $interval = $params["INTERVAL"];
    $user = $params["USER"];
    $md5_table = $md5;
    if (!GRAB_DATAS($ligne, $md5)) {
        build_progress("{unable_to_query_to_bigdata}", 110);
    }
    $q = new mysql_squid_builder();
    $per["10m"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1h"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1d"] = "DATE_FORMAT(zDate,'%m-%d') as tdate";
    $per["1w"] = "DATE_FORMAT(zDate,'%U') as tdate";
    $per["30d"] = "DATE_FORMAT(zDate,'%m') as tdate";
    $datformat = $per[$interval];
    $sql = "SELECT SUM(size) as size,{$datformat} FROM `{$md5}sites` GROUP BY tdate ORDER BY tdate";
    echo "{$sql}\n";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data} (2)", 25);
    $c = 0;
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $size = $ligne["size"] / 1024;
        $size = round($size / 1024);
        if ($GLOBALS["VERBOSE"]) {
            echo "{$ligne["tdate"]} = {$size}MB\n";
        }
        if ($size == 0) {
            continue;
        }
        $c++;
        $xdata[] = $ligne["tdate"];
        $ydata[] = $size;
    }
    build_progress("{$c} {rows}", 8);
    echo "{$c} rows....\n";
    if (count($xdata) < 2) {
        $q->QUERY_SQL("DROP TABLE `{$md5}sites`");
        build_progress("{$c} {rows} ({only})", 110);
        REMOVE_TABLES($md5);
        return;
    }
    $time = time();
    $MAIN["GRAPH1"]["xdata"] = $xdata;
    $MAIN["GRAPH1"]["ydata"] = $ydata;
    $xdata = array();
    $ydata = array();
    $sql = "SELECT SUM(size) as size,familysite FROM `{$md5}sites` GROUP BY familysite ORDER BY size DESC LIMIT 0,10";
    echo "{$sql}\n";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data} (2)", 30);
    $c = 0;
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $size = $ligne["size"];
        $FAMILYSITE = $ligne["familysite"];
        $PieData2[$FAMILYSITE] = $size;
        $size = $size / 1024;
        $size = round($size / 1024, 2);
        $PieData[$FAMILYSITE] = $size;
        $c++;
    }
    $MAIN["GRAPH2"]["PIEDATA"] = $PieData;
    $MAIN["GRAPH2"]["TABLE"] = $PieData2;
    build_progress("{saving}", 50);
    if ($GLOBALS["zMD5"] == null) {
        build_progress("MD5 - > NULL {failed}", 110);
        return;
    }
    build_progress("{$c} {rows}", 60);
    build_progress("{building_query} {$user} (3)", 70);
    $results = $q->QUERY_SQL("SELECT user,size FROM `{$md5}users` ORDER BY size DESC LIMIT 0,20");
    build_progress("{parsing_data} {$user} (3)", 90);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $size = $ligne["size"];
        $FAMILYSITE = $ligne["user"];
        $PieData4[$FAMILYSITE] = $size;
        $size = $size / 1024;
        $size = round($size / 1024, 2);
        $PieData3[$FAMILYSITE] = $size;
    }
    $MAIN["GRAPH3"]["PIEDATA"] = $PieData3;
    $MAIN["GRAPH3"]["TABLE"] = $PieData4;
    $MAIN["GRAPH3"]["TYPE"] = $user;
    $MAIN["CSV1"] = $GLOBALS["CSV1"];
    $MAIN["CSV2"] = $GLOBALS["CSV2"];
    echo "MD5:{$GLOBALS["zMD5"]}\n";
    REMOVE_TABLES($md5);
    $encoded_data = base64_encode(serialize($MAIN));
    $datasize = strlen($encoded_data);
    echo "Saving " . strlen($encoded_data) . " bytes...\n";
    $q->QUERY_SQL("UPDATE reports_cache SET `builded`=1,`values`='{$encoded_data}',`values_size`='{$datasize}' WHERE `zmd5`='{$GLOBALS["zMD5"]}'");
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("MySQL {failed}", 110);
        return;
    }
    build_progress("{success}", 100);
}
function BUILD_REPORT($md5)
{
    build_progress("{building_query}", 5);
    $unix = new unix();
    $q = new mysql_squid_builder();
    $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT * FROM reports_cache WHERE `zmd5`='{$md5}'"));
    $params = unserialize($ligne["params"]);
    $influx = new influx();
    $from = InfluxQueryFromUTC($params["FROM"]);
    $to = InfluxQueryFromUTC($params["TO"]);
    $interval = $params["INTERVAL"];
    $user = $params["USER"];
    $md5_table = $md5;
    if (!GRAB_DATAS($ligne, $md5)) {
        build_progress("{unable_to_query_to_bigdata}", 110);
        return;
    }
    $q = new mysql_squid_builder();
    $per["10m"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1h"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1d"] = "DATE_FORMAT(zDate,'%m-%d') as tdate";
    $per["1w"] = "DATE_FORMAT(zDate,'%U') as tdate";
    $per["30d"] = "DATE_FORMAT(zDate,'%m') as tdate";
    $datformat = $per[$interval];
    //----------------------------------------------------------------------------------------------------------
    $results = $q->QUERY_SQL("SELECT SUM(size) as size,familysite FROM `{$md5}user` GROUP BY familysite ORDER BY size DESC LIMIT 0,10");
    build_progress("{parsing_data} (2)", 25);
    $c = 0;
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $size = $ligne["size"];
        $size = round($size / 1024);
        $c++;
        $FAMILYSITE = $ligne["familysite"];
        $TOP_WEBSITES_SIZE[$FAMILYSITE] = $size;
    }
    build_progress("{$c} {rows}", 30);
    //----------------------------------------------------------------------------------------------------------
    $results = $q->QUERY_SQL("SELECT SUM(hits) as hits,familysite FROM `{$md5}user` GROUP BY familysite ORDER BY hits DESC LIMIT 0,10");
    build_progress("{parsing_data} (2)", 25);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $hits = $ligne["hits"];
        $FAMILYSITE = $ligne["familysite"];
        $TOP_WEBSITES_HITS[$FAMILYSITE] = $hits;
    }
    //----------------------------------------------------------------------------------------------------------
    $results = $q->QUERY_SQL("SELECT SUM(size) as size,{$user} FROM `{$md5}user` GROUP BY {$user} ORDER BY size DESC LIMIT 0,10");
    build_progress("{parsing_data} (2)", 25);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $size = $ligne["size"];
        $size = round($size / 1024);
        $USER = $ligne[$user];
        echo "USER: {$USER} (" . FormatBytes($size) . ")\n";
        $TOP_WEBSITES_MEMBERS[$USER] = $size;
    }
    //----------------------------------------------------------------------------------------------------------
    $MAIN["CSV"] = $GLOBALS["CSV1"];
    $MAIN["TOP_WEBSITES_SIZE"] = $TOP_WEBSITES_SIZE;
    $MAIN["TOP_WEBSITES_MEMBERS"] = $TOP_WEBSITES_MEMBERS;
    $MAIN["TOP_WEBSITES_HITS"] = $TOP_WEBSITES_HITS;
    echo "MD5:{$GLOBALS["zMD5"]}\n";
    REMOVE_TABLES($GLOBALS["zMD5"]);
    $encoded_data = base64_encode(serialize($MAIN));
    $datasize = strlen($encoded_data);
    echo "Saving " . strlen($encoded_data) . " bytes...\n";
    $q->QUERY_SQL("UPDATE reports_cache SET `builded`=1,`values`='{$encoded_data}',`values_size`='{$datasize}' WHERE `zmd5`='{$GLOBALS["zMD5"]}'");
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("MySQL {failed}", 110);
        return;
    }
    build_progress("{success}", 100);
}
function BUILD_REPORT($md5)
{
    build_progress("{building_query}", 5);
    $unix = new unix();
    $q = new mysql_squid_builder();
    $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT * FROM reports_cache WHERE `zmd5`='{$md5}'"));
    $params = unserialize($ligne["params"]);
    $influx = new influx();
    $from = InfluxQueryFromUTC($params["FROM"]);
    $to = InfluxQueryFromUTC($params["TO"]);
    $interval = $params["INTERVAL"];
    $user = $params["USER"];
    $md5_table = $md5;
    if (!GRAB_DATAS($ligne, $md5)) {
        build_progress("{unable_to_query_to_bigdata}", 110);
        return;
    }
    $q = new mysql_squid_builder();
    $per["10m"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1h"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1d"] = "DATE_FORMAT(zDate,'%m-%d') as tdate";
    $per["1w"] = "DATE_FORMAT(zDate,'%U') as tdate";
    $per["30d"] = "DATE_FORMAT(zDate,'%m') as tdate";
    $datformat = $per[$interval];
    $md5_table = "`{$md5}user`";
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(hits) as hits,UID,CLIENT FROM {$md5_table} GROUP BY UID,CLIENT ORDER by hits DESC LIMIT 0,15";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data} (2)", 15);
    $c = 0;
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $c++;
        $MAIN["GRAPH1"][$ligne["UID"] . "/" . $ligne["CLIENT"]] = $ligne["hits"];
    }
    build_progress("{$c} {rows}", 30);
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(hits) as hits,CATEGORY FROM {$md5_table} GROUP BY CATEGORY ORDER by hits DESC LIMIT 0,15";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data} (2)", 15);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $MAIN["GRAPH2"][$ligne["CATEGORY"]] = $ligne["hits"];
    }
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(hits) as hits,RULENAME FROM {$md5_table} GROUP BY RULENAME ORDER by hits DESC LIMIT 0,15";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data} (2)", 15);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $MAIN["GRAPH3"][$ligne["RULENAME"]] = $ligne["hits"];
    }
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(hits) as hits,WEBSITE FROM {$md5_table} GROUP BY WEBSITE ORDER by hits DESC LIMIT 0,15";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data} (2)", 15);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $MAIN["GRAPH4"][$ligne["WEBSITE"]] = $ligne["hits"];
    }
    //----------------------------------------------------------------------------------------------------------
    $MAIN["csv"] = $GLOBALS["CSV1"];
    echo "MD5:{$GLOBALS["zMD5"]}\n";
    REMOVE_TABLES($GLOBALS["zMD5"]);
    $encoded_data = base64_encode(serialize($MAIN));
    $datasize = strlen($encoded_data);
    echo "Saving " . strlen($encoded_data) . " bytes...\n";
    $q->QUERY_SQL("UPDATE reports_cache SET `builded`=1,`values`='{$encoded_data}',`values_size`='{$datasize}' WHERE `zmd5`='{$GLOBALS["zMD5"]}'");
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("MySQL {failed}", 110);
        return;
    }
    build_progress("{success}", 100);
}
function BUILD_REPORT($md5)
{
    build_progress("{building_query}", 5);
    $unix = new unix();
    $q = new mysql_squid_builder();
    $ligne = mysql_fetch_array($q->QUERY_SQL("SELECT * FROM reports_cache WHERE `zmd5`='{$md5}'"));
    $params = unserialize($ligne["params"]);
    $influx = new influx();
    $from = InfluxQueryFromUTC($params["FROM"]);
    $to = InfluxQueryFromUTC($params["TO"]);
    $interval = $params["INTERVAL"];
    $user = $params["USER"];
    $md5_table = $md5;
    if (!GRAB_DATAS($ligne, $md5)) {
        build_progress("{unable_to_query_to_bigdata}", 110);
        return;
    }
    $q = new mysql_squid_builder();
    $per["10m"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1h"] = "DATE_FORMAT(zDate,'%m-%d %Hh') as tdate";
    $per["1d"] = "DATE_FORMAT(zDate,'%m-%d') as tdate";
    $per["1w"] = "DATE_FORMAT(zDate,'%U') as tdate";
    $per["30d"] = "DATE_FORMAT(zDate,'%m') as tdate";
    $datformat = $per[$interval];
    $md5_table = "`{$md5}user`";
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT {$datformat},SUM(size) as size FROM {$md5_table} GROUP BY tdate ORDER by tdate";
    build_progress("{parsing_data} (1)", 7);
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("{parsing_data} (1) {failed}", 110);
        REMOVE_TABLES($md5);
        die;
    }
    $c = 0;
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $ligne["size"] = round($ligne["size"] / 1024);
        $MAIN["GRAPH0"]["ydata"][] = $ligne["size"];
        $MAIN["GRAPH0"]["xdata"][] = $ligne["tdate"];
        $c++;
    }
    build_progress("{$c} {rows}", 30);
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT {$datformat},SUM(hits) as hits FROM {$md5_table} GROUP BY zdate ORDER by tdate";
    build_progress("{parsing_data}", 35);
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("{parsing_data} {failed}", 110);
        REMOVE_TABLES($md5);
        die;
    }
    if (mysql_num_rows($results) == 0) {
        build_progress("{parsing_data} {no_data}", 7);
        echo $sql . "\n";
        sleep(10);
        REMOVE_TABLES($md5);
        build_progress("{parsing_data} {failed}", 110);
    }
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $MAIN["GRAPH1"]["ydata"][] = $ligne["hits"];
        $MAIN["GRAPH1"]["xdata"][] = $ligne["tdate"];
    }
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(size) as size,familysite FROM {$md5_table} GROUP BY familysite ORDER by size DESC LIMIT 0,15";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("{parsing_data} {failed}", 110);
        REMOVE_TABLES($md5);
        die;
    }
    if (mysql_num_rows($results) == 0) {
        build_progress("{parsing_data} {no_data}", 7);
        echo $sql . "\n";
        sleep(10);
        REMOVE_TABLES($md5);
        build_progress("{parsing_data} {failed}", 110);
    }
    build_progress("{parsing_data}", 40);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $ligne["size"] = round($ligne["size"] / 1024);
        $MAIN["GRAPH2"][$ligne["familysite"]] = $ligne["size"];
    }
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(size) as size,{$user} FROM {$md5_table} GROUP BY {$user} ORDER by size DESC LIMIT 0,15";
    $results = $q->QUERY_SQL($sql);
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("{parsing_data} {failed}", 110);
        REMOVE_TABLES($md5);
        die;
    }
    if (mysql_num_rows($results) == 0) {
        build_progress("{parsing_data} {no_data}", 7);
        echo $sql . "\n";
        sleep(10);
        REMOVE_TABLES($md5);
        build_progress("{parsing_data} {failed}", 110);
    }
    build_progress("{parsing_data}", 45);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $ligne["size"] = round($ligne["size"] / 1024);
        $MAIN["GRAPH3"][$ligne[$user]] = $ligne["size"];
    }
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(hits) as hits,category FROM {$md5_table} GROUP BY category ORDER by hits DESC LIMIT 0,15";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data}", 50);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $category = $ligne["category"];
        if ($category == null) {
            $category = "unknown";
        }
        $MAIN["GRAPH4"][$category] = $ligne["hits"];
    }
    //----------------------------------------------------------------------------------------------------------
    $sql = "SELECT SUM(size) as size,category FROM {$md5_table} GROUP BY category ORDER by size DESC LIMIT 0,15";
    $results = $q->QUERY_SQL($sql);
    build_progress("{parsing_data}", 55);
    while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) {
        $category = $ligne["category"];
        $ligne["size"] = $ligne["size"] / 1024;
        if ($category == null) {
            $category = "unknown";
        }
        $MAIN["GRAPH5"][$category] = $ligne["size"];
    }
    //----------------------------------------------------------------------------------------------------------
    $MAIN["csv"] = $GLOBALS["CSV1"];
    echo "MD5:{$GLOBALS["zMD5"]}\n";
    REMOVE_TABLES($GLOBALS["zMD5"]);
    $encoded_data = base64_encode(serialize($MAIN));
    $datasize = strlen($encoded_data);
    echo "Saving " . strlen($encoded_data) . " bytes...\n";
    $q->QUERY_SQL("UPDATE reports_cache SET `builded`=1,`values`='{$encoded_data}',`values_size`='{$datasize}' WHERE `zmd5`='{$GLOBALS["zMD5"]}'");
    if (!$q->ok) {
        echo $q->mysql_error . "\n";
        build_progress("MySQL {failed}", 110);
        return;
    }
    build_progress("{success}", 100);
}