ghost bar chart

This commit is contained in:
Čarodej
2022-01-11 08:27:07 +01:00
parent 9dbc3ab969
commit 62cbcd14ed
3 changed files with 137 additions and 26 deletions

View File

@@ -1,7 +1,9 @@
<?php
namespace App\Users\Resources;
use ByteUnits\Metric;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -20,18 +22,18 @@ class UserStorageResource extends JsonResource
$totalCapacity = match (get_settings('subscription_type')) {
'metered' => $this->usedCapacity / 1000000000,
'fixed' => $this->limitations->max_storage_amount,
default => $this->limitations->max_storage_amount,
'fixed' => $this->limitations->max_storage_amount,
default => $this->limitations->max_storage_amount,
};
return [
'data' => [
'id' => (string) $this->id,
'id' => (string)$this->id,
'type' => 'storage',
'attributes' => [
'used' => Metric::bytes($this->usedCapacity)->format(),
'capacity' => format_gigabytes($totalCapacity),
'percentage' => (float) get_storage_percentage($this->usedCapacity, $totalCapacity),
'percentage' => (float)get_storage_percentage($this->usedCapacity, $totalCapacity),
],
'meta' => [
'traffic' => [
@@ -44,23 +46,23 @@ class UserStorageResource extends JsonResource
],
'images' => [
'used' => Metric::bytes($images)->format(),
'percentage' => (float) get_storage_percentage($images, $totalCapacity),
'percentage' => (float)get_storage_percentage($images, $totalCapacity),
],
'audios' => [
'used' => Metric::bytes($audios)->format(),
'percentage' => (float) get_storage_percentage($audios, $totalCapacity),
'percentage' => (float)get_storage_percentage($audios, $totalCapacity),
],
'videos' => [
'used' => Metric::bytes($videos)->format(),
'percentage' => (float) get_storage_percentage($videos, $totalCapacity),
'percentage' => (float)get_storage_percentage($videos, $totalCapacity),
],
'documents' => [
'used' => Metric::bytes($documents)->format(),
'percentage' => (float) get_storage_percentage($documents, $totalCapacity),
'percentage' => (float)get_storage_percentage($documents, $totalCapacity),
],
'others' => [
'used' => Metric::bytes($others)->format(),
'percentage' => (float) get_storage_percentage($others, $totalCapacity),
'percentage' => (float)get_storage_percentage($others, $totalCapacity),
],
],
],
@@ -116,12 +118,6 @@ class UserStorageResource extends JsonResource
->where('created_at', '>', $period)
->max('download');
$trafficRecords = DB::table('traffic')
->where('user_id', $this->id)
->where('created_at', '>', $period)
->orderBy('created_at')
->get();
$downloadTotal = DB::table('traffic')
->where('user_id', $this->id)
->where('created_at', '>', $period)
@@ -132,8 +128,21 @@ class UserStorageResource extends JsonResource
->where('created_at', '>', $period)
->sum('upload');
$upload = $trafficRecords->map(fn ($record) => $uploadMax !== 0 ? round(($record->upload / $uploadMax) * 100, 2) : 0);
$download = $trafficRecords->map(fn ($record) => $downloadMax !== 0 ? round(($record->download / $downloadMax) * 100, 2) : 0);
$trafficRecords = DB::table('traffic')
->where('user_id', $this->id)
->where('created_at', '>', $period)
->orderBy('created_at')
->get();
$upload = $trafficRecords->map(fn($record) => [
'created_at' => format_date($record->created_at),
'amount' => $uploadMax !== 0 ? round(($record->upload / $uploadMax) * 100, 2) : 0,
]);
$download = $trafficRecords->map(fn($record) => [
'created_at' => format_date($record->created_at),
'amount' => $downloadMax !== 0 ? round(($record->download / $downloadMax) * 100, 2) : 0,
]);
return [$downloadTotal, $uploadTotal, $upload, $download];
}