Skip to content

Instantly share code, notes, and snippets.

@kapcod
kapcod / export_athena_usage.rb
Created Dec 29, 2021
Export AWS Athena usage history as CSV
View export_athena_usage.rb
require 'csv'
require 'aws-sdk-athena'
def export_athena_usage(limit:, region:, filter_scanned: 10**9)
athena = Aws::Athena::Client.new region: region
next_token = nil
fields = %w[query_execution_id query work_group]
stats_fields = %w[data_scanned_in_bytes total_execution_time_in_millis]
scanned = 0
found = 0
@kapcod
kapcod / cleanup_airflow_logs.rb
Created Oct 10, 2021
Airflow DAG log cleaner. Assumes log structure /var/log/airflow/<dag>/<operator>/<date>. Call: `cleanup_airflow_logs.rb 7` to retain only last week of logs
View cleanup_airflow_logs.rb
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'fileutils'
require 'time'
class AirflowLogCleaner
attr_reader :list, :to_delete
def list_folders
View simple_thread_pool.rb
# frozen_string_literal: true
# SimpleThreadPool gives a simple implementation for producer/consumers pattern
# Example:
#
# pool = SimpleThreadPool.new(10, max_queue: 5) do |user_params|
# Net::HTTP.post(UPDATE_API, user_params)
# end
# users.each do |user|
# pool << user.update_api_params
@kapcod
kapcod / action_rate_limit.rb
Last active Apr 9, 2021
Simple action rate limit module for Ruby on Rails, intended to be included in ApplicationController
View action_rate_limit.rb
# frozen_string_literal: true
module ActionRateLimit
LIMIT_BY_OPTIONS = {
user: proc { request.cookies['user_id'] },
ip: proc { request.remote_ip },
}.freeze
def self.included(klass)
klass.extend ClassMethods
@kapcod
kapcod / README.md
Created Aug 25, 2020 — forked from skwashd/README.md
Copy AWS SSM Parameter Store Path
View README.md

This Python (3.6+) script is for migrating Amazon AWS System Manager (SSM) Parameter Store keys from one path to another.

Quick Start

To install the script do the following:

  • Configure your AWS credentials
  • Grab the code from this gist
  • Make it executable (chmod +x /path/to/copy-ssm-ps-path.py)
  • pip install boto3 (if you don't have it installed already)
@kapcod
kapcod / case_sensitive_headers_post.rb
Created Oct 16, 2017
Hack to send request using Net::HTTP with preserved headers case
View case_sensitive_headers_post.rb
class CaseSensitiveHeadersPost < Net::HTTP::Post
def initialize_http_header(headers)
@header = {}
headers.each do |k,v|
@header[k] = [v]
end
end
def capitalize(name)
name
@kapcod
kapcod / deep_freeze.rb
Last active Jul 6, 2017
Ruby Deep Freeze for Hash and Array classes
View deep_freeze.rb
# Adds deep_freeze! method to Hash
Hash.class_eval do
def deep_freeze!
each_value{|val| val.deep_freeze! if val.respond_to?(:deep_freeze!) }
freeze
end
end
# Adds deep_freeze! method to Array
Array.class_eval do
@kapcod
kapcod / fix_cookies_escaping.rb
Created Jan 5, 2017
Fix Rack::Utils.set_cookie_header! and delete_cookie_header! escaping
View fix_cookies_escaping.rb
class << Rack::Utils
alias_method :escape_with_plus, :escape
def escape_without_plus(s)
URI.encode_www_form_component(s).gsub('+', '%20')
end
def mock_escape_for_cookies
singleton_class.send :alias_method, :escape, :escape_without_plus
yield
@kapcod
kapcod / ilya.track_sleep.plist
Created May 24, 2016
Track sleep of the computer with Mac OSX agent config for autoload
View ilya.track_sleep.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ilya.track_sleep</string>
<key>Program</key>
<string>/Users/sa/track_sleep.rb</string>
<key>WorkingDirectory</key>
<string>/Users/sa</string>
@kapcod
kapcod / find-commit.rb
Created May 1, 2016
Finds git commit that merged other commit into master
View find-commit.rb
commit = ARGV[0]
master = ARGV[1] || 'origin/master'
unless commit
puts "Usage: find-commit.rb commit [master-branch]"
puts "Will show commit that merged <commit> into <master-branch>"
exit 1
end
parents = `git rev-list #{commit}..#{master} --reverse --first-parent --merges`.split("\n")