Skip to content

Instantly share code, notes, and snippets.

Use Sturges’ formula to get a better suited number of bins, rather than a one size fits all of 8. This will give us a starting point, using the orders by subtotal as an example, we would have:

; Orders has a count of 17624
(Math/ceil (+ 1 (/ (Math/log10 17624) (Math/log10 2))))
;-> 16
@senior
senior / limit.sql
Last active March 23, 2017 21:05
SQL Query with Limit of 100 (slow)
with inactive_nodes AS (SELECT certname FROM certnames WHERE (deactivated IS NOT NULL OR expired IS NOT NULL))
SELECT pi.name AS package_name, pi.version AS version, count(*) count
FROM package_inventory pi LEFT JOIN certnames ON pi.certname_id = certnames.id
WHERE ((pi.name ~ 'vim' AND pi.name IS NOT NULL)
AND NOT ((certnames.certname) in (SELECT inactive_nodes.certname AS certname FROM inactive_nodes)))
GROUP BY pi.name, pi.version ORDER BY pi.name, pi.version limit 100;
#!/usr/bin/guile \
-e main -s
!#
(use-modules ((ice-9 rdelim)
#:prefix dlm:))
(use-modules ((ice-9 getopt-long)
#:prefix gl:))
(define (current-max)
(call-with-input-file "/sys/class/backlight/intel_backlight/max_brightness"
(defn- qname-reader [form]
(let [[local-part uri] form
prefix-index (.indexOf local-part ":")]
(if (= -1 prefix-index)
(make-qname uri
local-part
"")
(make-qname uri
(subs local-part (inc prefix-index))
(subs local-part 0 prefix-index)))))
@senior
senior / example.scm
Last active August 29, 2015 14:26
mutually authenticated connection code
(define (ssl-conn-factory ca-cert-path cert-path key-path)
(lambda (uri-host uri-port)
(let ((ssl-ctx (ssl-make-client-context 'tls)))
(ssl-load-certificate-chain! ssl-ctx cert-path)
(ssl-load-private-key! ssl-ctx key-path)
(ssl-load-suggested-certificate-authorities! ssl-ctx ca-cert-path)
(ssl-load-verify-root-certificates! ssl-ctx ca-cert-path)
(ssl-set-verify! ssl-ctx #t)
(ssl-connect uri-host
uri-port
@senior
senior / gist:3df55a058dbc4e91f0ef
Created December 18, 2014 21:20
Show all local branches, sorted by date
alias glist="git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'"
@senior
senior / gist:10322804
Created April 9, 2014 22:02
PuppetDB Full Stack CLJ test
(ns com.puppetlabs.puppetdb.test.cli.import-export-roundtrip
(:require [com.puppetlabs.puppetdb.cli.export :as export]
[com.puppetlabs.puppetdb.cli.import :as import]
[puppetlabs.trapperkeeper.services.webserver.jetty9-service :refer [jetty9-service]]
[com.puppetlabs.puppetdb.cli.services :refer [puppetdb-service]]
[puppetlabs.trapperkeeper.core :as tk]
[clojure.test :refer :all]
[com.puppetlabs.puppetdb.testutils :as testutils]
[fs.core :as fs]
[puppetlabs.kitchensink.core :as kitchensink]
@senior
senior / gist:8480800
Created January 17, 2014 20:27
simple-check
(sm/defn basic-diff
"Basic diffing that returns only the keys/values of `right` whose values don't match those of `left`.
This is different from clojure.data/diff in that it treats non-equal sets as completely different
(rather than returning only the differing items of the set) and only returns differences from `right`."
[left right]
(reduce-kv (fn [acc k right-value]
(let [left-value (get left k)]
(if (= left-value right-value)
acc
(assoc acc k right-value))))
@senior
senior / db stats
Created January 2, 2014 18:36
1.6.0 differential benchmark
-[ RECORD 1 ]-----+------------------------------
relid | 405164
schemaname | public
relname | resource_params
seq_scan | 0
seq_tup_read | 0
idx_scan | 0
idx_tup_fetch | 0
n_tup_ins | 102106
n_tup_upd | 0
@senior
senior / storage.clj
Created December 17, 2013 18:25
diff-fn
;; ## Catalog persistence
;;
;; Catalogs are persisted in a relational database. Roughly speaking,
;; the schema looks like this:
;;
;; * resource_parameters are associated 0 to N catalog_resources (they are
;; deduped across catalogs). It's possible for a resource_param to exist in the
;; database, yet not be associated with a catalog. This is done as a
;; performance optimization.
;;