Permalink
Browse files

booking here

  • Loading branch information...
bskaplou committed Apr 7, 2012
1 parent a6493ea commit bfe5d9826115d4f57ac5a5f96a945ab9f736dec9
View
@@ -1,8 +1,9 @@
source "http://rubygems.org"
-gem 'waffle'
+gem 'sinatra'
gem 'waffle'
gem 'tokyotyrant'
+gem 'riak'
gem 'mongo'
gem 'bson_ext'
View
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require 'event_chopper'
if ARGV.length > 2
- puts Object.const_get('EventChopper').const_get(ARGV[0]).new.get EventChopper::TimeKey.new(ARGV[1]), ARGV[2]
+ puts Object.const_get('EventChopper').const_get(ARGV[0]).new.fetch EventChopper::TimeKey.new(ARGV[1]), ARGV[2]
else
- puts Object.const_get('EventChopper').const_get(ARGV[0]).new.get EventChopper::TimeKey.new(ARGV[1])
+ puts Object.const_get('EventChopper').const_get(ARGV[0]).new.fetch EventChopper::TimeKey.new(ARGV[1])
end
View
@@ -20,9 +20,8 @@ Gem::Specification.new do |s|
# specify any dependencies here; for example:
# s.add_development_dependency "rspec"
-# s.add_runtime_dependency "tokyotyrant"
- s.add_runtime_dependency "comm"
- s.add_runtime_dependency "bunny"
+ s.add_runtime_dependency "tokyotyrant"
+ s.add_runtime_dependency "waffle"
s.add_runtime_dependency "yajl-ruby"
s.add_runtime_dependency "mongo"
s.add_runtime_dependency "sinatra"
View
@@ -8,6 +8,7 @@
#require "event_chopper/gate_slowness"
#require "event_chopper/search_click_directions"
require "event_chopper/reports/test"
+require "event_chopper/reports/scb"
require "event_chopper/store/ttstore"
require "event_chopper/store/riak_store"
require "event_chopper/store/eleminating_store"
@@ -27,8 +28,9 @@ def logger
end
def store name
- ReductionStore.new(EliminatingStore.new TTStore.new(name))
- #ReductionStore.new EliminatingStore.new RiakStore.new(name)
+ #ReductionStore.new(EliminatingStore.new TTStore.new(name))
+ #EliminatingStore.new TTStore.new(name)
+ ReductionStore.new EliminatingStore.new RiakStore.new(name)
#EliminatingStore.new RiakStore.new(name)
end
View
@@ -27,6 +27,10 @@ def map topic, record, stamp = nil
emit record, stamp, NOID
end
+ def finalize record
+ record
+ end
+
def get stamp, id = NOID
tor = @store.get stamp, id
if tor.nil?
@@ -43,11 +47,9 @@ def get stamp, id = NOID
tor
end
-# def run
-# Comm::Consumer.new(event_types).subscribe do |topic, message|
-# map topic, message, TimeKey.now
-# end
-# end
+ def fetch stamp, id = NOID
+ finalize get(stamp, id)
+ end
def run
transport = Waffle::Base.new eval("Waffle::Transports::#{Waffle::Config.transport.capitalize}").new
@@ -1,4 +1,3 @@
-require 'comm'
require 'mongo'
module EventChopper
@@ -0,0 +1,46 @@
+module EventChopper
+
+class Scb < Base
+ def event_types
+ ['search', 'click', 'booking']
+ end
+
+ def map topic, record, stamp
+ if topic == 'booking'
+ dt = DateTime.strptime record['booked_at'], '%Y-%m-%dT%H:%M:%SZ'
+ stamp = DateTime.strptime(record['occured_at'], '%Y-%m-%dT%H:%M:%S+04:00').to_time.to_i
+ tk = TimeKey.from_date dt
+ emit({topic => {record['order_number'] => {'state' => record['state'], 'stamp' => stamp}}}, tk)
+ else
+ emit({topic => 1})
+ end
+ end
+
+ def reduce data
+ t = data.inject({}) do |acc, entry|
+ acc.merge(entry) do |k, v1, v2|
+ if k.start_with? 'booking'
+ v1.merge(v2) do |k, b1, b2|
+ b1['stamp'] >= b2['stamp'] ? b1 : b2
+ end
+ else
+ v1 + v2
+ end
+ end
+ end
+ puts "REDUCE" + t.to_s
+ t
+ end
+
+ def finalize record
+ if ! record.nil? and record.has_key? 'booking'
+ record['paid_booking'] = record['booking'].inject(0) do |acc, k|
+ acc + (k[1]['state'] == 'paid' ? 1 : 0)
+ end
+ record['booking'] = record['booking'].length
+ end
+ record
+ end
+end
+
+end
@@ -35,12 +35,12 @@ def period
end
def single
- reporter.get(from)
+ reporter.fetch(from)
end
def range
from.till(to, period).inject({}) do |acc, key|
- acc[key] = reporter.get key
+ acc[key] = reporter.fetch key
acc
end
end
@@ -0,0 +1,80 @@
+<html>
+<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
+<script src="/js/highcharts.js" type="text/javascript"></script>
+<script src="http://datejs.googlecode.com/files/date.js" type="text/javascript"></script>
+<form method='get' onsubmit='onsub(); return false;'>
+Start date: <input type='text' name='start' id='start' value=''><br>
+Stop date: <input type='text' name='stop' id='stop' value=''><br>
+Group by: <select name='group' id='group'>
+<option value='minute'>10 Minutes</option>
+<option value='hour' selected="selected">Hour</option>
+<option value='day'>Day</option>
+<option value='month'>Month</option>
+<option value='year'>Year</option>
+</select><br>
+<input type='submit'/>
+</form>
+<div id="container" style="width: 100%; height: 100%"></div>
+<script>
+$(document).ready(function() {
+ var cur = Date.today().add(1).days();
+ var last = Date.today().add(-1).days();
+ $('#start').val(last.toString('yyyy-MM-dd hh:00'));
+ $('#stop').val(cur.toString('yyyy-MM-dd hh:00'));
+});
+
+function onsub() {
+ draw_graph($('#start').val(), $('#stop').val(), $('#group').val());
+}
+
+var chart;
+var d = [];
+var xticks = [];
+var all_gates = {};
+var dd;
+function draw_graph(from, to, period) {
+$.getJSON('/json?from=' + from + '&to=' + to + '&period=' + period + '&reporter=Scb', function(data) {
+ var stamps = [];
+ var searches = [];
+ var clicks = [];
+ var bookings = [];
+ var pbookings = [];
+ for(var stamp in data) {
+ stamps.push(stamp);
+ clicks.push(data[stamp] && data[stamp]['click'] ? data[stamp]['click'] : null);
+ searches.push(data[stamp] && data[stamp]['search'] ? data[stamp]['search'] : null);
+ bookings.push(data[stamp] && data[stamp]['booking'] ? data[stamp]['booking'] : null);
+ pbookings.push(data[stamp] && data[stamp]['paid_booking'] ? data[stamp]['paid_booking'] : null);
+ }
+ chart1 = new Highcharts.Chart({
+ chart: {
+ renderTo: 'container',
+ type: 'line',
+ spacingRight: 20,
+ zoomType : 'x'
+ },
+ title: {
+ text: 'clicks & searches & bookings'
+ },
+ yAxis: {
+ title: {
+ text: 'Count'
+ }
+ },
+ xAxis: {
+ categories: stamps,
+ labels: {
+ rotation: -90,
+ y: 50
+ }
+ },
+ series: [
+ {name : 'clicks' , data : clicks},
+ {name : 'searches', data : searches},
+ {name : 'bookings', data : bookings},
+ {name : 'paid_bookings', data : pbookings}
+ ]
+ });
+});
+}</script>
+</html>

0 comments on commit bfe5d98

Please sign in to comment.