From 63af49ba702746e6212fac825168eb1ab682a2a2 Mon Sep 17 00:00:00 2001 From: shibafu Date: Sun, 8 Nov 2020 23:41:51 +0900 Subject: [PATCH] =?UTF-8?q?=E6=A6=82=E6=B3=81=E8=A8=88=E7=AE=97=E3=81=A7di?= =?UTF-8?q?scard=20elapsed=20time=E3=82=92=E8=80=83=E6=85=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewComposers/ProfileStatsComposer.php | 50 ++++++++++++------- .../views/components/profile-stats.blade.php | 4 +- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/app/Http/ViewComposers/ProfileStatsComposer.php b/app/Http/ViewComposers/ProfileStatsComposer.php index ae632a2..6521333 100644 --- a/app/Http/ViewComposers/ProfileStatsComposer.php +++ b/app/Http/ViewComposers/ProfileStatsComposer.php @@ -19,6 +19,7 @@ class ProfileStatsComposer if (!$view->offsetExists('user')) { throw new \LogicException('View data "user" was not exist.'); } + /** @var \App\User $user */ $user = $view->offsetGet('user'); // 現在のオナ禁セッションの経過時間 @@ -35,35 +36,44 @@ class ProfileStatsComposer } // 概況欄のデータ取得 - $average = DB::select(<<<'SQL' + $average = 0; + $divisor = 0; + $averageSources = DB::select(<<<'SQL' SELECT - avg(span) AS average + extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span, + discard_elapsed_time FROM - ( - SELECT - extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span - FROM - ejaculations - WHERE - user_id = :user_id - ORDER BY - ejaculated_date DESC - LIMIT - 30 - ) AS temp + ejaculations +WHERE + user_id = :user_id +ORDER BY + ejaculated_date DESC +LIMIT + 30 SQL , ['user_id' => $user->id]); + foreach ($averageSources as $item) { + // 経過時間記録対象外のレコードがあったら、それより古いデータは平均の計算に加えない + if ($item->discard_elapsed_time) { + break; + } + $average += $item->span; + $divisor++; + } + if ($divisor > 0) { + $average /= $divisor; + } $summary = DB::select(<<<'SQL' SELECT max(span) AS longest, min(span) AS shortest, - sum(span) AS total_times, - count(*) AS total_checkins + sum(span) AS total_times FROM ( SELECT - extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span + extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span, + discard_elapsed_time FROM ejaculations WHERE @@ -71,9 +81,13 @@ FROM ORDER BY ejaculated_date DESC ) AS temp +WHERE + discard_elapsed_time = FALSE SQL , ['user_id' => $user->id]); - $view->with(compact('latestEjaculation', 'currentSession', 'average', 'summary')); + $total = $user->ejaculations()->count(); + + $view->with(compact('latestEjaculation', 'currentSession', 'average', 'summary', 'total')); } } diff --git a/resources/views/components/profile-stats.blade.php b/resources/views/components/profile-stats.blade.php index 09e936c..3d3b779 100644 --- a/resources/views/components/profile-stats.blade.php +++ b/resources/views/components/profile-stats.blade.php @@ -8,8 +8,8 @@ @endif
概況
-

平均記録: {{ Formatter::formatInterval($average[0]->average) }}

+

平均記録: {{ Formatter::formatInterval($average) }}

最長記録: {{ Formatter::formatInterval($summary[0]->longest) }}

最短記録: {{ Formatter::formatInterval($summary[0]->shortest) }}

合計時間: {{ Formatter::formatInterval($summary[0]->total_times) }}

-

通算回数: {{ number_format($summary[0]->total_checkins) }}回

\ No newline at end of file +

通算回数: {{ number_format($total) }}回