Skip to content

Instantly share code, notes, and snippets.

@jnm
jnm / refresh-front-end-files.sh
Created September 20, 2021 17:00
Refresh KPI front-end files without rebuilding them
#!/bin/bash
# jnm 20210526
set -e
test -e kpi && test -e kobo-install || (
echo 'This script must be run from the directory where `kpi` and `kobo-install` reside.'
exit 1
)
@jnm
jnm / crappy fake binary search.py
Created September 14, 2021 18:47
it's quick to get by PK and horribly slow to retrieve by date, but PKs increase monotonically with dates
# there's probably some way to teach postgres to do this itself
import datetime
# convenience scribbles
dd = [x - datetime.timedelta(days=1) for x in [datetime.date(2021, y, 1) for y in [4,5,6,7,8,9]]]
def print_quick(*args):
print(*args, end='', flush=True)
-- assumptions (many!)
-- you have a table called "nodestore_node"
-- you know how many rows it has and will stop this function manually
-- when its offset grows too large
-- the "id" column is a varchar
-- the "data" column is TOASTed
-- some of the TOAST storage is corrupt(!)
-- length(data) fails, revealing the corruption
-- you don't care about "data" and are happy to TRUNCATE any corrupted values
-- no one else is using the database (there is NO LOCKING)
@jnm
jnm / aws-elb-top-urls-over-time.py
Created May 8, 2020 21:11
Get counts of requests for the top 25 URLs over time, in 10-minute buckets
#!/usr/bin/env python3
import csv
import signal
import sys
from collections import Counter, defaultdict
TOP_URLS = 25
TIMESTAMP_GRANULARITY_MINUTES = 10
TIMESTAMP_FIELD_INDEX = 1
URL_FIELD_INDEX = 13

on the server:

sudo apt-get build-dep freerdp2-shadow-x11
sudo apt-get install ninja-build
sudo apt install libpam0g-dev # might be unnecessary?
git clone https://github.com/FreeRDP/FreeRDP/
cd FreeRDP/
cmake -GNinja -DWITH_SERVER=ON
cmake --build .
@jnm
jnm / copy-github-cards.py
Created June 25, 2018 22:21
Copy GitHub project cards between boards
import requests
headers = {'Authorization': 'token <ahem>', 'Accept': ' application/vnd.github.inertia-preview+json'}
# e.g. from https://api.github.com/repos/jnm/cuddly-doodle/projects
get_url = 'https://api.github.com/projects/columns/2942196/cards'
# e.g. from https://api.github.com/orgs/kobotoolbox/projects
post_url = 'https://api.github.com/projects/columns/2942356/cards'
<div class="tumblr-post" data-href="https://embed.tumblr.com/embed/post/1GjIG-_i_PMG0qAkoklkwQ/161507154717" data-did="2bd38ed99a4c97a215bb75c2085abf410fffcbbc"><a href="http://nancy-elsner-archive.tumblr.com/post/161507154717/a-gay-icon-batmanissue455oct1990">http://nancy-elsner-archive.tumblr.com/post/161507154717/a-gay-icon-batmanissue455oct1990</a></div> <script async src="https://assets.tumblr.com/post.js"></script>
@jnm
jnm / mongo_keys.js
Created July 15, 2016 14:46
Get all the keys from all documents in a Mongo collection
// mapReduce idea credit: http://stackoverflow.com/a/2308036
db.instances.mapReduce(
function() {
// map
for(var key in this) {
emit(key, null);
}
},
function(key, values) {
// reduce
import requests
import sys
import xml
import hashlib
import getpass
import datetime
from collections import OrderedDict
from django.conf import settings
@jnm
jnm / download-loop.html
Last active September 11, 2015 15:22
Test a connection by repeatedly downloading a file
<html>
<head><title>download test</title></head>
<body>
<input type="text" id="status" style="width: 100%">
<div id="log"></div>
<script>
/*
mostly cookbooked from
https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress