Making WordPress.org


Ignore:
Timestamp:
04/14/2020 04:55:52 AM (5 years ago)
Author:
dd32
Message:

Translate: Move the stats generation for /stats from the Stats screen router to the Custom Stats plugin, store the data in the project translation status table rather than the options.

This also avoids the query-timeout problems by running the query as a INSERT INTO ... SELECT FROM which should be more resiliant to timeouts.

See #3155.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-routes/inc/routes/class-stats.php

    r9713 r9720  
    1313 */
    1414class Stats extends GP_Route {
    15 
    16     /**
    17      * Cache some expensive queries to be run on a Cron task.
    18      */
    19     public function cache_waiting_strings() {
    20         global $wpdb;
    21 
    22         // Run the query on a primary DB server to avoid timing out.
    23         if ( method_exists( $qpdb, 'send_reads_to_masters' ) ) {
    24             $wpdb->send_reads_to_masters();
    25         }
    26 
    27         $cached_projects = [
    28             GP::$project->by_path( 'wp-plugins' ),
    29             GP::$project->by_path( 'wp-themes' ),
    30         ];
    31         $sql = "SELECT
    32                 locale, locale_slug,
    33                 SUM( stats.waiting ) + SUM( stats.fuzzy ) as waiting_strings
    34             FROM {$wpdb->project_translation_status} stats
    35                 LEFT JOIN {$wpdb->gp_projects} p ON stats.project_id = p.id
    36             WHERE
    37                 p.parent_project_id = %d
    38                 AND p.active = 1
    39             GROUP BY locale, locale_slug";
    40 
    41         foreach ( $cached_projects as $project ) {
    42             $rows = $wpdb->get_results( $wpdb->prepare( $sql, $project->id ) );
    43             if ( ! $rows ) {
    44                 continue;
    45             }
    46 
    47             $cached_data = [];
    48             foreach ( $rows as $set ) {
    49                 $locale_key = $set->locale;
    50                 if ( 'default' != $set->locale_slug ) {
    51                     $locale_key = $set->locale . '/' . $set->locale_slug;
    52                 }
    53    
    54                 $cached_data[ $locale_key ] = (int) $set->waiting_strings;
    55             }
    56             $cached_data[ 'last_updated' ] = time();
    57 
    58             update_option( __CLASS__ . '_cached_waiting_' . $project->slug, $cached_data, false );
    59         }
    60 
    61     }
    6215
    6316    public function get_stats_overview() {
     
    10053                path, locale, locale_slug,
    10154                (100 * stats.current/stats.all) as percent_complete,
    102                 stats.waiting+stats.fuzzy as waiting_strings
     55                stats.waiting+stats.fuzzy as waiting_strings,
     56                stats.date_modified as last_modified
    10357            FROM {$wpdb->project_translation_status} stats
    10458                LEFT JOIN {$wpdb->gp_projects} p ON stats.project_id = p.id
     
    12579            $translation_locale_statuses[ $locale_key ][ $set->path ] = $percent_complete;
    12680
     81            // Don't include these in the 'waiting' section, override the value to be waiting strings w/ Date Modified.
     82            if ( 'wp-plugins' === $set->path || 'wp-themes' === $set->path ) {
     83                $translation_locale_statuses[ $locale_key ][ $set->path ] = $set->waiting_strings;
     84                $projects[ $set->path ]->cache_last_updated = $set->last_modified;
     85                continue;
     86            }
     87
    12788            if ( ! isset( $translation_locale_statuses[ $locale_key ]['waiting'] ) ) {
    12889                $translation_locale_statuses[ $locale_key ]['waiting'] = 0;
     
    13192        }
    13293        unset( $rows, $locale_key, $set );
    133 
    134         // Append the Plugins/Themes waiting strings
    135         foreach ( [ 'wp-plugins', 'wp-themes' ] as $project_slug ) {
    136             $cached_data = get_option( __CLASS__ . '_cached_waiting_' . $project_slug, [] );
    137             $projects[ $project_slug ]->cache_last_updated = gmdate( 'Y-m-d H:i:s \U\T\C', $cached_data['last_updated'] );
    138             foreach ( $cached_data as $locale => $waiting ) {
    139                 $translation_locale_statuses[ $locale ][ $project_slug ] = $waiting;
    140             }
    141         }
    14294
    14395        // Calculate a list of [Locale] = % subtotals
    14496        $translation_locale_complete = array();
    14597        foreach ( $translation_locale_statuses as $locale => $sets ) {
    146             unset( $sets['waiting'] );
     98            unset( $sets['waiting'], $sets['wp-plugins'], $sets['wp-themes'] );
    14799            $sets_count = count( $sets );
    148100            if ( $sets_count ) {
Note: See TracChangeset for help on using the changeset viewer.