概況計算でdiscard elapsed timeを考慮

This commit is contained in:
shibafu 2020-11-08 23:41:51 +09:00
parent 3825228344
commit 63af49ba70
2 changed files with 34 additions and 20 deletions

View File

@ -19,6 +19,7 @@ class ProfileStatsComposer
if (!$view->offsetExists('user')) { if (!$view->offsetExists('user')) {
throw new \LogicException('View data "user" was not exist.'); throw new \LogicException('View data "user" was not exist.');
} }
/** @var \App\User $user */
$user = $view->offsetGet('user'); $user = $view->offsetGet('user');
// 現在のオナ禁セッションの経過時間 // 現在のオナ禁セッションの経過時間
@ -35,35 +36,44 @@ class ProfileStatsComposer
} }
// 概況欄のデータ取得 // 概況欄のデータ取得
$average = DB::select(<<<'SQL' $average = 0;
$divisor = 0;
$averageSources = DB::select(<<<'SQL'
SELECT 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 FROM
(
SELECT
extract(epoch from ejaculated_date - lead(ejaculated_date, 1, NULL) OVER (ORDER BY ejaculated_date DESC)) AS span
FROM
ejaculations ejaculations
WHERE WHERE
user_id = :user_id user_id = :user_id
ORDER BY ORDER BY
ejaculated_date DESC ejaculated_date DESC
LIMIT LIMIT
30 30
) AS temp
SQL SQL
, ['user_id' => $user->id]); , ['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' $summary = DB::select(<<<'SQL'
SELECT SELECT
max(span) AS longest, max(span) AS longest,
min(span) AS shortest, min(span) AS shortest,
sum(span) AS total_times, sum(span) AS total_times
count(*) AS total_checkins
FROM FROM
( (
SELECT 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 FROM
ejaculations ejaculations
WHERE WHERE
@ -71,9 +81,13 @@ FROM
ORDER BY ORDER BY
ejaculated_date DESC ejaculated_date DESC
) AS temp ) AS temp
WHERE
discard_elapsed_time = FALSE
SQL SQL
, ['user_id' => $user->id]); , ['user_id' => $user->id]);
$view->with(compact('latestEjaculation', 'currentSession', 'average', 'summary')); $total = $user->ejaculations()->count();
$view->with(compact('latestEjaculation', 'currentSession', 'average', 'summary', 'total'));
} }
} }

View File

@ -8,8 +8,8 @@
@endif @endif
<h6 class="font-weight-bold"><span class="oi oi-graph"></span> 概況</h6> <h6 class="font-weight-bold"><span class="oi oi-graph"></span> 概況</h6>
<p class="card-text mb-0">平均記録: {{ Formatter::formatInterval($average[0]->average) }}</p> <p class="card-text mb-0">平均記録: {{ Formatter::formatInterval($average) }}</p>
<p class="card-text mb-0">最長記録: {{ Formatter::formatInterval($summary[0]->longest) }}</p> <p class="card-text mb-0">最長記録: {{ Formatter::formatInterval($summary[0]->longest) }}</p>
<p class="card-text mb-0">最短記録: {{ Formatter::formatInterval($summary[0]->shortest) }}</p> <p class="card-text mb-0">最短記録: {{ Formatter::formatInterval($summary[0]->shortest) }}</p>
<p class="card-text mb-0">合計時間: {{ Formatter::formatInterval($summary[0]->total_times) }}</p> <p class="card-text mb-0">合計時間: {{ Formatter::formatInterval($summary[0]->total_times) }}</p>
<p class="card-text">通算回数: {{ number_format($summary[0]->total_checkins) }}</p> <p class="card-text">通算回数: {{ number_format($total) }}</p>