Skip to content

Instantly share code, notes, and snippets.

@bfolkens
bfolkens / postgres_counter_cache.sql
Last active November 8, 2023 17:21
PostgreSQL Counter Cache
-- Courtesy
-- http://shuber.io/porting-activerecord-counter-cache-behavior-to-postgres/
CREATE FUNCTION increment_counter(table_name text, column_name text, id integer, step integer)
RETURNS VOID AS $$
DECLARE
table_name text := quote_ident(table_name);
column_name text := quote_ident(column_name);
conditions text := ' WHERE id = $1';
updates text := column_name || '=' || column_name || '+' || step;
@bfolkens
bfolkens / String+javaScriptEscapedString.swift
Created November 3, 2023 18:43
Swift 5.1 - String javaScriptEscapedString extension
extension String {
var javaScriptEscapedString: String {
// Because JSON is not a subset of JavaScript, the LINE_SEPARATOR and PARAGRAPH_SEPARATOR unicode
// characters embedded in (valid) JSON will cause the webview's JavaScript parser to error. So we
// must encode them first. See here: http://timelessrepo.com/json-isnt-a-javascript-subset
// Also here: http://media.giphy.com/media/wloGlwOXKijy8/giphy.gif
let str = self.replacingOccurrences(of: "\u{2028}", with: "\\u2028")
.replacingOccurrences(of: "\u{2029}", with: "\\u2029")
// Because escaping JavaScript is a non-trivial task (https://github.com/johnezang/JSONKit/blob/master/JSONKit.m#L1423)
// we proceed to hax instead:
@bfolkens
bfolkens / RecordButton.swift
Created March 13, 2021 04:09
SwiftUI version of the VoiceMemo record button (with animation)
import SwiftUI
struct RecordButton: View {
@Binding var isActive: Bool
@State var startAction = { }
@State var stopAction = { }
@State var buttonColor: Color = .red
@State var borderStrokeColor: Color = .white
@State var borderStrokeWidth: CGFloat = 2
@bfolkens
bfolkens / statsd-exporter.yaml
Last active May 2, 2023 19:16
Kubernetes deployment for the statsd-exporter tool
apiVersion: apps/v1
kind: Deployment
metadata:
name: statsd-exporter
namespace: <namespace>
spec:
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
@bfolkens
bfolkens / alpine-speech.js
Created April 13, 2023 17:56
HTML5 SpeechRecognition for Alpine
export default function (Alpine) {
const recognizer = new window.SpeechRecognition()
recognizer.lang = navigator.language
recognizer.continuous = false
recognizer.interimResults = true
Alpine.store('speechRecognizer', {
state: 'stopped',
error: null,
api: recognizer
@bfolkens
bfolkens / kubectl_backup_secrets.sh
Created August 3, 2022 02:42
Backup kubernetes cluster secrets
kubectl get secrets -o json | \
jq -r '.items[].metadata.name' | \
while read i; do kubectl get secret $i -o json > $i.json; done
@bfolkens
bfolkens / httpdownloader.ex
Last active April 5, 2022 21:15
Stream wrapper around HTTPoison.get!(...)
defmodule HTTPDownloader do
@moduledoc """
Stream wrapper around HTTPoison.get!(...)
Source: https://elixirforum.com/t/how-to-stream-file-from-aws-to-client-through-elixir-backend/20693/15?u=bfolkens
HTTPDownloader.stream!(url_to_my_s3_file)
|> Enum.each(fn chunk -> send_chunk_to_client(client_conn, chunk) end)
"""
def stream!(url) do
@bfolkens
bfolkens / collate.ex
Created March 15, 2022 05:39
Collate a list of lists in sequence until the longest list is exhausted.
defmodule Util.Enum do
@doc ~S"""
Collates a list of lists in sequence until the longest
list is exhausted.
## Examples
iex> Util.Enum.collate([[1, 2, 3], [1, 2], [1, 2, 3, 4]])
[1, 1, 1, 2, 2, 2, 3, 3, 4]
@bfolkens
bfolkens / psql_utilization.sql
Created April 16, 2021 13:59
PostgreSQL to show all space used by objects
WITH RECURSIVE pg_inherit(inhrelid, inhparent) AS
(select inhrelid, inhparent
FROM pg_inherits
UNION
SELECT child.inhrelid, parent.inhparent
FROM pg_inherit child, pg_inherits parent
WHERE child.inhparent = parent.inhrelid),
pg_inherit_short AS (SELECT * FROM pg_inherit WHERE inhparent NOT IN (SELECT inhrelid FROM pg_inherit))
SELECT table_schema
, TABLE_NAME
@bfolkens
bfolkens / check_row_size.sh
Created February 23, 2021 02:45
ERROR 1118 (42000): Row size too large
#!/bin/bash
[ -z "$3" ] && echo "Usage: $0 host user password" >&2 && exit 1
dt="tmp_$RANDOM$RANDOM"
mysql -h $1 -u $2 -p$3 -ABNe "create database $dt;"
[ $? -ne 0 ] && echo "Error: $0 terminating" >&2 exit 1
echo