Skip to content

Commit 1a22278

Browse files
committed
Merge branch 'add-observation-diff' into main
2 parents 4bfb222 + 1dfb951 commit 1a22278

File tree

6 files changed

+62
-8
lines changed

6 files changed

+62
-8
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,15 @@ LabTech.summarize_errors "spiffy-search"
302302
Note that the `summarize_errors` method also takes an optional `:limit` keyword
303303
argument.
304304

305+
### Storing Diffs
306+
307+
If you're working with complex data, you might not want to recompute the diffs
308+
from console. As such, LabTech adds a `.diff` method to the experiment. If
309+
you call this with a block, that block will be passed the control and candidate
310+
for each candidate, and its result will be stored on the `LabTech::Observation`
311+
record. (See the "diff-generating behavior" spec in
312+
`spec/models/lab_tech/experiment_spec.rb` for examples.)
313+
305314
### A Note About Experimental Design
306315

307316
Scientist supports experiments with more than one candidate at a time, and

app/models/lab_tech/experiment.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ def compare_mismatches(limit: nil, width: 100, io: $stdout, &block)
7171
end
7272
end
7373

74+
def diff(&block)
75+
@diff_with = block
76+
end
77+
7478
def disable
7579
update_attribute :percent_enabled, 0
7680
end
@@ -92,7 +96,7 @@ def name=(value) ; write_attribute :name, value ; end
9296

9397
def publish(scientist_result)
9498
return if Rails.env.test? && !LabTech.publish_results_in_test_mode?
95-
LabTech::Result.record_a_science( self, scientist_result )
99+
LabTech::Result.record_a_science( self, scientist_result, diff_with: @diff_with )
96100
end
97101

98102
# I don't encourage the willy-nilly destruction of experimental results...

app/models/lab_tech/result.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class Result < ApplicationRecord
2222

2323
##### CLASS METHODS #####
2424

25-
def self.record_a_science( experiment, scientist_result )
25+
def self.record_a_science( experiment, scientist_result, **kwargs )
2626
self.create!(experiment: experiment) do |result|
27-
result.record_a_science scientist_result
27+
result.record_a_science scientist_result, **kwargs
2828
end
2929
end
3030

@@ -50,7 +50,7 @@ def compare_observations(io: $stdout, &block)
5050
return nil
5151
end
5252

53-
def record_a_science(scientist_result)
53+
def record_a_science(scientist_result, diff_with: nil)
5454
unless scientist_result.kind_of?( Scientist::Result )
5555
raise ArgumentError, "expected a Scientist::Result but got #{scientist_result.class}"
5656
end
@@ -59,7 +59,8 @@ def record_a_science(scientist_result)
5959

6060
record_observation scientist_result.control
6161
scientist_result.candidates.each do |candidate|
62-
record_observation candidate
62+
diff = diff_with&.call(scientist_result.control, candidate)
63+
record_observation candidate, diff: diff
6364
end
6465

6566
record_simple_stats scientist_result
@@ -93,8 +94,9 @@ def increment_experiment_counters
9394
end
9495
end
9596

96-
def record_observation(scientist_observation)
97+
def record_observation(scientist_observation, attrs = {})
9798
self.observations.build do |observation|
99+
observation.assign_attributes attrs if attrs.present?
98100
observation.record_a_science scientist_observation
99101
end
100102
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddObservationDiff < ActiveRecord::Migration[5.1]
2+
def change
3+
# Give us a place to put diffs generated by, e.g., check_please
4+
add_column "lab_tech_observations", "diff", :text
5+
end
6+
end

spec/dummy/db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2019_08_15_192130) do
13+
ActiveRecord::Schema.define(version: 20210205225332) do
1414

1515
create_table "lab_tech_experiments", force: :cascade do |t|
1616
t.string "name"
@@ -31,6 +31,7 @@
3131
t.text "exception_message"
3232
t.text "exception_backtrace"
3333
t.datetime "created_at"
34+
t.text "diff"
3435
t.index ["result_id"], name: "index_lab_tech_observations_by_result_id"
3536
end
3637

spec/models/lab_tech/experiment_spec.rb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ def wtf
4949
end
5050

5151
it "records the results when the experiment is run" do
52-
expect( LabTech::Result ).to receive( :record_a_science ).with( experiment, instance_of(Scientist::Result) )
52+
expect( LabTech::Result ).to \
53+
receive( :record_a_science ) \
54+
.with( experiment, instance_of(Scientist::Result), anything )
5355

5456
LabTech.science "wibble" do |e|
5557
e.use { :wibble }
@@ -90,6 +92,36 @@ def wtf
9092
end
9193
end
9294

95+
describe "diff-generating behavior" do
96+
let(:result) { experiment.results.first }
97+
98+
specify "if a #diff block IS provided, it is used" do
99+
LabTech.science "wibble" do |e|
100+
e.use { :control }
101+
e.try { :candidate }
102+
103+
e.diff { |control, candidate| "this is a diff" }
104+
end
105+
106+
result = experiment.results.first
107+
expect( result ).to be_kind_of( LabTech::Result )
108+
109+
expect( result.control .diff ).to be_blank # because it's the reference against which candidates are diffed
110+
expect( result.candidates.first .diff ).to eq( "this is a diff" )
111+
end
112+
113+
specify "if a #diff block IS NOT provided, nothing untoward occurs" do
114+
LabTech.science "wibble" do |e|
115+
e.use { :control }
116+
e.try { :candidate }
117+
end
118+
119+
result = experiment.results.first
120+
expect( result.control .diff ).to be_blank # because it's the reference against which candidates are diffed
121+
expect( result.candidates.first .diff ).to be_blank # because no block was provided
122+
end
123+
end
124+
93125
describe "result counts" do
94126
specify "when results are equivalent" do
95127
LabTech.science "wibble" do |e|

0 commit comments

Comments
 (0)