Skip to content

Instantly share code, notes, and snippets.

@jstanley0
jstanley0 / pg_ps.rb
Created August 28, 2018 16:24
list and kill Postgres queries from a Rails console
# list pid, execution time, and query text for running queries
def pg_ps
now = Time.now
ActiveRecord::Base.connection.execute("SELECT pid, query, query_start FROM pg_stat_activity WHERE state='active'").to_a.each do |q|
printf "%8d %10.2f %s\n", q['pid'], now - DateTime.parse(q['query_start']), q['query']
end
nil
end
# cancel the current query for the given process
@muness
muness / redshift_date_dim.sql
Last active March 14, 2024 17:38
Redshift Date Dimension
-- Potentially quirky when it comes to week numbers.
BEGIN TRANSACTION;
DROP TABLE IF EXISTS numbers_small;
CREATE TABLE numbers_small (
number SMALLINT NOT NULL
) DISTSTYLE ALL SORTKEY (number
);
INSERT INTO numbers_small VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9);
@sbealer
sbealer / redshift_dim_time.sql
Created November 1, 2016 20:43
Redshift Time Dimension SQL
CREATE VIEW dw.dim_time_vw AS
with nums AS (
SELECT TOP 86400
row_number() over (
PARTITION BY NULL ORDER BY id) AS num
FROM l_browser)
SELECT
to_char(
DATEADD(second, num-1, cast('2000-01-01' AS date)), 'HH24MISS') AS time_key,
# via https://ecommerce.shopify.com/c/api-announcements/t/upcoming-change-in-api-limit-calculations-159710
#
# This automatically slows down requests to Shopify if we exceed their rate limit
# NOTE that we'll always be calling shopify in background threads such as sidekiq,
# so this should not impact users directly.
#
# This has been tested by hand, it seems to do the right thing, but has not been
# truly load tested at scale.
module ActiveResource
class Connection
@kany
kany / sendmail_setup.md
Last active September 22, 2023 00:16
Setup SENDMAIL on Mac OSX Yosemite
@SauloSilva
SauloSilva / each_slice.js
Last active May 8, 2019 16:09
Each slice javascript
// without callback
Array.prototype.eachSlice = function (size){
this.arr = []
for (var i = 0, l = this.length; i < l; i += size){
this.arr.push(this.slice(i, i + size))
}
return this.arr
};
[1, 2, 3, 4, 5, 6].eachSlice(2)
@anamartinez
anamartinez / _localization.html.erb
Created January 22, 2014 21:53
Time zone select rails helper with formatted offset
<%= f.time_zone_select :time_zone, TimeZoneWithFormattedOffset.all.sort_by { |tz| [tz.now.utc_offset, tz.name] } %>
@julionc
julionc / 00.howto_install_phantomjs.md
Last active April 26, 2024 09:13
How to install PhantomJS on Debian/Ubuntu

How to install PhantomJS on Ubuntu

Version: 1.9.8

Platform: x86_64

First, install or update to the latest system software.

sudo apt-get update
sudo apt-get install build-essential chrpath libssl-dev libxft-dev
@sathishmanohar
sathishmanohar / install_passenger_nginx_digital_ocean.sh
Last active June 2, 2020 12:30
Steps to setup and install passenger nginx and rails on digital ocean
# Login as root
ssh root@domain
# Create deploy user
adduser <username> #Adds User with username given. Enter Password when Prompted. Other Details are Optional
# Add user to sudo group
usermod -g <groupname> <username>
# Add .ssh/authorized_keys for deploy user
@gokure
gokure / app.god
Last active November 19, 2019 07:00
Maintenance (with god) scripts of Rails (unicorn, resque/resque-scheduler)
# encoding: utf-8
# god -c config/app.god
module YourApp
module God
def self.generic_monitoring(w, options = {})
# start if process is not running
w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 10.seconds