Skip to content

Instantly share code, notes, and snippets.

View cjthompson's full-sized avatar

Chris Thompson cjthompson

  • Osiris Development
  • Colorado, USA
View GitHub Profile
@cjthompson
cjthompson / readableNumber.js
Created February 21, 2014 18:29
Convert a number into a human readable number with 3 significant digits followed by 'K' (thousand), 'M' (million), or 'B' (billion)
function readableNumber(num) {
var s = ['', 'K', 'M', 'B'];
var e = Math.floor(Math.log(num) / Math.log(1000));
return (num / Math.pow(1000, e)).toPrecision(3) + s[e];
}
@cjthompson
cjthompson / randomString.js
Created June 16, 2015 18:35
JavaScript: random string
var _ = require('lodash');
var randomString = function (length) {
return _(length).range().map(_.partial(_.random, 33, 126, false)).map(_.ary(String.fromCharCode)).join('');
};
#!/bin/bash
USER="user"
PASSWORD="password"
HOST="localhost"
MYSQL_OPTS="--compact --single-transaction --skip-opt --quick --no-create-info --skip-triggers"
if [ "$1" == "" ]; then
echo "Usage: $0 database-name [output-dir]"
exit 1
fi
@cjthompson
cjthompson / node56-nsswitch.conf
Created February 10, 2016 21:05
Node 5.6.0 crash with nsswitch.conf permissions set to 600
# /etc/nsswitch.conf is set to 600
strace ./node
execve("./node", ["./node"], [/* 32 vars */]) = 0
brk(0) = 0x2f42000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f01cb5de000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=26694, ...}) = 0
@cjthompson
cjthompson / strace.txt
Created February 10, 2016 21:28
Node 5.6.0 with /etc/nsswitch.conf permissions set to 644
execve("./node56", ["./node56"], [/* 17 vars */]) = 0
brk(0) = 0x340d000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f64b6c1b000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=30969, ...}) = 0
mmap(NULL, 30969, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f64b6c13000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
@cjthompson
cjthompson / strace55.txt
Created February 10, 2016 21:31
Node 5.5.0 with /etc/nsswitch.conf permissions set to 600
execve("./node", ["./node"], [/* 32 vars */]) = 0
brk(0) = 0x288c000
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd8ca824000
access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=26694, ...}) = 0
mmap(NULL, 26694, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd8ca81d000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
@cjthompson
cjthompson / gist:80cce78894edcbc04b8b
Created February 24, 2016 08:20
Lincastor - Two Browsers
(*
the 'args' dictionary contains following variables:
URL => my-http://myhost.domain.com:8080/mysite/a.html?search=blah#myanchor
URL_SCHEME => my-http
URL_HOST => myhost.domain.com
URL_PORT => 8080
URL_PATH => /mysite/a.html
URL_QUERY => ?search=blah
URL_FRAGMENT => #myanchor
URL_VALUE => everything that comes after the 'scheme:'
@cjthompson
cjthompson / findschema.js
Last active September 28, 2021 20:05
Find all possible fields and their types from a MongoDB collection
var obj = {};
function reduce(accum, doc) {
for (var key in doc) {
if (doc.hasOwnProperty(key) && typeof doc[key] !== 'function') {
const type = typeof doc[key]
if (type === 'object') {
const aKey = (accum[key] && typeof accum[key] === 'string') ? key + '_obj' : key
accum[aKey] = accum[aKey] ? accum[aKey] : {}
reduce(accum[aKey], doc[key])
@cjthompson
cjthompson / DeleteDuplicates.sql
Created March 20, 2013 17:43
Find duplicate records in a MySQL database, save the first row of a set of duplicates, and delete the rest.
-- Assume `Table` has 4 columns: id, col1, col2, col3
CREATE TABLE `dups` (
`id` int(10) unsigned DEFAULT NULL,
`hash` varbinary(32) DEFAULT NULL,
KEY `id` (`id`),
KEY `hash` (`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO dups SELECT MIN(id) AS id, MD5(CONCAT(col1, col2, col3)) AS hash FROM `Table` GROUP BY col1, col2, col3 HAVING COUNT(*) > 1;
@cjthompson
cjthompson / RobustPDO.php
Created February 3, 2014 19:52
Extended PDO class that detects dropped connections and reconnects
<?php
class RobustPDO extends PDO
{
/** Call setAttribute to set the session wait_timeout value */
const ATTR_MYSQL_TIMEOUT = 100;
/** @var array */
protected $config = [];
/** @var bool For lazy connection tracking */