Skip to content

Commit

Permalink
More work on server side graph generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie Love committed May 3, 2010
1 parent 04240f7 commit cd6ab82
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 22 deletions.
44 changes: 31 additions & 13 deletions server/handlers/500-r.js
Expand Up @@ -83,6 +83,7 @@ function handlePage(req, resp, sid, rNodeApi) {
}

var d = pageFiles[file];
SYS.debug('Streaming file "' + file + '" which is "' + d.file + '"');

UTILS.streamFile (pageFilePrefix + d.file, d.mimeType, resp, function (err) {
if (err)
Expand All @@ -95,21 +96,33 @@ function handlePage(req, resp, sid, rNodeApi) {
}

function handleGraphicalCommand (r, parsedRequest, httpRequest, resp, sid, rNodeApi) {
var filenames = rNodeApi.getRaccessibleTempFile('.png');
r.request (
'local({ \n' +
'png("' + filenames.r + '");' +
parsedRequest + ';' +
'dev.off();' +
'print("ok");' +
'});',

var context = rNodeApi.getSidContext(sid);
if (!context.graphing) {
context.graphing = {};
}

if (!context.graphing.file) // set up a file we write to.
context.graphing.file = rNodeApi.getRaccessibleTempFile('.png');

// Assume our current graphical device is our main one we've
// been tracking all graphing commands on.
var req = parsedRequest + ';\n' +
'png("' + context.graphing.file.r + '");\n' +
'dev.set(dev.prev());\n' +
'dev.copy(which=dev.next());\n' +
'dev.off();\n' +
'print("ok");\n';


r.request (req,
function (rResp) {
if (rResp.length && rResp[0] == "ok") {
var key = SHA256.hex_sha256 (filenames.ours);
var key = SHA256.hex_sha256 (context.graphing.file.ours);
pageFiles[key] = {
file: filenames.ours,
mimeType: 'image.png',
deleteFile: true
file: context.graphing.file.ours,
mimeType: 'image/png',
deleteFile: false
};
resp.writeHeader(200, { "Content-Type": "text/plain" });
resp.write (JSON.stringify ({
Expand All @@ -134,7 +147,12 @@ function handleGraphicalCommand (r, parsedRequest, httpRequest, resp, sid, rNode
}

function isGraphical(parsedRequest) {
return parsedRequest.beginsWith ('boxplot');
var commands = [ 'boxplot', 'title' ];
for (var i=0; i < commands.length; ++i) {
if (parsedRequest.beginsWith(commands[i]))
return true;
}
return false;
}

function handleR (req, resp, sid, rNodeApi) {
Expand Down
34 changes: 25 additions & 9 deletions server/r-node.js
Expand Up @@ -33,6 +33,12 @@ var sharedRConnection = null;
var Config = UTILS.loadJsonFile("configuration", "etc/config.js", "etc/config-example.js");
var AUTH = require ('./authenticators/' + Config.authentication.type.replace(/[^a-zA-Z-_]/g, '')).auth;
var Authenticator = AUTH.instance();
var sessions = {};

var requiredSetupSteps = {
"auth": false,
"testR": false
}

nodelog (null, "Using authenticator: '" + AUTH.name + "'");

Expand All @@ -53,6 +59,15 @@ var rNodeApi = {
r: Config.R.tempDirectoryFromRperspective + '/' + s
}
}
, getSidContext: function (sid) {
if (!sessions[sid])
return null;

if (!sessions[sid].context)
sessions[sid].context = {};

return sessions[sid].context;
}
, log: nodelog
, config: Config
}
Expand All @@ -72,8 +87,6 @@ FS.readdirSync('./handlers').sort().forEach (function (d) {
}
});

var sessions = {};

function cleanOutSessions() {
var maxTime = Config.R.idleSessionTimeout || 30; // default to 30 minutes;
maxTime = maxTime * 60 * 1000;
Expand Down Expand Up @@ -228,9 +241,17 @@ function authorizedRequestMgr (req, resp, sid) {
}

function setupRSession (connection, callback) {

// Each session has a graphing target that is
// set up to take all graphing commands. It then
// copies them to actual files for the user to download.
var graphingFile = rNodeApi.getRaccessibleTempFile ('.png');

var rnodeSetupCommands = [
"rNodePager = function (files, header, title, f) { r <- files; attr(r, 'class') <- 'RNodePager'; attr(r, 'header') <- header; attr(r, 'title') <- title; attr(r, 'delete') <- f; r; }",
"options(pager=rNodePager)"
"rNodePager = function (files, header, title, f) { r <- files; attr(r, 'class') <- 'RNodePager'; attr(r, 'header') <- header; attr(r, 'title') <- title; attr(r, 'delete') <- f; r; }"
, "options(pager=rNodePager)"
, "png('" + graphingFile.r + "');"
, "dev.control(\"enable\");"
]

var runs = function (i) {
Expand Down Expand Up @@ -338,11 +359,6 @@ function testRConnection (callback) {
}


var requiredSetupSteps = {
"auth": false,
"testR": false
}

function conditionallyGoLive () {
for (var s in requiredSetupSteps) {
if (requiredSetupSteps[s] == false) {
Expand Down
57 changes: 57 additions & 0 deletions shared/js/R/commands/10-graphical-commands.js
@@ -0,0 +1,57 @@
/*
Copyright 2010 Jamie Love. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY JAMIE LOVE ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JAMIE LOVE OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Jamie Love.
*/

rnode.command.GraphicalCommands = RNodeCore.extend (rnode.R.ParsedCommand, {
canHandle: function (parsedCommand) {
var commands = [
/^\s*boxplot/,
/^\s*title/,
];

var c = parsedCommand.get();

for (var i = 0; i < commands.length; ++i) {
if (c.search(commands[i]) >= 0)
return true;
}

return false;
},

// For graphical commands, we don't want to wrap them in a print request,
// we want the server to get them raw.
execute: function (rApi, parsedCommand, userCallback) {
rApi.directlyExecute (parsedCommand, function (result, data) {
userCallback (result, data);
});
}
});

rnode.command.CommandHandler.register (rnode.command.GraphicalCommands);


0 comments on commit cd6ab82

Please sign in to comment.