Permalink
Browse files

permit forking queries

  • Loading branch information...
1 parent 0b345c8 commit f910f456d6c57944a962b5d6a76911c6f1fe180c Peter van Hardenberg committed Sep 25, 2011
Showing with 51 additions and 28 deletions.
  1. +15 −24 graphor.rb
  2. +16 −1 lib/graphor/query.rb
  3. +20 −3 public/graph.html
View
@@ -15,48 +15,39 @@ class Graphor < Sinatra::Base
File.read(File.join('public', 'index.html'))
end
-get "/data" do
- grab_the_data(params["database_url"], params["query"])
-end
-
delete "/:id" do
q = Query[:alias => params["id"]]
q.destroy
end
get "/:id/data" do
- q = Query[:alias => params["id"]]
- grab_the_data(q[:database_url], q[:query])
+ grab_the_data(Query[:alias => params["id"]])
end
post "/" do
+ p = Query[params["parent"]]
+
+ puts params.inspect
+ puts p.inspect
+
q = Query.create(
:alias => (0...8).map{ ('a'..'z').to_a[rand(26)] }.join,
- :database_url => params["database_url"],
- :query => params["query"])
+ :database_url => params["database_url"] || p[:database_url],
+ :query => params["query"],
+ :parent_alias => params["parent"])
redirect q[:alias]
end
-get '/:alias' do
+get '/:id' do
File.read(File.join('public', 'graph.html'))
end
-
helpers do
- def grab_the_data(database_url, query)
- db = Sequel.connect(database_url)
- x = []; y = []
- xlabels = [];
- db.fetch(query).each do |row|
- if row[:x].instance_of? Time
- xlabels << row[:x].to_s
- x << row[:x].to_i
- else
- x << row[:x]
- end
- y << row[:y]
- end
- {:x => x, :xlabels => xlabels, :y => y, :query => query}.to_json
+ def grab_the_data(query)
+ {:data => query.execute,
+ :query => query[:query],
+ :alias => query[:alias]
+ }.to_json
end
end
View
@@ -1,4 +1,19 @@
class Query < Sequel::Model
-
unrestrict_primary_key
+
+ def execute
+ db = Sequel.connect(self[:database_url])
+ x = []; y = []
+ xlabels = [];
+ db.fetch(self[:query]).each do |row|
+ if row[:x].instance_of? Time
+ xlabels << row[:x].to_s
+ x << row[:x].to_i
+ else
+ x << row[:x]
+ end
+ y << row[:y]
+ end
+ {:x => x, :y => y, :xlabels => xlabels}
+ end
end
View
@@ -32,11 +32,15 @@
function updateSucceeded(content) {
console.log(content)
var r = Raphael("holder");
+
+ $('#query').val(content.query)
+ $('#alias').val(content.alias)
+
r.g.txtattr.font = "12px 'Fontin Sans', Fontin-Sans, sans-serif";
- var x = content.x
- var xlabels = content.xlabels
- var y = content.y
+ var x = content.data.x
+ var xlabels = content.data.xlabels
+ var y = content.data.y
r.g.linechart(10, 10, 1400, 220, x, y, {axisxlabels: xlabels});
@@ -47,6 +51,19 @@
</head>
<body class="raphael">
<div id="holder"></div>
+
+ <form action="/" method="post">
+ <label for="query">Query:</label>
+
+ <br/>
+ <textarea name="query" id="query"></textarea>
+
+ <br/>
+ <input type="hidden" name="parent" id="alias"/>
+
+ <input type="submit"/>
+ </form>
+
</body>
</html>

0 comments on commit f910f45

Please sign in to comment.