<?php
/**
 * 每分钟执行一次
 */
if (substr(PHP_SAPI_NAME(), 0, 3) !== 'cli') {
    die("The program runs only in CLI mode!");
}
$timestamp = time();
$sid =$lid =1;
include_once dirname(__FILE__) . "/crontab.inc.php";
include_once dirname(__FILE__) . "/../com.php";
require_once dirname(__FILE__) . '/../vendor/autoload.php';

set_time_limit(0);
// G(小时，24 小时格式，没有前导零，0 到 23), i(有前导零的分钟数，00 到 59>)
$time   = date('Gi', $timestamp);
$day    = date("j", $timestamp); // 月份中的第几天，没有前导零(1到31)
$week   = date('w', $timestamp);//星期中的星期几 周日是0 周一是1 依次排下去
$minute = date('i', $timestamp);
define('CURRENT_TIMESTAMP', $timestamp);
/*****统计在线在玩 Start*****/
$onlineNum = oo::commonOprModel('recordonline')->getAliMonitor('onlineStatistics.php line:'.__LINE__);
oo::commonOprModel('recordonline')->setOnlenCount(timeFuns::todaySecond(), $onlineNum);//每分钟在线用户统计
if( $minute % 5 == 0 ){
    !IS_DEBUF && oo::commonOprModel('recordonline')->crazyRecord($timestamp, $onlineNum);
}

/*****统计Fox在线数*****/
oo::commonOprModel('recordonline')->saveFoxRealTimeOnline($timestamp, $onlineNum);

$warnConfig = [
    400 => 3, // 3次超过400 报警
    200 => 5, // 5次超过200 报警
];
krsort($warnConfig);
$date = date('Y-m-d H:i:s', CURRENT_TIMESTAMP);
$inactiveNum = oo::commonOprModel('recordonline')->getAliMonitor('onlineStatistics.php line:'.__LINE__, 0, false);
foreach ($warnConfig as $warnNum => $warnTimes){
    if($inactiveNum >= $warnNum){   // 非活跃连接数 大于 阈值
        $lastTimes = oo::commonOprRedis('statistics')->incr(okeys::inactiveWarnTimes($warnNum));    // 告警次数 +1
        if($lastTimes >= $warnTimes){// 告警次数 大于 阈值 发出推送 将告警次数清0
            oo::logs()->debug3(['msg' => "【警报】FOX {$date} :非活跃连接数连续 {$lastTimes} 次超过 {$inactiveNum} ."], 'warning.log');
            oo::commonOprModel('Enwechat')->send("【警报】FOX {$date} :非活跃连接数连续 {$lastTimes} 次超过 {$inactiveNum} .",[],[],1);
            oo::commonOprRedis('statistics')->expire(okeys::inactiveWarnTimes($warnNum), 0);
            break;
        }
    }else{// 非活跃连接数 小于 阈值 将告警次数清0
        oo::commonOprRedis('statistics')->expire(okeys::inactiveWarnTimes($warnNum), 0);
    }
}


die('end');
