Skip to content
Newer
Older
100644 60 lines (49 sloc) 1.31 KB
72dbfe9 @fairchild update require paths
fairchild authored Apr 4, 2009
1 require 'sinatra'
2 require 'sequel'
ad4273b @adamwiggins point class
authored Aug 24, 2008
3
34ef7b3 @adamwiggins post dynamic points
authored Aug 24, 2008
4 module Points
5 def self.data
2e964d9 @adamwiggins use sequel orm
authored Aug 26, 2008
6 @@data ||= make
7 end
8
9 def self.make
35d0825 @adamwiggins use database_url
authored Sep 15, 2008
10 db = Sequel.connect(ENV['DATABASE_URL'] || 'sqlite://rifgraf.db')
cf2f59f @adamwiggins connect to production db via env vars
authored Aug 31, 2008
11 make_table(db)
c78b9f6 @adamwiggins catch table already exists error
authored Aug 31, 2008
12 db[:points]
cf2f59f @adamwiggins connect to production db via env vars
authored Aug 31, 2008
13 end
14
15 def self.make_table(db)
2e964d9 @adamwiggins use sequel orm
authored Aug 26, 2008
16 db.create_table :points do
17 varchar :graph, :size => 32
18 varchar :value, :size => 32
0306d76 @adamwiggins rename date -> timestamp
authored Sep 14, 2008
19 timestamp :timestamp
2e964d9 @adamwiggins use sequel orm
authored Aug 26, 2008
20 end
332c5ec @adamwiggins Revert "don't rescue error on make table"
authored Sep 14, 2008
21 rescue Sequel::DatabaseError
22 # assume table already exists
34ef7b3 @adamwiggins post dynamic points
authored Aug 24, 2008
23 end
24 end
8c665cc @adamwiggins init
authored Aug 24, 2008
25
1a470d1 @adamwiggins graph_from_params helper for and= uri syntax
authored Nov 1, 2008
26 helpers do
4579724 @adamwiggins flash has trouble with commas in the and= syntax, convert to + instead
authored Nov 1, 2008
27 def graphs_from_params(seperator)
28 [ params[:id] ] + (params[:and] || '').split(seperator)
1a470d1 @adamwiggins graph_from_params helper for and= uri syntax
authored Nov 2, 2008
29 end
30 end
31
df655f4 @adamwiggins about page (not quite finished)
authored Sep 9, 2008
32 get '/' do
33 erb :about
34 end
35
82516ec @adamwiggins bucket points by graph
authored Aug 25, 2008
36 get '/graphs/:id' do
4579724 @adamwiggins flash has trouble with commas in the and= syntax, convert to + instead
authored Nov 2, 2008
37 graphs_from_params(',').each do |graph|
edd2b7b @adamwiggins view multiple data sets in the same graph
authored Nov 1, 2008
38 throw :halt, [ 404, "No such graph \"#{graph}\"" ] unless Points.data.filter(:graph => graph).count > 0
39 end
4579724 @adamwiggins flash has trouble with commas in the and= syntax, convert to + instead
authored Nov 2, 2008
40 erb :graph, :locals => { :id => params[:id], :others => (params[:and] || '').gsub(/,/, '+') }
8c665cc @adamwiggins init
authored Aug 24, 2008
41 end
42
294d5e5 @adamwiggins switch to amstock
authored Aug 31, 2008
43 get '/graphs/:id/amstock_settings.xml' do
4579724 @adamwiggins flash has trouble with commas in the and= syntax, convert to + instead
authored Nov 2, 2008
44 erb :amstock_settings, :locals => { :graphs => graphs_from_params(' ') }
294d5e5 @adamwiggins switch to amstock
authored Aug 31, 2008
45 end
46
47 get '/graphs/:id/data.csv' do
0306d76 @adamwiggins rename date -> timestamp
authored Sep 14, 2008
48 erb :data, :locals => { :points => Points.data.filter(:graph => params[:id]).reverse_order(:timestamp) }
34ef7b3 @adamwiggins post dynamic points
authored Aug 24, 2008
49 end
50
82516ec @adamwiggins bucket points by graph
authored Aug 25, 2008
51 post '/graphs/:id' do
0306d76 @adamwiggins rename date -> timestamp
authored Sep 14, 2008
52 Points.data << { :graph => params[:id], :timestamp => (params[:timestamp] || Time.now), :value => params[:value] }
34ef7b3 @adamwiggins post dynamic points
authored Aug 24, 2008
53 "ok"
8c665cc @adamwiggins init
authored Aug 24, 2008
54 end
05b6bb9 @adamwiggins delete a graph
authored Aug 31, 2008
55
56 delete '/graphs/:id' do
f46cad4 @adamwiggins delete filtered to just current graph (thanks François Beausoleil)
authored Sep 17, 2008
57 Points.data.filter(:graph => params[:id]).delete
05b6bb9 @adamwiggins delete a graph
authored Aug 31, 2008
58 "ok"
59 end
Something went wrong with that request. Please try again.