Skip to content

Commit

Permalink
[IMP] Complex dashboard
Browse files Browse the repository at this point in the history
This dashboard adds more statistics and styles to look like a professional IoT dashboard.
Also, data is retrieved from database. Anyway, its pretended change that behavior to livewire components to make real time dashboard.

* [IMP] Remove header from dashboard
* [ADD] Board boxes status examples
* [ADD] No active sessions registered and styles
* [ADD] Change link boxes to statistics section
* [ADD] Statistics boxes (not finished)
* [FIX] Measurements seeder was creating just 5 session entries. Fixed to all sessions
* [ADD] Show last 4 boards with most recent activity
* [ADD] Statistics boxes with data database information
* [IMP] Statistics colors and styles
  • Loading branch information
rick-astral-cat authored Oct 14, 2023
1 parent 75c6795 commit 5335401
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 52 deletions.
49 changes: 49 additions & 0 deletions app/Http/Controllers/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace App\Http\Controllers;

use App\Models\Board;
use App\Models\Measurement;
use App\Models\Session;
use App\Services\BoardService;
use App\Services\ProfileService;
use App\Services\SessionService;
Expand Down Expand Up @@ -38,10 +41,56 @@ public function dashboard(): View
$xAxis = $xAxis ?? [];
$yAxis = $yAxis ?? [];

//Get last 4 boards with recent activity
$recentBoards = Board::orderBy('last_seen', 'desc')->limit(4)->get();
$recentBoards = $recentBoards->map(function ($board) {
return ['name' => $board->name, 'uuid' => $board->uuid, 'ip' => $board->ip, 'last_seen' => $board->last_seen];
});

$statistics = collect();

//Max temperature registered
$maxTempReg = Measurement::max('temperature');
$statistics->put('Max.Temp.Reg.', ['data' => $maxTempReg.' °C', 'class' => 'afterMaxTemp']);

//Largest session
$allMeasurements = Measurement::get()->groupBy('session_id');
$max = 0;
$sessionKey = null;
foreach ($allMeasurements as $sk => $m) {
$current = $m->count();
if ($current > $max) {
$sessionKey = $sk;
$max = $current;
}
}
$statistics->put('Longest Session', ['data' => CarbonInterval::seconds($max)->cascade()->forHumans(), 'class' => 'afterLongSession']);

//Max temperature of the day
$maxTempToday = Measurement::whereDay('created_at', (string) (now()->day))->max('temperature');
$maxTempToday = $maxTempToday != null ? $maxTempToday.' °C' : 'N/A';
$statistics->put('Max.Temp.Today', ['data' => $maxTempToday, 'class' => 'afterTodayTemp']);

//Board with most sessions
$sessions = Session::with('board')->orderBy('id', 'desc')->get()->groupBy('board_id');
$boardMostSessions = null;
$boardSessionCount = 0;
foreach ($sessions as $boardId => $s) {
$count = $s->count();
if ($count > $boardSessionCount) {
$boardSessionCount = $count;
$boardMostSessions = $s[0]->board ?? null;
}
}
$boardName = $boardMostSessions->name ?? 'N/A';
$statistics->put('Most sessions', ['data' => $boardName.' ('.$boardSessionCount.')', 'class' => 'afterMostSessions']);

return view('dashboard', [
'session' => $session,
'xAxis' => $xAxis,
'yAxis' => $yAxis,
'recentBoards' => $recentBoards,
'statistics' => $statistics,
'boardsCount' => $this->boardService->boardsCount(),
'profilesCount' => $this->profileService->profilesCount(),
'sessionsCount' => $this->sessionService->sessionsCount(),
Expand Down
8 changes: 4 additions & 4 deletions app/Models/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Carbon;

/**
Expand Down Expand Up @@ -59,11 +59,11 @@ class Session extends Model
];

/**
* @return HasOne<Board>
* @return BelongsTo<Board, Session>
*/
public function board(): HasOne
public function board(): BelongsTo
{
return $this->hasOne(Board::class);
return $this->belongsTo(Board::class);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion database/seeders/MeasurementSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private function randomFloat(float $min, float $max)
*/
public function run(): void
{
foreach (Session::where('id', '>', '0')->limit(5)->get() as $session) {
foreach (Session::get() as $session) {
$temp = 20;
$sequence = 1;

Expand Down
Binary file added public/img/circuit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions public/img/circuit_package.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
50 changes: 50 additions & 0 deletions public/img/circuit_package_white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5335401

Please sign in to comment.