mirror of
https://github.com/WordPress/five-for-the-future.git
synced 2025-04-16 16:43:42 +03:00
Stats: Add stats for the % of contributors that are active.
This commit is contained in:
parent
0be2f859f9
commit
5f1d091185
|
@ -66,9 +66,11 @@ function record_snapshot() {
|
|||
|
||||
bump_stats_extra( 'five-for-the-future', 'Self-sponsored hours', $stats['self_sponsored_hours'] );
|
||||
bump_stats_extra( 'five-for-the-future', 'Self-sponsored contributors', $stats['self_sponsored_contributors'] );
|
||||
bump_stats_extra( 'five-for-the-future', 'Self-sponsored contributor activity %', $stats['self_sponsored_contributor_activity'] );
|
||||
bump_stats_extra( 'five-for-the-future', 'Companies', $stats['companies'] );
|
||||
bump_stats_extra( 'five-for-the-future', 'Company-sponsored hours', $stats['company_sponsored_hours'] );
|
||||
bump_stats_extra( 'five-for-the-future', 'Company-sponsored contributors', $stats['company_sponsored_contributors'] );
|
||||
bump_stats_extra( 'five-for-the-future', 'Company-sponsored contributor activity %', $stats['company_sponsored_contributor_activity'] );
|
||||
|
||||
foreach ( array( 'team_company_sponsored_contributors', 'team_self_sponsored_contributors' ) as $key ) {
|
||||
foreach ( $stats[ $key ] as $team => $contributors ) {
|
||||
|
@ -94,6 +96,9 @@ function record_snapshot() {
|
|||
* @return array
|
||||
*/
|
||||
function get_snapshot_data() {
|
||||
$active_self_sponsored_contributors = 0;
|
||||
$active_company_sponsored_contributors = 0;
|
||||
|
||||
$snapshot_data = array(
|
||||
'company_sponsored_hours' => 0,
|
||||
'self_sponsored_hours' => 0,
|
||||
|
@ -135,17 +140,26 @@ function get_snapshot_data() {
|
|||
$all_contributor_profiles = XProfile\get_all_xprofile_contributor_hours_teams();
|
||||
$snapshot_data['company_sponsored_contributors'] = count( $company_contributor_user_ids );
|
||||
$snapshot_data['self_sponsored_contributors'] = count( $all_contributor_profiles ) - count( $company_contributor_user_ids );
|
||||
$full_users = Contributor\add_user_data_to_xprofile( $all_contributor_profiles );
|
||||
unset( $all_contributor_profiles );
|
||||
|
||||
foreach ( $all_contributor_profiles as $profile ) {
|
||||
$attribution_prefix = in_array( $profile->user_id, $company_contributor_user_ids, true )
|
||||
? 'company_sponsored'
|
||||
: 'self_sponsored';
|
||||
foreach ( $full_users as $user ) {
|
||||
$is_company_sponsored = in_array( $user['user_id'], $company_contributor_user_ids, true );
|
||||
$attribution_prefix = $is_company_sponsored ? 'company_sponsored' : 'self_sponsored';
|
||||
|
||||
if ( Contributor\is_active( $user['last_logged_in'] ) ) {
|
||||
if ( $is_company_sponsored ) {
|
||||
$active_company_sponsored_contributors++;
|
||||
} else {
|
||||
$active_self_sponsored_contributors++;
|
||||
}
|
||||
}
|
||||
|
||||
$team_contributor_key = sprintf( 'team_%s_contributors', $attribution_prefix );
|
||||
|
||||
$snapshot_data[ $attribution_prefix . '_hours'] += $profile->hours_per_week;
|
||||
$snapshot_data[ $attribution_prefix . '_hours'] += $user['hours_per_week'];
|
||||
|
||||
foreach ( $profile->team_names as $team ) {
|
||||
foreach ( $user['team_names'] as $team ) {
|
||||
if ( isset( $snapshot_data[ $team_contributor_key ][ $team ] ) ) {
|
||||
$snapshot_data[ $team_contributor_key ][ $team ] ++;
|
||||
} else {
|
||||
|
@ -159,6 +173,9 @@ function get_snapshot_data() {
|
|||
ksort( $snapshot_data['team_company_sponsored_contributors'] );
|
||||
ksort( $snapshot_data['team_self_sponsored_contributors'] );
|
||||
|
||||
$snapshot_data['self_sponsored_contributor_activity'] = round( $active_self_sponsored_contributors / $snapshot_data['self_sponsored_contributors'] * 100, 2 );
|
||||
$snapshot_data['company_sponsored_contributor_activity'] = round( $active_company_sponsored_contributors / $snapshot_data['company_sponsored_contributors'] * 100, 2 );
|
||||
|
||||
return $snapshot_data;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,11 +32,33 @@ function database_setup_before_class( WP_UnitTest_Factory $factory ) : array {
|
|||
$fixtures['users']['jane'] = $factory->user->create_and_get( array(
|
||||
'user_login' => 'jane',
|
||||
'user_email' => 'jane@example.org',
|
||||
'meta_input' => array(
|
||||
'last_logged_in' => date( 'Y-m-d H:i:s', strtotime( '95 days ago' ) )
|
||||
)
|
||||
) );
|
||||
|
||||
$fixtures['users']['ashish'] = $factory->user->create_and_get( array(
|
||||
'user_login' => 'ashish',
|
||||
'user_email' => 'ashish@example.org',
|
||||
'meta_input' => array(
|
||||
'last_logged_in' => date( 'Y-m-d H:i:s', strtotime( '2 hours ago' ) )
|
||||
)
|
||||
) );
|
||||
|
||||
$fixtures['users']['andrea'] = $factory->user->create_and_get( array(
|
||||
'user_login' => 'andrea',
|
||||
'user_email' => 'andrea@example.org',
|
||||
'meta_input' => array(
|
||||
'last_logged_in' => date( 'Y-m-d H:i:s', strtotime( '1 week ago' ) )
|
||||
)
|
||||
) );
|
||||
|
||||
$fixtures['users']['caleb'] = $factory->user->create_and_get( array(
|
||||
'user_login' => 'caleb',
|
||||
'user_email' => 'caleb@example.org',
|
||||
'meta_input' => array(
|
||||
'last_logged_in' => date( 'Y-m-d H:i:s', strtotime( '4 months ago' ) )
|
||||
)
|
||||
) );
|
||||
|
||||
// Pages
|
||||
|
@ -72,7 +94,7 @@ function database_setup_before_class( WP_UnitTest_Factory $factory ) : array {
|
|||
*
|
||||
* Call in `set_up()`.
|
||||
*/
|
||||
function database_set_up( int $jane_id, int $ashish_id ) : void {
|
||||
function database_set_up( array $user_ids ) : void {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb->query( 'TRUNCATE TABLE `bpmain_bp_xprofile_data` ' );
|
||||
|
@ -84,11 +106,19 @@ function database_set_up( int $jane_id, int $ashish_id ) : void {
|
|||
(NULL, 29, %d, '40', '2019-12-02 10:00:00' ),
|
||||
(NULL, 30, %d, 'a:1:{i:0;s:9:\"Core Team\";}', '2019-12-03 11:00:00' ),
|
||||
(NULL, 29, %d, '35', '2019-12-02 10:00:00' ),
|
||||
(NULL, 30, %d, 'a:1:{i:0;s:18:\"Documentation Team\";}', '2019-12-03 11:00:00' )",
|
||||
$jane_id,
|
||||
$jane_id,
|
||||
$ashish_id,
|
||||
$ashish_id
|
||||
(NULL, 30, %d, 'a:1:{i:0;s:18:\"Documentation Team\";}', '2019-12-03 11:00:00' ),
|
||||
(NULL, 29, %d, '7', '2019-12-02 10:00:00' ),
|
||||
(NULL, 30, %d, 'a:1:{i:0;s:14:\"Polyglots Team\";}', '2019-12-03 11:00:00' ),
|
||||
(NULL, 29, %d, '4', '2019-12-02 10:00:00' ),
|
||||
(NULL, 30, %d, 'a:1:{i:0;s:9:\"Meta Team\";}', '2019-12-03 11:00:00' )",
|
||||
$user_ids[0],
|
||||
$user_ids[0],
|
||||
$user_ids[1],
|
||||
$user_ids[1],
|
||||
$user_ids[2],
|
||||
$user_ids[2],
|
||||
$user_ids[3],
|
||||
$user_ids[3]
|
||||
) );
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ class Test_Contributor extends WP_UnitTestCase {
|
|||
*/
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
TestHelpers\database_set_up( self::$users['jane']->ID, self::$users['ashish']->ID );
|
||||
TestHelpers\database_set_up( array_values( wp_list_pluck( self::$users, 'ID' ) ) );
|
||||
reset_phpmailer_instance();
|
||||
}
|
||||
|
||||
|
|
89
plugins/wporg-5ftf/tests/test-stats.php
Normal file
89
plugins/wporg-5ftf/tests/test-stats.php
Normal file
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
use function WordPressDotOrg\FiveForTheFuture\Stats\{ get_snapshot_data };
|
||||
use WordPressDotOrg\FiveForTheFuture\{ Contributor };
|
||||
use WordPressDotOrg\FiveForTheFuture\Tests\Helpers as TestHelpers;
|
||||
|
||||
defined( 'WPINC' ) || die();
|
||||
|
||||
/**
|
||||
* @group stats
|
||||
*/
|
||||
class Test_Stats extends WP_UnitTestCase {
|
||||
protected static $users;
|
||||
protected static $pages;
|
||||
protected static $pledges;
|
||||
|
||||
/**
|
||||
* Run once when class loads.
|
||||
*/
|
||||
public static function set_up_before_class() {
|
||||
parent::set_up_before_class();
|
||||
|
||||
$fixtures = TestHelpers\database_setup_before_class( self::factory() );
|
||||
self::$users = $fixtures['users'];
|
||||
self::$pages = $fixtures['pages'];
|
||||
self::$pledges = $fixtures['pledges'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Run before every test.
|
||||
*/
|
||||
public function set_up() {
|
||||
parent::set_up();
|
||||
TestHelpers\database_set_up( array_values( wp_list_pluck( self::$users, 'ID' ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Run once after all tests are finished.
|
||||
*/
|
||||
public static function tear_down_after_class() {
|
||||
parent::tear_down_after_class();
|
||||
TestHelpers\database_tear_down_after_class();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::get_snapshot_data
|
||||
*/
|
||||
public function test_get_snapshot() : void {
|
||||
// Setup 2 company-sponsored contributors.
|
||||
$jane = self::$users['jane'];
|
||||
$ashish = self::$users['ashish'];
|
||||
$tenup = self::$pledges['10up'];
|
||||
$tenup_contributors = Contributor\add_pledge_contributors( $tenup->ID, array( $jane->user_login, $ashish->user_login ) );
|
||||
$tenup_jane_id = $tenup_contributors[ $jane->user_login ];
|
||||
$tenup_ashish_id = $tenup_contributors[ $ashish->user_login ];
|
||||
|
||||
wp_update_post( array(
|
||||
'ID' => $tenup_jane_id,
|
||||
'post_status' => 'publish',
|
||||
) );
|
||||
wp_update_post( array(
|
||||
'ID' => $tenup_ashish_id,
|
||||
'post_status' => 'publish',
|
||||
) );
|
||||
|
||||
$expected = array(
|
||||
'company_sponsored_hours' => 75,
|
||||
'self_sponsored_hours' => 11,
|
||||
|
||||
'team_company_sponsored_contributors' => array(
|
||||
'Core Team' => 1,
|
||||
'Documentation Team' => 1,
|
||||
),
|
||||
|
||||
'team_self_sponsored_contributors' => array(
|
||||
'Meta Team' => 1,
|
||||
'Polyglots Team' => 1,
|
||||
),
|
||||
|
||||
'companies' => 2,
|
||||
'company_sponsored_contributors' => 2,
|
||||
'self_sponsored_contributors' => 2,
|
||||
'self_sponsored_contributor_activity' => 50.0,
|
||||
'company_sponsored_contributor_activity' => 50.0,
|
||||
);
|
||||
|
||||
$this->assertSame( $expected, get_snapshot_data() );
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue