Milimetric has uploaded a new change for review.

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

Change subject: Clean up some of the js code, add package.json
......................................................................

Clean up some of the js code, add package.json

TODO: resolve the data urls that vega may be trying to fetch, because it
will look for them on the local server otherwise.  I added a little hack
that inserts mediawiki.org so I could test locally and got it to render
a graph but a lot of the time it has some kind of response empty problem

Change-Id: I0d34fe8425262dfbeffe0ebb50322372a214ec97
---
A .gitignore
M graphoid-worker.js
A package.json
3 files changed, 77 insertions(+), 42 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/graphoid 
refs/changes/81/191781/1

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c31c9d9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+*swp
+node_modules
diff --git a/graphoid-worker.js b/graphoid-worker.js
index 476c12d..2f8f7b0 100644
--- a/graphoid-worker.js
+++ b/graphoid-worker.js
@@ -1,4 +1,4 @@
-"use strict";
+'use strict';
 
 var cluster = require('cluster');
 
@@ -14,45 +14,47 @@
        child_process = require('child_process'),
        request = require('request-promise'),
        Promise = require('promise'), // https://www.npmjs.com/package/promise
-       http = require("http"),
+       http = require('http'),
        url = require('url'),
        querystring = require('querystring'),
        vega = null; // Visualization grammar - https://github.com/trifacta/vega
 
 
 try{
-       vega = require("vega");
+       vega = require('vega');
 } catch(err) {
-       console.log(err)
+       console.log(err);
 }
 
 var config;
 
 // Get the config
 try {
-       config = JSON.parse(fs.readFileSync('./graphoid.config.json', 'utf8'));
+       config = require('./graphoid.config.json');
 } catch ( e ) {
-       console.error("Please set up your graphoid.config.json");
+       console.error('Please set up your graphoid.config.json');
        process.exit(1);
 }
 
-var serverRe = new RegExp('^([-a-z0-9]+\.)?(m\.|zero\.)?(' + 
config.domains.join('|') + ')$');
+var serverRe = new RegExp('^([-a-z0-9]+\\.)?(m\\.|zero\\.)?(' + 
config.domains.join('|') + ')$');
 
 if (vega) {
        vega.config.domainWhiteList = config.domains;
        vega.config.safeMode = true;
 }
 
+// NOTE: there are a few libraries that do this
 function merge() {
-       var result = {};
-       for (var i = 0; i < arguments.length; i++) {
-               var obj = arguments[i];
-               for (var key in obj) {
-                       if (obj.hasOwnProperty(key)) {
-                               result[key] = obj[key];
-                       }
-               }
-       }
+       var result = {},
+               args = Array.prototype.slice.apply(arguments);
+       console.log(args);
+
+       args.forEach(function (arg) {
+               Object.getOwnPropertyNames(arg).forEach(function (prop) {
+                       result[prop] = arg[prop];
+               });
+       });
+
        return result;
 }
 
