kuvia/app/Models/Storage.php

65 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
class Storage extends Authenticatable
{
use HasFactory, Notifiable;
protected $table = "storage";
public $timestamps = true;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
];
public static function getLastDays(int $days = 7) {
$sql = "SELECT `date`, `tenant`, (sum(size)/24) AS size FROM `storage` WHERE `tenant` = 1 GROUP BY `date`, `tenant` ORDER BY `date` DESC LIMIT ".$days.";";
$res = DB::select($sql);
$now = new \DateTime( $days." days ago", new \DateTimeZone('UTC'));
$interval = new \DateInterval( 'P1D'); // 1 Day interval
$period = new \DatePeriod( $now, $interval, $days); // 7 Days
$result = [];
foreach( $period as $day) {
$key = $day->format('Y-m-d');
$result[$day->format('Y-m-d')] = 0;
foreach($res as $r) {
if($r->date == $day->format('Y-m-d')) {
$result[$day->format('Y-m-d')] = $r->size / 1000 / 1000;
}
}
}
return $result;
}
}