Skip to content

Commit

Permalink
Added highest CPU usage past 24 hours feature
Browse files Browse the repository at this point in the history
  • Loading branch information
cp6 committed Oct 7, 2023
1 parent 527adee commit b0bcc5e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/ServerUsageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Server;
use App\Models\ServerUsage;
use Carbon\Carbon;
use Inertia\Inertia;

class ServerUsageController extends Controller
Expand Down Expand Up @@ -35,8 +36,11 @@ public function ramUsage(Server $server): \Inertia\Response

public function cpuUsage(Server $server): \Inertia\Response
{
$high_24h = ServerUsage::where('server_id', $server->id)->where('created_at', '>=', Carbon::now()->subDay()->toDateTimeString())
->select(['cpu_usage', 'created_at'])->orderBy('cpu_usage', 'desc')->first();
return Inertia::render('Servers/Usage/Cpu', [
'resource' => $server,
'high_24h' => $high_24h,
'usage' => ServerUsage::where('server_id', $server->id)->select(['cpu_usage', 'created_at'])->orderBy('id', 'desc')->take(720)->get()
]);
}
Expand Down
7 changes: 4 additions & 3 deletions resources/js/Pages/Servers/Usage/Cpu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ export default function Cpu({auth}) {

const resource = usePage().props.resource;
const usage = usePage().props.usage;
const high = usePage().props.high_24h;

const time = usage.map((d) => format(new Date(d.created_at), 'hh:mma do LLL yyyy'));
const time = usage.map((d) => format(new Date(d.created_at), 'hh:mma do LLL yyyy'));
const usage_values = usage.map((value) => Math.round(value.cpu_usage * 10));

const data = {
Expand Down Expand Up @@ -89,7 +90,6 @@ export default function Cpu({auth}) {
}
};


return (
<AuthenticatedLayout
auth={auth}
Expand All @@ -105,6 +105,8 @@ export default function Cpu({auth}) {
</div>

<section className="bg-white/50 dark:bg-gray-700 rounded-lg shadow-sm">
<p className="p-2 text-gray-700 dark:text-gray-400">Highest usage past
24H: {high.cpu_usage} at {format(new Date(high.created_at), 'hh:mma')}</p>
<div className="w-full">
<Chart
options={data.options}
Expand All @@ -117,7 +119,6 @@ export default function Cpu({auth}) {
</section>
</div>


</AuthenticatedLayout>
);
}

0 comments on commit b0bcc5e

Please sign in to comment.