0, 'total_entries_examined' => 0, 'total_candidates_returned' => 0, 'total_duration_ms' => 0, 'avg_reduction_percent' => 0, 'last_reset' => abj_clock()->now(), ]; $statsRaw = get_option(self::OPTION_KEY, $defaultStats); /** @var array $stats */ $stats = is_array($statsRaw) ? $statsRaw : $defaultStats; $stats['total_queries'] = (isset($stats['total_queries']) && is_numeric($stats['total_queries'])) ? (int)$stats['total_queries'] + 1 : 1; $stats['total_entries_examined'] = (isset($stats['total_entries_examined']) && is_numeric($stats['total_entries_examined'])) ? (int)$stats['total_entries_examined'] + $examined : $examined; $stats['total_candidates_returned'] = (isset($stats['total_candidates_returned']) && is_numeric($stats['total_candidates_returned'])) ? (int)$stats['total_candidates_returned'] + $candidates : $candidates; $stats['total_duration_ms'] = (isset($stats['total_duration_ms']) && is_numeric($stats['total_duration_ms'])) ? (float)$stats['total_duration_ms'] + $duration : $duration; if ($totalInCache > 0) { $reductionPercent = (($totalInCache - $examined) / $totalInCache) * 100; $prevAvgReduction = (isset($stats['avg_reduction_percent']) && is_numeric($stats['avg_reduction_percent'])) ? (float)$stats['avg_reduction_percent'] : 0; $totalQueries = (int)$stats['total_queries']; $stats['avg_reduction_percent'] = (($prevAvgReduction * ($totalQueries - 1)) + $reductionPercent) / $totalQueries; } $monthAgo = abj_clock()->now() - self::RESET_INTERVAL_SECONDS; $lastReset = (isset($stats['last_reset']) && is_numeric($stats['last_reset'])) ? (int)$stats['last_reset'] : 0; if ($lastReset < $monthAgo) { $stats = [ 'total_queries' => 1, 'total_entries_examined' => $examined, 'total_candidates_returned' => $candidates, 'total_duration_ms' => $duration, 'avg_reduction_percent' => ($totalInCache > 0) ? (($totalInCache - $examined) / $totalInCache) * 100 : 0, 'last_reset' => abj_clock()->now(), ]; } update_option(self::OPTION_KEY, $stats); } /** * Get usage statistics with derived per-query averages. * * @return array */ public function getUsageStats() { $defaultStats = [ 'total_queries' => 0, 'total_entries_examined' => 0, 'total_candidates_returned' => 0, 'total_duration_ms' => 0, 'avg_reduction_percent' => 0, 'last_reset' => abj_clock()->now(), ]; $statsRaw = get_option(self::OPTION_KEY, $defaultStats); /** @var array $stats */ $stats = is_array($statsRaw) ? $statsRaw : $defaultStats; $totalQueries = (isset($stats['total_queries']) && is_numeric($stats['total_queries'])) ? (int)$stats['total_queries'] : 0; $totalExamined = (isset($stats['total_entries_examined']) && is_numeric($stats['total_entries_examined'])) ? (float)$stats['total_entries_examined'] : 0; $totalCandidates = (isset($stats['total_candidates_returned']) && is_numeric($stats['total_candidates_returned'])) ? (float)$stats['total_candidates_returned'] : 0; $totalDuration = (isset($stats['total_duration_ms']) && is_numeric($stats['total_duration_ms'])) ? (float)$stats['total_duration_ms'] : 0; if ($totalQueries > 0) { $stats['avg_examined_per_query'] = round($totalExamined / $totalQueries, 1); $stats['avg_candidates_per_query'] = round($totalCandidates / $totalQueries, 1); $stats['avg_duration_ms'] = round($totalDuration / $totalQueries, 2); } else { $stats['avg_examined_per_query'] = 0; $stats['avg_candidates_per_query'] = 0; $stats['avg_duration_ms'] = 0; } return $stats; } }