@@ -64,34 +66,37 @@
 
        processResult = function (response) {
                var body = JSON.parse(response);
-               if ('error' in body) {
-                       throw 'API result error: ' + data.error;
+               if (body.hasOwnProperty('error')) {
+                       throw 'API result error: ' + body.error;
                }
-               if ('warnings' in body) {
+               if (body.hasOwnProperty('warnings')) {
                        console.log('API warning: ' + 
JSON.stringify(body.warnings) + ' while getting ' + JSON.stringify({
                                url: url,
                                opts: qs
                        }));
                }
-               if ('query' in body && 'pages' in body.query) {
-                       var obj = body.query.pages;
-                       for (var k in obj) {
-                               if (obj.hasOwnProperty(k)) {
-                                       var page = obj[k];
-                                       if ('pageprops' in page && 
'graph_specs' in page.pageprops) {
-                                               var gs = 
JSON.parse(page.pageprops.graph_specs);
-                                               if (id in gs) {
-                                                       return gs[id];
-                                               }
+               if (body.hasOwnProperty('query') && 
body.query.hasOwnProperty('pages')) {
+                       var pages = body.query.pages,
+                               graph_spec = null;
+
+                       Object.getOwnPropertyNames(pages).some(function (k) {
+                               var page = pages[k];
+                               if (page.hasOwnProperty('pageprops') && 
page.pageprops.hasOwnProperty('graph_specs')) {
+                                       var gs = 
JSON.parse(page.pageprops.graph_specs);
+                                       if (gs.hasOwnProperty(id)) {
+                                               graph_spec = gs[id];
+                                               return true;
                                        }
                                }
+                               return false;
+                       });
+
+                       if (graph_spec) {
+                               return graph_spec;
                        }
                }
-               if ('continue' in body) {
-                       return callApiInt(url, merge(qs, body.continue));
-               } else {
-                       return false;
-               }
+               return body.hasOwnProperty('continue') ?
+                       callApiInt(url, merge(qs, body.continue)) : false;
        };
 
        callApiInt = function(url, options) {
@@ -117,19 +122,20 @@
 
 function validateRequest(req) {
        var query = url.parse(req.url, true).query;
-       if (!('revid' in query)) {
+
+       if (!query.hasOwnProperty('revid')) {
                throw 'no revid';
        }
-       if (String(Math.abs(~~Number(query.revid))) !== query.revid) {
+       if (!/^[0-9]+$/.test(query.revid)) {
                // must be a non-negative integer
                throw 'bad revid param';
        }
        // In case we switch to title, make sure to fail on 
query.title.indexOf('|') > -1
 
-       if (!('id' in query)) {
+       if (!query.hasOwnProperty('id')) {
                throw 'no id param';
        }
-       if (!('server' in query)) {
+       if (!query.hasOwnProperty('server')) {
                throw 'no server param';
        }
        // Remove optional part #2 from host (makes m. links appear as desktop 
to optimize cache)
@@ -155,14 +161,20 @@
 function renderOnCanvas(spec, response) {
        return new Promise(function (fulfill, reject){
                if (!vega) {
-                       throw "Unable to load Vega npm module";
+                       throw 'Unable to load Vega npm module';
                }
-               vega.headless.render({spec: spec, renderer: "canvas"}, function 
(err, result) {
+
+               // TODO: hack for local testing
+               // TODO: weird, but this only works some of the time
+               spec.data[1].url = 'http://www.mediawiki.org' + 
spec.data[1].url;
+               console.log(spec);
+
+               vega.headless.render({spec: spec, renderer: 'canvas'}, function 
(err, result) {
                        if (err) {
                                reject(err);
                        } else {
                                var stream = result.canvas.pngStream();
-                               response.writeHead(200, {"Content-Type": 
"image/png"});
+                               response.writeHead(200, {'Content-Type': 
'image/png'});
                                stream.on('data', function (chunk) {
                                        response.write(chunk);
                                });
@@ -191,7 +203,7 @@
 
 // robots.txt: no indexing.
 app.get(/^\/robots.txt$/, function ( req, response ) {
-    response.end( "User-agent: *\nDisallow: /\n" );
+    response.end( 'User-agent: *\nDisallow: /\n' );
 });
 
 
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..07aec3a
--- /dev/null
+++ b/package.json
@@ -0,0 +1,21 @@
+{
+  "name": "graphoid",
+  "version": "0.1.0",
+  "description": "renders vega graphs from mediawiki pages",
+  "keywords": [
+    "vega",
+    "graph",
+    "mediawiki"
+  ],
+  "author": "Yuri Astrakhan",
+  "contributors": [
+    "Dan Andreescu <[email protected]>"
+  ],
+  "dependencies": {
+    "express": "== 4.11.2",
+    "request-promise": "== 0.4.0",
+    "promise": "== 6.1.0",
+    "vega": "== 1.4.3"
+  },
+  "license": "MIT"
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0d34fe8425262dfbeffe0ebb50322372a214ec97
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/graphoid
Gerrit-Branch: master
Gerrit-Owner: Milimetric <[email protected]>

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

Reply via email to