Awight has uploaded a new change for review.
https://gerrit.wikimedia.org/r/239327
Change subject: WIP Encapsulate ORES fetch and store
......................................................................
WIP Encapsulate ORES fetch and store
Change-Id: I360606e53a1dc32002b7f61faa909c4ec38ad5f1
---
M includes/FetchScoreJob.php
A includes/OresCache.php
A includes/OresScoring.php
M maintenance/PurgeModelVersion.php
4 files changed, 108 insertions(+), 68 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ORES
refs/changes/27/239327/1
diff --git a/includes/FetchScoreJob.php b/includes/FetchScoreJob.php
index c4f5222..001d9f8 100644
--- a/includes/FetchScoreJob.php
+++ b/includes/FetchScoreJob.php
@@ -16,64 +16,10 @@
parent::__construct( 'ORESFetchScoreJob', $title, $params );
}
- private function getUrl() {
- global $wgOresBaseUrl, $wgOresModels;
-
- $url = str_replace( '$wiki', wfWikiID(), $wgOresBaseUrl );
- $params = array(
- 'models' => implode( '|', $wgOresModels ),
- 'revids' => $this->params['revid'],
- );
- return wfAppendQuery( $url, $params );
- }
-
public function run() {
- $url = $this->getUrl();
- $req = MWHttpRequest::factory( $url, null, __METHOD__ );
- $status = $req->execute();
- if ( !$status->isOK() ) {
- wfDebugLog( 'ORES', "No response from ORES server at
$url, "
- . $status->getMessage()->text() );
- return false;
- }
- $json = $req->getContent();
- $wireData = FormatJson::decode( $json, true );
- if ( !$wireData || !empty( $wireData['error'] ) ) {
- wfDebugLog( 'ORES', 'Bad response from ORES server: ' .
$json );
- return false;
- }
+ $scores = OresScoring::instance()->getScores(
$this->params['revid'] );
+ OresCache::instance()->storeScores( $scores,
$this->params['rcid'] );
- // Map from wire format to database fields.
- $dbData = array();
- foreach ( $wireData as $revision => $revisionData ) {
- foreach ( $revisionData as $model => $modelOutputs ) {
- if ( isset( $modelOutputs['error'] ) ) {
- wfDebugLog( 'ORES', 'Model output an
error: ' . $modelOutputs['error']['message'] );
- return false;
- }
-
- $prediction = $modelOutputs['prediction'];
- // Kludge out booleans so we can match
prediction against class name.
- if ( $prediction === false ) {
- $prediction = 'false';
- } elseif ( $prediction === true ) {
- $prediction = 'true';
- }
-
- foreach ( $modelOutputs['probability'] as
$class => $probability ) {
- $dbData[] = array(
- 'ores_rc' =>
$this->params['rcid'],
- 'ores_model' => $model,
- 'ores_model_version' => 0, //
FIXME: waiting for API support
- 'ores_class' => $class,
- 'ores_probability' =>
$probability,
- 'ores_is_predicted' => (
$prediction === $class ),
- );
- }
- }
- }
-
- wfGetDB( DB_MASTER )->insert( 'ores_classification', $dbData,
__METHOD__ );
return true;
}
}
diff --git a/includes/OresCache.php b/includes/OresCache.php
new file mode 100644
index 0000000..1ecd095
--- /dev/null
+++ b/includes/OresCache.php
@@ -0,0 +1,58 @@
+<?php
+
+class OresCache {
+ /**
+ * Save scores to the database
+ *
+ * @param array $scores in the same structure as is returned by ORES.
+ * @param integer $rcid Recent changes ID
+ *
+ * @throws RuntimeException
+ */
+ public function storeScores( $scores, $rcid ) {
+ // Map to database fields.
+ $dbData = array();
+ foreach ( $scores as $revision => $revisionData ) {
+ foreach ( $revisionData as $model => $modelOutputs ) {
+ if ( isset( $modelOutputs['error'] ) ) {
+ throw new RuntimeException( 'Model
contains an error: ' . $modelOutputs['error']['message'] );
+ }
+
+ $prediction = $modelOutputs['prediction'];
+ // Kludge out booleans so we can match
prediction against class name.
+ if ( $prediction === false ) {
+ $prediction = 'false';
+ } elseif ( $prediction === true ) {
+ $prediction = 'true';
+ }
+
+ foreach ( $modelOutputs['probability'] as
$class => $probability ) {
+ $dbData[] = array(
+ 'ores_rc' => $rcid,
+ 'ores_model' => $model,
+ 'ores_model_version' => 0, //
FIXME: waiting for API support
+ 'ores_class' => $class,
+ 'ores_probability' =>
$probability,
+ 'ores_is_predicted' => (
$prediction === $class ),
+ );
+ }
+ }
+ }
+
+ wfGetDB( DB_MASTER )->insert( 'ores_classification', $dbData,
__METHOD__ );
+ }
+
+ public function purge( $model, $version ) {
+ wfGetDb( DB_MASTER )->delete( 'ores_classification',
+ array(
+ 'ores_model' => $model,
+ 'ores_model_version' => $version,
+ ),
+ __METHOD__
+ );
+ }
+
+ public function instance() {
+ return new OresCache();
+ }
+}
diff --git a/includes/OresScoring.php b/includes/OresScoring.php
new file mode 100644
index 0000000..1f9b5d5
--- /dev/null
+++ b/includes/OresScoring.php
@@ -0,0 +1,46 @@
+<?php
+
+class OresScoring {
+ protected function getUrl( $revisions, $models ) {
+ global $wgOresBaseUrl;
+
+ $url = str_replace( '$wiki', wfWikiID(), $wgOresBaseUrl );
+ $params = array(
+ 'models' => implode( '|', (array) $models ),
+ 'revids' => implode( '|', (array) $revisions ),
+ );
+ return wfAppendQuery( $url, $params );
+ }
+
+ /**
+ * @param integer|array $revisions Single or multiple revisions
+ * @param string|array|null $models Single or multiple model names. If
+ * left empty, all configured models are queries.
+ * @return array Results in the form returned by ORES
+ * @throws RuntimeException
+ */
+ public function getScores( $revisions, $models = null ) {
+ if ( !$models ) {
+ global $wgOresModels;
+ $models = $wgOresModels;
+ }
+ $url = $this->getUrl( $revisions, $models );
+ $req = MWHttpRequest::factory( $url, null, __METHOD__ );
+ $status = $req->execute();
+ if ( !$status->isOK() ) {
+ throw new RuntimeException( "No response from ORES
server at $url, "
+ . $status->getMessage()->text() );
+ }
+ $json = $req->getContent();
+ $wireData = FormatJson::decode( $json, true );
+ if ( !$wireData || !empty( $wireData['error'] ) ) {
+ throw new RuntimeException( 'Bad response from ORES
server: ' . $json );
+ }
+
+ return $wireData;
+ }
+
+ public function instance() {
+ return new OresScoring();
+ }
+}
diff --git a/maintenance/PurgeModelVersion.php
b/maintenance/PurgeModelVersion.php
index 84e3021..1d4ad93 100644
--- a/maintenance/PurgeModelVersion.php
+++ b/maintenance/PurgeModelVersion.php
@@ -11,7 +11,7 @@
public function __construct() {
parent::__construct();
- $this->addDescription( "Purge all cached model results for a
given version." );
+ $this->addDescription( 'Purge all cached model results for a
given version.' );
$this->addOption( 'model', 'Model name', true, true );
$this->addOption( 'version', 'Model version number', true, true
);
@@ -21,17 +21,7 @@
$model = $this->getOption( 'model' );
$version = $this->getOption( 'version' );
- $this->purge( $model, $version );
- }
-
- public function purge( $model, $version ) {
- wfGetDb( DB_MASTER )->delete( 'ores_classification',
- array(
- 'ores_model' => $model,
- 'ores_model_version' => $version,
- ),
- __METHOD__
- );
+ OresCache::instance()->purge( $model, $version );
}
}
--
To view, visit https://gerrit.wikimedia.org/r/239327
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I360606e53a1dc32002b7f61faa909c4ec38ad5f1
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits