Support » Fixing WordPress » Running A Calculation On All Users Every Month

  • I want to schedule a calculation to run against every user on my site each month. I can setup some logic to limit the number of users this will run against e.g. if they’ve had any activity, but the crux of the matter is I have a user base of at least 25,000 users and need to calculate an average of 6 and (separately) 12 values of meta data on their account.

    I’ve come up with something like this – which not surprisingly causes the site to crash:

    function user_monthly_roi_calculation() {
    
    	/* Get all users */
    	global $wpdb;
    	$users = $wpdb->get_results("SELECT ID from $wpdb->users");
    	foreach ($users as $user) {
    
    		// Calculate 6 month ROI for user
    		$average_6 = array();
        	for ($i = 0; $i <= 5; $i++) { 
    
        		$user_meta = "month_profit_" . $i;
    			$six_month[] = get_the_author_meta( $user_meta, $user->ID );
    
    		}
    		$average_profit_6 = array_sum($average_6) / count($average_6);
    		echo $average_profit_6;
    		//update_user_meta( $user->ID, 'average_profit_6', $average_profit_6 );
    
    		// Calculate 12 month ROI for user
    		$average_12 = array();
    		for ($i = 0; $i <= 11; $i++) { 
    
    			$user_meta = "month_profit_" . $i;
    			$twelve_month[] = get_the_author_meta( $user_meta, $user->ID );
    
    		}
    		$average_profit_12 = array_sum($average_12) / count($average_12);
    		//update_user_meta( $user->ID, 'average_profit_12', $average_profit_12 );
    
    		// Rotate user ROI values
    		for ($i = 0; $i <= 11; $i++) { 
    
    			$user_meta_src = "month_profit_" . $i;
    			$user_meta_tar = "month_profit_" . ($i + 1);
    			//update_user_meta( $user->ID, $user_meta_tar, $user_meta_src );
    
    		}
    
    		// Reset current month to 0
    		//update_user_meta( $user->ID, "month_profit_0", 0 );
    
    	}
    }
    add_action('monthly_event','user_monthly_roi_calculation');

    Any suggestions on how I can get this running in such a way that the site isn’t brought down each month 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello John,

    Just giving a shot. 🙂

    What if you can distribute the users to process within XX day.. So it will not run one-time. maybe 1-5 users per minute?

    Just thinking out loud. 😀 Hope it helps! 🙂

    Best Regards,
    Calvin

    Been doing some reading on this Calvin and have come up with this process:

    1) Run monthly using wp_cron

    2) Check to see if highest user_id has process completed tag in meta data

    3) If there are still accounts to process (#2 returns FALSE) use start and end records plus MySQL LIMIT to the next process 25 user IDs.

    4) Update user’s account to flag as having been processed

    5) If number returned <25, final batch.

    I’ve not been able to piece this together yet but see no reason why it shouldn’t work 🙂

    Here’s what I came up with:

    function schedule_roi_calc() {
    
    	/* Get the highest user ID number */
    	global $wpdb;
    	$last_user = $wpdb->get_var ( "SELECT ID from wp_users ORDER BY id DESC LIMIT 1" );
    
    	/* Check the last user's account */
    	$processed = get_the_author_meta( "roi_processed", $last_user );
    	$diff = strtotime("-3 weeks"); // get unix timestamp of three weeks agao
    
    	/* Loop through users if processed value doesn't exist or last calculation took place more than three weeks ago */
    	if( $processed == '' || $processed <= $diff ) {
    
    		// Get total number of accounts
    		$total = count( $wpdb->get_results ( "SELECT ID from wp_users" ) );
    
    		for($i=0; $i<=$total; $i += 25) {
    
    			// create start and end records with 25 LIMITS
    			$query = "SELECT ID from wp_users LIMIT $i,25";
    			$accounts = $wpdb->get_results ( $query );
    
    			foreach ( $accounts as $users ) {
    
    				user_roi_calculation( $users->ID );
    
    			}
    		}
    	}
    }

    I’ve got 25K user accounts on this site and this timed out when I called it within the theme after 30 seconds and around 4000 users.

    I’ve hooked it into wp_cron to run daily to see if it works.

    Hello John Peden,

    Will keep an eye on this thread. Looking forward for the results! 🙂

    Best Regards,
    Calvin

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Running A Calculation On All Users Every Month’ is closed to new replies.