Skip to content

Commit

Permalink
Merge pull request #4 from saeedvaziry/analysis-nNgVd5
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
saeedvaziry authored Jan 21, 2022
2 parents 549d830 + 441b4ae commit 286ed41
Show file tree
Hide file tree
Showing 32 changed files with 139 additions and 121 deletions.
22 changes: 11 additions & 11 deletions config/monitoring.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@
* Route configurations
*/
'routes' => [
'prefix' => 'monitoring',
'middlewares' => ['web']
'prefix' => 'monitoring',
'middlewares' => ['web'],
],

/*
* Models
*/
'models' => [
'monitoring_record' => \SaeedVaziry\Monitoring\Models\MonitoringRecord::class,
'monitoring_alert' => \SaeedVaziry\Monitoring\Models\MonitoringAlert::class,
'monitoring_alert' => \SaeedVaziry\Monitoring\Models\MonitoringAlert::class,
],

/*
* Chart colors
*/
'chart_colors' => [
'cpu' => [
'border_color' => '#4f46e5',
'background_color' => '#a5b4fc'
'border_color' => '#4f46e5',
'background_color' => '#a5b4fc',
],
'memory' => [
'border_color' => '#e11d48',
'background_color' => '#fda4af'
'border_color' => '#e11d48',
'background_color' => '#fda4af',
],
'disk' => [
'border_color' => '#9333ea',
'background_color' => '#d8b4fe'
'border_color' => '#9333ea',
'background_color' => '#d8b4fe',
],
],

Expand All @@ -63,7 +63,7 @@
/*
* Fill it if you want the Slack channel
*/
'slack_webhook_url' => env('MONITORING_SLACK_WEBHOOK_URL')
'slack_webhook_url' => env('MONITORING_SLACK_WEBHOOK_URL'),
],

