Skip to content

Instantly share code, notes, and snippets.

View ddrscott's full-sized avatar

Scott Pierce ddrscott

View GitHub Profile
@ddrscott
ddrscott / histogram.sql
Last active December 9, 2022 18:03
Histogram for PostgreSQL without width_bucket.
WITH params AS (
-- Parameters for down stream queries
SELECT
15 AS bucket_count,
80 AS max_bars
),
numbers AS (
-- Change this query to select real data.
-- For now we make random set of numbers.
SELECT
@ddrscott
ddrscott / tk_table.rb
Last active October 31, 2017 14:35
Demo of basic Tk::Tile::Treeview
require 'tk'
# set up fake data
data = ('a'..'z').map do |i|
('a'..'z').map {|j| i + j}
end
headings = data.shift
root = TkRoot.new { title 'TREE' }
root.minsize = [400, 300]
require 'tempfile'
Pry::Commands.create_command 'golumn', keep_retval: true, use_shellwords: false do |text|
description 'Send SQL or scope to `psql` and then to `golumn`: golumn Model.all'
banner <<-BANNER
Usage: golumn [SQL or Ruby which evaluates to String or a Rails Model]
Saves SQL to Dir.tmpdir/pry-psql.sql with adds basic formatting.
Then sends statemnet to `psql` or Rails determined system command.
@ddrscott
ddrscott / ssh_reverse_tunnel.sh
Last active November 10, 2017 19:32
Example SSH reverse proxy
# 1) Listen for data on 65000
nc -vv -kl 65000
# 2) In a new [session|tab|window|split], connect to `example.server` forwarding 65001 back to clients 65000
ssh -R 65001:localhost:65000 example.server
# 3) Within 2's `ssh` session, send data to the client
ls -al | nc localhost 65001
@ddrscott
ddrscott / spooled_temporary_file.py
Created November 12, 2017 11:25
Example checking for std in data.
import logging
import sys
from tempfile import SpooledTemporaryFile
try:
with SpooledTemporaryFile() as temp:
temp.write(sys.stdin.read())
logging.debug("tell: %i" % temp.tell())
if temp.tell() > 0:
temp.seek(0)
# do something with the data
@ddrscott
ddrscott / griddy.sh
Last active November 13, 2017 20:42
tee /usr/local/bin/griddy <<-EOF
#!/bin/bash
cat /dev/stdin > /tmp/griddy.csv
open -a Griddy.app /tmp/griddy.csv
EOF
chmod +x /usr/local/bin/griddy
#!/bin/sh
# open results in golumn
psql -Ac 'select * from pg_tables' | golumn --title pg_tables
@ddrscott
ddrscott / pop.py
Created November 28, 2017 21:37
Script to open website in dialog always on top of other windows using wxPython
#!/usr/bin/env python2
"""
Requirements:
brew install wxMac
brew install pip
pip install wxPython
Usage:
pop.py https://www.gdax.com/trade/BTC-USD
"""
@ddrscott
ddrscott / stream_zip_controller.rb
Created November 29, 2017 21:50
Streaming zip file contents in Rails 4
class StreamZipController < ApplicationController
include ActionController::Live
# list of interesting file paths to include in the zip.
def paths
[]
end
def send_message
response.headers['Content-Type'] = 'text/event-stream'
CREATE TABLE __test AS
SELECT *,
CURRENT_TIMESTAMP
FROM generate_series(0, 100);
SELECT * FROM __test;
UPDATE __test SET now = COALESCE(NULLIF('' || pg_sleep(0.01), '')::timestamp, clock_timestamp());
SELECT * FROM __test;