function tables_hours()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid";
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($GLOBALS["VERBOSE"]) {
        echo "timefile={$timefile}\n";
    }
    $pid = @file_get_contents($pidfile);
    if (!$GLOBALS["FORCE"]) {
        if ($pid < 100) {
            $pid = null;
        }
        $unix = new unix();
        if ($unix->process_exists($pid, basename(__FILE__))) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Already executed pid {$pid}\n";
            }
            return;
        }
        $timeexec = $unix->file_time_min($timefile);
        if ($timeexec < 60) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Only each 60mn - current {$timeexec}mn, use --force to bypass\n";
            }
            return;
        }
        $mypid = getmypid();
        @file_put_contents($pidfile, $mypid);
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $sock = new sockets();
    $GLOBALS["Q"] = new mysql_squid_builder();
    $prefix = date("YmdH");
    $currenttable = "ngixattck_{$prefix}";
    if ($GLOBALS["VERBOSE"]) {
        echo "Current Table: {$currenttable}\n";
    }
    $tablesBrutes = $GLOBALS["Q"]->LIST_TABLES_NGINX_BLOCKED_RT();
    while (list($tablename, $none) = each($tablesBrutes)) {
        if ($tablename == $currenttable) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Skip table: {$tablename}\n";
            }
            continue;
        }
        $t = time();
        if ($GLOBALS["VERBOSE"]) {
            echo "_table_hours_perform({$tablename})\n";
        }
        if (_table_hours_perform($tablename)) {
            $took = $unix->distanceOfTimeInWords($t, time());
            if ($GLOBALS["VERBOSE"]) {
                echo "Remove table: {$tablename}\n";
            }
            $GLOBALS["Q"]->QUERY_SQL("DROP TABLE `{$tablename}`");
            if (system_is_overloaded()) {
                ufdbguard_admin_events("Fatal: Overloaded system: {$GLOBALS["SYSTEM_INTERNAL_LOAD"]} sleeping stopping function", __FUNCTION__, __FILE__, __LINE__, "stats");
                return true;
            }
        }
    }
}
function tables_hours()
{
    $unix = new unix();
    $pidfile = "/etc/artica-postfix/pids/exec.squid.stats.hours.php.tables_hours.pid";
    $timefile = "/etc/artica-postfix/pids/exec.squid.stats.hours.php.tables_hours.time";
    $RepairTimefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".Repair.time";
    if ($GLOBALS["VERBOSE"]) {
        echo "timefile={$timefile}\n";
    }
    $pid = @file_get_contents($pidfile);
    if (!$GLOBALS["FORCE"]) {
        if ($pid < 100) {
            $pid = null;
        }
        $unix = new unix();
        if ($unix->process_exists($pid, basename(__FILE__))) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Already executed pid {$pid}\n";
            }
            return;
        }
        $timeexec = $unix->file_time_min($timefile);
        if ($timeexec < 60) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Only each 60mn - current {$timeexec}mn, use --force to bypass\n";
            }
            return;
        }
        $mypid = getmypid();
        @file_put_contents($pidfile, $mypid);
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $sock = new sockets();
    $EnableImportOldSquid = $sock->GET_INFO("EnableImportOldSquid");
    $ArticaProxyStatisticsBackHourTables = $sock->GET_INFO("ArticaProxyStatisticsBackHourTables");
    if (!is_numeric($EnableImportOldSquid)) {
        $EnableImportOldSquid = 0;
    }
    if (!is_numeric($ArticaProxyStatisticsBackHourTables)) {
        $ArticaProxyStatisticsBackHourTables = 1;
    }
    if ($EnableImportOldSquid == 1) {
        if (!CheckIfAllImported()) {
            return false;
        }
    }
    $GLOBALS["Q"] = new mysql_squid_builder();
    $prefix = date("YmdH");
    if ($GLOBALS["VERBOSE"]) {
        echo "**********\nsquid_stats_tools->check_sizehours() in line " . __LINE__ . "\n**********\n";
    }
    $squid_stats_tools = new squid_stats_tools();
    $squid_stats_tools->check_sizehours();
    $squid_stats_tools->check_cachehours();
    if ($GLOBALS["VERBOSE"]) {
        echo "**********\nDone\n**********\n";
    }
    $currenttable = "squidhour_{$prefix}";
    if ($GLOBALS["VERBOSE"]) {
        echo "Current Table: {$currenttable}\n";
    }
    $tablesBrutes = $GLOBALS["Q"]->LIST_TABLES_WORKSHOURS();
    while (list($tablename, $none) = each($tablesBrutes)) {
        if ($tablename == $currenttable) {
            if ($GLOBALS["VERBOSE"]) {
                echo "Skip table: {$tablename}\n";
            }
            continue;
        }
        $t = time();
        $q = new mysql_squid_builder();
        if ($GLOBALS["VERBOSE"]) {
            echo "_table_hours_perform({$tablename})\n";
        }
        ToSyslog("Parsing table `{$tablename}`");
        if (_table_hours_perform($tablename)) {
            $Entries = FormatNumber($q->COUNT_ROWS($tablename));
            $took = $unix->distanceOfTimeInWords($t, time());
            ToSyslog("Parsing table `{$tablename}` took: {$took}");
            if ($GLOBALS["VERBOSE"]) {
                echo "Remove table: {$tablename}\n";
            }
            if ($Entries > 0) {
                stats_admin_events(2, "{$tablename}, {$Entries} entrie(s) Took {$took}", "", __FILE__, __LINE__);
            }
            if ($ArticaProxyStatisticsBackHourTables == 1) {
                backup_hourly_table($tablename);
            }
            $GLOBALS["Q"]->QUERY_SQL("DROP TABLE `{$tablename}`");
        }
    }
    $q = new mysql_squid_builder();
    $q->QUERY_SQL("FLUSH TABLES");
    $RepairTime = $unix->file_time_min($RepairTimefile);
    if ($RepairTime > 180) {
        $squid_stats_tools = new squid_stats_tools();
        $squid_stats_tools->NoCategorize = true;
        $squid_stats_tools->check_to_hour_tables();
        @unlink($RepairTimefile);
        @file_put_contents($RepairTimefile, time());
    }
}