snapstats 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a0ca87d7c50d7d0d47d8d2aa212bc8a3e7b72037
4
- data.tar.gz: fae35dab5d5a2e7dc6f1b6c6775a139532fb12d0
3
+ metadata.gz: eece7c49770b47faa4ddbbcfceac38deac8b3609
4
+ data.tar.gz: 472bdc6c237d80e9013309acbaf0ec4b8ea48f23
5
5
  SHA512:
6
- metadata.gz: f977f60aa20bbcb1879e843e5eaca246f0b154e2d83eecfa962a9672331d6bfbf0164c09bfb0686c5e747fed37d01653b343886f7196cd1843b38ae941069901
7
- data.tar.gz: 1382fa641600ae869c49b4a0fe9855e1a915c290dd8ce3637e58e0cc739c8875661b25835f2cc1ff285c1df0a8396354e95c5c92afd3848647a32cb657678e65
6
+ metadata.gz: bceab64fbc63c1e0ae7c677b6d949605b8b3940a6dd9fbecd55f1ca7025ebe61a636aed6267345e9b02e76c7b04338232ee297ed868879d9e7f6b9b6f4689bec
7
+ data.tar.gz: 780d1c312e68e970ce374165c8456b82cb7528ba85e40b6ad7696b77a4b325d544a1401682ecc5f4b2fac5340fb5453e889af19a9d8d92f9e6d63e21844f45e6
@@ -40,10 +40,10 @@
40
40
  <td><%= @clicks[:cpm] %></td>
41
41
  </tr>
42
42
 
43
- <tr>
43
+ <!-- <tr>
44
44
  <td>Clicks per hour</td>
45
45
  <td><%= @clicks[:cph] %></td>
46
- </tr>
46
+ </tr> -->
47
47
 
48
48
  <tr>
49
49
  <td>Clicks per day</td>
@@ -15,7 +15,7 @@
15
15
  <th>Path</th>
16
16
  </tr>
17
17
 
18
- <% @users.each do |i| %>
18
+ <% @users.sort_by{ |i| i.date }.each do |i| %>
19
19
 
20
20
  <tr>
21
21
  <td> <%= link_to i.email, activity_user_path(i.user_id) %> </td>
@@ -4,11 +4,15 @@ module Snapstats
4
4
 
5
5
  @@is_started = false
6
6
 
7
- def self.start
7
+ def self.start opt={}
8
8
  unless @@is_started
9
-
10
- init_events
11
- subscribe
9
+
10
+ Snapstats.set_redis opt[:redis]
11
+
12
+ unless opt[:disable_logging] == true
13
+ init_events opt
14
+ subscribe
15
+ end
12
16
 
13
17
  @@is_started = true
14
18
  end
@@ -18,10 +22,25 @@ module Snapstats
18
22
  ActiveSupport::Notifications.subscribe('process_action.action_controller', self.new)
19
23
  end
20
24
 
21
- def self.init_events
25
+ def self.init_events opt={}
22
26
 
23
27
  ActionController::Instrumentation.send(:define_method, "process_action") do |arg|
24
28
 
29
+ fetch_user_id = -> params {
30
+ model = params.try(:[], :devise_model).try(:[], :model) || :user
31
+ request.env['warden'].try(model.to_sym).try(:id)
32
+ }
33
+
34
+ fetch_user_email = -> params {
35
+ model = params.try(:[], :devise_model).try(:[], :model) || :user
36
+
37
+ if params[:devise_model].present? && params[:devise_model].try(:[], :login_fields).present?
38
+ return params[:devise_model][:login_fields].map{ |field| request.env['warden'].try(model.to_sym).try(field.to_sym) }.compact.first
39
+ else
40
+ return request.env['warden'].try(model.to_sym).try(:email)
41
+ end
42
+ }
43
+
25
44
  raw_payload = {
26
45
  :controller => self.class.name,
27
46
  :action => self.action_name,
@@ -33,8 +52,8 @@ module Snapstats
33
52
  :ip => request.remote_ip,
34
53
  :stash => request.session['flash'] && request.session['flash'][:log],
35
54
 
36
- :user_id => request.env['warden'].try(:user).try(:id),
37
- :user_email => request.env['warden'].try(:user).try(:email),
55
+ :user_id => fetch_user_id.call(opt),
56
+ :user_email => fetch_user_email.call(opt),
38
57
  :user_agent => request.user_agent
39
58
  }
