26use UnexpectedValueException;
27use Wikimedia\Assert\Assert;
45 private const SHARDS_OFF = 1;
49 private const COUNTERS = [
50 'ss_total_edits' =>
'edits',
51 'ss_total_pages' =>
'pages',
52 'ss_good_articles' =>
'articles',
53 'ss_users' =>
'users',
54 'ss_images' =>
'images'
62 $this->articles = $good;
69 Assert::parameterType( __CLASS__, $update,
'$update' );
70 '@phan-var SiteStatsUpdate $update';
72 foreach ( self::COUNTERS as $field ) {
73 $this->$field += $update->$field;
90 public static function factory( array $deltas ) {
91 $update =
new self( 0, 0, 0 );
93 foreach ( $deltas as $name => $unused ) {
94 if ( !in_array( $name, self::COUNTERS ) ) {
95 throw new UnexpectedValueException( __METHOD__ .
": no field called '$name'" );
99 foreach ( self::COUNTERS as $field ) {
100 $update->$field = $deltas[$field] ?? 0;
108 $metric = $services->getStatsFactory()->getCounter(
'site_stats_total' );
113 foreach ( self::COUNTERS as $type ) {
114 $delta = $this->$type;
115 if ( $delta !== 0 ) {
116 $metric->setLabel(
'engagement', $type )
117 ->copyToStatsdAt(
"site.$type" )
118 ->incrementBy( $delta );
120 $deltaByType[$type] = $delta;
124 $services->getConnectionProvider()->getPrimaryDatabase(),
126 static function (
IDatabase $dbw, $fname ) use ( $deltaByType, $shards ) {
130 $shard = mt_rand( 1, $shards );
135 $hasNegativeDelta =
false;
136 foreach ( self::COUNTERS as $field => $type ) {
137 $delta = (int)$deltaByType[$type];
138 $initValues[$field] = $delta;
144 } elseif ( $delta < 0 ) {
145 $hasNegativeDelta =
true;
154 if ( $hasNegativeDelta ) {
156 ->update(
'site_stats' )
158 ->where( [
'ss_row_id' => $shard ] )
159 ->caller( $fname )->execute();
162 ->insertInto(
'site_stats' )
163 ->row( array_merge( [
'ss_row_id' => $shard ], $initValues ) )
164 ->onDuplicateKeyUpdate()
165 ->uniqueIndexFields( [
'ss_row_id' ] )
167 ->caller( $fname )->execute();
183 $config = $services->getMainConfig();
185 $dbr = $services->getConnectionProvider()->getReplicaDatabase(
false,
'vslow' );
186 # Get non-bot users than did some recent action other than making accounts.
187 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
188 $activeUsers = $dbr->newSelectQueryBuilder()
189 ->select(
'COUNT(DISTINCT rc_actor)' )
190 ->from(
'recentchanges' )
191 ->join(
'actor',
'actor',
'actor_id=rc_actor' )
194 $dbr->expr(
'actor_user',
'!=',
null ),
195 $dbr->expr(
'rc_bot',
'=', 0 ),
196 $dbr->expr(
'rc_log_type',
'!=',
'newusers' )->or(
'rc_log_type',
'=',
null ),
197 $dbr->expr(
'rc_timestamp',
'>=',
200 ->caller( __METHOD__ )
203 ->update(
'site_stats' )
204 ->set( [
'ss_active_users' => intval( $activeUsers ) ] )
205 ->where( [
'ss_row_id' => 1 ] )
206 ->caller( __METHOD__ )->execute();
216class_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()