Skip to content

Instantly share code, notes, and snippets.

View lcowell's full-sized avatar

Luke Cowell lcowell

View GitHub Profile
@lcowell
lcowell / bisect_flakey_specs.rb
Created June 5, 2020 21:01
Code from my bisect experiment
module ListBisector
# anything that responds to call: block, lambda, method
# the function being called needs to perform a function against 2 bounded(eg. has length), enumerable objects(eg. has each)
def bisect(a, b, &block)
a_result = block.call(a)
b_result = block.call(b)
if a_result == b_result
puts "done: both sets are interesting or uninteresting, we're done"
[a, b]

I've been looking at some lower level storage stuff for postgres and I was curious about when postgres was writing stuff to disk, specifically when working with updates or transactions.

The ctid represents the physical storage location of the row on disk. Something like (5,8) could be thought of as page 5 + (offset 8 x row width). By looking at this we can know the position of the last written row, which means we'll know if a new row is being written of if the row is being reused.

For example, the last row written to the users table is (5,8).

front_desk_development=# select MAX(ctid) from users;
  max
-------
@lcowell
lcowell / regex_bench.rb
Last active August 29, 2015 14:14
regex_bench.rb
require 'benchmark/ips'
str = "this.is.a.top.level.domain/foo/bar"
by_char = lambda {|s|
s.each_char.inject("") {|acc,c|
if c == "/"
break acc
end
acc << c
package main
func main() {
}
SELECT r.staff_member_id, concat_ws(' ', u1.first_name, u1.last_name) staff_name,
r.patient_id, concat_ws(' ', u2.first_name, u2.last_name) patient_name
from company_10.transaction_report r
join staff_members s on s.id=staff_member_id
join patients p on p.id=patient_id
join users u1 on s.id=u1.staff_member_id
join users u2 on s.id=u2.patient_id
class ActiveRecord::Base
# check that cache_key is already defined here
include Jane::MultiTenantedCacheKeys
end
module MultiTenantedCacheKeys
def cache_key
database_name + super
end
require 'rubygems'
require 'rspec'
require 'date'
require 'time'
module MyHelper
extend self
def convert_to_datetime(input)
if input.is_a?(String)
#PASS #1
# treatment_id that don't exist
treatment_ids = Appointment.includes(:treatment).where("treatments.id IS NULL").map {|a| a.treatment_id }.uniq.reject {|t| t.nil?}
# rebuild with the most common treatment length
treat_staff_duration = treatment_ids.map {|t| Appointment.where(:treatment_id => t).map {|a| [a.treatment_id, a.staff_member_id, a.end_at-a.start_at]}.max {|a| a.last } }
# treatments for the same practitioner that have the same length
matchable_replacements = treat_staff_duration.map {|tuple| [*tuple, StaffMember.find(tuple[1]).treatments.detect {|t| t.duration == tuple.last && t.name != "Break" }] }.reject {|t| t.last.nil? }
@lcowell
lcowell / gist:5268332
Created March 29, 2013 02:27
json_spec backtrace
json_spec(master) $ bundle exec rake spec
/Users/lukec/.rbenv/versions/1.9.3-p392/bin/ruby -S rspec ./spec/component/api_spec.rb ./spec/component/generator_spec.rb ./spec/system/integration_spec.rb
Exception encountered: #<LoadError: cannot load such file -- /Users/lukec/code/front_desk/spec/component/api_spec.rb>
backtrace:
/Users/lukec/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
/Users/lukec/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `block in load'
/Users/lukec/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
/Users/lukec/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:245:in `load'
/Users/lukec/.rbenv/versions/1.9.3-p392/lib/ruby/gems/1.9.1/gems/rspec-core-2.10.1/lib/rspec/core/configuration.rb:746:in `block in load_
@lcowell
lcowell / truth_table.rb
Last active December 11, 2015 22:18
add ops in to expand your truth table
#!/usr/bin/env ruby
ops = [
"l && r",
"l || r",
"l ^ r",
"!l && !r",
"!(l ^ r)"
]
ops.each do |key|