40
59
 
@@ -69,7 +69,7 @@ module Snapstats
69
69
  email: @payload[:user_email]
70
70
  }.to_json
71
71
 
72
- @redis.hset mkey("activity:users"), @payload[:user_id], value
72
+ @redis.hset mday("activity:users"), @payload[:user_id], value
73
73
  end
74
74
 
75
75
  # Performance
@@ -109,10 +109,10 @@ module Snapstats
109
109
  cpm = data[Time.now.beginning_of_minute.to_i.to_s] || 0
110
110
  cpd = data.values.reduce(0){ |sum, i| sum + i.to_i }
111
111
 
112
- cph = data.group_by{ |k, v| Time.at(k.to_i).beginning_of_hour }.reduce({}){|sum, (k, v)| sum[k] = v.map{|i| i.last.to_i}.reduce(:+); sum }
113
- cph = cph.values.reduce(:+) / cph.keys.count
112
+ # cph = data.group_by{ |k, v| Time.at(k.to_i).beginning_of_hour }.reduce({}){|sum, (k, v)| sum[k] = v.map{|i| i.last.to_i}.reduce(:+); sum }
113
+ # cph = cph.values.reduce(:+) / cph.keys.count
114
114
 
115
- { cpm: cpm, cph: cph, cpd: cpd }
115
+ { cpm: cpm, cph: 0, cpd: cpd }
116
116
  end
117
117
 
118
118
  def self.fetch_all_chart
@@ -134,7 +134,7 @@ module Snapstats
134
134
  attribute :version, String
135
135
 
136
136
  def self.fetch_all
137
- Snapstats.redis.hgetall(Snapstats.mkey("activity:users")).map do |uid, values|
137
+ Snapstats.redis.hgetall(Snapstats.mday("activity:users")).map do |uid, values|
138
138
  values = JSON.parse(values, :symbolize_names => true)
139
139
  self.new(email: values[:email], date: Time.at(values[:ts].to_i), path: values[:path], user_id: uid)
140
140
  end
@@ -154,7 +154,7 @@ module Snapstats
154
154
  end
155
155
 
156
156
  def self.fetch_email_by_uid user_id
157
- data = Snapstats.redis.hgetall(Snapstats.mkey("activity:users"))[user_id]
157
+ data = Snapstats.redis.hgetall(Snapstats.mday("activity:users"))[user_id]
158
158
  JSON.parse(data, :symbolize_names => true)[:email] if data
159
159
  end
160
160
 
data/lib/ext/redis.rb CHANGED
@@ -1,7 +1,11 @@
1
1
  module Snapstats
2
2
 
3
3
  def self.redis
4
- @@redis ||= Redis.new(:host => 'localhost', :port => 6379)
4
+ @@redis # ||= Redis.new(:host => 'localhost', :port => 6379)
5
+ end
6
+
7
+ def self.set_redis opt={}
8
+ @@redis = Redis.new( opt.present? ? opt : { :host => 'localhost', :port => 6379 } )
5
9
  end
6
10
 
7
11
  def self.mkey name
@@ -1,3 +1,3 @@
1
1
  module Snapstats
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/snapstats.rb CHANGED
@@ -4,5 +4,5 @@ require "event_logger/event_logger"
4
4
  require "event_reader/event_reader"
5
5
 
6
6
  module Snapstats
7
- EventLogger.start
7
+
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snapstats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - s3k
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-10 00:00:00.000000000 Z
11
+ date: 2015-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails