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;
145 } elseif ( $delta < 0 ) {
146 $hasNegativeDelta =
true;
155 if ( $hasNegativeDelta ) {
157 ->update(
'site_stats' )
159 ->where( [
'ss_row_id' => $shard ] )
160 ->caller( $fname )->execute();
163 ->insertInto(
'site_stats' )
164 ->row( array_merge( [
'ss_row_id' => $shard ], $initValues ) )
165 ->onDuplicateKeyUpdate()
166 ->uniqueIndexFields( [
'ss_row_id' ] )
168 ->caller( $fname )->execute();
184 $config = $services->getMainConfig();
186 $dbr = $services->getConnectionProvider()->getReplicaDatabase(
false,
'vslow' );
187 # Get non-bot users than did some recent action other than making accounts.
188 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
189 $activeUsers = $dbr->newSelectQueryBuilder()
190 ->select(
'COUNT(DISTINCT rc_actor)' )
191 ->from(
'recentchanges' )
192 ->join(
'actor',
'actor',
'actor_id=rc_actor' )
195 $dbr->expr(
'actor_user',
'!=',
null ),
196 $dbr->expr(
'rc_bot',
'=', 0 ),
197 $dbr->expr(
'rc_log_type',
'!=',
'newusers' )->or(
'rc_log_type',
'=',
null ),
198 $dbr->expr(
'rc_timestamp',
'>=',
201 ->caller( __METHOD__ )
204 ->update(
'site_stats' )
205 ->set( [
'ss_active_users' => intval( $activeUsers ) ] )
206 ->where( [
'ss_row_id' => 1 ] )
207 ->caller( __METHOD__ )->execute();
217class_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()