Skip to content

Commit

Permalink
Merge branch 'master' into crawler
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/Checkers/Uptime.php
#	composer.lock
  • Loading branch information
OwenMelbz committed Oct 12, 2019
2 parents 435fa84 + 3ffae39 commit 26e709a
Show file tree
Hide file tree
Showing 12 changed files with 604 additions and 261 deletions.
5 changes: 2 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ MAIL_DRIVER=mailgun
MAIL_FROM_ADDRESS=[email protected]
MAIL_FROM_NAME="${APP_NAME}"

ENFORCE_DOMAIN=true
HORIZON_ENABLED=false
DEBUGBAR_ENABLED=false
ENFORCE_DOMAIN=true

LOGIN_EMAIL=[email protected]
REGISTRATIONS_ENABLED=true
36 changes: 32 additions & 4 deletions app/Checkers/Uptime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ public function run()
{
$this->fetch();
$this->notify();
$this->cache();
}

private function fetch()
{
$client = new Client();

$response_time = 3001;
<<<<<<< HEAD

$response = $client->request('GET', $this->website->url, [
RequestOptions::ON_STATS => function ($stats) use (&$response_time) {
Expand All @@ -43,12 +45,33 @@ private function fetch()
]);

$keywordFound = Str::contains($response->getBody(), $this->website->uptime_keyword);
=======
$keywordFound = false;

try {
$response = $client->request('GET', $this->website->url, [
'on_stats' => function ($stats) use (&$response_time) {
$response_time = $stats->getTransferTime();
},
'http_errors' => false,
'verify' => false,
'allow_redirects' => true,
'headers' => [
'User-Agent' => 'Mozilla/5.0+(compatible; UptimeRobot/2.0; http://www.uptimerobot.com/; Odin)'
],
]);

$keywordFound = Str::contains($response->getBody(), $this->website->uptime_keyword);
>>>>>>> master

if (!$keywordFound && $response->getStatusCode() == '200') {
$reason = sprintf('Keyword: %s not found (%d)', $this->website->uptime_keyword, 200);
} else {
$reason = sprintf('%s (%d)', $response->getReasonPhrase(), $response->getStatusCode());
}
$reason = sprintf('Keyword: %s not found (%d)', $this->website->uptime_keyword, 200);
} else {
$reason = sprintf('%s (%d)', $response->getReasonPhrase(), $response->getStatusCode());
}
} catch (\Exception $exception) {
$reason = $exception->getMessage();
}

$scan = new UptimeScan([
'response_status' => $reason,
Expand Down Expand Up @@ -80,4 +103,9 @@ private function notify()
);
}
}

private function cache()
{
$this->website->generateUptimeReport(true);
}
}
29 changes: 28 additions & 1 deletion app/HasUptime.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,34 @@ trait HasUptime
{
public function uptimes()
{
return $this->hasMany(UptimeScan::class)->orderBy('created_at', 'desc');
return $this->hasMany(UptimeScan::class)
->where('created_at', '<', now()->addDays(31))
->orderBy('created_at', 'desc');
}

public function generateUptimeReport($refreshCache = false)
{
if ($refreshCache) {
cache()->forget($this->cache_key);
$this->load(['uptimes']);
}

return cache()->rememberForever($this->cache_key, function () {
return [
'uptime' => $this->uptime_summary,
'response_time' => $this->response_time,
'response_times' => $this->response_times,
'online' => $this->current_state,
'online_time' => $this->uptime,
'last_incident' => $this->last_incident,
'events' => $this->recent_events,
];
});
}

public function getCacheKeyAttribute()
{
return 'uptime_' . $this->getKey();
}

public function getLastIncidentAttribute()
Expand Down
14 changes: 3 additions & 11 deletions app/Http/Controllers/UptimeReportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use Exception;
use App\Website;
use App\Jobs\UptimeCheck;
use Illuminate\Http\Request;
Expand All @@ -14,24 +15,15 @@ class UptimeReportController extends Controller
* @param Request $request
* @param Website $website
* @return array
* @throws Exception
*/
public function __invoke(Request $request, Website $website)
{
if ($request->has('refresh')) {
UptimeCheck::dispatchNow($website);
}

$website->load(['uptimes']);

$response = [
'uptime' => $website->uptime_summary,
'response_time' => $website->response_time,
'response_times' => $website->response_times,
'online' => $website->current_state,
'online_time' => $website->uptime,
'last_incident' => $website->last_incident,
'events' => $website->recent_events,
];
$response = $website->generateUptimeReport();

return response($response);
}
Expand Down
11 changes: 9 additions & 2 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

Expand Down Expand Up @@ -52,8 +53,14 @@ public function map()
protected function mapWebRoutes()
{
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
->namespace($this->namespace)
->group(function () {
Auth::routes([
'register' => config('auth.registrations_enabled'),
]);

include_once base_path('routes/web.php');
});
}

/**
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"require": {
"php": "^7.1.3",
"doctrine/annotations": "^1.7",
"doctrine/dbal": "^2.9",
"embed/embed": "^3.4",
"fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "^6.3",
Expand Down Expand Up @@ -40,6 +41,12 @@
"preferred-install": "dist",
"sort-packages": true
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/OwenMelbz/DNSParser"
}
],
"extra": {
"laravel": {
"dont-discover": []
Expand Down
Loading

0 comments on commit 26e709a

Please sign in to comment.