2012-11-30 41 views
1

設想一個呼叫中心不能被客戶每分鐘超過2次呼叫淹沒。因此,該範圍之外的任何人都將獲得電子郵件支持鏈接($ bTrigger = FALSE)。其他人($ bTrigger = TRUE)將獲得技術支持電話號碼。每分鐘間隔觸發x個動作的最有效方法是什麼?

腳本是PHP。那麼,建立這個最有效率和最準確的方法是什麼?

這是我到目前爲止,但不幸的是它只是每分鐘觸發一次。我似乎無法弄清楚爲什麼它不會每分鐘運行兩次。

<?php 

$bTrigger = FALSE; 
$sDir = dirname(__FILE__); 
$sDir = rtrim($sDir,'/'); 
$sFile = $sDir . '/MINUTE-TIMER.txt'; 

$sLine = @ file_get_contents($sFile); 
$sLine = str_replace("\r\n",'',$sLine); 
$sLine = str_replace("\r",'',$sLine); 
$sLine = str_replace("\n",'',$sLine); 
$sLine = str_replace("\t",'',$sLine); 
$asParts = explode(',',$sLine); 
$nLetThru = @ $asParts[0]; 
$nLetThru = intval($nLetThru); 
$nLastMin = @ $asParts[1]; 
$nLastMin = intval($nLastMin); 
$nCurMin = intval(date('i')); 
if (empty($sLine)) { 
    $nLetThru = 0; 
    $nLastMin = 0; 
} 

$nMaxLetThru = 2; 

if ($nCurMin != $nLastMin) { // meaning, a new minute since last checked 
    if ($nLetThru <= $nMaxLetThru) { // meaning, we haven't hit more than max allowed 
    $bTrigger = TRUE; 
    ++$nLetThru; 
    file_put_contents($sFile,"$nLetThru,$nCurMin"); 
    } else { 
    file_put_contents($sFile,"0,$nCurMin"); 
    } 
} 

if ($bTrigger) { 
    echo 'TRIGGERED!!!!'; 
} else { 
    echo 'not triggered'; 
} 
+2

我們在這裏幫助您解決特定問題,而不是爲您做好工作。 –

+0

@MarcB我即將回答它,然後人們可以挑戰這個答案。我將從挑戰中學習。 – Volomike

+0

您的問題需要改進。如果你有答案,爲什麼不包括它? – Kermit

回答

2

的問題是一個簡單的編碼錯誤:$ nLetThru沒有被複位時一刻改變。 (另外,你< =本來應該<,但你已經注意到了。)

這裏是固定的代碼(基於原來的版本,在這個問題):

if (empty($sLine)) { 
    $nLastMin = -1; # (instead of 0) just affects the 1st time thru, 1 chance in 60 
} 

...

if ($nCurMin != $nLastMin) { // new minute 
    $bTrigger = TRUE; 
    $nLetThru = 1; 
} else { // another hit, same minute 
    if ($nLetThru < $nMaxLetThru) { // not too many yet 
    $bTrigger = TRUE; 
    ++$nLetThru; 
    } 
} 
if ($bTrigger) { 
    file_put_contents($sFile,"$nLetThru,$nCurMin"); 
} 
1
<?php 

$bTrigger = TRUE; 
$config = (object) array(); 
$config->THROTTLE_ENABLED = TRUE; 
$config->THROTTLE_MAX_PER_MINUTE = 2; 
if ($config->THROTTLE_ENABLED) { 

    $bThrottleTrigger = FALSE; 
    $sDir = dirname(__FILE__); 
    $sDir = rtrim($sDir,'/'); 
    $sFile = $sDir . '/MINUTE-TIMER.txt'; 

    $sLine = @ file_get_contents($sFile); 
    $sLine = str_replace("\r\n",'',$sLine); 
    $sLine = str_replace("\r",'',$sLine); 
    $sLine = str_replace("\n",'',$sLine); 
    $sLine = str_replace("\t",'',$sLine); 
    $asParts = explode(',',$sLine); 
    $nLetThru = @ $asParts[0]; 
    $nLetThru = intval($nLetThru); 
    $nLastMin = @ $asParts[1]; 
    $nLastMin = intval($nLastMin); 
    $nCurMin = intval(date('i')); 
    if (empty($sLine)) { 
     $nLetThru = 0; 
     $nLastMin = 0; 
    } 

    if ($nCurMin != $nLastMin) { // meaning, a new minute since last checked 
     if ($nLetThru < $config->THROTTLE_MAX_PER_MINUTE) { // meaning, we haven't hit more than max allowed 
      $bThrottleTrigger = TRUE; 
      ++$nLetThru; 
      @ file_put_contents($sFile,"$nLetThru,$nLastMin"); 
     } else { 
      @ file_put_contents($sFile,"0,$nCurMin"); 
     } 
    } else { 
     @ file_put_contents($sFile,"0,$nCurMin"); 
    } 

    if (!$bThrottleTrigger) { // will be like most of the time 
     $bTrigger = FALSE; // don't show the number 
    } 

} // end if ($config->THROTTLE_ENABLED) 
+0

這幾乎是正確的,但是當分鐘沒有改變時,您需要檢查計數。當分鐘HAS改變時,你應該繼續並讓用戶通過,並將計數器設置爲1.當分鐘沒有改變時,然後檢查計數器,如果計數器小於2,則增加計數器讓呼叫通過,否則不要讓通話通過。 –

相關問題