I created a web monitoring tool that chechs the status of the website and send notification in case something went wrong with website. if the website is down or the response code is different from 200, it calculated the elapse time, which is the difference between now and the last check(or last notification send). the check frequency is working fine. the problem is with the alert frequency. the elpase time for the alert frequency is calculated as the time difference between now and the last notification send, and the time stamp of last frequency send is recoreded in a table called alerFreqeuncy table. And there is a notification table, which has website url to be checked, the check frequency and the alert Frequency. there is a one to many relationship between notification table and alertFrequency table. i have models for both tables.
class AlertFrequency extends Model{
protected $table = 'alertFrequencies';
public function notification(){
return $this->belongsTo('App\Notification');}}
and a second notification model
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;}
now in my Guzzle controller i want to use the notification and alerFrequenct table to calculate the time stamp, to send notification and more over to keep the record of each website in the new time stamp in the alertFrequency table dynamically. Here is my Guzzle cotroller and the codes. but it is failed to work. i would apprciate your help?
<?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
);