| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Linter; |
| 22 |
|
| 23 |
use MediaWiki\Config\ServiceOptions; |
| 24 |
use MediaWiki\WikiMap\WikiMap; |
| 25 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 26 |
use Wikimedia\Rdbms\Database as MWDatabase; |
| 27 |
use Wikimedia\Stats\IBufferingStatsdDataFactory; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class TotalsLookup { |
| 34 |
public const CONSTRUCTOR_OPTIONS = [ |
| 35 |
'LinterStatsdSampleFactor', |
| 36 |
]; |
| 37 |
|
| 38 |
private ServiceOptions $options; |
| 39 |
private WANObjectCache $cache; |
| 40 |
private IBufferingStatsdDataFactory $statsdDataFactory; |
| 41 |
private CategoryManager $categoryManager; |
| 42 |
private Database $database; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( |
| 52 |
ServiceOptions $options, |
| 53 |
WANObjectCache $cache, |
| 54 |
IBufferingStatsdDataFactory $statsdDataFactory, |
| 55 |
CategoryManager $categoryManager, |
| 56 |
Database $database |
| 57 |
) { |
| 58 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 59 |
$this->options = $options; |
| 60 |
$this->cache = $cache; |
| 61 |
$this->statsdDataFactory = $statsdDataFactory; |
| 62 |
$this->categoryManager = $categoryManager; |
| 63 |
$this->database = $database; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private function makeKey( $cat ) { |
| 71 |
return $this->cache->makeKey( 'linter', 'total', $cat ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function getTotals(): array { |
| 80 |
$cats = $this->categoryManager->getVisibleCategories(); |
| 81 |
$fetchedTotals = false; |
| 82 |
$totals = []; |
| 83 |
foreach ( $cats as $cat ) { |
| 84 |
$totals[$cat] = $this->cache->getWithSetCallback( |
| 85 |
$this->makeKey( $cat ), |
| 86 |
WANObjectCache::TTL_INDEFINITE, |
| 87 |
function ( $oldValue, &$ttl, &$setOpts, $oldAsOf ) use ( $cat, &$fetchedTotals ) { |
| 88 |
$setOpts += MWDatabase::getCacheSetOptions( |
| 89 |
$this->database->getDBConnectionRef( DB_REPLICA ) |
| 90 |
); |
| 91 |
if ( $fetchedTotals === false ) { |
| 92 |
$fetchedTotals = $this->database->getTotals(); |
| 93 |
} |
| 94 |
return $fetchedTotals[$cat]; |
| 95 |
}, |
| 96 |
[ |
| 97 |
'checkKeys' => [ |
| 98 |
$this->cache->makeKey( 'linter', 'totals' ), |
| 99 |
$this->makeKey( $cat ), |
| 100 |
], |
| 101 |
'lockTSE' => 30, |
| 102 |
] |
| 103 |
); |
| 104 |
} |
| 105 |
return $totals; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public function updateStats( array $changes ) { |
| 114 |
$linterStatsdSampleFactor = $this->options->get( 'LinterStatsdSampleFactor' ); |
| 115 |
|
| 116 |
if ( $linterStatsdSampleFactor === false ) { |
| 117 |
|
| 118 |
$raw = $changes['added']; |
| 119 |
foreach ( $changes['deleted'] as $cat => $count ) { |
| 120 |
if ( isset( $raw[$cat] ) ) { |
| 121 |
$raw[$cat] -= $count; |
| 122 |
} else { |
| 123 |
|
| 124 |
$raw[$cat] = 0 - $count; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
foreach ( $raw as $cat => $count ) { |
| 129 |
if ( $count != 0 ) { |
| 130 |
|
| 131 |
$this->touchCategoryCache( $cat ); |
| 132 |
} |
| 133 |
} |
| 134 |
return; |
| 135 |
} elseif ( mt_rand( 1, $linterStatsdSampleFactor ) != 1 ) { |
| 136 |
return; |
| 137 |
} |
| 138 |
|
| 139 |
$totals = $this->database->getTotals(); |
| 140 |
$wiki = WikiMap::getCurrentWikiId(); |
| 141 |
$stats = $this->statsdDataFactory; |
| 142 |
foreach ( $totals as $name => $count ) { |
| 143 |
$stats->gauge( "linter.category.$name.$wiki", $count ); |
| 144 |
} |
| 145 |
$stats->gauge( "linter.totals.$wiki", array_sum( $totals ) ); |
| 146 |
|
| 147 |
$this->touchAllCategoriesCache(); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function touchCategoryCache( $cat ) { |
| 156 |
$this->cache->touchCheckKey( $this->makeKey( $cat ) ); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function touchAllCategoriesCache() { |
| 163 |
$this->cache->touchCheckKey( $this->cache->makeKey( 'linter', 'totals' ) ); |
| 164 |
} |
| 165 |
} |