Skip to content

Commit a48d0b5

Browse files
committed
provider_dashboard_fix: Modified Query for better result searching
1 parent b03f505 commit a48d0b5

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

amp-laravel/app/Services/Provider/GetAverageVoltageService.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,35 @@
22

33
namespace App\Services\Provider;
44

5+
use DateTime;
56
use App\Models\Master;
67
use App\Models\Metric;
7-
use App\Models\User;
88

99
class GetAverageVoltageService
1010
{
11-
public static function getAverageVoltage(int $providerId): float
11+
public static function getAverageVoltage(int $providerId): array
1212
{
1313
$masterIds = Master::where('user_id', $providerId)->pluck('id');
14-
return round(Metric::whereIn('master_id', $masterIds)->avg('voltage'), 2);
14+
15+
$averageVoltageByDay = Metric::whereIn('master_id', $masterIds)
16+
->selectRaw('SUBSTRING(date_month, 1, 5) as day_month, AVG(voltage) as average_voltage')
17+
->groupBy('day_month')
18+
->orderBy('day_month')
19+
->get()
20+
->mapWithKeys(function ($item) {
21+
try {
22+
$date = DateTime::createFromFormat('m-d', $item->day_month);
23+
if ($date) {
24+
return [$date->format('Y-m-d') => round($item->average_voltage, 2)];
25+
}
26+
} catch (\Exception $e) {
27+
return [];
28+
}
29+
return [];
30+
})
31+
->filter()
32+
->toArray();
33+
34+
return $averageVoltageByDay;
1535
}
1636
}

0 commit comments

Comments
 (0)