Yurik has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/327418 )

Change subject: Add client usage of structured data
......................................................................


Add client usage of structured data

Really gotta add the ability to do this kind of stuff to reportupdater,
but no time right now.

Bug: T153272
Change-Id: I40bac9a1c7ae38455fc0111fc2b4c1bb1206a4cf
---
A bin/daily.sh
M src/Graphite.php
A structured-usage.php
3 files changed, 64 insertions(+), 2 deletions(-)

Approvals:
  Yurik: Verified; Looks good to me, approved



diff --git a/bin/daily.sh b/bin/daily.sh
new file mode 100755
index 0000000..1a411af
--- /dev/null
+++ b/bin/daily.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+
+BASEDIR=`dirname "$0"`/..
+
+/usr/bin/php $BASEDIR/structured-usage.php
diff --git a/src/Graphite.php b/src/Graphite.php
index 7a65164..85e848d 100644
--- a/src/Graphite.php
+++ b/src/Graphite.php
@@ -16,8 +16,11 @@
         $this->timestamp = time();
     }
 
-    public function record( $metric, $value ) {
-        $packet = "{$metric} {$value} {$this->timestamp}";
+    public function record( $metric, $value, $timestamp = null ) {
+        if ( $timestamp === null ) {
+            $timestamp = $this->timestamp;
+        }
+        $packet = "{$metric} {$value} {$timestamp}";
         $nc = "nc -q0 {$this->host} {$this->port}";
         $command = "echo \"$packet\" | $nc";
 
diff --git a/structured-usage.php b/structured-usage.php
new file mode 100644
index 0000000..06456a3
--- /dev/null
+++ b/structured-usage.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace DiscoveryStats;
+
+require_once( __DIR__ . '/vendor/autoload.php' );
+
+$config = json_decode( file_get_contents( __DIR__ . '/config.json' ) );
+$wikiBlacklist = [
+    'labswiki',
+    'labtestwiki',
+];
+
+$matrix = new SiteMatrix();
+$db = Mysql::connect( '/etc/mysql/conf.d/discovery-stats-client.cnf',
+    'analytics-store.eqiad.wmnet'
+);
+$graphite = new Graphite( $config );
+
+// Start of today
+$timestamp = mktime( 0, 0, 0 );
+
+foreach ( $matrix->getSites() as $site ) {
+    $dbName = $site->getDbName();
+    // Can't quote it, have to validate
+    if ( !preg_match( '/^[a-z0-9_]+$/', $dbName ) ) {
+        throw new \Exception( "Invalid database '$dbName'" );
+    }
+    if ( $site->isPrivate() || in_array( $dbName, $wikiBlacklist ) ) {
+        continue;
+    }
+
+    query( "USE $dbName" );
+    $siteKey = $site->getFamily() . '.' . $site->getCode();
+
+    $res = query( "SELECT count(*) AS num FROM page_props WHERE 
pp_propname='jsonconfig_getdata'" );
+    if ( $res && ( $row = $res->fetch() ) && $row['num'] ) {
+        $graphite->record( "daily.structured-data.client.pagecount.$siteKey",
+            $row['num'],
+            $timestamp
+        );
+    }
+}
+
+function query( $sql ) {
+    global $db;
+
+    $res = $db->query( $sql );
+    if ( !$res ) {
+        $err = $db->errorInfo();
+        throw new \Exception( "{$err[0]}: {$err[2]}" );
+    }
+
+    return $res;
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I40bac9a1c7ae38455fc0111fc2b4c1bb1206a4cf
Gerrit-PatchSet: 3
Gerrit-Project: analytics/discovery-stats
Gerrit-Branch: master
Gerrit-Owner: MaxSem <maxsem.w...@gmail.com>
Gerrit-Reviewer: Yurik <yu...@wikimedia.org>

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

Reply via email to