根据 http 响应更改警报频率

Changing alert frequencies based on the http response

我的代码在发送 http 请求时运行良好,如果网站出现故障或响应代码不是 200,它会发送松弛通知。我现在想弄清楚的是,在通知 table 中我有 check_frequencyalert_frequence。如果网站宕机,则不应使用检查频率来计算运行时间,而应使用 alert_frequence.

namespace App\Http\Controllers;


use GuzzleHttp\Client;
use App\Utilities\Reporter;
use GuzzleHttp\Exception\ClientException;
use App\Notification;
use App\Status;
use App\Setting;

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();
  }

  public function status()
  {
    $notifications = Notification::where('active', 1)->get();

    $status = Status::where('name', 'health')->first();

    foreach ($notifications as $notification) {
      $this->updateStatus($notification, $status);


    }
  }

  private function updateStatus(Notification $notification, Status $status)
  {
    $status_health = $notification->status('health');



    $frequency = $this->getFrequency($notification);

    $elapsed_time = \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();

    if($elapsed_time >= $frequency) {

      $response = $this->client->get($notification->website_url, [
        'http_errors' => false
      ]);  


      $resCode = $response->getStatusCode();

      $notification->statuses()->attach($status, [
        'values' => $resCode === 200 ? 'up' : 'down'
      ]);

      if($resCode != 200){
        /* how to send slack to different slach channels, now it is sending only to one channel!*/

        $this->reporter->slack($notification->website_url.':'.'  is down'. ' please check your email for the status code!'.' @- '.$notification->email, 
                $notification->slack_channel);
        $this->reporter->mail($notification->email,$resCode );

      }
    }

  }

  private function getFrequency(Notification $notification)
  {
    return isset($notification->check_frequency) 
    ? intval($notification->check_frequency) 
    : $this->default_check_frequency;
  }
}

我不确定这是否是您需要的,但您可以根据状态对 select 与 table 不同的列执行以下操作:

<?php

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();
    }

    public function status()
    {
        $notifications = Notification::where('active', 1)->get();

        $status = Status::where('name', 'health')->first();

        foreach ($notifications as $notification) {
            $this->updateStatus($notification, $status);
        }
    }

    private function updateStatus(Notification $notification, Status $status)
    {
        $status_health = $notification->status('health');

        /// move it here
        $response = $this->client->get($notification->website_url, [
            'http_errors' => false
        ]);
        $resCode = $response->getStatusCode();
        /// --- end

        $frequency = $this->getFrequency($notification, $resCode);

        $elapsed_time = \Carbon\Carbon::parse($status_health['timestamp'])->diffInMinutes();

        if($elapsed_time >= $frequency) {
            $notification->statuses()->attach($status, [
                'values' => $resCode === 200 ? 'up' : 'down'
            ]);

            if($resCode != 200){
                /* how to send slack to different slach channels, now it is sending only to one channel!*/
                $this->reporter->slack($notification->website_url.':'.'  is down'. ' please check your email for the status code!'.' @- '.$notification->email,
                $notification->slack_channel);
                $this->reporter->mail($notification->email,$resCode );
            }
        }
    }

    private function getFrequency(Notification $notification, $resCode)
    {
        /// -- select your column here
        $column = $resCode == '200' ? 'check_frequency' : 'alert_frequence';

        return isset($notification->{$column})
            ? intval($notification->{$column})
            : $this->default_check_frequency;
    }
}

而且我冒昧地重构了一下,分离方法关注点:

<?php

use Carbon\Carbon;

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' => $resCode === 200
                ? 'up'
                : 'down'
        ]);
    }

    private function report(Notification $notification, $resCode)
    {
        /* how to send slack to different slach channels, now it is sending only to one channel!*/
        $this->reporter->slack($notification->website_url . ':' . '  is down' . ' please check your email for the status code!' . ' @- ' . $notification->email,
                               $notification->slack_channel);

        $this->reporter->mail($notification->email, $resCode);
    }

    private function sendNotification(Notification $notification, Status $status, $status_health, $frequency, $resCode)
    {
        $elapsed_time = Carbon::parse($status_health['timestamp'])->diffInMinutes();

        if ($elapsed_time >= $frequency) {
            $this->addStatusToNotification($notification, $status, $resCode);

            if ($resCode != 200) {
                $this->report($notification, $resCode);
            }
        }
    }

    public function status()
    {
        $notifications = Notification::where('active', 1)->get();

        $status = Status::where('name', 'health')->first();

        foreach ($notifications as $notification) {
            $this->updateStatus($notification, $status);
        }
    }

    private function updateStatus(Notification $notification, Status $status)
    {
        $resCode = $this->getStatusCode($notification->website_url);

        $this->sendNotification(
            $notification,
            $status,
            $notification->status('health'),
            $this->getFrequency($notification, $resCode),
            $resCode
        );
    }

    private function getFrequency(Notification $notification, $resCode)
    {
        $column = $resCode == '200' ? 'check_frequency' : 'alert_frequence';

        return isset($notification->{$column})
            ? intval($notification->{$column})
            : $this->default_check_frequency;
    }

    private function getStatusCode($url)
    {
        $response = $this->client->get($url, [
            'http_errors' => false
        ]);

        return $response->getStatusCode();
    }
}