Skip to content

Instantly share code, notes, and snippets.

View drbobbeaty's full-sized avatar

Bob Beaty drbobbeaty

View GitHub Profile
@drbobbeaty
drbobbeaty / database.rb
Created November 9, 2012 21:02
Optionally queueing Ruby CouchDB/CouchRest client
# encoding: utf-8
require 'quantum_lead/application'
require 'singleton'
require 'logger'
require 'version'
require 'couchrest'
require 'java'
java_import 'java.util.concurrent.ConcurrentLinkedQueue'
class Database
@drbobbeaty
drbobbeaty / gist:4052640
Created November 10, 2012 21:45
JRuby extensions to Enumerable for parallel processing and more
require 'java'
java_import 'java.util.concurrent.Executors'
java_import 'java.util.concurrent.TimeUnit'
module Enumerable
# From activesupport/lib/active_support/core_ext/enumerable.rb
def sum(identity = 0, &block)
if block_given?
map(&block).sum(identity)
else
@drbobbeaty
drbobbeaty / vacuum.sh
Last active December 11, 2015 04:39
Nice script that cleans up stale deployment jars after 30 days, but _always_ preserves the last 5 so you can go back to any one of them if need be. Great for jruby and clojure projects where it's all in a single jar.
#!/bin/bash
#
# This script clears out all the "stale" jars that we no longer need.
# They have to be AT LEAST 30 days old, and we MUST keep the last 5.
# Other than that, they can go.
#
#
# Set upt he environment for this - as we do in all scripts
@drbobbeaty
drbobbeaty / dark-magic.html
Last active December 11, 2015 13:29
Nice little page for getting and displaying JSON for the user. It uses a slick little classifier for the syntax highlighting and then CSS classes to set the attributes of those tags. Very nice.
<html>
<head>
<title>Dark Magic Query Tools for Quantum Lead</title>
<link href='//fonts.googleapis.com/css?family=Ubuntu' rel='stylesheet' type='text/css'>
<script type='text/javascript' src='zingchart/resources/jquery.min.js'></script>
<script type='text/javascript'>
// This is a clever little function I found that does a very simplistic
// syntax coloring scheme for JSON. What it really is doing is to classify
// all the elements, and then with CSS we can color them. Very slick.
function syntaxHighlight(json) {
@drbobbeaty
drbobbeaty / fast_child_loader.sql
Created February 4, 2013 16:52
I needed a way to load up the children of a very large table with decent referential integrity a lot faster than joins and sub-selects. What I came up with was a stored procedure that loops over the parent ids, and loads up the results into a temp table.
create or replace function locations_for_demand_set(ds_id uuid)
returns table(id uuid,
demand_id uuid,
is_anchor_zip boolean,
latitude double precision,
longitude double precision,
name varchar,
subdivision_rank varchar,
type varchar,
division_permalink varchar,
@drbobbeaty
drbobbeaty / REPL.txt
Last active December 14, 2015 16:39
Problem with asynchronously calls
dark-magic.main=> (require 'slam :reload-all)
nil
dark-magic.main=> (slam/unbound 4)
nil
dark-magic.main=> (slam/bound 4)
nil
@drbobbeaty
drbobbeaty / server.clj
Last active March 10, 2022 12:24
Simple clojure Ring example
(ns performance.server
"The routes for the web server."
(:require [cheshire.core :as json]
[clj-time.core :refer [now]]
[clojure.tools.logging :refer [infof warn warnf error errorf]]
[compojure
[core :refer [defroutes GET POST]]
[handler :as handler]]
[ring.middleware.jsonp :refer [wrap-json-with-padding]]
[ring.middleware.params :refer [wrap-params]]
@drbobbeaty
drbobbeaty / nimbus.clj
Created September 8, 2014 14:07
Clojure code to get the bolt metrics for a Storm Topology
(ns gym.storm.nimbus
"Namespace for exercising Nimbus to find out facts about the specific storm
cluster. This data can be used to monitor the cluster and look at stats in
a way that's got very low load on the overall system."
(:require [backtype.storm.clojure :refer :all]
[backtype.storm.config :refer :all]
[backtype.storm.ui.core :refer :all]
[clj-endpoints :as ep]
[clj-endpoints.persistence.redis :refer [wcar]]
[clj-endpoints.util :as util]
@drbobbeaty
drbobbeaty / bucket.clj
Created September 19, 2014 17:10
Bucketing Service code for experiment variants
(ns finch-experiments.bucket
"Namespace to deal with all the bucketing issues with the experiments and
users so that we can respond as the finch client would."
(:require [clj-endpoints :as ep]
[clj-endpoints.logging :refer [log-execution-time!]]
[clj-endpoints.util :refer [md5 to-uuid nil-if-empty compact]]
[finch-experiments.routing :as routing]
[finch-experiments.web :as web]
[finch-experiments.data :refer [experiment-list]]))
@drbobbeaty
drbobbeaty / Snippet for JSONP
Created October 28, 2014 16:07
Need to have Gary take a look at this to see if it'll work
(defn return-json
"Creates a ring response for returning the given object as JSON."
([ob] (return-json ob (now) 200))
([ob lastm] (return-json ob lastm 200))
([ob lastm code]
{:status code
:headers {"Content-Type" "application/json; charset=UTF-8"
"Last-Modified" (str (or lastm (now)))}
;; TODO: the input-stream tactic is foiled by ring.middleware.jsonp
;; which slurps the whole stream before adding the callback. Could