26use UnexpectedValueException;
27use Wikimedia\Assert\Assert;
46 private const SHARDS_OFF = 1;
50 private const COUNTERS = [
51 'ss_total_edits' =>
'edits',
52 'ss_total_pages' =>
'pages',
53 'ss_good_articles' =>
'articles',
54 'ss_users' =>
'users',
55 'ss_images' =>
'images'
63 $this->articles = $good;
70 Assert::parameterType( __CLASS__, $update,
'$update' );
71 '@phan-var SiteStatsUpdate $update';
73 foreach ( self::COUNTERS as $field ) {
74 $this->$field += $update->$field;
91 public static function factory( array $deltas ) {
92 $update =
new self( 0, 0, 0 );
94 foreach ( $deltas as $name => $unused ) {
95 if ( !in_array( $name, self::COUNTERS ) ) {
96 throw new UnexpectedValueException( __METHOD__ .
": no field called '$name'" );
100 foreach ( self::COUNTERS as $field ) {
101 $update->$field = $deltas[$field] ?? 0;
109 $stats = $services->getStatsFactory();
115 foreach ( self::COUNTERS as $type ) {
116 $delta = $this->$type;
117 $deltaByType[$type] = $delta;
126 $stats->getCounter(
'site_stats_total' )
127 ->setLabel(
'engagement', $type )
128 ->copyToStatsdAt(
"site.$type" )
129 ->incrementBy( $delta );
134 $services->getConnectionProvider()->getPrimaryDatabase(),
136 static function (
IDatabase $dbw, $fname ) use ( $deltaByType, $shards ) {
140 $shard = mt_rand( 1, $shards );
145 $hasNegativeDelta =
false;
146 foreach ( self::COUNTERS as $field => $type ) {
147 $delta = (int)$deltaByType[$type];
148 $initValues[$field] = $delta;
151 } elseif ( $delta < 0 ) {
152 $hasNegativeDelta =
true;
156 ) .
'-' . abs( $delta ) );
161 if ( $hasNegativeDelta ) {
163 ->update(
'site_stats' )
165 ->where( [
'ss_row_id' => $shard ] )
166 ->caller( $fname )->execute();
169 ->insertInto(
'site_stats' )
170 ->row( array_merge( [
'ss_row_id' => $shard ], $initValues ) )
171 ->onDuplicateKeyUpdate()
172 ->uniqueIndexFields( [
'ss_row_id' ] )
174 ->caller( $fname )->execute();
190 $config = $services->getMainConfig();
192 $dbr = $services->getConnectionProvider()->getReplicaDatabase(
false,
'vslow' );
193 # Get non-bot users than did some recent action other than making accounts.
194 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
195 $activeUsers = $dbr->newSelectQueryBuilder()
196 ->select(
'COUNT(DISTINCT rc_actor)' )
197 ->from(
'recentchanges' )
198 ->join(
'actor',
'actor',
'actor_id=rc_actor' )
201 $dbr->expr(
'actor_user',
'!=',
null ),
202 $dbr->expr(
'rc_bot',
'=', 0 ),
203 $dbr->expr(
'rc_log_type',
'!=',
'newusers' )->or(
'rc_log_type',
'=',
null ),
204 $dbr->expr(
'rc_timestamp',
'>=',
207 ->caller( __METHOD__ )
210 ->update(
'site_stats' )
211 ->set( [
'ss_active_users' => intval( $activeUsers ) ] )
212 ->where( [
'ss_row_id' => 1 ] )
213 ->caller( __METHOD__ )->execute();
223class_alias( SiteStatsUpdate::class,
'SiteStatsUpdate' );
A class containing constants representing the names of configuration variables.
const MultiShardSiteStats
Name constant for the MultiShardSiteStats setting, for use with Config::get()
const ActiveUserDays
Name constant for the ActiveUserDays setting, for use with Config::get()