patty 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -30,7 +30,7 @@ module Patty
30
30
  end
31
31
 
32
32
  def decode(data = '')
33
- Yajl::Parser.parse data
33
+ Yajl::Parser.parse data.to_s
34
34
  end
35
35
 
36
36
  def key(signature = nil, marker = 'main')
data/lib/patty/base.rb CHANGED
@@ -6,7 +6,11 @@ module Patty
6
6
  class Base
7
7
 
8
8
  def initialize(storage = nil)
9
- @storage = storage
9
+ @storage = Patty::AggregatedStorage.new Patty::Storages::Riak.new(title)
10
+ end
11
+
12
+ def title
13
+ self.class.to_s.downcase
10
14
  end
11
15
 
12
16
  def flow
@@ -18,7 +22,7 @@ module Patty
18
22
  end
19
23
 
20
24
  def emit(signature = nil, value = nil, marker = nil)
21
- if signature.present?
25
+ if signature.nil?
22
26
  signature = Patty::TimeSignature.from_datetime.align
23
27
  end
24
28
 
@@ -44,17 +48,16 @@ module Patty
44
48
  def fetch(signature = nil, marker = nil)
45
49
  current_value = @storage.get signature, marker
46
50
 
47
- unless current_value.present?
51
+ if current_value.nil?
48
52
  data = signature.children.inject([]) do |acc, child|
49
53
  child_value = fetch(child, marker)
50
- acc << child_value unless child_value.nil?
54
+ acc << child_value
51
55
  acc
52
56
  end
53
57
 
54
- if data.size > 0
55
- current_value = reduce data
56
- @storage.put signature, current_value, marker
57
- end
58
+ current_value = reduce data
59
+
60
+ @storage.put signature, current_value, marker
58
61
  end
59
62
 
60
63
  current_value
@@ -0,0 +1,57 @@
1
+ <html>
2
+ <head>
3
+ <title>Patty Server</title>
4
+ <script type="text/javascript" src="http://yandex.st/jquery/1.7.2/jquery.min.js"></script>
5
+ <script type="text/javascript" src="https://www.google.com/jsapi"></script>
6
+ <script type="text/javascript">
7
+ google.load("visualization", "1", {packages:["corechart"]});
8
+
9
+ $(document).ready(function(){
10
+ $('#report-params').submit(function(){
11
+ var from = $('#report-params input[name=from]').val()
12
+ var to = $('#report-params input[name=to]').val()
13
+ var step = $('#report-params input[name=step]').val()
14
+ var report = $('#report-params input[name=report]').val()
15
+ var marker = $('#report-params input[name=marker]').val()
16
+ var direction = $('#report-params input[name=direction]').val()
17
+
18
+ $.getJSON('json?from='+from+'&to='+to+'&step='+step+'&report='+report+'&marker='+marker, function(data){
19
+
20
+ var data_table = new google.visualization.DataTable();
21
+ data_table.addColumn('string', 'Date and time');
22
+ data_table.addColumn('number', 'Searches');
23
+
24
+ $.each(data, function(i, o){
25
+ console.log(o.k)
26
+ console.log(o.v)
27
+ console.log(o.v[direction])
28
+ data_table.addRows([[o.k, o.v[direction]]]);
29
+ });
30
+
31
+ var options = {
32
+ title: 'Searches ' + direction,
33
+ hAxis: {title: 'Date and time', titleTextStyle: {color: 'red'}}
34
+ };
35
+
36
+ var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
37
+ chart.draw(data_table, options);
38
+ });
39
+
40
+ return false;
41
+ });
42
+ });
43
+ </script>
44
+ <head>
45
+ <body>
46
+ <form action="/" id="report-params">
47
+ <input name="from" type="text" value="2012-03-20 12:00" />
48
+ <input name="to" type="text" value="2012-04-10 12:00" />
49
+ <input name="step" type="text" value="day" />
50
+ <input name="report" type="hidden" value="search" />
51
+ <input name="marker" type="text" value="direct" />
52
+ <input name="direction" type="text" value="MOW-LED" />
53
+ <input value="Draw graph" type="submit" />
54
+ </form>
55
+ <div id="chart_div" style="width: 900px; height: 450px;"></div>
56
+ </body>
57
+ </html>
@@ -0,0 +1,58 @@
1
+ require 'sinatra'
2
+ require 'yajl'
3
+
4
+ module Patty
5
+ class Server < Sinatra::Base
6
+ set :port, 5353
7
+ set :static, true
8
+ set :public_folder, File.dirname(__FILE__) + '/server'
9
+
10
+ TIMELINE_STEPS = {
11
+ :minute => 1.0 / 24 / 6,
12
+ :hour => 1.0 / 24,
13
+ :day => 1.0,
14
+ :month => 25,
15
+ :year => 365
16
+ }
17
+
18
+ TIMELINE_MEASURES = [:hour, :day, :month, :year]
19
+
20
+ def handler
21
+ Object.const_get("#{params[:report].capitalize}Handler").new
22
+ end
23
+
24
+ def timeline
25
+ from = params[:from]
26
+ to = params[:to]
27
+ step = params[:step].to_sym
28
+
29
+ unless TIMELINE_STEPS.keys.include?(step)
30
+ raise ArgumentError, "invalid step #{step} for timeline"
31
+ end
32
+
33
+ from_datetime = Patty::TimeSignature.new(from).to_datetime
34
+ to_datetime = Patty::TimeSignature.new(to).to_datetime
35
+
36
+ from_datetime.step(to_datetime, TIMELINE_STEPS[step]).map do |s|
37
+ current_step = Patty::TimeSignature.from_datetime(s)
38
+
39
+ if step == :minute
40
+ current_step
41
+ else
42
+ current_step.parents[TIMELINE_MEASURES.index(step)]
43
+ end
44
+ end
45
+ end
46
+
47
+ get '/json' do
48
+ content_type 'application/json'
49
+
50
+ response = timeline.map do |signature|
51
+ { :k => signature.to_s, :v => handler.fetch(signature, params[:marker] || 'direct') }
52
+ end
53
+
54
+ Yajl::Encoder.encode response
55
+ end
56
+
57
+ end
58
+ end
@@ -19,7 +19,7 @@ module Patty
19
19
  if @bucket.exists? key
