Making WordPress.org


Ignore:
Timestamp:
04/14/2020 04:55:52 AM (6 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-custom-stats/stats/project.php

    r4355 r9720  
    99 *
    1010 * NOTE: The counts includes all sub-projects in the count, as that's more useful for querying (top-level projects excluded)
    11  * for example, wp-plugins won't exist, but wp-plugins/akismet will include wp-plugins/akismet/stable wp-plugins/akismet/stable-readme
     11 * for example, wp-plugins/akismet will include wp-plugins/akismet/stable and wp-plugins/akismet/stable-readme
     12 * wp-plugins and wp-themes are special top-level projects which are included here, where their string counts are updated
     13 * on a twice-daily cron task.
    1214 *
    1315 * @author dd32
     
    2729                add_action( 'shutdown', array( $this, 'shutdown' ) );
    2830
     31                // Cron task to cache the wp-themes/wp-plugins string counts.
     32                if ( ! wp_next_scheduled ( 'wporg_gp_stats_cache_waiting_strings' ) ) {
     33                        wp_schedule_event( time(), 'twicedaily', 'wporg_gp_stats_cache_waiting_strings' );
     34                }
     35                add_action( 'wporg_gp_stats_cache_waiting_strings', [ $this, 'cache_wp_themes_wp_plugins_strings' ] );
     36
    2937                $wpdb->project_translation_status = $gp_table_prefix . 'project_translation_status';
    3038        }
     
    4654        }
    4755
    48         // Counts up all the
     56        // Count up all the strings
    4957        function get_project_translation_counts( $project_id, $locale, $locale_slug, &$counts = array() ) {
    5058                if ( ! $counts ) {
     
    7583
    7684                return $counts;
     85        }
     86
     87        /**
     88         * Cron task to cache the string counts for the wp-themes and wp-plugins parent categories.
     89         *
     90         * These don't have any translation sets, but we need to be able to query the waiting strings for them.
     91         */
     92        public function cache_wp_themes_wp_plugins_strings() {
     93                global $wpdb;
     94
     95                $cached_projects = [
     96                        GP::$project->by_path( 'wp-plugins' ),
     97                        GP::$project->by_path( 'wp-themes' ),
     98                ];
     99
     100                // Store the counts for these parent projects as the sum of their children.
     101                $sql = "INSERT INTO {$wpdb->project_translation_status} ( `project_id`, `locale`, `locale_slug`, `all`, `current`, `waiting`, `fuzzy`, `warnings`, `untranslated`, `date_added`, `date_modified`)
     102                SELECT
     103                        p.parent_project_id as project_id,
     104                        locale, locale_slug,
     105                        SUM( stats.all ) as `all`, SUM( stats.current ) as `current`, SUM( stats.waiting ) as `waiting`,
     106                        SUM( stats.fuzzy ) as `fuzzy`, SUM( stats.warnings ) as `warnings`, SUM( stats.untranslated ) as `untranslated`,
     107                        NOW() as `date_added`, NOW() as `date_modified`
     108                FROM {$wpdb->project_translation_status} stats
     109                        LEFT JOIN {$wpdb->gp_projects} p ON stats.project_id = p.id
     110                WHERE
     111                        p.parent_project_id = %d
     112                        AND p.active = 1
     113                GROUP BY locale, locale_slug
     114                ON DUPLICATE KEY UPDATE
     115                        `all` = VALUES(`all`), `current` = VALUES(`current`),
     116                        `waiting` = VALUES(`waiting`), `fuzzy` = VALUES(`fuzzy`),
     117                        `warnings` = VALUES(`warnings`), `untranslated` = VALUES(`untranslated`),
     118                        `date_modified` = VALUES(`date_modified`);
     119                ";
     120
     121                foreach ( $cached_projects as $project ) {
     122                        $wpdb->query( $wpdb->prepare( $sql, $project->id ) );
     123                }
     124
    77125        }
    78126
Note: See TracChangeset for help on using the changeset viewer.