21use Wikimedia\Assert\Assert;
40 private const COUNTERS = [
41 'ss_total_edits' =>
'edits',
42 'ss_total_pages' =>
'pages',
43 'ss_good_articles' =>
'articles',
44 'ss_users' =>
'users',
45 'ss_images' =>
'images'
49 public function __construct( $views, $edits, $good, $pages = 0, $users = 0 ) {
51 $this->articles = $good;
58 Assert::parameterType( __CLASS__, $update,
'$update' );
59 '@phan-var SiteStatsUpdate $update';
61 foreach ( self::COUNTERS as $field ) {
62 $this->$field += $update->$field;
71 public static function factory( array $deltas ) {
72 $update =
new self( 0, 0, 0 );
74 foreach ( $deltas as $name => $unused ) {
75 if ( !in_array( $name, self::COUNTERS ) ) {
76 throw new UnexpectedValueException( __METHOD__ .
": no field called '$name'" );
80 foreach ( self::COUNTERS as $field ) {
81 $update->$field = $deltas[$field] ?? 0;
88 $services = MediaWikiServices::getInstance();
89 $stats = $services->getStatsdDataFactory();
92 foreach ( self::COUNTERS as
$type ) {
93 $delta = $this->$type;
95 $stats->updateCount(
"site.$type", $delta );
97 $deltaByType[
$type] = $delta;
101 $services->getDBLoadBalancer()->getConnectionRef(
DB_PRIMARY ),
103 static function (
IDatabase $dbw, $fname ) use ( $deltaByType ) {
105 foreach ( self::COUNTERS as $field =>
$type ) {
106 $delta = (int)$deltaByType[
$type];
112 } elseif ( $delta < 0 ) {
121 $dbw->
update(
'site_stats', $set, [
'ss_row_id' => 1 ], $fname );
135 $services = MediaWikiServices::getInstance();
136 $config = $services->getMainConfig();
138 $dbr = $services->getDBLoadBalancer()->getConnectionRef(
DB_REPLICA,
'vslow' );
139 # Get non-bot users than did some recent action other than making accounts.
140 # If account creation is included, the number gets inflated ~20+ fold on enwiki.
141 $activeUsers =
$dbr->newSelectQueryBuilder()
142 ->select(
'COUNT(DISTINCT rc_actor)' )
143 ->from(
'recentchanges' )
144 ->join(
'actor',
'actor',
'actor_id=rc_actor' )
147 'actor_user IS NOT NULL',
149 'rc_log_type != ' .
$dbr->addQuotes(
'newusers' ) .
' OR rc_log_type IS NULL',
150 'rc_timestamp >= ' .
$dbr->addQuotes(
151 $dbr->timestamp( time() - $config->get(
'ActiveUserDays' ) * 24 * 3600 ) ),
153 ->caller( __METHOD__ )
157 [
'ss_active_users' => intval( $activeUsers ) ],
158 [
'ss_row_id' => 1 ],
Deferrable Update for closure/callback updates that should use auto-commit mode.
Class for handling updates to the site_stats table.
static factory(array $deltas)
doUpdate()
Perform the actual work.
__construct( $views, $edits, $good, $pages=0, $users=0)
static cacheUpdate(IDatabase $dbw)
merge(MergeableUpdate $update)
Merge this enqueued update with a new MergeableUpdate of the same qualified class name.
static unload()
Trigger a reload next time a field is accessed.
Interface that deferrable updates should implement.
Interface that deferrable updates can implement to signal that updates can be combined.