Permalink
Browse files

Adds DataMapper compatibility

  • Loading branch information...
1 parent 2535b2b commit 6c1f0232789fa38c2c2644155a7b12282860e8f2 @snusnu snusnu committed May 22, 2010
View
10 app/controllers/rails_metrics_controller.rb
@@ -40,7 +40,7 @@ def destroy
# DELETE /rails_metrics/destroy_all
def destroy_all
- count = all_scopes(RailsMetrics.store).delete_all
+ count = all_scopes(RailsMetrics.store).send(RailsMetrics::ORM.delete_all)
flash[:notice] = "All #{count} selected metrics were deleted."
redirect_to rails_metrics_path
end
@@ -50,7 +50,11 @@ def destroy_all
def with_pagination(scope)
@limit = (params[:limit].presence || 50).to_i
@offset = (params[:offset].presence || 0).to_i
- scope.limit(@limit).offset(@offset).all
+ if scope.respond_to?(:limit)
+ scope.limit(@limit).offset(@offset).all
+ else
+ scope.all(:limit => @limit, :offset => @offset)
+ end
end
def by_scopes(store)
@@ -73,6 +77,6 @@ def valid_order_by?
end
def find_store(id)
- RailsMetrics.store.find(id)
+ RailsMetrics.store.send(RailsMetrics::ORM.primary_key_finder, id)
end
end
View
15 lib/generators/rails_metrics_generator.rb
@@ -14,14 +14,13 @@ def copy_public_files
end
def invoke_model
- invoke "model", [name].concat(migration_columns),
+ require "rails_metrics/orm/#{Rails::Generators.options[:rails][:orm]}"
+ invoke "model", [name].concat(RailsMetrics::ORM.metric_model_properties),
:timestamps => false, :test_framework => false, :migration => options.migration?
end
def add_model_config
- inject_into_class "app/models/#{file_name}.rb", class_name, <<-CONTENT
- include RailsMetrics::ORM::#{Rails::Generators.options[:rails][:orm].to_s.camelize}
-CONTENT
+ RailsMetrics::ORM.add_metric_model_config(self, file_name, class_name)
end
def add_application_config
@@ -31,10 +30,4 @@ def add_application_config
CONTENT
end
-
- protected
-
- def migration_columns
- %w(name:string duration:integer request_id:integer parent_id:integer payload:text started_at:datetime created_at:datetime)
- end
-end
+end
View
11 lib/rails_metrics.rb
@@ -1,4 +1,6 @@
require 'active_support/core_ext/module/delegation'
+require 'active_support/core_ext/class/attribute'
+
Thread.abort_on_exception = Rails.env.development? || Rails.env.test?
module RailsMetrics
@@ -11,6 +13,13 @@ module RailsMetrics
module ORM
autoload :ActiveRecord, 'rails_metrics/orm/active_record'
+ autoload :DataMapper, 'rails_metrics/orm/data_mapper'
+
+ class << self
+ class_attribute :primary_key_finder
+ class_attribute :delete_all
+ class_attribute :metric_model_properties
+ end
end
# Set which store to use in RailsMetrics.
@@ -109,4 +118,4 @@ def self.valid_for_storing?(args) #:nodoc:
end
end
-require 'rails_metrics/engine'
+require 'rails_metrics/engine'
View
20 lib/rails_metrics/orm/active_record.rb
@@ -20,6 +20,26 @@ module ORM
# include RailsMetrics::ORM::ActiveRecord
# end
#
+
+ ORM.primary_key_finder = :find
+ ORM.delete_all = :delete_all
+
+ ORM.metric_model_properties = %w[
+ name:string
+ duration:integer
+ request_id:integer
+ parent_id:integer
+ payload:text
+ started_at:datetime
+ created_at:datetime
+ ]
+
+ def self.add_metric_model_config(generator, file_name, class_name)
+ generator.inject_into_class "app/models/#{file_name}.rb", class_name, <<-CONTENT
+ include RailsMetrics::ORM::#{Rails::Generators.options[:rails][:orm].to_s.camelize}
+ CONTENT
+ end
+
module ActiveRecord
extend ActiveSupport::Concern
include RailsMetrics::Store
View
88 lib/rails_metrics/orm/data_mapper.rb
@@ -0,0 +1,88 @@
+# Setup to ignore any query which is not a SELECT, INSERT, UPDATE
+# or DELETE and queries made by the own store.
+RailsMetrics.ignore :invalid_queries do |name, payload|
+ name == "data_mapper.sql" &&
+ payload[:sql] !~ /^(SELECT|INSERT|UPDATE|DELETE)/
+end
+
+module RailsMetrics
+ module ORM
+
+ # Include in your model to store metrics. For DataMapper, you need the
+ # following setup:
+ #
+ # script/generate model Metric script/generate name:string duration:integer
+ # request_id:integer parent_id:integer payload:object started_at:datetime created_at:datetime --skip-timestamps
+ #
+ # You can use any model name you wish. Next, you need to include
+ # RailsMetrics::ORM::DataMapper:
+ #
+ # class Metric
+ # include DataMapper::Resource
+ # include RailsMetrics::ORM::DataMapper
+ # end
+ #
+
+ ORM.primary_key_finder = :get
+ ORM.delete_all = :destroy! # use bang version here cause we don't need no hooks
+
+ ORM.metric_model_properties = %w[
+ name:string
+ duration:integer
+ request_id:integer
+ parent_id:integer
+ payload:object
+ started_at:datetime
+ created_at:datetime
+ ]
+
+ def self.add_metric_model_config(generator, file_name, class_name)
+ generator.inject_into_file "app/models/#{file_name}.rb",
+ " include RailsMetrics::ORM::DataMapper\n",
+ {:after => " include DataMapper::Resource\n"}
+ end
+
+ module DataMapper
+ extend ActiveSupport::Concern
+ include RailsMetrics::Store
+
+ included do
+ # Set required validations
+ validates_presence_of :name, :started_at, :duration
+ end
+
+ module ClassMethods
+
+ # Select scopes
+
+ def requests; all(:name => 'rack.request') end
+ def by_name(name); all(:name => name ) end
+ def by_request_id(request_id); all(:request_id => request_id ) end
+
+ # Order scopes
+ # We need to add the id in the earliest and latest scope since the database
+ # does not store miliseconds. The id then comes as second criteria, since
+ # the ones started first are first saved in the database.
+
+ def earliest; all(:order => [:started_at.asc, :id.asc ]) end
+ def latest; all(:order => [:started_at.desc, :id.desc]) end
+ def slowest; all(:order => [:duration.desc ]) end
+ def fastest; all(:order => [:duration.asc ]) end
+
+ end
+
+ # Destroy all children if it's a request metric.
+ def destroy
+ self.class.by_request_id(self.id).destroy! if rack_request?
+ super
+ end
+
+ protected
+
+ def save_metric!
+ save!
+ end
+
+ end
+ end
+end
View
2 lib/rails_metrics/store.rb
@@ -89,7 +89,7 @@ def rack_request?
# However, if it was already saved, we lose microseconds information from
# timestamps and we must rely solely in id and parent_id information.
def parent_of?(node)
- if new_record?
+ if !persisted?
start = (self.started_at - node.started_at) * 1000000
start <= 0 && (start + self.duration >= node.duration)
else
View
1 rails_metrics.gemspec
@@ -38,6 +38,7 @@ Gem::Specification.new do |s|
"lib/rails_metrics/engine.rb",
"lib/rails_metrics/middleware.rb",
"lib/rails_metrics/orm/active_record.rb",
+ "lib/rails_metrics/orm/data_mapper.rb",
"lib/rails_metrics/payload_parser.rb",
"lib/rails_metrics/store.rb",
"lib/rails_metrics/version.rb",

0 comments on commit 6c1f023

Please sign in to comment.