Skip to content

Commit

Permalink
initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
geowrgetudor committed Dec 5, 2023
1 parent 28c6738 commit 692742a
Show file tree
Hide file tree
Showing 13 changed files with 377 additions and 100 deletions.
70 changes: 51 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@
[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/geowrgetudor/disk-metrics/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/geowrgetudor/disk-metrics/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/geowrgetudor/disk-metrics.svg?style=flat-square)](https://packagist.org/packages/geowrgetudor/disk-metrics)

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.
A Laravel Pulse package that adds metrics about your storage.
Supports `local` and `s3` drivers.

## Support us

[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/disk-metrics.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/disk-metrics)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
- Total Size
- Total Files
- Total Directories (only for `local` driver)

## Installation

Expand All @@ -23,13 +20,6 @@ You can install the package via composer:
composer require geowrgetudor/disk-metrics
```

You can publish and run the migrations with:

```bash
php artisan vendor:publish --tag="disk-metrics-migrations"
php artisan migrate
```

You can publish the config file with:

```bash
Expand All @@ -40,6 +30,31 @@ This is the contents of the published config file:

```php
return [
/**
* Determines the state o the package
*/
'enabled' => env('GEOW_DISK_METRICS', true),

/**
* Track disks defined in filesystems.php config file.
* Support only 'local' or 's3' driver.
*
* You can pass an array ['directories', 'files'] ONLY to a local disk
* which will determine what to be counted. To count both,
* you can pass an empty array.
*/
'disks' => [
'local' => [],
// 'public' => [],
// 's3' => []
],

/**
* How often (in minutes) should the Laravel Pulse capture data?
* The value should be greated than 1!
* Default: 10
*/
'record_interval' => 10
];
```

Expand All @@ -51,9 +66,26 @@ php artisan vendor:publish --tag="disk-metrics-views"

## Usage

Register the recorder inside `config/pulse.php`. (If you don\'t have this file make sure you have published the config file of Larave Pulse using `php artisan vendor:publish --tag=pulse-config`)

```
return [
// ...
'recorders' => [
// Existing recorders...
\Geow\DiskMetrics\Recorders\DiskRecorder::class => []
]
]
```

Publish Laravel Pulse `dashboard.blade.php` view using `php artisan vendor:publish --tag=pulse-dashboard`

Then you can modify the file and add the disk-metrics livewire template.

```php
$diskMetrics = new Geow\DiskMetrics();
echo $diskMetrics->echoPhrase('Hello, Geow!');
<livewire:disk-metrics cols="4" rows="2" />
```

## Testing
Expand All @@ -76,8 +108,8 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [George Tudor](https://github.com/geowrgetudor)
- [All Contributors](../../contributors)
- [George Tudor](https://github.com/geowrgetudor)
- [All Contributors](../../contributors)

## License

Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "geowrgetudor/disk-metrics",
"description": "This is my package disk-metrics",
"description": "Monitor disk size, file & directory count for Laravel Pulse",
"keywords": [
"George Tudor",
"laravel",
"pulse",
"disk-metrics"
],
"homepage": "https://github.com/geowrgetudor/disk-metrics",
Expand All @@ -16,9 +16,10 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.2",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
"illuminate/contracts": "^10.0",
"laravel/pulse": "^1.0@beta"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand Down Expand Up @@ -75,6 +76,6 @@
}
}
},
"minimum-stability": "dev",
"minimum-stability": "beta",
"prefer-stable": true
}
}
25 changes: 24 additions & 1 deletion config/disk-metrics.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
<?php

// config for Geow/DiskMetrics
return [
/**
* Determines the state o the package
*/
'enabled' => env('GEOW_DISK_METRICS', true),

/**
* Track disks defined in filesystems.php config file.
* Support only 'local' or 's3' driver.
*
* You can pass an array ['directories', 'files'] ONLY to a local disk
* which will determine what to be counted. To count both,
* you can pass an empty array.
*/
'disks' => [
'local' => [],
// 'public' => [],
// 's3' => []
],

/**
* How often (in minutes) should the Laravel Pulse capture data?
* The value should be greated than 1!
* Default: 10
*/
'record_interval' => 10
];
19 changes: 0 additions & 19 deletions database/factories/ModelFactory.php

This file was deleted.

19 changes: 0 additions & 19 deletions database/migrations/create_disk_metrics_table.php.stub

This file was deleted.

67 changes: 67 additions & 0 deletions resources/views/livewire/disk-metrics.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<x-pulse::card :cols="$cols" :rows="$rows" :class="$class">
<x-pulse::card-header name="{{ __('Disk Metrics') }}">
<x-slot:icon>
<x-dynamic-component :component="'pulse::icons.sparkles'" />
</x-slot:icon>
</x-pulse::card-header>

