Permalink
Browse files

Report Editor: File Managament Prototype

  • Loading branch information...
1 parent fbeb3d2 commit 521722dba6d95f086d8b207a5573f51ea0742573 @mhils committed Dec 28, 2012
@@ -14,14 +14,12 @@ require(["dojo/request","dojo/request/notify","dojo/Deferred"],function(request,
var def = new Deferred();
- request("/api/token").then(function(data){
+ request("/api/token",{handleAs:"json"}).then(function(data){
var token = data.token;
notify("send", function(req){
if(req.options.method !== "GET") {
- console.log("send",req);
- console.warn(token);
+ req.xhr.setRequestHeader("X-Request-Token",token);
}
-
});
def.resolve(true);
},function(){
@@ -1,5 +1,5 @@
-define([ "dojo/_base/declare", "./_DetailViewPane", "dojo/dom-construct",
- "dojo/text!./templates/DetailsPane.ejs" ], function(declare, _DetailViewPane,
+define([ "lodash", "dojo/_base/declare", "./_DetailViewPane", "dojo/dom-construct",
+ "dojo/text!./templates/DetailsPane.ejs" ], function(_, declare, _DetailViewPane,
domConstruct, template) {
return declare([ _DetailViewPane ], {
@@ -17,7 +17,6 @@ define([ "dojo/_base/declare", "./_DetailViewPane", "dojo/dom-construct",
function(formData) {
var fragment = document.createDocumentFragment();
for ( var i = 0; i < formData.length; i++) {
- //FIXME: Replace underscore.js escaping.
fragment.appendChild(domConstruct.toDom("<tr><td>"
+ _.escape(decodeURIComponent(formData[i].name)) + "</td><td>"+
_.escape(decodeURIComponent(formData[i].value))+
@@ -1,6 +1,15 @@
-define([ "lodash", "dojo/_base/declare", "codemirror/all", "dijit/form/Button",
- "../util/sampleFlow", "../util/_DynamicTemplatedWidget",
- "dojo/text!./templates/ReportEditor.ejs" ], function(_, declare,
+define([ "lodash",
+ "dojo/_base/declare",
+ "dojo/_base/array",
+ "dojo/Deferred",
+ "dojo/dom-construct",
+ "dojo/on",
+ "dojo/request",
+ "codemirror/all",
+ "dijit/form/Button",
+ "../util/sampleFlow",
+ "../util/_DynamicTemplatedWidget",
+ "dojo/text!./templates/ReportEditor.ejs" ], function(_, declare, array, Deferred, domConstruct, on, request,
CodeMirrorPromise, Button, sampleFlow, _DynamicTemplatedWidget, template) {
return declare([ _DynamicTemplatedWidget ], {
@@ -12,8 +21,9 @@ define([ "lodash", "dojo/_base/declare", "codemirror/all", "dijit/form/Button",
postCreate: function() {
this.inherited(arguments);
- var reportCodeNode = this.reportCodeNode;
- CodeMirrorPromise.then((function(CodeMirror) {
+ var self = this;
+
+ CodeMirrorPromise.then(function(CodeMirror) {
CodeMirror.commands.autocomplete = function(cm) {
CodeMirror.simpleHint(cm, CodeMirror.javascriptHint, {
completeSingle: false,
@@ -36,7 +46,7 @@ define([ "lodash", "dojo/_base/declare", "codemirror/all", "dijit/form/Button",
};
cm.autoIndentRange(range.from, range.to);
};
- this.codeMirror = CodeMirror.fromTextArea(reportCodeNode, {
+ self.codeMirror = CodeMirror.fromTextArea(self.reportCodeNode, {
lineNumbers: true,
mode: "javascript",
matchBrackets: true,
@@ -46,10 +56,90 @@ define([ "lodash", "dojo/_base/declare", "codemirror/all", "dijit/form/Button",
"Shift-Ctrl-F": "autoformat"
},
onCursorActivity: function() {
- this.codeMirror.matchHighlight("CodeMirror-matchhighlight");
+ self.codeMirror.matchHighlight("CodeMirror-matchhighlight");
}
});
- }).bind(this));
+
+ //Editor Synchronization
+ var API_PATH = "/api/fs/report_scripts/";
+
+ var saveReq, lastState, currentFilename, files = [];
+ //Load all available scripts
+ request(API_PATH+"?recursive=true",{handleAs:"json"}).then(function(dirs){
+
+ var optionsFragment = document.createDocumentFragment();
+ array.forEach(dirs,function(dir){
+ array.forEach(dir[2],function(file){
+ var filename = (dir[0].replace("\\","/") + "/" + file).substr(1);
+ var option = domConstruct.create("option", {value: filename, selected: filename=="=intro.js" ? true : false}, optionsFragment);
+ option.textContent = filename;
+ files.push(filename);
+ });
+ });
+ domConstruct.place(optionsFragment, self.scriptSelectionNode);
+
+ });
+ function isSaved(){
+ return lastState === undefined || lastState === self.getCode();
+ }
+ function exists(filename){
+ return array.indexOf(files, filename) >= 0;
+ }
+
+ function save(){
+ if(isSaved())
+ return (new Deferred()).resolve();
+ if(saveReq)
+ saveReq.cancel();
+
+ self.statusNode.textContent = "saving...";
+ currentFilename = currentFilename.replace(/[^ \w\.\-\/\\]/g,"");
+ var _exists = exists(currentFilename);
+ var method = _exists ? request.put : request.post;
+ if(!_exists)
+ files.push(currentFilename);
+ var code = self.getCode();
+ saveReq = method(API_PATH + currentFilename,
+ {data:JSON.stringify({"content":code})});
+ saveReq.then(function(){
+ self.statusNode.textContent = "saved.";
+ lastState = code;
+ saveReq = null;
+ });
+ return saveReq;
+ }
+ function newfile(filename){
+ if(exists(filename))
+ return load(filename);
+ var option = domConstruct.create("option", {value: filename, selected: true}, self.scriptSelectionNode);
+ option.textContent = filename;
+ currentFilename = filename;
+ }
+ function load(filename){
+ save().then(function(){
+ request.get(API_PATH+filename).then(function(script){
+ currentFilename = filename;
+ lastState = script;
+ self.codeMirror.setValue(script);
+ });
+
+ });
+ }
+
+ on(self.newScriptNode,"click",function(){
+ newfile(window.prompt("New file name:"));
+ });
+ self.codeMirror.on("change",function(){
+ save();
+ });
+
+ on(self.scriptSelectionNode,"change",function(){
+ console.log("scriptSelectionNode:onChange");
+ load(this.options[this.selectedIndex].value);
+ });
+ load("=intro.js");
+ lastState = undefined;
+ });
this.startButton = new Button({
iconClass: "dijitIcon dijitIconFunction"
@@ -1,71 +1,10 @@
<div class="sidebar reportEditor">
<h3>Report Editor</h3>
-<textarea data-dojo-attach-point="reportCodeNode">
-require([
- "dojo/dom-style",
- "dojox/charting/Chart",
- "dojox/charting/themes/Claro",
- "dojox/charting/plot2d/Pie",
- "dojox/charting/action2d/Tooltip",
- "dojox/charting/action2d/MoveSlice",
- "HoneyProxy/util/formatSize",
- "dojox/charting/plot2d/Markers",
- "dojox/charting/axis2d/Default",
-], function(domStyle, Chart, theme, Pie, Tooltip, MoveSlice,formatSize) {
-
- domStyle.set(outNode, {
- width: "100%",
- height: "100%"
- });
-
- var trafficPerHost = {};
- traffic.each(function(t){
- var host = t.request.host;
- if(!(host in trafficPerHost))
- trafficPerHost[host] = {y:0,text:"",count:0};
- trafficPerHost[host]["y"] += t.request.contentLength + t.response.contentLength;
- trafficPerHost[host]["count"] += 1;
- });
- var data = [];
-
- for (host in trafficPerHost){
- trafficPerHost[host]["tooltip"] = trafficPerHost[host]["count"] + " requests";
- trafficPerHost[host]["text"] = host + " ("+formatSize(trafficPerHost[host]["y"])+")";
- data.push(trafficPerHost[host]);
- }
-
- // Create the chart within it's "holding" node
- var chart = new Chart(outNode,{
- title: "Traffic per host"
- });
-
- // Set the theme
- chart.setTheme(theme);
-
- // Add the only/default plot
- chart.addPlot("default", {
- type: Pie,
- markers: true,
- radius:200
- });
-
- // Add axes
- chart.addAxis("x");
- chart.addAxis("y", { vertical: true, fixLower: "major", fixUpper: "major"/*, max: 5000*/ });
-
- // Add the series of data
- chart.addSeries("Traffic",data);
-
- var tip = new Tooltip(chart,"default");
-
- // Create the slice mover
- var mag = new MoveSlice(chart,"default");
-
- // Render the chart!
- chart.render();
-
-});
-</textarea>
+<button data-dojo-attach-point="newScriptNode">New</button>
+<div data-dojo-attach-point="statusNode"></div>
+<select data-dojo-attach-point="scriptSelectionNode">
+</select>
+<textarea data-dojo-attach-point="reportCodeNode"></textarea>
<br>
<div data-dojo-attach-point="startButtonNode">Generate Report</div>
</div>
View
@@ -19,7 +19,12 @@ def __init__(self):
def requiresAuth(f):
assert f.__name__ in ["render_POST","render_PUT","render_DELETE"]
def auth_func(self, request, *args, **kwargs):
- if "token" in request.args and request.args["token"][0] == HoneyProxy.getApiAuthToken():
+ token = None
+ if "token" in request.args:
+ token = request.args["token"]
+ elif request.requestHeaders.hasHeader("X-Request-Token"):
+ token = request.requestHeaders.getRawHeaders("X-Request-Token",[None])[0]
+ if token == HoneyProxy.getApiAuthToken():
return f(self, request, *args, **kwargs)
else:
return ForbiddenResource(message="Invalid response Token").render(request)
@@ -49,7 +54,7 @@ def __init__(self, path):
self.path = path
@staticmethod
def clean(filename):
- return re.sub(r'[^\w\.\-]','',filename).strip(".")
+ return re.sub(r'[^\w\.\- =]','',filename).strip(".") # the equal sign is used for read only files
def getChild(self, name, request):
name = FileSystemCRUDApi.clean(name)
childPath = os.path.join(self.path,name)
View
@@ -0,0 +1 @@
+worksforme.
@@ -0,0 +1,4 @@
+/**
+ * HoneyProxy Report Editor.
+ *
+ */
@@ -0,0 +1,64 @@
+require([
+ "dojo/dom-style",
+ "dojox/charting/Chart",
+ "dojox/charting/themes/Claro",
+ "dojox/charting/plot2d/Pie",
+ "dojox/charting/action2d/Tooltip",
+ "dojox/charting/action2d/MoveSlice",
+ "HoneyProxy/util/formatSize",
+ "dojox/charting/plot2d/Markers",
+ "dojox/charting/axis2d/Default",
+], function(domStyle, Chart, theme, Pie, Tooltip, MoveSlice,formatSize) {
+
+ domStyle.set(outNode, {
+ width: "100%",
+ height: "100%"
+ });
+
+ var trafficPerHost = {};
+ traffic.each(function(t){
+ var host = t.request.host;
+ if(!(host in trafficPerHost))
+ trafficPerHost[host] = {y:0,text:"",count:0};
+ trafficPerHost[host]["y"] += t.request.contentLength + t.response.contentLength;
+ trafficPerHost[host]["count"] += 1;
+ });
+ var data = [];
+
+ for (host in trafficPerHost){
+ trafficPerHost[host]["tooltip"] = trafficPerHost[host]["count"] + " requests";
+ trafficPerHost[host]["text"] = host + " ("+formatSize(trafficPerHost[host]["y"])+")";
+ data.push(trafficPerHost[host]);
+ }
+
+ // Create the chart within it's "holding" node
+ var chart = new Chart(outNode,{
+ title: "Traffic per host"
+ });
+
+ // Set the theme
+ chart.setTheme(theme);
+
+ // Add the only/default plot
+ chart.addPlot("default", {
+ type: Pie,
+ markers: true,
+ radius:200
+ });
+
+ // Add axes
+ chart.addAxis("x");
+ chart.addAxis("y", { vertical: true, fixLower: "major", fixUpper: "major"/*, max: 5000*/ });
+
+ // Add the series of data
+ chart.addSeries("Traffic",data);
+
+ var tip = new Tooltip(chart,"default");
+
+ // Create the slice mover
+ var mag = new MoveSlice(chart,"default");
+
+ // Render the chart!
+ chart.render();
+
+});

1 comment on commit 521722d

Owner

mhils commented on 521722d Dec 28, 2012

File Management, not Managament. 😒

Please sign in to comment.