true, 'reason' => ...]`. */ public const SKIPPED_RESULT_PREFIX = '{"skipped":true'; /** * How a result Core\Database\JobResultCleaner compacted starts. Compacting * drops the marker of a skip that "only fill empty" kept, so such a row * can no longer be told from real work and every compacted row is left * out. The cleaner compacts after 30 days by default, the same as * WINDOW_DAYS; a site that shortens that retention shortens the window. */ public const COMPACTED_RESULT_PREFIX = '{"compacted":true'; /** Fewer counted products than this is not a usual speed: one tiny job proves nothing. */ public const MIN_PRODUCTS = 20; /** Less working time than this is a burst, not a usual speed. */ public const MIN_WORKING_SECONDS = 600; /** * Products per hour measured from this store's recent finished work of * one tool. * * @param object $wpdb WordPress database object. * @param string $tool_type The job's tool, one of AutomationJob::VALID_TOOL_TYPES. * @return float|null Null for an unknown tool, too little history of that tool, or a failed query. */ public static function measure($wpdb, string $tool_type) { if (!in_array($tool_type, AutomationJob::VALID_TOOL_TYPES, true)) { return null; } $cache_key = self::CACHE_KEY_PREFIX . $tool_type; $cached = get_transient($cache_key); if (is_array($cached) && array_key_exists('speed', $cached)) { return null === $cached['speed'] ? null : (float) $cached['speed']; } $products = $wpdb->prefix . 'aipt_job_products'; $jobs = $wpdb->prefix . 'aipt_automation_jobs'; $cutoff = gmdate('Y-m-d H:i:s', time() - self::WINDOW_DAYS * DAY_IN_SECONDS); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table names from $wpdb->prefix; the values use placeholders; no WP API for these custom tables. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.job_id, p.processed_at FROM {$products} p INNER JOIN {$jobs} j ON j.id = p.job_id WHERE p.status = 'completed' AND p.processed_at IS NOT NULL AND p.processed_at >= %s AND j.tool_type = %s AND (p.result IS NULL OR (p.result NOT LIKE %s AND p.result NOT LIKE %s)) ORDER BY p.processed_at DESC LIMIT %d", $cutoff, $tool_type, $wpdb->esc_like(self::SKIPPED_RESULT_PREFIX) . '%', $wpdb->esc_like(self::COMPACTED_RESULT_PREFIX) . '%', self::SAMPLE_LIMIT ), ARRAY_A ); // phpcs:enable if (!is_array($rows) || '' !== (string) $wpdb->last_error) { Logger::warn('automation', 'The store speed could not be measured', ['error' => (string) $wpdb->last_error]); return null; } $times_by_job = []; foreach ($rows as $row) { if (!is_array($row) || !isset($row['job_id'], $row['processed_at'])) { continue; } $times_by_job[(int) $row['job_id']][] = (string) $row['processed_at']; } $speed = self::productsPerHourByJob($times_by_job); set_transient($cache_key, ['speed' => $speed], self::CACHE_SECONDS); return $speed; } /** * The speed of one job's completions. * * @param array $utc_times UTC 'Y-m-d H:i:s' completion times of one job, any order. * @return float|null Products per hour, or null below the history thresholds. */ public static function productsPerHour(array $utc_times) { return self::productsPerHourByJob([$utc_times]); } /** * The speed of several jobs: their working times and products added up, * so no gap between two jobs is counted. * * Every counted gap is at most MAX_IDLE_GAP_SECONDS, so a reported speed * is never below one product per six hours and never needs a floor. * * @param array $utc_times_by_job Per job, its UTC 'Y-m-d H:i:s' completion times, any order. * @return float|null Products per hour to two decimals, or null below the history thresholds. */ public static function productsPerHourByJob(array $utc_times_by_job) { $products = 0; $seconds = 0; foreach ($utc_times_by_job as $utc_times) { [$job_products, $job_seconds] = self::workingTime((array) $utc_times); $products += $job_products; $seconds += $job_seconds; } if ($products < self::MIN_PRODUCTS || $seconds < self::MIN_WORKING_SECONDS) { return null; } return round($products * HOUR_IN_SECONDS / $seconds, 2); } /** * @param array $utc_times UTC completion times of one job. * @return array{0: int, 1: int} Counted products and their working seconds. */ private static function workingTime(array $utc_times): array { $timestamps = []; foreach ($utc_times as $value) { $timestamp = JobTime::toTimestamp($value); if (false !== $timestamp) { $timestamps[] = $timestamp; } } sort($timestamps); $products = 0; $seconds = 0; for ($i = 1, $count = count($timestamps); $i < $count; $i++) { $gap = $timestamps[$i] - $timestamps[$i - 1]; if ($gap <= self::MAX_IDLE_GAP_SECONDS) { $products++; $seconds += $gap; } } return [$products, $seconds]; } }