kuvia/app/Http/Controllers/DashboardController.php

39 lines
1.7 KiB
PHP
Raw Normal View History

2021-01-12 14:49:45 +00:00
<?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 = 1;")[0]->size;
2021-01-12 16:58:01 +00:00
$currentTraffic = DB::select("SELECT SUM(size) AS size FROM access WHERE tenant = 1 AND created_at >= NOW() - INTERVAL 1 DAY;")[0]->size;
2021-01-12 14:49:45 +00:00
$monthlyTraffic = DB::select("SELECT SUM(traffic) AS `traffic` FROM traffic WHERE `year` = 2021 AND `month` = 1 and tenant = 1;")[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];
}
}