-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStatisticsService.php
More file actions
66 lines (60 loc) · 2.06 KB
/
Copy pathStatisticsService.php
File metadata and controls
66 lines (60 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace App\Services;
use App\Experiments;
use App\Membrana;
use App\Trayectoria;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
/**
* Read-only aggregate statistics for the database.
*
* Ported from StatisticsController so it can be reused by the MCP server without
* rendering a view.
*/
class StatisticsService
{
/**
* Cached high-level totals for the database.
*
* @return array<string, mixed>
*/
public function totals(): array
{
$totals = Cache::remember('statistics.totals', now()->addHours(6), function () {
return [
'totalTrayectorias' => Trayectoria::select('id')->count(),
'totalMembranas' => Membrana::select('id')->count(),
'totalExperiments' => Experiments::select('id')->count(),
'lastUpdate' => DB::table('update_record')->latest('updated_at')->first(),
];
});
$lastUpdate = $totals['lastUpdate'];
$lastUpdatedAt = null;
if ($lastUpdate && $lastUpdate->updated_at) {
$lastUpdatedAt = Carbon::parse($lastUpdate->updated_at, 'UTC')
->setTimezone(config('app.timezone'))
->toIso8601String();
}
return [
'total_trajectories' => $totals['totalTrayectorias'],
'total_membranes' => $totals['totalMembranas'],
'total_experiments' => $totals['totalExperiments'],
'last_update' => $lastUpdatedAt,
];
}
/**
* Force-field breakdown: number of membranes per force field.
*
* @return array<int, array{name:string, total:int}>
*/
public function forceFieldBreakdown(): array
{
return Membrana::groupBy('forcefields.name')
->join('forcefields', 'membranes.forcefield_id', '=', 'forcefields.id')
->select('forcefields.name', DB::raw('count(*) as total'))
->get()
->map(fn ($row) => ['name' => $row->name, 'total' => (int) $row->total])
->all();
}
}