mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-29 11:15:58 +00:00
record daily usage
This commit is contained in:
@@ -8,6 +8,7 @@ use Support\Scheduler\Actions\DeleteFailedFilesAction;
|
||||
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
|
||||
use Support\Scheduler\Actions\DeleteUnverifiedUsersAction;
|
||||
use Support\Scheduler\Actions\DeleteExpiredShareLinksAction;
|
||||
use Support\Scheduler\Actions\ReportUsageAction;
|
||||
|
||||
class Kernel extends ConsoleKernel
|
||||
{
|
||||
@@ -26,7 +27,7 @@ class Kernel extends ConsoleKernel
|
||||
*/
|
||||
protected function schedule(Schedule $schedule): void
|
||||
{
|
||||
if (! is_storage_driver(['local'])) {
|
||||
if (! is_storage_driver('local')) {
|
||||
$schedule->call(
|
||||
fn () => resolve(DeleteFailedFilesAction::class)()
|
||||
)->everySixHours();
|
||||
@@ -38,7 +39,11 @@ class Kernel extends ConsoleKernel
|
||||
|
||||
$schedule->call(
|
||||
fn () => resolve(DeleteUnverifiedUsersAction::class)()
|
||||
)->daily();
|
||||
)->daily()->at('00:05');
|
||||
|
||||
$schedule->call(
|
||||
fn () => resolve(ReportUsageAction::class)()
|
||||
)->daily()->at('00:10');
|
||||
|
||||
// Run queue jobs every minute
|
||||
$schedule->command('queue:work --stop-when-empty')
|
||||
@@ -48,10 +53,10 @@ class Kernel extends ConsoleKernel
|
||||
// Backup app database daily
|
||||
$schedule->command('backup:clean')
|
||||
->daily()
|
||||
->at('01:00');
|
||||
->at('00:15');
|
||||
$schedule->command('backup:run --only-db')
|
||||
->daily()
|
||||
->at('01:30');
|
||||
->at('00:20');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -76,7 +76,7 @@ class UploadFileAction
|
||||
$disk_local->move("chunks/$chunkName", "files/$user_id/$fileName");
|
||||
|
||||
// Move files to external storage
|
||||
if (! is_storage_driver(['local'])) {
|
||||
if (! is_storage_driver('local')) {
|
||||
($this->moveFileToExternalStorage)($fileName, $user_id);
|
||||
}
|
||||
|
||||
|
||||
@@ -93,7 +93,7 @@ class File extends Model
|
||||
$links = [];
|
||||
|
||||
// Generate thumbnail link for external storage service
|
||||
if ($this->type === 'image' && ! is_storage_driver(['local'])) {
|
||||
if ($this->type === 'image' && ! is_storage_driver('local')) {
|
||||
foreach (config('vuefilemanager.image_sizes') as $item) {
|
||||
$filePath = "files/{$this->user_id}/{$item['name']}-{$this->basename}";
|
||||
|
||||
@@ -127,7 +127,7 @@ class File extends Model
|
||||
public function getFileUrlAttribute(): string
|
||||
{
|
||||
// Get file from external storage
|
||||
if (! is_storage_driver(['local'])) {
|
||||
if (! is_storage_driver('local')) {
|
||||
$file_pretty_name = is_storage_driver('backblaze')
|
||||
? Str::snake(mb_strtolower($this->attributes['name']))
|
||||
: get_pretty_name($this->attributes['basename'], $this->attributes['name'], $this->attributes['mimetype']);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
namespace Domain\Traffic\Models;
|
||||
|
||||
use Database\Factories\TrafficFactory;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -22,6 +23,11 @@ class Traffic extends Model
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
protected static function newFactory(): TrafficFactory
|
||||
{
|
||||
return TrafficFactory::new();
|
||||
}
|
||||
|
||||
public function scopeCurrentDay($query): Builder
|
||||
{
|
||||
return $query->whereDate('created_at', today());
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Support\Scheduler\Actions;
|
||||
|
||||
use DB;
|
||||
use VueFileManager\Subscription\Domain\Subscriptions\Models\Subscription;
|
||||
|
||||
class ReportUsageAction
|
||||
{
|
||||
public function __invoke()
|
||||
{
|
||||
Subscription::whereIn('type', ['pre-paid', 'auto-renew'])
|
||||
->where('status', 'active')
|
||||
->cursor()
|
||||
->each(function($subscription) {
|
||||
$this->recordBandwidth($subscription);
|
||||
$this->recordStorageCapacity($subscription);
|
||||
});
|
||||
}
|
||||
|
||||
private function recordStorageCapacity(Subscription $subscription): void
|
||||
{
|
||||
// Sum all file size
|
||||
$filesize = DB::table('files')
|
||||
->where('user_id', $subscription->user->id)
|
||||
->sum('filesize');
|
||||
|
||||
// We count storage size in GB, e.g. 0.15 is 150mb
|
||||
$amount = $filesize / 1000000000;
|
||||
|
||||
// Record storage capacity usage
|
||||
$subscription->recordUsage('storage', $amount);
|
||||
}
|
||||
|
||||
private function recordBandwidth(Subscription $subscription): void
|
||||
{
|
||||
// We count storage size in GB, e.g. 0.15 is 150mb
|
||||
$record = $subscription
|
||||
->user
|
||||
->traffics()
|
||||
->where('created_at', today()->subDay())
|
||||
->first();
|
||||
|
||||
$amount = ($record->download ?? 0) / 1000000000;
|
||||
|
||||
// Record storage capacity usage
|
||||
$subscription->recordUsage('bandwidth', $amount);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user