Skip to content

Instantly share code, notes, and snippets.

@rbrooks
rbrooks / get-code-owner.sh
Last active October 5, 2022 16:47
Get Code Owner from CODEOWNERS
# Given a path on CODEOWNERS file, returns the username of code owner.
# CODEOWNERS file is either space or Tab-delimated. Hence the 2nd `cut` that slices on Tab.
# That's an actual Tab char between the ' '. It wouldn't accept '\t'.
# Here it is working on a Tab-deleimited line:
grep app/interactors/account/assign_reports_to/ .github/CODEOWNERS | cut -f2- -d ' ' | cut -f2- -d ' '
# @templeman15
@rbrooks
rbrooks / uuid-valid.rb
Created August 12, 2022 20:03
UUID is valid?
class Uuid
class << self
def valid?(uuid: uuid)
uuid =~ /^[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}$/i
end
end
# Sometimes you need to reindex all Rails Models. The reindex() method
# is specific to the SearchKick gem for ElasticSearch, and it only exists
# on ElasticSearch-indexed tables. We do a check for its existence below,
# or it would throw an error.
Dir[Rails.root.join('app/models/*.rb').to_s].each do |filename|
klass = File.basename(filename, '.rb').camelize.constantize
next unless klass.ancestors.include?(ActiveRecord::Base)
next if klass.abstract_class?
@rbrooks
rbrooks / pg-index-usage.sql
Created December 8, 2020 01:32
Postgres Index Usage Metrics
-- Index Size and Usage
SELECT
t.schemaname,
t.tablename,
indexname,
c.reltuples AS num_rows,
pg_size_pretty(pg_relation_size(quote_ident(t.schemaname)::text || '.' || quote_ident(t.tablename)::text)) AS table_size,
pg_size_pretty(pg_relation_size(quote_ident(t.schemaname)::text || '.' || quote_ident(indexrelname)::text)) AS index_size,
CASE WHEN indisunique THEN 'Y'
ELSE 'N'
@rbrooks
rbrooks / Postgres-Table-and-Index-Size-Metrics.sql
Created December 8, 2020 00:40
Postgres Table and Index Size Metrics
WITH RECURSIVE pg_inherit(inhrelid, inhparent) AS
(select inhrelid, inhparent
FROM pg_inherits
UNION
SELECT child.inhrelid, parent.inhparent
FROM pg_inherit child, pg_inherits parent
WHERE child.inhparent = parent.inhrelid),
pg_inherit_short AS (SELECT * FROM pg_inherit WHERE inhparent NOT IN (SELECT inhrelid FROM pg_inherit))
SELECT table_schema
, TABLE_NAME
@rbrooks
rbrooks / custom_log_level.py
Last active January 17, 2017 22:48
Custom Log Level in Python
logging.addLevelName(51, 'AUTH')
log = logging.getLogger()
# log.setLevel(10)
print log.level
print log.getEffectiveLevel()
log.log(10, '********** 10 ************')
log.log(20, '********** 20 ************')
log.log(30, '********** 30 ************')
@rbrooks
rbrooks / create-pem-keypair.sh
Created October 27, 2016 14:45
Create Public/Private keypair in PEM format for JWTs
# ssh-keygen creates Keys that aren't in PEM format, but JWT tokens mandate
# PEM format. So, we have to convert them with OpenSSL.
ssh-keygen -t rsa -b 4096 -f jwtRS256.key
# Press Enter when prompted for passphrase.
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub
cat jwtRS256.key
cat jwtRS256.key.pub
@rbrooks
rbrooks / jwt.js
Last active September 9, 2016 14:32
// First:
// npm install jsonwebtoken
// https://github.com/auth0/node-jsonwebtoken
//
// Then launch `node` shell.
var jwtLib = require('jsonwebtoken');
var jwtStr = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb250ZXh0Ijoic3N2cHpmYWZoZCIsInN1YiI6InVybjpzd2l0Y2hjb25uZXg6dXNlcjo0MCIsImlzcyI6InVybjpzd2l0Y2hjb25uZXg6c3ZjOmF1dGgiLCJleHAiOjE0NzMzNjMxNTMsImlhdCI6MTQ3MzM2Mjg1MywiYXVkIjoidXJuOnN3aXRjaGNvbm5leDpzdmM6bW9iaWxlIn0.tZj08Vlci3g7SYUm-8MxfH_Ty_1rtBGgUzC311-NSaU';
@rbrooks
rbrooks / merge_dicts.py
Created August 19, 2016 17:49
Merge Dictionaries
def merge_dicts(x, y):
z = x.copy()
z.update(y)
return z
@rbrooks
rbrooks / whitelist_dict.py
Created August 19, 2016 17:48
Whitelist Dictionary
def whitelist_dict(dic, keys):
# Filters a Dict by only including certain keys.
key_set = set(keys) & set(dic.keys())
return { key: dic[key] for key in key_set }