This commit is contained in:
Kekskurse 2021-02-09 19:00:04 +01:00
parent 946281b56e
commit c1e859a81b
8 changed files with 198 additions and 1 deletions

View file

@ -0,0 +1,73 @@
<?php
namespace App\Console\Commands;
use App\Models\Storage;
use Illuminate\Console\Command;
class Server extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'server:set';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create Server and update';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$hostname = gethostname();
if($hostname === false) {
$this->info("Can't get hostname");
return self::FAILURE;
}
//Used cache
$size = $this->getUsedCached();
$server = \App\Models\Server::query()->where("servername", "=", $hostname)->first();
if(is_null($server)) {
$server = new \App\Models\Server();
$server->servername = $hostname;
$server->storage = disk_free_space(storage_path("cache")) - (1024*1000000) + $size;
}
$server->last_seen = date("Y-m-d H:i:s");
$server->userd = $size;
$server->saveOrFail();
return 0;
}
private function getUsedCached() {
$r = \Illuminate\Support\Facades\Storage::disk("cache")->allFiles();
$size = 0;
foreach ($r as $item) {
$size += \Illuminate\Support\Facades\Storage::disk("cache")->size($item);
}
return $size;
}
}

View file

@ -6,6 +6,7 @@ use App\Console\Commands\CalculateSpace;
use App\Console\Commands\CalculateTraffic;
use App\Console\Commands\EXIFRead;
use App\Console\Commands\ImageCacheAll;
use App\Console\Commands\Server;
use App\Console\Commands\ThemesScann;
use App\Console\Commands\ThemesScannTenant;
use App\Console\Commands\UserAdmin;
@ -25,7 +26,8 @@ class Kernel extends ConsoleKernel
ImageCacheAll::class,
EXIFRead::class,
UserAdmin::class,
ThemesScann::class
ThemesScann::class,
Server::class
];
/**
@ -41,6 +43,7 @@ class Kernel extends ConsoleKernel
$schedule->command('calculate:traffic')->everyFiveMinutes()->withoutOverlapping();
$schedule->command('exit:read')->everyMinute()->withoutOverlapping();
$schedule->command('theme:scan')->everyMinute()->withoutOverlapping();
$schedule->command("server:set")->everyMinute()->withoutOverlapping();
}
/**

View file

@ -2,12 +2,14 @@
namespace App\Http\Controllers;
use App\Models\Server;
use App\Models\Theme;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
class AdminController extends BaseController
@ -27,6 +29,13 @@ class AdminController extends BaseController
$theme->img_pixel = (bool)$request->input("img_pixel", false);
$theme->saveOrFail();
return Redirect::back();
}
public function serverView() {
if(Auth::user()->typ != "admin") {
abort(401);
}
$servers = Server::query()->get();
return view("admin.servers", ["servers" => $servers]);
}
}

42
app/Models/Server.php Normal file
View file

@ -0,0 +1,42 @@
<?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;
class Server extends Authenticatable
{
use HasFactory, Notifiable;
#protected $table = "server";
#public $timestamps = false;
/**
* 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 = [
];
}

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Server extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create("servers", function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string("servername");
$table->timestamp("last_seen")->nullable();
$table->bigInteger("storage")->comment("In Byte, for cache");
$table->bigInteger("userd")->comment("Userd Space in Byte")->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists("servers");
}
}

View file

@ -0,0 +1,33 @@
@extends('layout/template')
@section('content')
<div class="row" style="margin-top: 20px;">
<div class="col-md-12">
<h1>Servers</h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
<table class="table table-striped">
<tr>
<th style="width: 200px;">Servername</th>
<th>Storage</th>
</tr>
@foreach($servers as $s)
<tr>
<td>{{ $s->servername }}</td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: {{ round($s->storage / $s->userd) }}%;" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100">{{ round($s->storage / $s->userd) }}%</div>
</div>
</td>
</tr>
@endforeach
</table>
</div>
</div>
@endsection

View file

@ -40,6 +40,7 @@
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Admin</a>
<div class="dropdown-menu dropdown-menu-right">
<a class="dropdown-item" href="/a/themes">Themes</a>
<a class="dropdown-item" href="/a/server">Server</a>
</div>
</li>
@endif

View file

@ -59,6 +59,7 @@ Route::middleware([\App\Http\Middleware\TenanMiddleware::class, \App\Http\Middle
Route::get("a/themes", [\App\Http\Controllers\AdminController::class, "themesView"]);
Route::post("a/themes", [\App\Http\Controllers\AdminController::class, "themesSettings"]);
Route::get("a/server", [\App\Http\Controllers\AdminController::class, "serverView"]);
});