jenkins-bot has submitted this change and it was merged.

Change subject: Move storeScores stuff into another method
......................................................................


Move storeScores stuff into another method

Moving actions in nested loops to another method makes the code
cleaner and easier to read

Change-Id: I8bbc568666ced7b2acf83d4e18df5a871d6a1555
---
M includes/Cache.php
1 file changed, 50 insertions(+), 40 deletions(-)

Approvals:
  Ladsgroup: Looks good to me, approved
  Thiemo Mättig (WMDE): Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/includes/Cache.php b/includes/Cache.php
index baccb45..0d002ba 100644
--- a/includes/Cache.php
+++ b/includes/Cache.php
@@ -32,48 +32,9 @@
         * @throws RuntimeException
         */
        public function storeScores( $scores ) {
-               global $wgOresModelClasses;
-               // Map to database fields.
                $dbData = [];
                foreach ( $scores as $revision => $revisionData ) {
-                       foreach ( $revisionData as $model => $modelOutputs ) {
-                               if ( isset( $modelOutputs['error'] ) ) {
-                                       call_user_func( $this->errorCallback, 
$modelOutputs['error']['message'], $revision );
-                                       continue;
-                               }
-
-                               $prediction = $modelOutputs['prediction'];
-                               // Kludge out booleans so we can match 
prediction against class name.
-                               if ( $prediction === false ) {
-                                       $prediction = 'false';
-                               } elseif ( $prediction === true ) {
-                                       $prediction = 'true';
-                               }
-
-                               $modelId = $this->getModelId( $model );
-                               if ( !isset( $wgOresModelClasses[ $model ] ) ) {
-                                       throw new RuntimeException( "Model 
$model is not configured" );
-                               }
-                               foreach ( $modelOutputs['probability'] as 
$class => $probability ) {
-                                       $ores_is_predicted = $prediction === 
$class;
-                                       if ( !isset( $wgOresModelClasses[ 
$model ][ $class ] ) ) {
-                                               throw new RuntimeException( 
"Class $class in model $model is not configured" );
-                                       }
-                                       $class = $wgOresModelClasses[ $model ][ 
$class ];
-                                       if ( $class === 0 ) {
-                                               // We don't store rows for 
class 0, because we can compute the class 0 probability by
-                                               // subtracting the sum of the 
probabilities of the other classes from 1
-                                               continue;
-                                       }
-                                       $dbData[] = [
-                                               'oresc_rev' => $revision,
-                                               'oresc_model' => $modelId,
-                                               'oresc_class' => $class,
-                                               'oresc_probability' => 
$probability,
-                                               'oresc_is_predicted' => ( 
$ores_is_predicted ),
-                                       ];
-                               }
-                       }
+                       $this->processRevision( $dbData, $revision, 
$revisionData );
                }
 
                \wfGetDB( DB_MASTER )->insert( 'ores_classification', $dbData, 
__METHOD__ );
@@ -147,6 +108,55 @@
                return $modelId;
        }
 
+       /**
+        * @param array $dbData
+        * @param int $revision
+        * @param array $revisionData
+        */
+       public function processRevision( &$dbData, int $revision, array 
$revisionData ) {
+               global $wgOresModelClasses;
+               // Map to database fields.
+
+               foreach ( $revisionData as $model => $modelOutputs ) {
+                       if ( isset( $modelOutputs['error'] ) ) {
+                               call_user_func( $this->errorCallback, 
$modelOutputs['error']['message'], $revision );
+                               continue;
+                       }
+
+                       $prediction = $modelOutputs['prediction'];
+                       // Kludge out booleans so we can match prediction 
against class name.
+                       if ( $prediction === false ) {
+                               $prediction = 'false';
+                       } elseif ( $prediction === true ) {
+                               $prediction = 'true';
+                       }
+
+                       $modelId = $this->getModelId( $model );
+                       if ( !isset( $wgOresModelClasses[ $model ] ) ) {
+                               throw new RuntimeException( "Model $model is 
not configured" );
+                       }
+                       foreach ( $modelOutputs['probability'] as $class => 
$probability ) {
+                               $ores_is_predicted = $prediction === $class;
+                               if ( !isset( $wgOresModelClasses[ $model ][ 
$class ] ) ) {
+                                       throw new RuntimeException( "Class 
$class in model $model is not configured" );
+                               }
+                               $class = $wgOresModelClasses[ $model ][ $class 
];
+                               if ( $class === 0 ) {
+                                       // We don't store rows for class 0, 
because we can compute the class 0 probability by
+                                       // subtracting the sum of the 
probabilities of the other classes from 1
+                                       continue;
+                               }
+                               $dbData[] = [
+                                       'oresc_rev' => $revision,
+                                       'oresc_model' => $modelId,
+                                       'oresc_class' => $class,
+                                       'oresc_probability' => $probability,
+                                       'oresc_is_predicted' => ( 
$ores_is_predicted ),
+                               ];
+                       }
+               }
+       }
+
        public function getModels() {
                $models = \wfGetDB( DB_SLAVE )->selectFieldValues( 'ores_model',
                        'oresm_name',

-- 
To view, visit https://gerrit.wikimedia.org/r/309824
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I8bbc568666ced7b2acf83d4e18df5a871d6a1555
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Catrope <roan.katt...@gmail.com>
Gerrit-Reviewer: Ladsgroup <ladsgr...@gmail.com>
Gerrit-Reviewer: Legoktm <legoktm.wikipe...@gmail.com>
Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to