Making WordPress.org


Ignore:
Timestamp:
03/10/2016 11:14:57 PM (10 years ago)
Author:
ocean90
Message:

Translate, User Stats: Delay database writes until shutdown to bulk-update the stats during imports.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/wporg-gp-custom-stats/stats/user.php

    r2657 r2734  
    1010class WPorg_GP_User_Stats {
    1111
    12         function __construct() {
     12        private $user_stats = array();
     13
     14        public function __construct() {
    1315                global $wpdb, $gp_table_prefix;
    1416
    15                 add_action( 'gp_translation_created', array( $this, 'translation_created' ) );
    16                 add_action( 'gp_translation_saved', array( $this, 'translation_saved' ) );
     17                add_action( 'gp_translation_created', array( $this, 'translation_updated' ) );
     18                add_action( 'gp_translation_saved', array( $this, 'translation_updated' ) );
     19
     20                // DB Writes are delayed until shutdown to bulk-update the stats during imports.
     21                add_action( 'shutdown', array( $this, 'write_stats_to_database' ) );
    1722
    1823                $wpdb->user_translations_count = $gp_table_prefix . 'user_translations_count';
    1924        }
    2025
    21         function translation_created( $translation ) {
    22                 return $this->translation_saved( $translation, 'created' );
    23         }
    24 
    25         function translation_saved( $translation, $action = 'saved' ) {
     26        public function translation_updated( $translation ) {
    2627                if ( ! $translation->user_id ) {
    2728                        return;
     
    3435                        $this->bump_user_stat( $translation->user_id, $translation_set->locale, $translation_set->slug, 1, 0 );
    3536
    36                 } elseif ( 'current' === $translation->status && 'created' === $action ) {
     37                } elseif ( 'current' === $translation->status && 'gp_translation_created' === current_filter() ) {
    3738                        // New translation suggested & approved
    3839                        $this->bump_user_stat( $translation->user_id, $translation_set->locale, $translation_set->slug, 1, 1 );
     
    4546        }
    4647
    47         function bump_user_stat( $user_id, $locale, $locale_slug, $suggested = 0, $accepted = 0 ) {
    48                 global $wpdb;
    49                 $wpdb->query( $wpdb->prepare(
    50                         "INSERT INTO {$wpdb->user_translations_count} (`user_id`, `locale`, `locale_slug`, `suggested`, `accepted`) VALUES (%d, %s, %s, %d, %d)
    51                         ON DUPLICATE KEY UPDATE `suggested`=`suggested` + VALUES(`suggested`), `accepted`=`accepted` + VALUES(`accepted`)",
    52                         $user_id, $locale, $locale_slug, $suggested, $accepted
    53                 ) );
     48        private function bump_user_stat( $user_id, $locale, $locale_slug, $suggested = 0, $accepted = 0 ) {
     49                $key = "$user_id,$locale,$locale_slug";
     50
     51                if ( isset( $this->user_stats[ $key ] ) ) {
     52                        $this->user_stats[ $key ]->suggested += $suggested;
     53                        $this->user_stats[ $key ]->accepted  += $accepted;
     54                } else {
     55                        $this->user_stats[ $key ] = (object) array(
     56                                'suggested' => $suggested,
     57                                'accepted'  => $accepted,
     58                        );
     59                }
    5460        }
    5561
     62        public function write_stats_to_database() {
     63                global $wpdb;
     64
     65                $values = array();
     66                foreach ( $this->user_stats as $key => $stats ) {
     67                        list( $user_id, $locale, $locale_slug ) = explode( ',', $key );
     68
     69                        $values[] = $wpdb->prepare( '(%d, %s, %s, %d, %d)',
     70                                $user_id,
     71                                $locale,
     72                                $locale_slug,
     73                                $stats->suggested,
     74                                $stats->accepted
     75                        );
     76
     77                        // If we're processing a large batch, add them as we go to avoid query lengths & memory limits.
     78                        if ( count( $values ) > 50 ) {
     79                                $r = $wpdb->query(
     80                                        "INSERT INTO {$wpdb->user_translations_count} (`user_id`, `locale`, `locale_slug`, `suggested`, `accepted`)
     81                                        VALUES " . implode( ', ', $values ) . "
     82                                        ON DUPLICATE KEY UPDATE `suggested`=`suggested` + VALUES(`suggested`), `accepted`=`accepted` + VALUES(`accepted`)"
     83                                );
     84                                error_log( print_r($r,true));
     85                                $values = array();
     86                        }
     87                }
     88
     89                if ( $values ) {
     90                        $r = $wpdb->query(
     91                                "INSERT INTO {$wpdb->user_translations_count} (`user_id`, `locale`, `locale_slug`, `suggested`, `accepted`)
     92                                VALUES " . implode( ', ', $values ) . "
     93                                ON DUPLICATE KEY UPDATE `suggested`=`suggested` + VALUES(`suggested`), `accepted`=`accepted` + VALUES(`accepted`)"
     94                        );
     95                }
     96        }
    5697}
    5798
Note: See TracChangeset for help on using the changeset viewer.