Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions amp-laravel/app/Models/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ class Metric extends Model
'date_month',
];

protected $casts = [
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

public function master()
{
return $this->belongsTo(Master::class);
Expand Down
48 changes: 26 additions & 22 deletions amp-laravel/app/Services/Client/ClientDashboardService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use DateTimeZone;
use App\Models\Metric;
use Illuminate\Support\Collection;

class ClientDashboardService
{
Expand All @@ -13,39 +14,42 @@ public static function getDashboardData($slaveId): array
$timezone = new DateTimeZone('Asia/Beirut');
$startOfMonth = (new DateTime('first day of this month', $timezone))->format('Y-m-d H:i:s');
$endOfMonth = (new DateTime('last day of this month', $timezone))->format('Y-m-d 23:59:59');
$currentYear = now()->year;

// Fetch power usage per day for the current month
$powerUsagePerDayData = Metric::where('slave_id', $slaveId)
// Fetch all relevant data ordered by date_month and then created_at
$allMetrics = Metric::where('slave_id', $slaveId)
->whereBetween('created_at', [$startOfMonth, $endOfMonth])
->selectRaw('DATE(created_at) as date, SUM(power) as total_power')
->groupByRaw('DATE(created_at)')
->orderByRaw('DATE(created_at)')
->orderBy('date_month')
->orderBy('created_at')
->get();

$powerUsagePerDay = $powerUsagePerDayData->pluck('total_power', 'date')->toArray();
// Group by the combined year and date_month using createFromFormat
$groupedByDateMonth = $allMetrics->groupBy(function ($item) use ($currentYear) {
$date = DateTime::createFromFormat('m-d', $item->date_month);
if ($date) {
$date->setDate($currentYear, $date->format('m'), $date->format('d'));
return $date->format('Y-m-d');
}
return null; // Handle cases where parsing fails
})->filter(); // Remove any null groupings

// Fetch cumulative power usage per day for the current month
$cumulativePowerUsageData = Metric::where('slave_id', $slaveId)
->whereBetween('created_at', [$startOfMonth, $endOfMonth])
->orderBy('created_at')
->selectRaw('DATE(created_at) as date, power')
->get();
$powerUsagePerDay = $groupedByDateMonth->map(function (Collection $dailyMetrics) {
return round($dailyMetrics->sum('power'), 2);
})->toArray();

$cumulativePower = 0;
$cumulativePowerUsage = $cumulativePowerUsageData->mapWithKeys(function ($item, $key) use (&$cumulativePower) {
$cumulativePower += $item['power'];
return [$item['date'] => round($cumulativePower, 2)];
$cumulativePowerUsage = $groupedByDateMonth->map(function (Collection $dailyMetrics) {
$cumulative = 0;
return $dailyMetrics->map(function ($metric) use (&$cumulative) {
$cumulative += $metric->power;
return round($cumulative, 2);
})->toArray();
})->toArray();

// Calculate total power usage this month
$totalPowerUsageThisMonth = Metric::where('slave_id', $slaveId)
->whereBetween('created_at', [$startOfMonth, $endOfMonth])
->sum('power');
$totalPowerUsageThisMonth = $allMetrics->sum('power');

// Calculate average voltage reach
$averageVoltageReach = Metric::where('slave_id', $slaveId)
->whereBetween('created_at', [$startOfMonth, $endOfMonth])
->avg('voltage');
$averageVoltageReach = $allMetrics->avg('voltage');

// Expected power limit
$expectedPowerLimit = 100;
Expand Down
Loading