Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 7db5eb7a49f3c205c290ec5de08a72cbb02327a8 @mat committed Nov 1, 2011
Showing with 238 additions and 0 deletions.
  1. +9 −0 Gemfile
  2. +27 −0 Gemfile.lock
  3. +5 −0 README
  4. +21 −0 config.rb
  5. +99 −0 stats.rb
  6. +35 −0 views/detail.erb
  7. +42 −0 views/index.erb
@@ -0,0 +1,9 @@
+source :rubygems
+
+gem 'statsd-instrument', :path => "/Users/mat/repos/git/statsd-instrument"
+
+gem 'sinatra'
+gem 'shotgun'
+
+gem 'activesupport'
+gem 'i18n'
@@ -0,0 +1,27 @@
+PATH
+ remote: /Users/mat/repos/git/statsd-instrument
+ specs:
+ statsd-instrument (1.0.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activesupport (3.0.10)
+ i18n (0.5.0)
+ rack (1.3.2)
+ shotgun (0.9)
+ rack (>= 1.0)
+ sinatra (1.2.6)
+ rack (~> 1.1)
+ tilt (>= 1.2.2, < 2.0)
+ tilt (1.3.2)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activesupport
+ i18n
+ shotgun
+ sinatra
+ statsd-instrument!
5 README
@@ -0,0 +1,5 @@
+
+A simple HTML frontend for Graphite.
+
+bundle install
+bundle exec shotgun stats.rb
@@ -0,0 +1,21 @@
+
+require 'ostruct'
+App = OpenStruct.new({
+ :host => "graphite",
+ :timez => %w(10min 30min 60min 3hours 6hours 12hours 24hours 1week 1month),
+})
+
+Graph.define do |g|
+ g.title "Twitter action"
+ g.target "stats.twitter.halloween"
+ g.target "stats.twitter.obama"
+ g.target "stats.twitter.rchh"
+end
+
+Graph.define do |g|
+ g.title "Ping battle"
+ g.vtitle "ms"
+ g.target "stats.timers.pingtime.wimdu.com.mean"
+ g.target "stats.timers.pingtime.simfy.de.mean"
+end
+
@@ -0,0 +1,99 @@
+require 'sinatra'
+require 'i18n'
+require 'active_support/all'
+
+class Graph
+
+ def title(title=nil)
+ if title
+ self.params[:title] = title
+ end
+ self.params[:title]
+ end
+
+ def id
+ self.title.parameterize
+ end
+
+ def targets
+ @targets ||= {}
+ end
+
+ def params
+ @params ||= {}
+ end
+
+ def to_url(params = {})
+ time = (params[:time] || '30min').strip
+ time = "-#{time}" unless time =~ /^[-]/
+ "http://#{App.host}/render?#{to_param}&from=#{time}"
+ end
+
+
+
+ def self.define
+ new_graph = Graph.new
+ yield(new_graph)
+ new_graph.title.present? || raise('Need a title, Sir.')
+ graphs << new_graph
+ end
+
+ def self.graphs
+ @@graphs ||= []
+ end
+
+ def target(target_name, *args)
+ opts = args.pop
+ self.targets[target_name] = opts
+ end
+
+ def param(param_hash)
+ self.params.merge!(param_hash)
+ end
+
+ def vtitle(vtitle)
+ self.params[:vtitle] = vtitle
+ end
+
+ private
+ def to_param
+ # http://readthedocs.org/docs/graphite/en/latest/url-api.html
+ "target=#{targets_params}&#{default_params.merge(params).to_param}"
+ end
+
+ def default_params
+ {
+ :template => "plain",
+ :width => 1000,
+ :height => 300,
+ :tz=>"CET",
+ :fontSize => '11',
+ :min => 0,
+ }
+ end
+
+ def targets_params
+ target_names.join("&target=")
+ end
+
+ def target_names
+ targets.keys
+ end
+end
+
+require 'config.rb'
+
+get '/' do
+ @graphs = Graph.graphs
+ erb :index
+end
+
+get '/graphs/?' do
+ @graphs = Graph.graphs
+ erb :index
+end
+
+get '/graphs/:id/?' do |id|
+ @graph = Graph.graphs.find { |g| g.id == id }
+ if @graph then erb(:detail) else halt(404, 'Graph not found :(') end
+end
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Numbers</title>
+ <meta http-equiv="refresh" content="<%=params[:refresh] || 10 %>">
+
+ <style type="text/css" media="screen">
+ * { margin: 0; padding: 0; }
+ body { font-family: Helvetica, Arial, 'sans-serif'; background-color: #fff; overflow: auto; color: #000; font-size: 20px; }
+ h1 { font-weight: normal; margin-bottom: 30px; color: #000; font-size: 40px; }
+ a { text-decoration: underline; color: #111; padding: 0.3em; }
+ a:hover { color: #444; }
+ #container { width: 100%; text-align: center; margin: 100px 0; background-repeat:no-repeat; background-position: 8em center ; background-color:white; height:300px;
+} div { padding: 0.5em; }
+ </style>
+ </head>
+
+<body class="">
+<div>
+ <center>
+ <h1><%=@graph.title%></h1>
+ <a href="/">home</a>
+ </center>
+</div>
+
+<% App.timez.each do |t| %>
+ <center>
+ <div>
+ <%=t%>
+ </div>
+ <img id='<%=@graph.id%>' src='<%= @graph.to_url(:time => t) %>' />
+ </center>
+<% end %>
+</body>
+</html>
@@ -0,0 +1,42 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Numbers</title>
+ <meta http-equiv="refresh" content="<%=params[:refresh] || 10 %>">
+
+ <style type="text/css" media="screen">
+ * { margin: 0; padding: 0; }
+ body { font-family: Helvetica, Arial, 'sans-serif'; background-color: #fff; overflow: auto; color: #000; font-size: 20px; }
+ h1 { font-weight: normal; margin-bottom: 30px; color: #000; font-size: 40px; }
+ a { text-decoration: underline; color: #111; padding: 0.3em; }
+ a:hover { color: #444; }
+ #container { width: 100%; text-align: center; margin: 100px 0; background-repeat:no-repeat; background-position: 8em center ; background-color:white; height:300px;
+} div { padding: 0.5em; }
+ </style>
+ </head>
+
+<% time = params[:time] || '30min' %>
+
+<body class="">
+<div>
+ <center>
+ <% App.timez.reverse.each do |t| %>
+ <a href="/graphs/?time=<%= t %>">last <%= t %></a>
+ <% end %>
+ </center>
+</div>
+
+<% @graphs.each do |g| %>
+ <div>
+ <a href="/graphs/<%=g.id%>" id='<%=g.id%>'>
+ <%=g.title%>
+ </a>
+ (<%=time%>)
+ </div>
+ <a href="/graphs/<%=g.id%>" id='<%=g.id%>'>
+ <img id='<%=g.id%>_img' src='<%= g.to_url(:time => time) %>' />
+ </a>
+ <% end %>
+
+</body>
+</html>

0 comments on commit 7db5eb7

Please sign in to comment.