Skip to content

Commit 0d20508

Browse files
authored
Merge pull request #139 from Riyad-Murad/laravel_test_and_seeds_fix
Laravel test and seeds fix
2 parents 8a6bf9d + 1ac9da4 commit 0d20508

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

amp-laravel/app/Models/Metric.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Metric extends Model
1919
'date_month',
2020
];
2121

22+
protected $casts = [
23+
'created_at' => 'datetime',
24+
'updated_at' => 'datetime',
25+
];
26+
2227
public function master()
2328
{
2429
return $this->belongsTo(Master::class);

amp-laravel/app/Services/Client/ClientDashboardService.php

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use DateTime;
66
use DateTimeZone;
77
use App\Models\Metric;
8+
use Illuminate\Support\Collection;
89

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

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

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

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

34-
$cumulativePower = 0;
35-
$cumulativePowerUsage = $cumulativePowerUsageData->mapWithKeys(function ($item, $key) use (&$cumulativePower) {
36-
$cumulativePower += $item['power'];
37-
return [$item['date'] => round($cumulativePower, 2)];
40+
$cumulativePowerUsage = $groupedByDateMonth->map(function (Collection $dailyMetrics) {
41+
$cumulative = 0;
42+
return $dailyMetrics->map(function ($metric) use (&$cumulative) {
43+
$cumulative += $metric->power;
44+
return round($cumulative, 2);
45+
})->toArray();
3846
})->toArray();
3947

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

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

5054
// Expected power limit
5155
$expectedPowerLimit = 100;

0 commit comments

Comments
 (0)