MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/319262

Change subject: DO NOT MERGE WIP: count page with geo tags
......................................................................

DO NOT MERGE WIP: count page with geo tags

Bug: T149722
Change-Id: I1fa2d69549af5527105efa4403329182399ab238
---
A geo-tag-counts.php
A src/Graphite.php
A src/Mysql.php
M vendor/composer/autoload_classmap.php
M vendor/composer/autoload_static.php
5 files changed, 76 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/analytics/discovery-stats 
refs/changes/62/319262/1

diff --git a/geo-tag-counts.php b/geo-tag-counts.php
new file mode 100644
index 0000000..2999053
--- /dev/null
+++ b/geo-tag-counts.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace DiscoveryStats;
+
+require_once( __DIR__ . '/vendor/autoload.php' );
+
+$config = json_decode( file_get_contents( __DIR__ . '/config.json' ) );
+
+$matrix = new SiteMatrix();
+$db = '/etc/mysql/conf.d/analytics-research-client.cnf';
+$switchToDb = $db->prepare( 'USE :dbname' );
+$graphite = new Graphite( $config );
+
+foreach ( $matrix->getSites() as $site ) {
+    $switchToDb->execute( [ ':dbname' => $site->getDbName() ] );
+    $siteKey = $site->getFamily() . '.' . $site->getCode();
+    $res = $db->query( 'SELECT count(*) AS num FROM geo_tags WHERE 
gt_primary=1' );
+    if ( $res && ( $row = reset( $res ) ) ) {
+        $siteKey = $site->getFamily() . '.' . $site->getCode();
+        $graphite->record( "geodata.pages.$siteKey.hourly" );
+    }
+}
diff --git a/src/Graphite.php b/src/Graphite.php
new file mode 100644
index 0000000..7a65164
--- /dev/null
+++ b/src/Graphite.php
@@ -0,0 +1,26 @@
+<?php
+
+namespace DiscoveryStats;
+
+class Graphite {
+    /** @var string */
+    private $host;
+    /** @var int */
+    private $port;
+    /* @var int */
+    private $timestamp;
+
+    public function __construct( $config ) {
+        $this->host = $config->graphiteHost;
+        $this->port = $config->graphitePort;
+        $this->timestamp = time();
+    }
+
+    public function record( $metric, $value ) {
+        $packet = "{$metric} {$value} {$this->timestamp}";
+        $nc = "nc -q0 {$this->host} {$this->port}";
+        $command = "echo \"$packet\" | $nc";
+
+        exec( $command );
+    }
+}
diff --git a/src/Mysql.php b/src/Mysql.php
new file mode 100644
index 0000000..82fefcb
--- /dev/null
+++ b/src/Mysql.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace DiscoveryStats;
+
+use Exception;
+use PDO;
+
+class Mysql {
+    /**
+     *
+     */
+    public static function connect( $config, $host ) {
+        $ini = parse_ini_file( $config );
+
+        if ( !$ini ) {
+            throw new Exception( "Error opening mysql config $config" );
+        }
+
+        return new PDO( "mysql:host=$host",
+            $config['client']['user'],
+            $config['client']['password']
+        );
+    }
+}
diff --git a/vendor/composer/autoload_classmap.php 
b/vendor/composer/autoload_classmap.php
index 19c5ac2..a23625b 100644
--- a/vendor/composer/autoload_classmap.php
+++ b/vendor/composer/autoload_classmap.php
@@ -7,6 +7,8 @@
 
 return array(
     'DiscoveryStats\\Api' => $baseDir . '/src/Api.php',
+    'DiscoveryStats\\Graphite' => $baseDir . '/src/Graphite.php',
+    'DiscoveryStats\\Mysql' => $baseDir . '/src/Mysql.php',
     'DiscoveryStats\\NormalSite' => $baseDir . '/src/NormalSite.php',
     'DiscoveryStats\\Site' => $baseDir . '/src/Site.php',
     'DiscoveryStats\\SiteMatrix' => $baseDir . '/src/SiteMatrix.php',
diff --git a/vendor/composer/autoload_static.php 
b/vendor/composer/autoload_static.php
index 43a02a2..9b64739 100644
--- a/vendor/composer/autoload_static.php
+++ b/vendor/composer/autoload_static.php
@@ -8,6 +8,8 @@
 {
     public static $classMap = array (
         'DiscoveryStats\\Api' => __DIR__ . '/../..' . '/src/Api.php',
+        'DiscoveryStats\\Graphite' => __DIR__ . '/../..' . '/src/Graphite.php',
+        'DiscoveryStats\\Mysql' => __DIR__ . '/../..' . '/src/Mysql.php',
         'DiscoveryStats\\NormalSite' => __DIR__ . '/../..' . 
'/src/NormalSite.php',
         'DiscoveryStats\\Site' => __DIR__ . '/../..' . '/src/Site.php',
         'DiscoveryStats\\SiteMatrix' => __DIR__ . '/../..' . 
'/src/SiteMatrix.php',

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1fa2d69549af5527105efa4403329182399ab238
Gerrit-PatchSet: 1
Gerrit-Project: analytics/discovery-stats
Gerrit-Branch: production
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to