Skip to content

Instantly share code, notes, and snippets.

@jaimerson
jaimerson / .gitconfig
Created August 14, 2014 18:01
Git aliases
[alias]
# lazy commit
friday-night = !git commit -am 'Finish stuff' && git push origin `git symbolic-ref --short -q HEAD`
# delete merged branches
delete-merged = !git branch --merged | grep -v \* | xargs git branch -d
@jaimerson
jaimerson / .vimrc
Created October 8, 2014 00:21
Minimal vimrc
" Set vim-specific functions available, breaking compatibility with vi
set nocompatible
" Line Number
set number
" Syntax highlighting options
syntax on
set t_Co=256
" Search Options
set incsearch
set hlsearch
@jaimerson
jaimerson / a_bikes_report_approach.rb
Last active August 1, 2016 20:33
Bikes report test for tutorial
require 'rails_helper'
shared_examples 'a bikes report approach' do
subject(:approach) { described_class.new }
describe '#to_a' do
let(:cheap_bike) { create(:bike, price: 3.14) }
let(:expensive_bike) { create(:bike, price: 210) }
let!(:cheap_rentals) do
@jaimerson
jaimerson / naive_approach.rb
Last active August 1, 2016 20:33
Naïve approach for bikes tutorial
class BikesReport::NaiveApproach
def to_a
rentals.group_by { |r| r.bike_id }.reduce([]) do |result, (id, rentals)|
rental = rentals.first
result << {
name: rental.bike.name,
model: rental.bike.model,
times_rented: rentals.count,
revenue: rental.bike.price * total_interval(rentals)
}
Benchmark.bm { |x| x.report { BikesReport::NaiveApproach.new.to_a } }
# user system total real
# 9.670000 0.650000 10.320000 ( 10.540757)
@jaimerson
jaimerson / active_record_sql_approach.rb
Created August 1, 2016 20:36
ActiveRecord + Sql approach for bikes problem
class BikesReport::ActiveRecordSqlApproach
def to_a
Rental.joins(:bike)
.select(query)
.group('bikes.id')
.order('times_rented DESC')
.as_json(except: :id)
.map { |r| r.symbolize_keys!.merge!(model: MODELS.fetch(r[:model])) }
end
@jaimerson
jaimerson / benchmark_naive_vs_ar_sql.rb
Created August 1, 2016 20:43
Benchmark of naive vs AR+SQL
Benchmark.bm do |x|
x.report(:naive) { BikesReport::NaiveApproach.new.to_a }
x.report(:ar_and_sql) { BikesReport::ActiveRecordSqlApproach.new.to_a }
end
# user system total real
# 9.600000 0.790000 10.390000 ( 10.639153)
# 0.020000 0.000000 0.020000 ( 0.137610)
@jaimerson
jaimerson / create_bikes_report_view.rb
Created August 1, 2016 21:11
First approach at creating view
class CreateBikesReportView < ActiveRecord::Migration
def change
reversible do |dir|
dir.up do
execute(
<<-QUERY
CREATE MATERIALIZED VIEW bikes_reports AS
SELECT
bikes.name, bikes.model,
count(rentals.id) as times_rented,
@jaimerson
jaimerson / [timestamp]_create_bikes_reports.rb
Created August 1, 2016 21:28
Materialized view with scenic
class CreateBikesReports < ActiveRecord::Migration
def change
create_view :bikes_reports, materialized: true
end
end
@jaimerson
jaimerson / bikes_report.rb
Created August 1, 2016 21:41
Bikes report final form
class BikesReport < ActiveRecord::Base
def self.refresh
Scenic
.database
.refresh_materialized_view(table_name, concurrently: false)
end
def readonly?
true
end