Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Forward import event performance to StatHat

  • Loading branch information...
commit d54545577512cd4b2928b3cba9559cd616f65600 1 parent 76083c2
@rwdaigle authored
View
3  Gemfile
@@ -11,13 +11,14 @@ gem 'dynamic_form'
gem 'wordnik'
gem 'queue_classic', '~> 2.0.0'
gem 'clockwork'
-gem 'scrolls'
+gem 'stathat'
gem 'haml'
gem 'jquery-rails'
gem 'unicorn'
gem 'lograge'
+gem 'scrolls'
group :production do
gem 'newrelic_rpm'
View
12 Gemfile.lock
@@ -38,16 +38,20 @@ GEM
dynamic_form (1.1.4)
erubis (2.7.0)
eventmachine (0.12.10)
+ excon (0.13.4)
execjs (1.4.0)
multi_json (~> 1.0)
foreman (0.46.0)
thor (>= 0.13.6)
haml (3.1.6)
- heroku (2.25.0)
+ heroku (2.26.2)
+ heroku-api (~> 0.2.1)
launchy (>= 0.3.2)
- netrc (~> 0.7.1)
+ netrc (~> 0.7.2)
rest-client (~> 1.6.1)
rubyzip
+ heroku-api (0.2.1)
+ excon (~> 0.13.3)
hike (1.2.1)
htmlentities (4.3.1)
i18n (0.6.0)
@@ -69,7 +73,7 @@ GEM
make_resourceful (1.0.1)
mime-types (1.18)
multi_json (1.3.5)
- netrc (0.7.1)
+ netrc (0.7.2)
newrelic_rpm (3.3.4.1)
nokogiri (1.5.2)
permalink_fu (1.0.0)
@@ -132,6 +136,7 @@ GEM
hike (~> 1.2)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
+ stathat (0.0.4)
thin (1.3.1)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
@@ -181,6 +186,7 @@ DEPENDENCIES
rspec-rails
sass-rails
scrolls
+ stathat
thin
uglifier
unicorn
View
2  app/loaders/csv_loader.rb
@@ -5,7 +5,7 @@ class CSVLoader
class << self
def load!
- log({action: "csv-import", url: ENV['VOCABULARY_CSV_URL']}) do
+ log({'action' => "csv-import", 'url' => ENV['VOCABULARY_CSV_URL']}) do
file = "tmp/#{Time.now.utc.to_f}-import.csv"
`curl -q "#{ENV['VOCABULARY_CSV_URL']}" > #{file}`
View
4 app/loaders/inbound_email_loader.rb
@@ -3,8 +3,8 @@ class InboundEmailLoader
class << self
def load!(from, text)
- log_context({user: 'ryan', user_id: 1}) do
- log({action: "email-import", from: from, text: text}) do
+ log_context({'user' => 'ryan', 'user_id' => 1}) do
+ log({'action' => "email-import", 'from' => from, 'text' => text}) do
Subject.add_vocabulary_word(text)
end
end
View
12 app/loaders/wordnik_wod_loader.rb
@@ -5,18 +5,16 @@ class WordnikWODLoader
class << self
def load!(from = Date.today)
- log_context({user: 'ryan', user_id: 1}) do
- today = Date.today
- while(from <= today)
- load_wod(from)
- from = from + 1.day
- end
+ today = Date.today
+ while(from <= today)
+ load_wod(from)
+ from = from + 1.day
end
end
def load_wod(day)
result = question = nil
- log({action: "wod-import", day: day}) do
+ log({'action' => "wod-import", 'day' => day.to_s}) do
result = Wordnik.words.get_word_of_the_day(date: day.strftime("%Y-%m-%d"))
word = result && !result.empty? ? result['word'] : nil
Subject.add_vocabulary_word(word)
View
10 app/models/subject.rb
@@ -19,9 +19,9 @@ def add_vocabulary_word(word)
subject = vocabulary
unit = subject.units.create(question: word)
if(unit && unit.valid?)
- log({action: "add-word" }, unit, { status: "success" })
+ log({'action' => "add-word" }, unit, { 'status' => "success" })
else
- log({action: "add-word" }, unit, { status: "failure" }, { message: unit.errors.full_messages.join(", ") })
+ log({'action' => "add-word" }, unit, { 'status' => "failure" }, { 'message' => unit.errors.full_messages.join(", ") })
end
end
@@ -30,7 +30,7 @@ def seedRandom(seed)
end
def process!(subject_id, overwrite=false)
- log({ subject_id: subject_id, overwrite: overwrite }) do
+ log({ 'subject_id' => subject_id, 'overwrite' => overwrite }) do
subject = self.find(subject_id)
subject.process!(overwrite) if subject
end
@@ -57,7 +57,7 @@ def to_param; permalink; end
def to_s; title; end
def to_log
- { subject_id: self.id, subject_title: self.title }
+ { 'subject_id' => self.id, 'subject_title' => self.title }
end
private
@@ -67,7 +67,7 @@ def resolve_processor_class
begin
@resolved_processor_class ||= "#{unit_processor_type}_processor".classify.constantize
rescue
- # log({ status: error, self, "message=\"Unable to resolve processor class '#{unit_processor_type}' #{$!}\"", "error=#{$!}")
+ # log({ 'status' => error, self, "message=\"Unable to resolve processor class '#{unit_processor_type}' #{$!}\"", "error=#{$!}")
end
end
end
View
4 app/models/unit.rb
@@ -54,7 +54,7 @@ def <=>(other)
end
def process!(overwrite = false)
- log(subject, self, { overwrite: overwrite }) do
+ log(subject, self, { 'overwrite' => overwrite }) do
if answer.blank? || overwrite
subject.with_processor do |processor_klass|
processor_klass.process!(self, unit)
@@ -64,7 +64,7 @@ def process!(overwrite = false)
end
def to_log
- { unit_id: self.id, unit: question }
+ { 'unit_id' => self.id, 'unit' => question }
end
def normalize_question
View
2  app/processors/wordnik_processor.rb
@@ -6,7 +6,7 @@ class << self
def process!(subject, unit)
result = answer = nil
- log(subject, unit, { answer: answer }) do
+ log(subject, unit, { 'answer' => answer }) do
result = Wordnik.word.get_definitions(unit.question, :limit => 1)
answer = result && !result.empty? ? result[0]['text'] : nil
unit.update_attributes(answer: answer)
View
39 app/receivers/log_receiver.rb
@@ -0,0 +1,39 @@
+class LogReceiver
+
+ class << self
+
+ def receive(log_data)
+ if(log_data)
+ event_receivers.each do |receiver|
+ receiver.call(log_data['action'], log_data)
+ end
+ end
+ end
+
+ def event_receivers
+ [
+
+ # EVENT_IMPORT_WOD_WORDNIK_PERFORMANCE
+ lambda { |action, log_data|
+ if(action == 'wod-import')
+ stat_hat_post_performance(StatHatConfig::EVENT_IMPORT_WOD_WORDNIK_PERFORMANCE, log_data)
+ end
+ },
+
+ # EVENT_IMPORT_EMAIL_PERFORMANCE
+ lambda { |action, log_data|
+ if(action == 'email-import')
+ stat_hat_post_performance(StatHatConfig::EVENT_IMPORT_EMAIL_PERFORMANCE, log_data)
+ end
+ }
+ ]
+ end
+
+ def stat_hat_post_performance(event, log_data)
+ if(value = log_data['elapsed'])
+ Scrolls.log({'action' => 'stathat-post-value', 'event' => event, 'value' => value})
+ StatHat::API.ez_post_value(event, ENV['STAT_HAT_KEY'], value)
+ end
+ end
+ end
+end
View
7 config/initializers/stat_hat.rb
@@ -0,0 +1,7 @@
+module StatHatConfig
+
+ # Imports
+ EVENT_IMPORT_WOD_WORDNIK_PERFORMANCE = 'Wordnik WoD Import Performance'
+ EVENT_IMPORT_EMAIL_PERFORMANCE = 'Email Import Performance'
+
+end
View
11 lib/event_logging.rb
@@ -11,7 +11,16 @@ def log_context(*segments, &block)
end
def log(*segments, &block)
- Scrolls.log(log_data_from(*segments), &block)
+ log_data = log_data_from(*segments)
+
+ # Would use Scrolls to execute block but wouldn't have 'elapsed' in log_data for log receivers
+ if block_given?
+ elapsed_ms = Benchmark.ms { yield }
+ log_data.merge!({ 'elapsed' => elapsed_ms.round(2) })
+ end
+
+ Scrolls.log(log_data)
+ $queue.enqueue('LogReceiver.receive', log_data) # Send it off for archival, forwarding, whatever.
end
private
Please sign in to comment.
Something went wrong with that request. Please try again.