Skip to content

Instantly share code, notes, and snippets.

View davidh-raybeam's full-sized avatar

David Hollis davidh-raybeam

View GitHub Profile
@davidh-raybeam
davidh-raybeam / 0_ConstEnum.scala
Created July 16, 2018 18:27
Constant Enum implementation with accessors and json serializers
import play.api.libs.json._
trait ConstEnumType[T] { self: T =>
val name: String
val jsonKey: String = "type"
lazy val reads: Reads[T] = (JsPath \ jsonKey).read[String].filter(_ == name).map(_ => self)
lazy val matcher: PartialFunction[String, Option[T]] = {
implicit class JoinableMap[K, V1](left: Map[K, V1]) {
def innerJoin[V2](right: Map[K, V2]): Map[K, (V1, V2)] = (
for {
k <- (left.keySet & right.keySet).toSeq
l <- left.get(k)
r <- right.get(k)
} yield (k -> (l, r))
).toMap
def leftJoin[V2](right: Map[K, V2]): Map[K, (V1, Option[V2])] = (
@davidh-raybeam
davidh-raybeam / unenqueue.rb
Last active August 2, 2016 20:58
Paste this function into the master's rails console to enable it. It removes an item from the queue and marks it as "in progress" without assigning it to a worker, which will cause it to fail within 30 seconds or so. I wouldn't recommend calling this on tasks that are likely to be pulled off the queue any time soon.
# Example:
# unenqueue '411a80c7-fbcf-4312-a504-ebddfdbf5b1f'
def unenqueue(runtime_id)
default_queue = Mimir::Runtime::WorkQueue.new
Redis.current.multi do
default_queue.upcoming.delete(runtime_id)
Redis.current.lpush(default_queue.in_progress.key, runtime_id)
end
end
#! /bin/bash
# USAGE ./check-container.sh $CONATINER_ID
[[ "$(docker inspect --format='{{ .State.Running }}' --type=container $1)" == "true" ]] && exit 0 || exit 1
@davidh-raybeam
davidh-raybeam / bar.rb
Created December 6, 2013 16:59
bar usage: bar foo
#! /usr/bin/env ruby
bar_char = '-'
lead_count = 5
cols = `tput cols`.strip.to_i
max_len = [cols - ((2 * lead_count) + 4), 0].max
bar_color = `tput setaf 3`
reset = `tput sgr0`
title = ARGV.join ' '
@davidh-raybeam
davidh-raybeam / tputcolors.sh
Created December 6, 2013 17:02
Shamelessly copied from somewhere on the Internet. Please comment if you know the actual source, and I'll update this.
#!/bin/bash
# tputcolors
echo
echo -e "$(tput bold) reg bld und tput-command-colors$(tput sgr0)"
for i in $(seq 1 7); do
echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0) \$(tput setaf $i)"
done
@davidh-raybeam
davidh-raybeam / avg_timespan.sql
Last active December 25, 2015 05:19
avg_timespan.sql is a Vertica SQL statement which will, given a table like the one in setup.sql, calculate the mean amount of time a user spends on our website before completing a purchase, plus its standard deviation. Note that very little effort has been made to optimize this operation.
SELECT
AVG(session_length) as mean_session_length,
STDDEV(session_length_seconds) as session_length_stddev
FROM (
SELECT
event_name,
MAX(action_timestamp) OVER(PARTITION BY user_hostname, pattern_id)
- MIN(action_timestamp) OVER(PARTITION BY user_hostname, pattern_id)
as session_length,
datediff('second',
@davidh-raybeam
davidh-raybeam / markup-problem.md
Created May 28, 2013 18:10
The problem with tabular data and markup languages

Part of the problem is that markup languages are inherently 1-dimensional (and the ones that aren't suck to actually write in), whereas tablular data is often 2+-dimensional. This problem is compounded if you want to represent anything that isn't strictly a plain m by n table (e.g., something with additional formatting or cells with non-1 column or row spans).

Frankly, I think the best way to make tables is to have a GUI that generates some machine-readable markup for the table (or even just an image of it). I'd like for there to be something like Grid that does this.

<%= <<WAT
<%= whatever %>
WAT %>
@davidh-raybeam
davidh-raybeam / .bashrc
Created April 3, 2013 20:15
make and change to a dir
# ...
mdcd () {
mkdir -p $1 && cd $1
}
# ...