mirror of
https://github.com/samsonjs/watch-yourself.git
synced 2026-04-27 15:17:38 +00:00
first commit
This commit is contained in:
commit
a0b9bab130
4 changed files with 73 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Gemfile.lock
|
||||||
9
Gemfile
Normal file
9
Gemfile
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
source :rubygems
|
||||||
|
|
||||||
|
gem 'json'
|
||||||
|
gem 'sinatra'
|
||||||
|
gem 'redis'
|
||||||
|
gem 'hiredis'
|
||||||
|
gem 'sinatra-redis'
|
||||||
|
gem 'rack'
|
||||||
|
gem 'thin'
|
||||||
10
public/in.html
Normal file
10
public/in.html
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html lang=en>
|
||||||
|
<meta charset=utf8>
|
||||||
|
<meta name=viewport content='width=device-width, initial-scale=1'>
|
||||||
|
<title>watch yourself</title>
|
||||||
|
<form method=post action=/in>
|
||||||
|
<p><input type=number required=true name=sys value=120> / <input type=number required=true name=dia value=80></p>
|
||||||
|
<p><input type=number required=true name=pulse value=100></p>
|
||||||
|
<p><input type=submit value='save it'></p>
|
||||||
|
</form>
|
||||||
53
watch-yourself.rb
Normal file
53
watch-yourself.rb
Normal file
|
|
@ -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
|
||||||
Loading…
Reference in a new issue