|
|
@@ -160,7 +160,11 @@ class HomePageController extends Controller
|
|
160
|
160
|
$yearStart = $now->copy()->startOfYear();
|
|
161
|
161
|
$prevYearEnd = $now->copy()->subYear()->endOfDay();
|
|
162
|
162
|
|
|
163
|
|
- $giorniBrevi = [1 => 'lun', 2 => 'mar', 3 => 'mer', 4 => 'gio', 5 => 'ven', 6 => 'sab', 7 => 'dom'];
|
|
|
163
|
+ $mediaDa = $today->copy()->subYear();
|
|
|
164
|
+ $mediaGiornaliera = $this->mediaMinutiPerGiorno($byDay, $mediaDa, $today);
|
|
|
165
|
+ $giorniSettimana = $weekOffsetDays + 1;
|
|
|
166
|
+ $giorniMese = (int) $now->day;
|
|
|
167
|
+ $giorniAnno = (int) $now->dayOfYear;
|
|
164
|
168
|
|
|
165
|
169
|
return [
|
|
166
|
170
|
$this->buildTempoPeriodo(
|
|
|
@@ -174,8 +178,8 @@ class HomePageController extends Controller
|
|
174
|
178
|
$now,
|
|
175
|
179
|
$today->copy()->subDay(),
|
|
176
|
180
|
$today->copy()->subDay()->endOfDay(),
|
|
177
|
|
- $this->mediaMinutiGiornaliera($byDay, $lookbackStart, $today, (int) $now->isoWeekday()),
|
|
178
|
|
- 'media '.$giorniBrevi[(int) $now->isoWeekday()],
|
|
|
181
|
+ $mediaGiornaliera,
|
|
|
182
|
+ 'media gg',
|
|
179
|
183
|
),
|
|
180
|
184
|
$this->buildTempoPeriodo(
|
|
181
|
185
|
'Settimana',
|
|
|
@@ -188,7 +192,7 @@ class HomePageController extends Controller
|
|
188
|
192
|
$now,
|
|
189
|
193
|
$prevWeekStart,
|
|
190
|
194
|
$prevWeekEnd,
|
|
191
|
|
- $this->mediaMinutiSettimanale($byDay, $lookbackStart, $weekStart, $weekOffsetDays),
|
|
|
195
|
+ $this->mediaProporzionata($mediaGiornaliera, $giorniSettimana),
|
|
192
|
196
|
'media sett.',
|
|
193
|
197
|
),
|
|
194
|
198
|
$this->buildTempoPeriodo(
|
|
|
@@ -202,7 +206,7 @@ class HomePageController extends Controller
|
|
202
|
206
|
$now,
|
|
203
|
207
|
$prevMonthStart,
|
|
204
|
208
|
$prevMonthSameDay,
|
|
205
|
|
- $this->mediaMinutiMensile($byDay, $lookbackStart, $monthStart, (int) $now->day),
|
|
|
209
|
+ $this->mediaProporzionata($mediaGiornaliera, $giorniMese),
|
|
206
|
210
|
'media mesi',
|
|
207
|
211
|
),
|
|
208
|
212
|
$this->buildTempoPeriodo(
|
|
|
@@ -216,7 +220,7 @@ class HomePageController extends Controller
|
|
216
|
220
|
$now,
|
|
217
|
221
|
$yearStart->copy()->subYear(),
|
|
218
|
222
|
$prevYearEnd,
|
|
219
|
|
- $this->mediaMinutiAnnuale($byDay, $lookbackStart, $yearStart, $now),
|
|
|
223
|
+ $this->mediaProporzionata($mediaGiornaliera, $giorniAnno),
|
|
220
|
224
|
'media anni',
|
|
221
|
225
|
),
|
|
222
|
226
|
];
|
|
|
@@ -325,20 +329,17 @@ class HomePageController extends Controller
|
|
325
|
329
|
}
|
|
326
|
330
|
|
|
327
|
331
|
/**
|
|
328
|
|
- * Media dei giorni precedenti con lo stesso weekday (es. media dei martedì).
|
|
329
|
|
- * I giorni senza registrazioni contano come 0.
|
|
|
332
|
+ * Media minuti al giorno (tutti i giorni, weekend compresi) nel periodo [from, untilExclusive).
|
|
330
|
333
|
*
|
|
331
|
334
|
* @param array<string, int> $byDay
|
|
332
|
335
|
* @return array{minuti: int, campioni: int}
|
|
333
|
336
|
*/
|
|
334
|
|
- private function mediaMinutiGiornaliera(array $byDay, Carbon $lookbackStart, Carbon $today, int $isoWeekday): array
|
|
|
337
|
+ private function mediaMinutiPerGiorno(array $byDay, Carbon $from, Carbon $untilExclusive): array
|
|
335
|
338
|
{
|
|
336
|
339
|
$sum = 0;
|
|
337
|
340
|
$campioni = 0;
|
|
338
|
|
- for ($d = $lookbackStart->copy()->startOfDay(); $d->lt($today); $d->addDay()) {
|
|
339
|
|
- if ((int) $d->isoWeekday() !== $isoWeekday) {
|
|
340
|
|
- continue;
|
|
341
|
|
- }
|
|
|
341
|
+ $end = $untilExclusive->copy()->startOfDay();
|
|
|
342
|
+ for ($d = $from->copy()->startOfDay(); $d->lt($end); $d->addDay()) {
|
|
342
|
343
|
$sum += $byDay[$d->toDateString()] ?? 0;
|
|
343
|
344
|
$campioni++;
|
|
344
|
345
|
}
|
|
|
@@ -347,67 +348,17 @@ class HomePageController extends Controller
|
|
347
|
348
|
}
|
|
348
|
349
|
|
|
349
|
350
|
/**
|
|
350
|
|
- * Media delle settimane precedenti, stessa porzione lun → weekday corrente.
|
|
351
|
|
- *
|
|
352
|
|
- * @param array<string, int> $byDay
|
|
353
|
|
- * @return array{minuti: int, campioni: int}
|
|
354
|
|
- */
|
|
355
|
|
- private function mediaMinutiSettimanale(array $byDay, Carbon $lookbackStart, Carbon $weekStart, int $weekOffsetDays): array
|
|
356
|
|
- {
|
|
357
|
|
- $sum = 0;
|
|
358
|
|
- $campioni = 0;
|
|
359
|
|
- for ($cursor = $weekStart->copy()->subWeek(); $cursor->gte($lookbackStart); $cursor->subWeek()) {
|
|
360
|
|
- $from = $cursor->copy()->startOfDay();
|
|
361
|
|
- $to = $cursor->copy()->addDays($weekOffsetDays)->endOfDay();
|
|
362
|
|
- $sum += $this->sumMinutiInRange($byDay, $from, $to);
|
|
363
|
|
- $campioni++;
|
|
364
|
|
- }
|
|
365
|
|
-
|
|
366
|
|
- return $this->mediaFromSum($sum, $campioni);
|
|
367
|
|
- }
|
|
368
|
|
-
|
|
369
|
|
- /**
|
|
370
|
|
- * Media dei mesi precedenti, dal 1 al medesimo giorno del mese.
|
|
|
351
|
+ * Proporziona la media giornaliera ai giorni già trascorsi nel periodo (es. 23, non 30/31).
|
|
371
|
352
|
*
|
|
372
|
|
- * @param array<string, int> $byDay
|
|
|
353
|
+ * @param array{minuti: int, campioni: int} $mediaGiornaliera
|
|
373
|
354
|
* @return array{minuti: int, campioni: int}
|
|
374
|
355
|
*/
|
|
375
|
|
- private function mediaMinutiMensile(array $byDay, Carbon $lookbackStart, Carbon $monthStart, int $dayOfMonth): array
|
|
|
356
|
+ private function mediaProporzionata(array $mediaGiornaliera, int $giorni): array
|
|
376
|
357
|
{
|
|
377
|
|
- $sum = 0;
|
|
378
|
|
- $campioni = 0;
|
|
379
|
|
- for ($cursor = $monthStart->copy()->subMonth(); $cursor->gte($lookbackStart); $cursor->subMonth()) {
|
|
380
|
|
- $from = $cursor->copy()->startOfDay();
|
|
381
|
|
- $day = min($dayOfMonth, (int) $cursor->copy()->endOfMonth()->day);
|
|
382
|
|
- $to = $cursor->copy()->day($day)->endOfDay();
|
|
383
|
|
- $sum += $this->sumMinutiInRange($byDay, $from, $to);
|
|
384
|
|
- $campioni++;
|
|
385
|
|
- }
|
|
386
|
|
-
|
|
387
|
|
- return $this->mediaFromSum($sum, $campioni);
|
|
388
|
|
- }
|
|
389
|
|
-
|
|
390
|
|
- /**
|
|
391
|
|
- * Media degli anni precedenti, dal 1° gennaio alla medesima data.
|
|
392
|
|
- *
|
|
393
|
|
- * @param array<string, int> $byDay
|
|
394
|
|
- * @return array{minuti: int, campioni: int}
|
|
395
|
|
- */
|
|
396
|
|
- private function mediaMinutiAnnuale(array $byDay, Carbon $lookbackStart, Carbon $yearStart, Carbon $now): array
|
|
397
|
|
- {
|
|
398
|
|
- $sum = 0;
|
|
399
|
|
- $campioni = 0;
|
|
400
|
|
- for ($yearsBack = 1; ; $yearsBack++) {
|
|
401
|
|
- $from = $yearStart->copy()->subYears($yearsBack);
|
|
402
|
|
- if ($from->lt($lookbackStart)) {
|
|
403
|
|
- break;
|
|
404
|
|
- }
|
|
405
|
|
- $to = $now->copy()->subYears($yearsBack)->endOfDay();
|
|
406
|
|
- $sum += $this->sumMinutiInRange($byDay, $from, $to);
|
|
407
|
|
- $campioni++;
|
|
408
|
|
- }
|
|
409
|
|
-
|
|
410
|
|
- return $this->mediaFromSum($sum, $campioni);
|
|
|
358
|
+ return [
|
|
|
359
|
+ 'minuti' => ((int) ($mediaGiornaliera['minuti'] ?? 0)) * max(0, $giorni),
|
|
|
360
|
+ 'campioni' => (int) ($mediaGiornaliera['campioni'] ?? 0),
|
|
|
361
|
+ ];
|
|
411
|
362
|
}
|
|
412
|
363
|
|
|
413
|
364
|
/**
|