kuvia/app/Models/Storage.php

65 lines
1.6 KiB
PHP
Raw Normal View History

2021-01-12 14:49:45 +00:00
<?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) {
2021-02-07 23:38:05 +00:00
$sql = "SELECT `date`, `tenant`, (sum(size)/24) AS size FROM `storage` WHERE `tenant` = ".session("current_tenant_id")." GROUP BY `date`, `tenant` ORDER BY `date` DESC LIMIT ".$days + 2.";";
2021-01-12 14:49:45 +00:00
$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;
}
}