Skip to content

Instantly share code, notes, and snippets.

@bitdivine
bitdivine / number-with-slider-web-component.html
Created July 11, 2016 15:40
Number input with slider as a web component.
(function(){
// Input a number in either a text field or with a slider.
// Usage: <input type="number" is="with-slider" min="20" max="100" name="..." value="...">
try {
var proto = Object.create(HTMLInputElement.prototype);
proto.createdCallback = function() {
var self = this;
this.type = "number";
this.addEventListener("change", this.setDecimalPlaces);
this.slider = document.createElement('input');
@bitdivine
bitdivine / is-git-clean.bash
Created June 30, 2016 15:20
Check whether git is clean using plumbing methods
if git ls-files -mo --exclude-standard | grep -q .
then
echo ":-X Dirty git. Say sorry:"
read sorry ; [[ "${sorry:-unrepentant}" == "sorry" ]] || exit 1
else echo ":-) Git clean"
fi
# Get (all, including multiline) postgres log file entries for [26748-
sed -n ':a;/\[26748-/{ h;:b;n;/^\s/{H;bb};x;p;:a }' /var/log/postgresql/postgresql-9.5-warehouse.log | less
@bitdivine
bitdivine / postgres-timeseries-arrays.sql
Created June 9, 2016 14:34
An experiment storing timeseries in postgres arrays
-- Note: There is a similar project: https://github.com/tgres/tgres
-- I haven't looked into it much.
-- This is nice and readable: http://grisha.org/blog/2015/09/23/storing-time-series-in-postgresql-efficiently/
CREATE TABLE ts ( tsid SERIAL PRIMARY KEY, name text, current INTEGER, value DOUBLE PRECISION, data DOUBLE PRECISION[], UNIQUE(name));
-- Initialise a timeseries:
CREATE OR REPLACE FUNCTION pllua_init(len INTEGER, next INTEGER, value DOUBLE PRECISION) RETURNS DOUBLE PRECISION[] AS $$
local ans = {}; local i; for i=1,len,1 do ans[i] = 0 end
@bitdivine
bitdivine / whats_listening.sh
Created June 7, 2016 22:51
TCP listening process working directory and command
ss -ltpn | sed -r '1d;s/^(\S+\s+){3}(\S+).*,(pid=)?([0-9]+),(fd=)?[0-9]+[)][).*]/\2 \4/g' | while read line ; do set $line ; echo $1 $(readlink -f /proc/$2/cwd) $(cat /proc/$2/cmdline) ; done | sort
@bitdivine
bitdivine / first_non_null.sql
Last active June 3, 2016 09:04
Postgres aggregator to get the first non-null value.
-- Like the Postgres first_val() aggregator, exceppt that this returns the first non-null value.
CREATE OR REPLACE FUNCTION first_non_null_fun(numeric, numeric) RETURNS numeric
AS 'SELECT case when $1 is null then $2 else $1 end;'
LANGUAGE SQL IMMUTABLE STRICT;
CREATE AGGREGATE first_non_null ( numeric ) (
SFUNC = first_non_null_fun,
STYPE = numeric
);
@bitdivine
bitdivine / daynum_now.sql
Last active February 8, 2018 07:41
Postgres: days since epoch.
create or replace function daynum_now() returns integer as $$
begin
RETURN FLOOR(EXTRACT(EPOCH FROM now())/(24*3600));
end
$$ LANGUAGE 'plpgsql';
@bitdivine
bitdivine / inf_refresh_recursive.sql
Last active October 14, 2019 04:38
Refresh postgres materialized views recursively
-- Refresh materialized views recursively
-- DEPENDS:
-- List the tables that a view depends on.
-- Thanks to Dave: http://stackoverflow.com/questions/4229468/getting-a-list-of-tables-that-a-view-table-depends-on-in-postgresql
create or replace function inf_view_dependencies(v text)
returns table (kind text, name text) as $$
SELECT cl_d.relkind::text as kind
, cl_d.relname::text AS name
FROM pg_rewrite AS r
@bitdivine
bitdivine / inf_view_dependencies.sql
Last active June 3, 2016 09:03
List the tables that a view depends on.
-- List the tables that a view depends on.
-- Usage: select * from inf_view_dependencies(SOME_VIEW);
-- Thanks to Dave: http://stackoverflow.com/questions/4229468/getting-a-list-of-tables-that-a-view-table-depends-on-in-postgresql
create function inf_view_dependencies(v text)
returns table (kind text, name text) as $$
SELECT cl_d.relkind::text as kind
, cl_d.relname::text AS name
FROM pg_rewrite AS r
JOIN pg_class AS cl_r ON r.ev_class=cl_r.oid
JOIN pg_depend AS d ON r.oid=d.objid
@bitdivine
bitdivine / iochill.sh
Created May 19, 2016 20:59
Make IO-heavy processes play nice
iotop -b | head | gawk '{print $1}' | tee /dev/stderr | xargs --replace={} ionice -c3 -p{}