Skip to content

Instantly share code, notes, and snippets.

@momer
momer / example-hash.go
Created May 8, 2014 13:17
Golang example of creating a timestamped key for an immutable key/value store like Groupcache.
package main
import "fmt"
import "time"
import "encoding/hex"
import "hash/fnv"
func main() {
h := fnv.New64a()
@momer
momer / model_method.rb
Created December 18, 2013 20:53
Large custom query in postgreSQL for a Rails app
def moderate_plus_vigorous_durations_adjusted(start_date=(1.week.ago.beginning_of_day), end_date=(DateTime.now))
query_args = [{value: self.id}, {value: start_date}, {value: end_date}]
sql = %Q[
WITH my_vars(intensity_2_duration, intensity_3_duration) AS (VALUES
(
(SELECT SUM(duration) as intensity_2_duration
FROM users
INNER JOIN workouts
ON workouts.user_id = users.id
WHERE
@momer
momer / final_query.sql
Last active December 31, 2015 18:29
Thank you to davidfetter_disq for helping me reach this conclusion. Thank you as well to johto and ASnyder for offering their help :)
WITH my_vars(intensity_2_duration, intensity_3_duration) AS (VALUES
(
(SELECT (SUM(duration)*2) FROM users INNER JOIN workouts ON workouts.user_id = users.id
WHERE intensity = 3 AND workouts.deleted_at IS NULL),
(SELECT SUM(duration) FROM users INNER JOIN workouts ON workouts.user_id = users.id
WHERE intensity = 2 AND workouts.deleted_at IS NULL)
)
)
SELECT MAX(date), (SELECT (intensity_2_duration + intensity_3_duration) FROM my_vars) as total_duration, SUM(duration)
FROM workouts
@momer
momer / nginx.conf
Last active December 22, 2015 17:59
Example nginx config with unicorn and SSL redirection. - I think a lot of it was inspired/copy pasta'd from other gists/examples.
upstream unicorn {
# fail_timeout=0 means we always retry an upstream even if it failed
# to return a good HTTP response (in case the Unicorn master nukes a
# single worker for timing out).
# for UNIX domain socket setups:
server unix:/tmp/unicorn.examplesitehere.sock fail_timeout=0;
}
server {
listen 80;
@momer
momer / discrete_math_uccs.py
Created September 2, 2013 21:46
University of Colorado - Colorado Springs puts up a bunch of math course video lectures & more online for free. Simple script to download all of the Discrete Math lectures
import urllib
import urllib.request
def DownloadClass():
url = "http://cmes.uccs.edu/Fall2011/Math215/Videos/"
ext = ".mov"
movie_name = "Math215Lecture"
for i in range(1, 27):
@momer
momer / MyFirstBrainfuck
Last active December 19, 2015 03:59
Continually prints 'Get Money' while incrementing the ptr ad infinitum (or at least until size of int for cell #(0))
+[ Increment ptr(0) and enter loop
>++++++++++ Increment byte at ptr(1) 10 times
[ While ptr
>+++++++ incremement ptr(2) 7 times
>++++++++++ increment ptr(3) 10 times
>+ increment ptr(4) 1 time
>+++ increment ptr(5) 3 times
<<<< - back down to ptr 1 and decrement ptr 1
]
@momer
momer / explain_analyze.sql
Last active December 16, 2015 11:29
Super Fast Random Postgres Queries
--
-- Person's unoptimized query
--
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Seq Scan on keyword_tracked_tweets (cost=0.06..484854.06 rows=50015 width=172) (actual time=16639.692..32543.957 rows=1 loops=1)
Filter: ((id)::double precision = trunc((random() * ($1)::double precision)))
InitPlan 2 (returns $1)
-> Result (cost=0.05..0.06 rows=1 width=0) (actual time=0.045..0.047 rows=1 loops=1)
# include <stdio.h>
int main() {
char str[] = "Reverse me!";
int length;
for (length = 0; str[length] != '\0'; length++) {
@momer
momer / ability.rb
Last active December 15, 2015 11:18
As promised, here's the collaboration set-up I created with CanCan. I'd found the original idea through a suggestion in stack overflow (which even had some ideas for tests listed), which I then heavily adapted to my use case. Tests pass below; next steps for anyone else looking to use something like this is just implementing the fancy footwork i…
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # IE: Guest user isn't signed in, create a user
if user.try(:is?, :admin)
can :manage, :all
elsif !user.roles.empty? && user.approved?
@momer
momer / PG_auto_backup_upload_to_remote_cronjob
Last active December 14, 2015 21:58
Automagically backup & upload postgres database to a remote server at 5:45am every day.
# m h dom mon dow user command
45 5 * * * postgres pg_dumpall -l YOURDBNAME | gzip -c | ssh backup@MYBACKUPSERVER "cat | gzip /YOURDIRECTORYPATH/pg_backups/$(date +\"%Y-%m-%d:%T\")YOURFILENAME.gz"