20
20
  @bucket.get(key).data
21
21
  else
22
- ''
22
+ nil
23
23
  end
24
24
  end
25
25
 
@@ -113,5 +113,9 @@ module Patty
113
113
  end
114
114
  end
115
115
 
116
+ def to_datetime
117
+ DateTime.new year, month, day, hour, minute
118
+ end
119
+
116
120
  end
117
121
  end
data/lib/patty/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Patty
2
2
 
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
 
5
5
  end
data/lib/patty.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'patty/version'
2
2
  require 'patty/base'
3
3
  require 'patty/aggregated_storage'
4
+ require 'patty/server'
4
5
 
5
6
  module Patty
6
7
  module Storages
data/patty.gemspec CHANGED
@@ -17,4 +17,5 @@ Gem::Specification.new do |s|
17
17
 
18
18
  s.add_runtime_dependency 'yajl-ruby'
19
19
  s.add_runtime_dependency 'riak-client'
20
+ s.add_runtime_dependency 'sinatra'
20
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: patty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-02 00:00:00.000000000Z
12
+ date: 2012-04-06 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: yajl-ruby
16
- requirement: &70183040 !ruby/object:Gem::Requirement
16
+ requirement: &77495610 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70183040
24
+ version_requirements: *77495610
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: riak-client
27
- requirement: &70182340 !ruby/object:Gem::Requirement
27
+ requirement: &77494450 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,18 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70182340
35
+ version_requirements: *77494450
36
+ - !ruby/object:Gem::Dependency
37
+ name: sinatra
38
+ requirement: &77443870 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *77443870
36
47
  description: Server part of Patty statistics server
37
48
  email: alexander.lomakin@gmail.com
38
49
  executables: []
@@ -46,6 +57,8 @@ files:
46
57
  - lib/patty.rb
47
58
  - lib/patty/aggregated_storage.rb
48
59
  - lib/patty/base.rb
60
+ - lib/patty/server.rb
61
+ - lib/patty/server/index.html
49
62
  - lib/patty/storages/riak.rb
50
63
  - lib/patty/time_signature.rb
51
64
  - lib/patty/version.rb