prisma 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Christof Dorner
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ # prisma [![Continuous Integration](https://secure.travis-ci.org/chdorner/prisma.png?branch=master)](https://secure.travis-ci.org/chdorner/prisma)
2
+
3
+ Prisma is a simple request stats collector for Rails applications with configurable groups.
4
+
5
+ ## Contributing to prisma
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ ## Copyright
16
+
17
+ Copyright (c) 2012 Christof Dorner. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ begin
5
+ require 'vegas'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'vegas'
9
+ end
10
+ require 'prisma/server'
11
+
12
+
13
+ Vegas::Runner.new(Prisma::Server, 'prisma-web') do |runner, opts, app|
14
+ opts.on('-N NAMESPACE', "--namespace NAMESPACE", "set the Redis namespace") {|namespace|
15
+ runner.logger.info "Using Redis namespace '#{namespace}'"
16
+ Prisma.redis_namespace = namespace
17
+ }
18
+ opts.on('-r redis-connection', "--redis redis-connection", "set the Redis connection string") {|redis_connection|
19
+ runner.logger.info "Using Redis connection '#{redis_connection}'"
20
+ Prisma.redis = redis_connection
21
+ }
22
+ end
@@ -0,0 +1,108 @@
1
+ require 'rails/all'
2
+ require 'redis-namespace'
3
+
4
+ require 'prisma/group'
5
+ require 'prisma/railtie'
6
+ require 'prisma/filter'
7
+
8
+ # Used for configuration, typically in a Rails initializer.
9
+ module Prisma
10
+ mattr_reader :groups
11
+ # @!visibility private
12
+ @@groups = {}
13
+
14
+ # @!visibility public
15
+ # Set your own +Redis+ instance, it will be wrapped into a +Redis::Namespace+ object with the configured namespace.
16
+ # Useful for when Redis is not available on the standard IP and port.
17
+ # Allows:
18
+ # - hostname:port
19
+ # - hostname:port:db
20
+ # - redis://hostname:port/db
21
+ # - +Redis+ instance
22
+ mattr_accessor :redis
23
+ # @!visibility private
24
+ @@redis = ::Redis.new
25
+
26
+ # @!visibility public
27
+ # String for redis namespace, defaults to +prisma+
28
+ mattr_accessor :redis_namespace
29
+ # @!visibility private
30
+ @@redis_namespace = 'prisma'
31
+
32
+ # @!visibility public
33
+ # Duration in seconds for expiring redis keys (easy to use with Rails duration helpers +1.day+)
34
+ mattr_accessor :redis_expiration_duration
35
+
36
+ # Configure prisma. Example usage:
37
+ # Prisma.setup do |config|
38
+ # config.group :active_api_clients { |controller| controller.current_client.id }
39
+ # config.redis = Redis.new(:db => 1)
40
+ # config.redis_namespace = 'stats'
41
+ # config.redis_expiration_duration = 2.days
42
+ # end
43
+ def self.setup
44
+ yield self
45
+ store_configuration
46
+ end
47
+
48
+ # Configures a group. The instance of the current {http://api.rubyonrails.org/classes/ActionController/Base.html ActionController} is being passed as an argument into the block.
49
+ # As an example, tracking daily active users could be as simple as:
50
+ # Prisma.setup |config|
51
+ # config.group :logged_in { |controller| controller.current_user.id }
52
+ # end
53
+ #
54
+ # @param [Symbol/String] name for identifying the group, it is used as part of the redis key.
55
+ # @param [String] description for describing the gorup, it is used in the admin interface.
56
+ # @param [Block] block returning a String (or a meaningful +.to_s output+) which is used for identifying a counter inside a group, Prisma doesn't count a request if block returns +nil+ or +false+
57
+ def self.group(name, description=nil, &block)
58
+ @@groups[name] = Group.new(:name => name, :description => description, :block => block)
59
+ end
60
+
61
+ # Returns a default or configured +Redis+ instance wrapped in a +Redis::Namespace+
62
+ # @return [Redis::Namespace]
63
+ def self.redis
64
+ @@namespaced_redis ||= lambda do
65
+ case @@redis
66
+ when String
67
+ if @@redis =~ /redis\:\/\//
68
+ redis = Redis.connect(:url => @@redis, :thread_safe => true)
69
+ else
70
+ host, port, db = @@redis.split(':')
71
+ redis = Redis.new(:host => host, :port => port, :thread_safe => true, :db => db)
72
+ end
73
+ Redis::Namespace.new(redis_namespace, :redis => redis)
74
+ else
75
+ Redis::Namespace.new(redis_namespace, :redis => @@redis)
76
+ end
77
+ end.call
78
+ end
79
+
80
+ # @!visibility private
81
+ # Returns redis key for a group name and optional date
82
+ # @param [Symbol/String] group_name for which group the redis key is for
83
+ # @param [Date] date for which date the redis key is for, if not given it uses today's date
84
+ # @return [String] the redis key
85
+ def self.redis_key(group_name, date=nil)
86
+ date = Time.now.utc.to_date unless date
87
+ "#{group_name}:#{date.strftime('%Y:%m:%d')}"
88
+ end
89
+
90
+ # @!visibility private
91
+ # Returns duration of from beginning of day to now + given or configured duration
92
+ # @param [Numeric] duration in seconds (defaults to configured +redis_expiration_duration+)
93
+ # @return [Numeric] duration
94
+ def self.redis_expire(duration=nil)
95
+ duration = redis_expiration_duration unless duration
96
+ (Time.now.utc.beginning_of_day + duration).to_i - Time.now.utc.to_i
97
+ end
98
+
99
+ # @!visibility private
100
+ # Stores the configured groups inside of redis
101
+ def self.store_configuration
102
+ redis.del 'configuration'
103
+ groups.values.each do |group|
104
+ redis.hset 'configuration', group.name, group.description
105
+ end
106
+ end
107
+ end
108
+
@@ -0,0 +1,23 @@
1
+ module Prisma
2
+ # Gets included into controllers and runs after_filter method
3
+ module Filter
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ after_filter :prisma_disperse_request
8
+ end
9
+
10
+ protected
11
+
12
+ def prisma_disperse_request
13
+ Prisma.groups.each do |name, group|
14
+ redis_key = Prisma.redis_key(name)
15
+ value = group.block.call(self)
16
+ Prisma.redis.hincrby redis_key, value, 1 if value
17
+
18
+ Prisma.redis.expire redis_key, Prisma.redis_expire if Prisma.redis_expiration_duration
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,66 @@
1
+ module Prisma
2
+ # Represents a configured group, has convenience methods for getting data.
3
+ class Group
4
+ # The name of the group, typically a +Symbol+
5
+ attr_accessor :name
6
+
7
+ # The description of the group, typcially a +String+
8
+ attr_accessor :description
9
+
10
+ # The block which evaluates to a +String+ or meaningful +Object.to_s+
11
+ attr_accessor :block
12
+
13
+ # Initialize +Group+ from a hash
14
+ def initialize(options={})
15
+ self.name = options[:name]
16
+ self.description = options[:description]
17
+ self.block = options[:block]
18
+ end
19
+
20
+ # Get a +Hash+ with the +Date+ as key and amount of items as the value. Grouped by day.
21
+ # group.range(5.days.ago.to_date..Date.today)
22
+ # group.daily(5.days.ago.to_date..Date.today)
23
+ # @param [Range] range of days
24
+ # @return [Hash]
25
+ def range(range)
26
+ range = range..range if range.is_a? Date
27
+ data = {}
28
+ range.each do |date|
29
+ data[date] = Prisma.redis.hlen Prisma.redis_key(name, date)
30
+ end
31
+ data
32
+ end
33
+ alias_method :daily, :range
34
+
35
+ # Get a +Hash+ with the +Date+ as key and amount of items as the value. Grouped by week, key represents a +Date+ object of the first day of the week.
36
+ # group.weekly(1.week.ago.to_date..Date.today)
37
+ # @param [Range] range of days
38
+ # @return [Hash]
39
+ def weekly(range)
40
+ data = range(range)
41
+
42
+ data = data.group_by { |date, value| date.beginning_of_week }
43
+ sum_up_grouped_data(data)
44
+ end
45
+
46
+ # Get a +Hash+ with the +Date+ as key and amount of items as the value. Grouped by month, key represents a +Date+ object of the first day of the month.
47
+ # group.monthly(1.month.ago.to_date..Date.today)
48
+ # @param [Range] range of days
49
+ # @return [Hash]
50
+ def monthly(range)
51
+ data = range(range)
52
+
53
+ data = data.group_by { |date, value| date.beginning_of_month }
54
+ sum_up_grouped_data(data)
55
+ end
56
+
57
+ private
58
+
59
+ def sum_up_grouped_data(data)
60
+ data.each do |date, values|
61
+ data[date] = values.map { |value| value.second }.inject{ |sum, count| sum + count }
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,10 @@
1
+ module Prisma
2
+ class Railtie < Rails::Railtie
3
+ initializer 'prisma.insert_into_action_controller' do
4
+ ActiveSupport.on_load :action_controller do
5
+ ActionController::Base.send(:include, ::Prisma::Filter)
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,54 @@
1
+ require 'sinatra/base'
2
+ require 'erb'
3
+ require 'prisma'
4
+ require 'active_support/core_ext'
5
+
6
+ module Prisma
7
+ # Sinatra application for viewing request stats
8
+ class Server < Sinatra::Base
9
+ dir = File.join(File.dirname(File.expand_path(__FILE__)), 'server')
10
+
11
+ set :views, File.join(dir, 'views')
12
+ set :public_folder, File.join(dir, 'public')
13
+
14
+ get '/' do
15
+ redirect to('/daily')
16
+ end
17
+
18
+ get '/daily' do
19
+ @date_format = '%m-%d'
20
+ @groups = groups.map do |group|
21
+ values = group.daily (Date.today-1.month)..Date.today
22
+ [group, values]
23
+ end
24
+ erb :index
25
+ end
26
+
27
+ get '/weekly' do
28
+ @date_format = '%W'
29
+ @groups = groups.map do |group|
30
+ values = group.weekly (Date.today-3.months)..Date.today
31
+ [group, values]
32
+ end
33
+ erb :index
34
+ end
35
+
36
+ get '/monthly' do
37
+ @date_format = '%Y-%m'
38
+ @groups = groups.map do |group|
39
+ values = group.monthly (Date.today-1.year)..Date.today
40
+ [group, values]
41
+ end
42
+ erb :index
43
+ end
44
+
45
+ private
46
+
47
+ def groups
48
+ Prisma.redis.hgetall('configuration').map do |name, description|
49
+ Prisma::Group.new(:name => name, :description => description)
50
+ end
51
+ end
52
+ end
53
+ end
54
+
metadata ADDED
@@ -0,0 +1,331 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prisma
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Christof Dorner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: redis-namespace
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.0.2
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.0.2
46
+ - !ruby/object:Gem::Dependency
47
+ name: sinatra
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.0.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.0.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: vegas
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 0.1.2
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 0.1.2
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec-rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 2.10.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 2.10.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: yard
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: '0.7'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: '0.7'
110
+ - !ruby/object:Gem::Dependency
111
+ name: kramdown
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: bundler
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: jeweler
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 1.8.3
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 1.8.3
158
+ - !ruby/object:Gem::Dependency
159
+ name: guard
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ - !ruby/object:Gem::Dependency
175
+ name: guard-rspec
176
+ requirement: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ type: :development
183
+ prerelease: false
184
+ version_requirements: !ruby/object:Gem::Requirement
185
+ none: false
186
+ requirements:
187
+ - - ! '>='
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ - !ruby/object:Gem::Dependency
191
+ name: guard-livereload
192
+ requirement: !ruby/object:Gem::Requirement
193
+ none: false
194
+ requirements:
195
+ - - ! '>='
196
+ - !ruby/object:Gem::Version
197
+ version: '0'
198
+ type: :development
199
+ prerelease: false
200
+ version_requirements: !ruby/object:Gem::Requirement
201
+ none: false
202
+ requirements:
203
+ - - ! '>='
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ - !ruby/object:Gem::Dependency
207
+ name: rb-fsevent
208
+ requirement: !ruby/object:Gem::Requirement
209
+ none: false
210
+ requirements:
211
+ - - ! '>='
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ type: :development
215
+ prerelease: false
216
+ version_requirements: !ruby/object:Gem::Requirement
217
+ none: false
218
+ requirements:
219
+ - - ! '>='
220
+ - !ruby/object:Gem::Version
221
+ version: '0'
222
+ - !ruby/object:Gem::Dependency
223
+ name: growl
224
+ requirement: !ruby/object:Gem::Requirement
225
+ none: false
226
+ requirements:
227
+ - - ! '>='
228
+ - !ruby/object:Gem::Version
229
+ version: '0'
230
+ type: :development
231
+ prerelease: false
232
+ version_requirements: !ruby/object:Gem::Requirement
233
+ none: false
234
+ requirements:
235
+ - - ! '>='
236
+ - !ruby/object:Gem::Version
237
+ version: '0'
238
+ - !ruby/object:Gem::Dependency
239
+ name: simplecov
240
+ requirement: !ruby/object:Gem::Requirement
241
+ none: false
242
+ requirements:
243
+ - - ! '>='
244
+ - !ruby/object:Gem::Version
245
+ version: '0'
246
+ type: :development
247
+ prerelease: false
248
+ version_requirements: !ruby/object:Gem::Requirement
249
+ none: false
250
+ requirements:
251
+ - - ! '>='
252
+ - !ruby/object:Gem::Version
253
+ version: '0'
254
+ - !ruby/object:Gem::Dependency
255
+ name: timecop
256
+ requirement: !ruby/object:Gem::Requirement
257
+ none: false
258
+ requirements:
259
+ - - ! '>='
260
+ - !ruby/object:Gem::Version
261
+ version: '0'
262
+ type: :development
263
+ prerelease: false
264
+ version_requirements: !ruby/object:Gem::Requirement
265
+ none: false
266
+ requirements:
267
+ - - ! '>='
268
+ - !ruby/object:Gem::Version
269
+ version: '0'
270
+ - !ruby/object:Gem::Dependency
271
+ name: shotgun
272
+ requirement: !ruby/object:Gem::Requirement
273
+ none: false
274
+ requirements:
275
+ - - ! '>='
276
+ - !ruby/object:Gem::Version
277
+ version: '0'
278
+ type: :development
279
+ prerelease: false
280
+ version_requirements: !ruby/object:Gem::Requirement
281
+ none: false
282
+ requirements:
283
+ - - ! '>='
284
+ - !ruby/object:Gem::Version
285
+ version: '0'
286
+ description:
287
+ email: christof@chdorner.me
288
+ executables:
289
+ - prisma-web
290
+ extensions: []
291
+ extra_rdoc_files:
292
+ - LICENSE.txt
293
+ - README.md
294
+ files:
295
+ - bin/prisma-web
296
+ - lib/prisma.rb
297
+ - lib/prisma/filter.rb
298
+ - lib/prisma/group.rb
299
+ - lib/prisma/railtie.rb
300
+ - lib/prisma/server.rb
301
+ - LICENSE.txt
302
+ - README.md
303
+ homepage: https://github.com/chdorner/prisma
304
+ licenses:
305
+ - MIT
306
+ post_install_message:
307
+ rdoc_options: []
308
+ require_paths:
309
+ - lib
310
+ required_ruby_version: !ruby/object:Gem::Requirement
311
+ none: false
312
+ requirements:
313
+ - - ! '>='
314
+ - !ruby/object:Gem::Version
315
+ version: '0'
316
+ segments:
317
+ - 0
318
+ hash: -2724509723049501504
319
+ required_rubygems_version: !ruby/object:Gem::Requirement
320
+ none: false
321
+ requirements:
322
+ - - ! '>='
323
+ - !ruby/object:Gem::Version
324
+ version: '0'
325
+ requirements: []
326
+ rubyforge_project:
327
+ rubygems_version: 1.8.24
328
+ signing_key:
329
+ specification_version: 3
330
+ summary: Simple request stats collector for Rails applications
331
+ test_files: []