Skip to content

Commit

Permalink
Adds DataMapper compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
snusnu committed May 25, 2010
1 parent 2535b2b commit 6c1f023
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 16 deletions.
10 changes: 7 additions & 3 deletions app/controllers/rails_metrics_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def destroy


# DELETE /rails_metrics/destroy_all # DELETE /rails_metrics/destroy_all
def 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." flash[:notice] = "All #{count} selected metrics were deleted."
redirect_to rails_metrics_path redirect_to rails_metrics_path
end end
Expand All @@ -50,7 +50,11 @@ def destroy_all
def with_pagination(scope) def with_pagination(scope)
@limit = (params[:limit].presence || 50).to_i @limit = (params[:limit].presence || 50).to_i
@offset = (params[:offset].presence || 0).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 end


def by_scopes(store) def by_scopes(store)
Expand All @@ -73,6 +77,6 @@ def valid_order_by?
end end


def find_store(id) def find_store(id)
RailsMetrics.store.find(id) RailsMetrics.store.send(RailsMetrics::ORM.primary_key_finder, id)
end end
end end
15 changes: 4 additions & 11 deletions lib/generators/rails_metrics_generator.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -14,14 +14,13 @@ def copy_public_files
end end


def invoke_model 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? :timestamps => false, :test_framework => false, :migration => options.migration?
end end


def add_model_config def add_model_config
inject_into_class "app/models/#{file_name}.rb", class_name, <<-CONTENT RailsMetrics::ORM.add_metric_model_config(self, file_name, class_name)
include RailsMetrics::ORM::#{Rails::Generators.options[:rails][:orm].to_s.camelize}
CONTENT
end end


def add_application_config def add_application_config
Expand All @@ -31,10 +30,4 @@ def add_application_config
CONTENT CONTENT
end end

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
11 changes: 10 additions & 1 deletion lib/rails_metrics.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,6 @@
require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/class/attribute'

Thread.abort_on_exception = Rails.env.development? || Rails.env.test? Thread.abort_on_exception = Rails.env.development? || Rails.env.test?


module RailsMetrics module RailsMetrics
Expand All @@ -11,6 +13,13 @@ module RailsMetrics


module ORM module ORM
autoload :ActiveRecord, 'rails_metrics/orm/active_record' 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 end


# Set which store to use in RailsMetrics. # Set which store to use in RailsMetrics.
Expand Down Expand Up @@ -109,4 +118,4 @@ def self.valid_for_storing?(args) #:nodoc:
end end
end end


require 'rails_metrics/engine' require 'rails_metrics/engine'
20 changes: 20 additions & 0 deletions lib/rails_metrics/orm/active_record.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ module ORM
# include RailsMetrics::ORM::ActiveRecord # include RailsMetrics::ORM::ActiveRecord
# end # 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 module ActiveRecord
extend ActiveSupport::Concern extend ActiveSupport::Concern
include RailsMetrics::Store include RailsMetrics::Store
Expand Down
88 changes: 88 additions & 0 deletions lib/rails_metrics/orm/data_mapper.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/rails_metrics/store.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def rack_request?
# However, if it was already saved, we lose microseconds information from # However, if it was already saved, we lose microseconds information from
# timestamps and we must rely solely in id and parent_id information. # timestamps and we must rely solely in id and parent_id information.
def parent_of?(node) def parent_of?(node)
if new_record? if !persisted?
start = (self.started_at - node.started_at) * 1000000 start = (self.started_at - node.started_at) * 1000000
start <= 0 && (start + self.duration >= node.duration) start <= 0 && (start + self.duration >= node.duration)
else else
Expand Down
1 change: 1 addition & 0 deletions rails_metrics.gemspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Gem::Specification.new do |s|
"lib/rails_metrics/engine.rb", "lib/rails_metrics/engine.rb",
"lib/rails_metrics/middleware.rb", "lib/rails_metrics/middleware.rb",
"lib/rails_metrics/orm/active_record.rb", "lib/rails_metrics/orm/active_record.rb",
"lib/rails_metrics/orm/data_mapper.rb",
"lib/rails_metrics/payload_parser.rb", "lib/rails_metrics/payload_parser.rb",
"lib/rails_metrics/store.rb", "lib/rails_metrics/store.rb",
"lib/rails_metrics/version.rb", "lib/rails_metrics/version.rb",
Expand Down

0 comments on commit 6c1f023

Please sign in to comment.