/*
Expand All @@ -75,5 +75,5 @@
* Purge recorded data
* Supports PHP strtotime options like: '-1 day', '-2 hours', ...
*/
'purge_before' => '-1 day'
'purge_before' => '-1 day',
];
8 changes: 4 additions & 4 deletions database/factories/MonitoringAlertFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public function definition()
{
return [
'instance_name' => config('monitoring.instance_name'),
'cpu' => 40,
'memory' => 30,
'disk' => 20,
'occurred' => 0
'cpu' => 40,
'memory' => 30,
'disk' => 20,
'occurred' => 0,
];
}
}
6 changes: 3 additions & 3 deletions database/factories/MonitoringRecordFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function definition()
{
return [
'instance_name' => config('monitoring.instance_name'),
'cpu' => 10,
'memory' => 20,
'disk' => 30,
'cpu' => 10,
'memory' => 20,
'disk' => 30,
];
}
}
16 changes: 10 additions & 6 deletions src/Actions/CreateAlert.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class CreateAlert
{
/**
* @param array $input
* @return mixed
*
* @throws ValidationException
*
* @return mixed
*/
public function create(array $input)
{
Expand All @@ -19,7 +21,7 @@ public function create(array $input)
$alert = app(config('monitoring.models.monitoring_alert'))
->where('instance_name', $input['instance_name'])
->firstOrCreate([
'instance_name' => $input['instance_name']
'instance_name' => $input['instance_name'],
], $input);
$alert->update($input);

Expand All @@ -28,13 +30,15 @@ public function create(array $input)

/**
* @param array $input
* @return void
*
* @throws ValidationException
*
* @return void
*/
protected function validate(array $input)
{
$rules = [
'instance_name' => 'required'
'instance_name' => 'required',
];

if (isset($input['cpu']) && !empty($input['cpu'])) {
Expand All @@ -56,9 +60,9 @@ protected function validate(array $input)
(empty($input['cpu']) && empty($input['memory']) && empty($input['disk']))
) {
throw ValidationException::withMessages([
'cpu' => __('You must fill at least one item'),
'cpu' => __('You must fill at least one item'),
'memory' => __('You must fill at least one item'),
'disk' => __('You must fill at least one item'),
'disk' => __('You must fill at least one item'),
])->errorBag('createAlert');
}
}
Expand Down
13 changes: 8 additions & 5 deletions src/Actions/RecordUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ class RecordUsage
{
/**
* @param array $resources
* @return MonitoringRecord
*
* @throws \Exception
*
* @return MonitoringRecord
*/
public function record(array $resources)
{
Expand All @@ -19,18 +21,19 @@ public function record(array $resources)
$model = config('monitoring.models.monitoring_record');
$record = new $model([
'instance_name' => str_replace(' ', '', config('monitoring.instance_name')),
'cpu' => $resources['cpu'] ?? null,
'memory' => $resources['memory'] ?? null,
'disk' => $resources['disk'] ?? null,
'cpu' => $resources['cpu'] ?? null,
'memory' => $resources['memory'] ?? null,
'disk' => $resources['disk'] ?? null,
]);
$record->save();

return $record;
}

/**
* @return void
* @throws \Exception
*
* @return void
*/
protected function checkOS()
{
Expand Down
7 changes: 4 additions & 3 deletions src/Channels/BaseChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ abstract class BaseChannel implements Channel
*/
protected function subject($record)
{
return __("Resource usage for :instance", ['instance' => $record->instance_name]);
return __('Resource usage for :instance', ['instance' => $record->instance_name]);
}

/**
* @param $record
*
* @return string
*/
protected function message($record)
{
return __("CPU: :cpu\n Memory: :memory\n Disk: :disk", [
'cpu' => $record->cpu,
'cpu' => $record->cpu,
'memory' => $record->memory,
'disk' => $record->disk,
'disk' => $record->disk,
]);
}
}
1 change: 1 addition & 0 deletions src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface Channel
{
/**
* @param $record
*
* @return void
*/
public function send($record);
Expand Down
1 change: 1 addition & 0 deletions src/Channels/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Email extends BaseChannel
{
/**
* @param $record
*
* @return void
*/
public function send($record)
Expand Down
3 changes: 2 additions & 1 deletion src/Channels/Slack.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ class Slack extends BaseChannel
{
/**
* @param $record
*
* @return void
*/
public function send($record)
{
Http::post(config('monitoring.notifications.slack_webhook_url'), [
'text' => '*' . $this->subject($record) . '*' . "\n" . $this->message($record),
'text' => '*'.$this->subject($record).'*'."\n".$this->message($record),
]);
}
}
3 changes: 1 addition & 2 deletions src/Commands/PurgeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Exception;
use Illuminate\Console\Command;
use SaeedVaziry\Monitoring\Models\MonitoringRecord;

class PurgeCommand extends Command
{
Expand Down Expand Up @@ -35,9 +34,9 @@ public function __construct()
/**
* Execute the console command.
*
* @return void
* @throws Exception
*
* @return void
*/
public function handle()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/RecordCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ public function __construct()
/**
* Execute the console command.
*
* @return void
* @throws Exception
*
* @return void
*/
public function handle()
{
tap(app(RecordUsage::class)->record([
'cpu' => Monitoring::cpu()->usage(),
'cpu' => Monitoring::cpu()->usage(),
'memory' => Monitoring::memory()->usage(),
'disk' => Monitoring::disk()->usage(),
'disk' => Monitoring::disk()->usage(),
]), function ($record) {
app(CheckForAlerts::class)->check($record);
});
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/OSIsNotSupported.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class OSIsNotSupported extends Exception
*/
public function __construct($message)
{
parent::__construct($message . ' is not supported');
parent::__construct($message.' is not supported');
}
}
1 change: 1 addition & 0 deletions src/HasAlerts.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ trait HasAlerts
{
/**
* @param array $instances
*
* @return array
*/
public function getAlerts(array $instances)
Expand Down
48 changes: 26 additions & 22 deletions src/HasRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace SaeedVaziry\Monitoring;

use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;

trait HasRecords
{
Expand All @@ -26,6 +25,7 @@ protected function getInstances()

/**
* @param array $instances
*
* @return array
*/
protected function getRecords(array $instances)
Expand All @@ -44,6 +44,7 @@ protected function getRecords(array $instances)

/**
* @param array $instances
*
* @return array
*/
protected function getLastRecords(array $instances)
Expand All @@ -61,6 +62,7 @@ protected function getLastRecords(array $instances)

/**
* @param Collection $records
*
* @return array
*/
protected function getRecordsChart(Collection $records)
Expand All @@ -77,40 +79,42 @@ protected function getRecordsChart(Collection $records)
}

return [
'labels' => $labels,
'labels' => $labels,
'datasets' => [
[
'label' => 'CPU',
'data' => $cpu,
'borderWidth' => 1.5,
'fill' => false,
'borderColor' => config('monitoring.chart_colors.cpu.border_color'),
'backgroundColor' => config('monitoring.chart_colors.cpu.background_color')
'label' => 'CPU',
'data' => $cpu,
'borderWidth' => 1.5,
'fill' => false,
'borderColor' => config('monitoring.chart_colors.cpu.border_color'),
'backgroundColor' => config('monitoring.chart_colors.cpu.background_color'),
],
[
'label' => 'Memory',
'data' => $memory,
'borderWidth' => 1.5,
'fill' => false,
'borderColor' => config('monitoring.chart_colors.memory.border_color'),
'backgroundColor' => config('monitoring.chart_colors.memory.background_color')
'label' => 'Memory',
'data' => $memory,
'borderWidth' => 1.5,
'fill' => false,
'borderColor' => config('monitoring.chart_colors.memory.border_color'),
'backgroundColor' => config('monitoring.chart_colors.memory.background_color'),
],
[
'label' => 'Disk',
'data' => $disk,
'borderWidth' => 1.5,
'fill' => false,
'borderColor' => config('monitoring.chart_colors.disk.border_color'),
'backgroundColor' => config('monitoring.chart_colors.disk.background_color')
]
'label' => 'Disk',
'data' => $disk,
'borderWidth' => 1.5,
'fill' => false,
'borderColor' => config('monitoring.chart_colors.disk.border_color'),
'backgroundColor' => config('monitoring.chart_colors.disk.background_color'),
],
],
];
}

/**
* @param array $records
* @return array
*
* @throws \Exception
*
* @return array
*/
protected function getCharts(array $records)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@

class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
use AuthorizesRequests;
use DispatchesJobs;
use ValidatesRequests;
}
Loading

0 comments on commit 286ed41

Please sign in to comment.