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 $metric = $services->getStatsFactory()->getCounter(
'site_stats_total' );
114 foreach ( self::COUNTERS as $type ) {
115 $delta = $this->$type;
116 if ( $delta !== 0 ) {
117 $metric->setLabel(
'engagement', $type )
118 ->copyToStatsdAt(
"site.$type" )
119 ->incrementBy( $delta );
121 $deltaByType[$type] = $delta;
125 $services->getConnectionProvider()->getPrimaryDatabase(),
127 static function (
IDatabase $dbw, $fname ) use ( $deltaByType, $shards ) {
131 $shard = mt_rand( 1, $shards );
136 $hasNegativeDelta =
false;
137 foreach ( self::COUNTERS as $field => $type ) {
138 $delta = (int)$deltaByType[$type];
139 $initValues[$field] = $delta;
142 } elseif ( $delta < 0 ) {
143 $hasNegativeDelta =
true;
147 ) .
'-' . abs( $delta ) );
152 if ( $hasNegativeDelta ) {
154 ->update(
'site_stats' )
156 ->where( [
'ss_row_id' => $shard ] )
157 ->caller( $fname )->execute();
160 ->insertInto(
'site_stats' )
161 ->row( array_merge( [
'ss_row_id' => $shard ], $initValues ) )
162 ->onDuplicateKeyUpdate()
163 ->uniqueIndexFields( [
'ss_row_id' ] )
165 ->caller( $fname )->execute();
181 $config = $services->getMainConfig();
183 $dbr = $services->getConnectionProvider()->getReplicaDatabase(
false,
'vslow' );
184 # Get non-bot users than did some recent action other than making accounts.
185 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
186 $activeUsers = $dbr->newSelectQueryBuilder()
187 ->select(
'COUNT(DISTINCT rc_actor)' )
188 ->from(
'recentchanges' )
189 ->join(
'actor',
'actor',
'actor_id=rc_actor' )
192 $dbr->expr(
'actor_user',
'!=',
null ),
193 $dbr->expr(
'rc_bot',
'=', 0 ),
194 $dbr->expr(
'rc_log_type',
'!=',
'newusers' )->or(
'rc_log_type',
'=',
null ),
195 $dbr->expr(
'rc_timestamp',
'>=',
198 ->caller( __METHOD__ )
201 ->update(
'site_stats' )
202 ->set( [
'ss_active_users' => intval( $activeUsers ) ] )
203 ->where( [
'ss_row_id' => 1 ] )
204 ->caller( __METHOD__ )->execute();
214class_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()