forked from loe/graphiti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphiti.rb
146 lines (118 loc) · 2.9 KB
/
graphiti.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require 'rubygems'
require 'bundler'
Bundler.setup
if !defined?(RACK_ENV)
RACK_ENV = ENV['RACK_ENV'] || 'development'
end
require 'sinatra/base'
require 'sinatra/contrib'
require 'redis/namespace'
require 'compass'
require 'typhoeus'
require 'yajl'
require 'digest/sha1'
require './lib/s3/request'
require './lib/s3/signature'
require './lib/redised'
require './lib/metric'
require './lib/graph'
require './lib/dashboard'
class Graphiti < Sinatra::Base
VERSION = '0.1.0'
register Sinatra::Contrib
config_file 'config/settings.yml'
configure do
set :logging, true
Compass.configuration do |config|
config.project_path = settings.root
config.sass_dir = File.join(settings.views, 'stylesheets')
config.output_style = :compact
end
set :haml, :format => :html5
set :scss, Compass.sass_engine_options
set :method_override, true
Graph.redis = settings.redis_url
Dashboard.redis = settings.redis_url
Metric.redis = settings.redis_url
end
before do
S3::Request.logger = logger
end
get '/graphs/:uuid.js' do
json Graph.find(params[:uuid])
end
get '/metrics.js' do
json :metrics => Metric.find(params[:q])
end
get '/graphs.js' do
json :graphs => Graph.all
end
get '/dashboards/:slug.js' do
json Dashboard.find(params[:slug], true)
end
get '/dashboards.js' do
if params[:uuid]
json :dashboards => Dashboard.without_graph(params[:uuid])
else
json :dashboards => Dashboard.all
end
end
post '/graphs' do
uuid = Graph.save(params[:graph])
json :uuid => uuid
end
post '/graphs/:uuid/snapshot' do
url = Graph.snapshot(params[:uuid])
json :url => url
end
put '/graphs/:uuid' do
uuid = Graph.save(params[:uuid], params[:graph])
json :uuid => uuid
end
post '/dashboards' do
dashboard = Dashboard.save(params[:dashboard])
json :dashboard => dashboard
end
post '/graphs/dashboards' do
json Dashboard.add_graph(params[:dashboard], params[:uuid])
end
delete '/graphs/dashboards' do
json Dashboard.remove_graph(params[:dashboard], params[:uuid])
end
delete '/graphs/:uuid' do
Graph.destroy(params[:uuid])
end
delete '/dashboards/:slug' do
Dashboard.destroy(params[:slug])
end
post '/snapshot' do
filename = Graph.snapshot(params[:uuid])
json :filename => filename
end
# Routes that are entirely handled by Sammy/frontend
# and just need to load the empty index
%w{
/graphs/workspace
/graphs/new
/graphs/:uuid
/graphs/:uuid/snapshots
/graphs
/dashboards/:slug
/dashboards
/
}.each do |path|
get path do
haml :index
end
end
get '/stylesheets/:name.css' do
content_type 'text/css'
scss :"stylesheets/#{params[:name]}"
end
def default_graph
{
:options => settings.default_options,
:targets => settings.default_metrics.collect {|m| [m, {}] }
}
end
end