From a0b9bab130c37831fd6f7aca1e4e2cebc6aec82d Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Fri, 16 Sep 2011 22:54:47 -0700 Subject: [PATCH] first commit --- .gitignore | 1 + Gemfile | 9 ++++++++ public/in.html | 10 +++++++++ watch-yourself.rb | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 public/in.html create mode 100644 watch-yourself.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b844b14 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b04573b --- /dev/null +++ b/Gemfile @@ -0,0 +1,9 @@ +source :rubygems + +gem 'json' +gem 'sinatra' +gem 'redis' +gem 'hiredis' +gem 'sinatra-redis' +gem 'rack' +gem 'thin' diff --git a/public/in.html b/public/in.html new file mode 100644 index 0000000..2ea0c02 --- /dev/null +++ b/public/in.html @@ -0,0 +1,10 @@ + + + + +watch yourself +
+

/

+

+

+
diff --git a/watch-yourself.rb b/watch-yourself.rb new file mode 100644 index 0000000..c895272 --- /dev/null +++ b/watch-yourself.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby + +require 'rubygems' +require 'bundler' +Bundler.require + +def time_now + (Time.now.to_f * 1000).to_i +end + +module WatchYourself + class Server < Sinatra::Base + + set :port, 8008 + + enable :logging + + # serve static files from /public + set :public, File.dirname(__FILE__) + '/public' + + def redis + @redis ||= Redis.new + end + + def key *parts + 'watch-yourself:' + (parts ? parts.join(':') : '') + end + + get '/' do + redirect '/in.html' + end + + post '/in' do + now = time_now + stats = { :time => now, :sys => params['sys'], :dia => params['dia'], :pulse => params['pulse'] } + $stderr.puts "[#{now}] IN: #{stats.inspect}" + redis.zadd key('stats'), now, JSON.stringify(stats) + redirect '/stats.html' + end + + get '/stats' do + content_type 'application/json', :charset => 'utf8' + stats = redis.zrevrange key('stats'), 0, -1, :withscores => true + stats ||= [] + JSON.generate(stats.map { |s| JSON.parse(s) }) + end + + end +end + +if $0 == __FILE__ + WatchYourself::Server.run! +end