Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.67% |
23 / 30 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| AnniversaryUtils | |
76.67% |
23 / 30 |
|
50.00% |
1 / 2 |
6.46 | |
0.00% |
0 / 1 |
| getElevationStats | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
3 | |||
| getPrettySource | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Olz\Anniversary\Utils; |
| 4 | |
| 5 | use Olz\Utils\WithUtilsTrait; |
| 6 | |
| 7 | /** |
| 8 | * @phpstan-type OlzElevationStats array{ |
| 9 | * sumMeters: float, |
| 10 | * completion: float, |
| 11 | * diffMeters: float, |
| 12 | * diffDays: float, |
| 13 | * diffKind: 'ahead'|'behind', |
| 14 | * } |
| 15 | */ |
| 16 | class AnniversaryUtils { |
| 17 | use WithUtilsTrait; |
| 18 | |
| 19 | /** @return OlzElevationStats */ |
| 20 | public function getElevationStats() { |
| 21 | $db = $this->dbUtils()->getDb(); |
| 22 | |
| 23 | $is_before_2026 = intval($this->dateUtils()->getCurrentDateInFormat('Y')) < 2026; |
| 24 | $goal_meters_per_day = 4478; |
| 25 | $year_start_secs = 1767222000; // 2026-01-01 00:00:00 |
| 26 | $now_secs = $is_before_2026 ? $year_start_secs : strtotime($this->dateUtils()->getIsoNow()); |
| 27 | $sum_days = floatval($now_secs - $year_start_secs) / 86400; |
| 28 | $goal_meters = $sum_days * $goal_meters_per_day; |
| 29 | $sql = <<<'ZZZZZZZZZZ' |
| 30 | SELECT SUM(elevation_meters) as sum_meters |
| 31 | FROM anniversary_runs |
| 32 | WHERE |
| 33 | run_at >= '2026-01-01' |
| 34 | AND run_at <= '2026-12-31' |
| 35 | AND is_counting = '1' |
| 36 | AND on_off = '1' |
| 37 | ZZZZZZZZZZ; |
| 38 | $res_sum_meters = $db->query($sql); |
| 39 | $this->generalUtils()->checkNotBool($res_sum_meters, "Query error: {$sql}"); |
| 40 | $sum_meters = floatval($res_sum_meters->fetch_assoc()['sum_meters'] ?? 0); |
| 41 | |
| 42 | $completion = $sum_meters / ($goal_meters_per_day * 356); |
| 43 | $diff_meters = $sum_meters - $goal_meters; |
| 44 | $diff_days = $diff_meters / $goal_meters_per_day; |
| 45 | $diff_kind = $diff_days >= 0 ? 'ahead' : 'behind'; |
| 46 | |
| 47 | return [ |
| 48 | 'sumMeters' => $sum_meters, |
| 49 | 'completion' => $completion, |
| 50 | 'diffMeters' => $diff_meters, |
| 51 | 'diffDays' => $diff_days, |
| 52 | 'diffKind' => $diff_kind, |
| 53 | ]; |
| 54 | } |
| 55 | |
| 56 | public function getPrettySource(string $source): string { |
| 57 | $source_short = mb_split('-', $source)[0] ?? '?'; |
| 58 | if ($source_short === 'manuell') { |
| 59 | return "✍️ manuell"; |
| 60 | } |
| 61 | if ($source_short === 'strava') { |
| 62 | $code_href = $this->envUtils()->getCodeHref(); |
| 63 | return "<img src='{$code_href}assets/icns/strava_16.svg' alt='s' class='noborder'> strava"; |
| 64 | } |
| 65 | return "🤷 {$source_short}"; |
| 66 | } |
| 67 | } |