Skip to content

Instantly share code, notes, and snippets.

View drbobbeaty's full-sized avatar

Bob Beaty drbobbeaty

View GitHub Profile
@drbobbeaty
drbobbeaty / create_all.sql
Last active January 15, 2019 14:18
Nice Postgres Trigger for Immutable Data with Audit Log
--
-- Assume that you have a table - `customers`... create a new table called
-- `customers_audit` with the following qualities:
--
create table if not exists customer (
-- assumming you have a primary key that's a uuid
id uuid not null,
-- ...make sure to have a version and timestamp
version integer not null,
as_of timestamp with time zone not null,
@drbobbeaty
drbobbeaty / db (slash) polaris.clj
Last active October 16, 2015 17:13
Base database namespace, and a specific one for easy database usage
(ns ns-toolkit.db.polaris
"Namespace for accessing the Polaris database for all the data it has."
(:require [ns-toolkit.config :as cfg]
[ns-toolkit.db :as db]
[ns-toolkit.logging :refer [log-execution-time!]]
[cheshire.core :as json]
[clj-time.coerce :refer [to-timestamp from-date]]
[clojure.java.jdbc :refer [IResultSetReadColumn ISQLValue result-set-read-column] :as sql]
[clojure.string :as cs]
[clojure.tools.logging :refer [error errorf warnf infof info]]
@drbobbeaty
drbobbeaty / core.clj
Last active October 9, 2015 16:53
Lazy problem of not being lazy
(defn- get-block
"Function to get one block from LenderX based on the URL and start and limit
values provided. Typically we should keep the `limit` to 25 or less, but this
function will be called for each block - as needed, from LenderX for the
provided URL."
[url st lim & [sess]]
(if (and (string? url) (not (neg? st)) (pos? lim))
(let [ep (str url (if (neg? (.indexOf url "?")) "?" "&") "start=" st "&limit=" lim)
]
(-> (do-get* cm ep (mk-headers "GET" ep nil sess) lenderx-opts)
@drbobbeaty
drbobbeaty / metrics.clj
Created August 18, 2015 15:02
Metrics namespace for sending simple metrics to local Datadog Agent
(ns ns-toolkit.metrics
"This is the code that handles the metrics and events through the Dropwizard
Metrics core library, which, in turn, will ship it over UDP to the DataDog
Agent running on localhost."
(:require [clojure.tools.logging :refer [infof debugf warnf errorf]])
(:import [com.codahale.metrics MetricRegistry]
[org.coursera.metrics.datadog DatadogReporter]
[org.coursera.metrics.datadog.transport UdpTransportFactory
UdpTransport]
[java.util.concurrent TimeUnit]))
@drbobbeaty
drbobbeaty / project.clj
Created June 29, 2015 17:12
Quick and easy Base-64 encoding and decoding
:dependencies [[base64-clj "0.1.1"]]
@drbobbeaty
drbobbeaty / logging.clj
Last active August 29, 2015 14:22
Nice logging clojure namespace to easily track the execution time and log it
(ns cq.logging
"Logging utilities."
(:require [clojure.tools.logging :refer [log]]
[robert.hooke :refer [add-hook]]))
(defn now [] (System/currentTimeMillis))
(defn execution-time-logging-hook
"Given a config map, returns a hook function that logs execution time."
[{:keys [level func-name msg msg-fn ns] :or {level :info}}]
@drbobbeaty
drbobbeaty / database.clj
Last active August 29, 2015 14:21
Nice JDBC namespace for apps
(ns my-prog.database
"Namespace for accessing the database for all the persistent data we will
be gathering and serving up."
(:require [my-prog.config :as cfg]
[my-prog.logging :refer [log-execution-time!]]
[cheshire.core :as json]
[clj-time.coerce :refer [to-timestamp from-date]]
[clojure.java.jdbc :refer [IResultSetReadColumn result-set-read-column] :as sql]
[clojure.string :as cs]
[clojure.tools.logging :refer [error errorf warnf infof info]]
@drbobbeaty
drbobbeaty / dcm.clj
Last active August 29, 2015 14:16
OAuth2 Login to Google
(ns bartender.dcm
"The DCM-specific functions for loading data. The Google OAuth2 data for the
application is obtained from the Developer Console."
(:require [bartender.util :refer [parse-json update lcase]]
[cheshire.core :as json]
[clj-http.client :as http]
[clj-oauth2.client :as oauth2]
[clj-time.coerce :refer [to-long from-long]]
[clj-time.core :refer [now]]
[clojure.set :refer [rename-keys]]
@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
@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]]))