Skip to content

Instantly share code, notes, and snippets.

View marcotc's full-sized avatar

Marco Costa marcotc

View GitHub Profile
@marcotc
marcotc / fetch_rubygems_search_query.rb
Created October 27, 2023 21:13
Download all RubyGems.org search results
require 'faraday'
require 'json'
def fetch_data(search_query)
response = [:just_started_sentinel]
list = []
page = 1
until response.empty? do
sleep 0.11 # 10 request/sec is the remote rate limit
@marcotc
marcotc / profiler.ruby
Created January 26, 2022 00:13
profiler proposal
module Datadog
module Profiling
# Starts the profiler, if the profiler is supported by in
# this runtime environment and if the profiler has been enabled
# in configuration.
#
# @return [Boolean] `true` if the profiler has successfully started, otherwise `false`.
# @public_api
def self.start
return false unless Datadog.send(:components).profiler
@marcotc
marcotc / wrapper.rb
Created November 29, 2019 14:26
Configurable Ruby Modules: The Module Builder Pattern
module Wrapper
InstanceMethods = Module.new
def self.for(klass, accessor_name: nil)
InstanceMethods.module_eval do
define_method :initialize do |object|
raise TypeError, "not a #{klass}" unless object.is_a?(klass)
@object = object
end
@marcotc
marcotc / string_scan_benchmark.rb
Created October 9, 2019 22:28
Fastest way to retrive all numbers from a string
# Code mostly adapted from original by Jesus: https://www.ruby-forum.com/t/extract-numbers-from-string/115434/5
require 'benchmark'
n = 100_000
header = 't=1570633834.463123'
Benchmark.bmbm do |x|
x.report('scan') do
n.times {header.scan(/\d/).join}
@marcotc
marcotc / ruby-pact.yaml
Last active February 8, 2019 07:12
CircleCI orb for Pacts - Ruby
# This code is licensed from CircleCI to the user under the MIT license. See
# https://circleci.com/orbs/registry/licensing for details.
commands:
hello-triggerer:
parameters:
to:
default: ${CIRCLE_USERNAME}
type: string
steps:
- run: echo "Hello << parameters.to >>"
@marcotc
marcotc / ruby-docker-alpine-jemalloc.sh
Created October 19, 2018 20:47
Ruby+Jemalloc still not working with Alpine
linking miniruby
generating encdb.h
./miniruby: [BUG] Segmentation fault at 0x0000000000060ee0
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux-musl]
-- Control frame information -----------------------------------------------
c:0001 p:0000 s:0003 E:000e10 (none) [FINISH]
-- Machine register context ------------------------------------------------
@marcotc
marcotc / query_caller.rb
Last active February 11, 2021 14:02
ActiveRecord debug query caller
::ActiveSupport::Notifications.subscribe('sql.active_record') do |_, _, _, _, payload|
puts " == Query : #{payload[:sql]}"
puts " == Caller: #{caller.reject{|x|x.include?('/gems/')||x.include?('/lib/ruby/')}.join("\n")}"
puts
end
Rails.application.routes.recognize_path('/api/path/to')
@marcotc
marcotc / ar_list_index.rb
Created June 18, 2018 21:35
List Postgres indexes with ActiveRecord
ActiveRecord::Base.connection.exec_query("SELECT t.tablename, indexname, c.reltuples AS num_rows, pg_size_pretty(pg_relation_size(quote_ident(t.tablename)::text)) AS table_size, pg_size_pretty(pg_relation_size(quote_ident(indexrelname)::text)) AS index_size, CASE WHEN indisunique THEN 'Y' ELSE 'N' END AS UNIQUE, idx_scan AS number_of_scans, idx_tup_read AS tuples_read, idx_tup_fetch AS tuples_fetched FROM pg_tables t LEFT OUTER JOIN pg_class c ON t.tablename=c.relname LEFT OUTER JOIN ( SELECT c.relname AS ctablename, ipg.relname AS indexname, x.indnatts AS number_of_columns, idx_scan, idx_tup_read, idx_tup_fetch, indexrelname, indisunique FROM pg_index x JOIN pg_class c ON c.oid = x.indrelid JOIN pg_class ipg ON ipg.oid = x.indexrelid JOIN pg_stat_all_indexes psai ON x.indexrelid = psai.indexrelid ) AS foo ON t.tablename = foo.ctablename WHERE t.schemaname='public' ORDER BY 1,2;")
@marcotc
marcotc / ar_current_queries.rb
Last active February 7, 2018 17:02
Find long running queries (an kill them if you wish)
ActiveRecord::Base.connection.exec_query("SELECT pid, age(query_start, clock_timestamp()), state, query FROM pg_stat_activity WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%' and state = 'active' ORDER BY age(query_start, clock_timestamp())")
#ActiveRecord::Base.connection.exec_query("select pg_cancel_backend(123456)") # Kill by PID