kuvia/app/Console/Commands/CalculateTraffic.php

115 lines
3.3 KiB
PHP
Raw Normal View History

2021-01-12 14:49:45 +00:00
<?php
namespace App\Console\Commands;
use App\Models\Access;
use App\Models\Traffic;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class CalculateTraffic extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'calculate:traffic';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Recalculate the Traffic for all Access Data';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$result = [];
$res = DB::select("SELECT * FROM access");
foreach($res as $r) {
2021-01-21 15:05:26 +00:00
$key = $r->tenant."-".$r->gallery."-".$r->year."-".$r->month."-".$r->day."-".$r->hour."-".$r->typ;
2021-01-12 14:49:45 +00:00
if(isset($result[$key])) {
$result[$key] = $result[$key] + $r->size;
} else {
$result[$key] = $r->size;
}
}
//Save result in db
var_dump($result);
foreach ($result as $key => $trafficSize) {
$split = explode("-", $key);
2021-01-21 14:05:24 +00:00
if(empty($split[1])) {
$traffic = Traffic::query()
->where("tenant", "=", (int)$split[0])
->whereNull("gallery")
->where("year", "=", (int)$split[2])
->where("month", "=", (int)$split[3])
->where("day", "=", (int)$split[4])
->where("hour", "=", (int)$split[5])
2021-01-21 15:05:26 +00:00
->where("typ", "=", $this->getTyp($split[6]))
2021-01-21 14:05:24 +00:00
->first();
} else {
$traffic = Traffic::query()
->where("tenant", "=", (int)$split[0])
->where("gallery", "=", (int)$split[1])
->where("year", "=", (int)$split[2])
->where("month", "=", (int)$split[3])
->where("day", "=", (int)$split[4])
->where("hour", "=", (int)$split[5])
2021-01-21 15:05:26 +00:00
->where("typ", "=", $this->getTyp($split[6]))
2021-01-21 14:05:24 +00:00
->first();
}
2021-01-12 14:49:45 +00:00
if($traffic == null) {
$traffic = new Traffic();
$traffic->tenant = $split[0];
$traffic->gallery = $split[1];
$traffic->year = $split[2];
$traffic->month =$split[3];
$traffic->day = $split[4];
$traffic->hour = $split[5];
2021-01-21 15:05:26 +00:00
$traffic->typ = $this->getTyp($split[6]);
2021-01-12 14:49:45 +00:00
$traffic->date = $split[2]."-".$split[3]."-".$split[4];
}
2021-01-21 00:13:41 +00:00
if($traffic->gallery == "") {
$traffic->gallery = null;
}
if($traffic->tenant == "") {
continue;
}
2021-01-12 14:49:45 +00:00
if($traffic->traffic != $trafficSize) {
$traffic->traffic = $trafficSize;
$traffic->saveOrFail();
}
}
}
2021-01-21 15:05:26 +00:00
private function getTyp($typ) {
if($typ == "Access") {
return "s3";
}
if($typ == "Cache") {
return "cache";
}
return "generic";
}
2021-01-12 14:49:45 +00:00
}