kuvia/app/Http/Controllers/DashboardController.php
2021-01-12 23:04:38 +01:00

38 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\Storage;
use App\Models\Traffic;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\DB;
class DashboardController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function dashboardView() {
$currentSize = DB::select("SELECT SUM(size) AS `size` FROM images LEFT JOIN galleries ON images.gallery = galleries.id WHERE galleries.tenant = ".session("current_tenant_id").";")[0]->size;
$currentTraffic = DB::select("SELECT SUM(size) AS size FROM access WHERE tenant = ".session("current_tenant_id")." AND created_at >= NOW() - INTERVAL 1 DAY;")[0]->size;
$monthlyTraffic = DB::select("SELECT SUM(traffic) AS `traffic` FROM traffic WHERE `year` = 2021 AND `month` = 1 and tenant = ".session("current_tenant_id").";")[0]->traffic;
$lastDaysTreffic = Traffic::getLastDays(7);
$lastDaysSize = Storage::getLastDays(7);
return view("dashboard.index", [
"currentSize" => $this->human_filesize($currentSize),
"currentTraffic" => $this->human_filesize($currentTraffic),
"monthlyTraffic" => $this->human_filesize($monthlyTraffic),
"lastDaysTreffic" => $lastDaysTreffic,
"lastDaysSize" => $lastDaysSize
]);
}
private function human_filesize($bytes, $decimals = 2) {
$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}
}