我创建了一个网络监控工具,可以检查网站的状态,并在网站出现问题时发送通知。如果网站关闭或响应代码与200不同,则计算经过时间,即现在和最后一次检查(或最后一次通知发送)之间的差异。检查频率工作正常。问题在于警报频率。警报频率的elpase时间计算为now和最后一次发送通知之间的时间差,最后一次频率发送的时间戳记在一个名为alerFreqeuncy table的表中。并且有一个通知表,其中包含要检查的网站URL,检查频率和警报频率。通知表和alertFrequency表之间存在一对多的关系。我有两张桌子的模型。
class AlertFrequency extends Model{
protected $table = 'alertFrequencies';
public function notification(){
return $this->belongsTo('App\Notification');}}
和第二个通知模型
public function alertFrequencies(){
return $this->hasMany('App\AlertFrequency');}
public function alert(){
$alert_timestamp = AlertFrequency::with('notification')->orderBy('created_at','desc')->select('created_at')->first();
//$alert_timestamp=$alert_timestamp->created_at->toDateTimeString();
if($alert_timestamp==null){
return false; }
return $alert_timestamp;}
现在在我的Guzzle控制器中我想使用通知和alerFrequenct表来计算时间戳,发送通知等,以便动态地将每个网站的记录保存在alertFrequency表中的新时间戳中。这是我的Guzzle cotroller和代码。但它无法奏效。我会给你的帮助吗?
<?php
namespace App\Http\Controllers;
use \GuzzleHttp\Client;
use App\Utilities\Reporter;
use GuzzleHttp\Exception\ClientException;
use App\Notification;
use App\Status;
use App\Setting;
use Carbon;
use App\AlertFrequency;
class GuzzleController extends Controller{
private $default_check_frequency;
protected $client;
protected $reporter;
public function __construct()
{
$this->client = new Client();
$this->reporter = new Reporter;
$this->default_check_frequency = Setting::defaultCheckFrequency();
}
private function addStatusToNotification(Notification $notification, Status $status, $resCode)
{
$notification->statuses()->attach($status, [
'values' => strval($resCode)
]);
}
/*function to add new time stamp into the alertFrequency table*/
private function add(Notification $notification, AlertFrequency $alert){
/*$notification->alertFrequency()->save();*/
$alert->notification()->save(); }
private function report(Notification $notification, $resCode)
{
if(empty($resCode)){
$resCode = "no response found";
}
$status = Notification::health($resCode);
$this->reporter->slack($notification->website_url . ':' . ' is '. $status . ' this is the status code!' . ' @- ' .$resCode, $notification->slack_channel);
$this->reporter->mail($notification->email,$notification->website_url.' is '. $status . ' this is the status Code: '. $resCode);
}
private function sendNotification(Notification $notification, $status_health, $alert_frequency, $resCode,AlertFrequency $alert)
{
echo "elpse time alert";
var_dump(\Carbon\Carbon::parse($alert->created_at->toDateTimeString())->diffInMinutes());
// If this is the first time we check, OR if the status changed from up to down and vice versa, notify!!!
if (empty($status_health['timestamp']) || Notification::health($resCode) <> Notification::health($status_health['value'])){
$this->report($notification,$resCode);
return;
}
// If the website is (still) down and the alert frequency is exceeded, notify!!!
if(Notification::health($resCode) === 'down' && \Carbon\Carbon::parse($alert)->diffInMinutes() >= $alert_frequency){
$this->report($notification,$resCode);
$this->add($notification,$alert);} }
public function status()
{
$notifications = Notification::where('active', 1)->get();
$alerttimeStamp = AlertFrequency::with('notification')->first();
$status = Status::where('name', 'health')->first();
foreach ($notifications as $notification) {
$frequency = $this->updateStatus($notification, $status,$aler);
if (!empty($frequency)) {
$notification->alertFrequencies()->create([
'notification_id' => $frequency
]);
}
}
}
private function updateStatus(Notification $notification, Status $status, AlertFrequency $alerttimeStamp)
{
$status_health = $notification->status('health');
$check = empty($status_health['timestamp']);
$elapsed_time = $check ? 10000 : \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();
$check_frequency = $this->getCheckFrequency($notification);
/* create an attachemtn in to the alerFrequenct table*/
$alert = $alerttimeStamp->alert();
var_dump($alert);
if ($check || $elapsed_time >= $check_frequency) {
$resCode = $this->getStatusCode($notification->website_url);
$this->addStatusToNotification($notification, $status, $resCode);
$this->sendNotification(
$notification,
$status_health,
$this->getAlertFrequency($notification),
$resCode,
$alert
);