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 $stats = $services->getStatsdDataFactory();
113 foreach ( self::COUNTERS as $type ) {
114 $delta = $this->$type;
115 if ( $delta !== 0 ) {
116 $stats->updateCount(
"site.$type", $delta );
118 $deltaByType[$type] = $delta;
122 $services->getDBLoadBalancerFactory()->getPrimaryDatabase(),
124 static function (
IDatabase $dbw, $fname ) use ( $deltaByType, $shards ) {
128 $shard = mt_rand( 1, $shards );
133 $hasNegativeDelta =
false;
134 foreach ( self::COUNTERS as $field => $type ) {
135 $delta = (int)$deltaByType[$type];
136 $initValues[$field] = $delta;
142 } elseif ( $delta < 0 ) {
143 $hasNegativeDelta =
true;
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->getDBLoadBalancerFactory()->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()