<x-pulse::scroll :expand="$expand" wire:poll.5s="">
@if (empty($data))
<x-pulse::no-results />
@else
<div class="grid grid-cols-1 @lg:grid-cols-2 @3xl:grid-cols-3 @6xl:grid-cols-4 gap-2">
<x-pulse::table>
<colgroup>
<col width="100%" />
<col width="0%" />
<col width="0%" />
</colgroup>
<x-pulse::thead>
<tr>
<x-pulse::th>{{ __('Disk') }}</x-pulse::th>
<x-pulse::th class="text-right">{{ __('Directories') }}</x-pulse::th>
<x-pulse::th class="text-right">{{ __('Files') }}</x-pulse::th>
<x-pulse::th class="text-right">{{ __('Size') }}</x-pulse::th>
</tr>
</x-pulse::thead>
<tbody>
@foreach ($data as $diskName => $details)
<tr class="h-2 first:h-0"></tr>
<tr wire:key="{{ $diskName }}">
<x-pulse::td class="max-w-[1px]">
<code class="block text-xs text-gray-900 dark:text-gray-100 truncate"
title="{{ $diskName }}">
{{ ucfirst($diskName) }}
</code>
<p class="mt-1 text-xs text-gray-500 dark:text-gray-400 truncate" title="">
@if ($details['disk_data']['root'])
{{ $details['disk_data']['root'] }}
@endif
@if ($details['disk_data']['bucket'])
{{ $details['disk_data']['bucket'] }}
@endif
</p>
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold">
@if (isset($details['metrics']['directory_count']))
{{ $details['metrics']['directory_count']->value }}
@endif
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold">
@if (isset($details['metrics']['file_count']))
{{ $details['metrics']['file_count']->value }}
@endif
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold">
@if (isset($details['metrics']['total_size']))
{{ $details['metrics']['total_size']->value }}
@endif
</x-pulse::td>
</tr>
@endforeach
</tbody>
</x-pulse::table>
</div>
@endif
</x-pulse::scroll>
</x-pulse::card>
19 changes: 0 additions & 19 deletions src/Commands/DiskMetricsCommand.php

This file was deleted.

52 changes: 52 additions & 0 deletions src/DiskMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,58 @@

namespace Geow\DiskMetrics;

use Exception;

class DiskMetrics
{
public static function interval(): int
{
$intervalInMinutes = config('disk-metrics.record_interval', 10);

if (!is_int($intervalInMinutes)) {
throw new Exception("Invalid interval format. 'disk-metrics.record_inteval' should be integer.");
}

if ($intervalInMinutes <= 1) {
$intervalInMinutes = 10;
}

return $intervalInMinutes;
}

public static function disks(): array
{
$disks = config('disk-metrics.disks', []);

if (!is_array($disks)) {
throw new Exception("Invalid disks format. 'disk-metrics.disks' should be array.");
}

$filesystemDisks = [];

foreach ($disks as $disk => $resources) {
$filesystemDisk = config("filesystems.disks.{$disk}");

if (!$filesystemDisk) {
throw new Exception("Invalid disk driver. Make sure '{$disk}' is defined inside filesystems.php config file and it uses 'local' or 's3' driver.");
}

if (!in_array($filesystemDisk['driver'], ['local', 's3'], true)) {
throw new Exception('Invalid disk driver. Supported disk drivers: "local", "s3"');
}

if ($filesystemDisk['driver'] === 'local' && !is_array($resources)) {
throw new Exception('Invalid disk settings. Please check "disk-metrics.php" config file for guidance.');
}

$filesystemDisks[$disk] = [
'driver' => $filesystemDisk['driver'],
'root' => $filesystemDisk['root'] ?? null,
'bucket' => $filesystemDisk['bucket'] ?? null,
'resources' => $resources,
];
}

return $filesystemDisks;
}
}
22 changes: 13 additions & 9 deletions src/DiskMetricsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@

namespace Geow\DiskMetrics;

use Geow\DiskMetrics\Livewire\DiskMetrics;
use Illuminate\Foundation\Application;
use Livewire\LivewireManager;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Geow\DiskMetrics\Commands\DiskMetricsCommand;

class DiskMetricsServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
/*
* This class is a Package Service Provider
*
* More info: https://github.com/spatie/laravel-package-tools
*/
$package
->name('disk-metrics')
->hasConfigFile()
->hasViews()
->hasMigration('create_disk-metrics_table')
->hasCommand(DiskMetricsCommand::class);
->hasViews();
}

public function boot(): void
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'disk-metrics');

$this->callAfterResolving('livewire', function (LivewireManager $livewire, Application $app) {
$livewire->component('disk-metrics', DiskMetrics::class);
});
}
}
Loading

0 comments on commit 692742a

Please sign in to comment.