Skip to content

Instantly share code, notes, and snippets.

View bastjan's full-sized avatar

Sebastian Widmer bastjan

View GitHub Profile
@bastjan
bastjan / hello.jsonnet
Created March 24, 2020 18:27
a cronjob definition for ksonnet/tanka
(import "ksonnet-util/kausal.libsonnet") +
{
local cronJob = $.batch.v1beta1.cronJob,
local container = $.core.v1.container,
hello_container::
container.new("hello", "busybox") +
container.withCommand([
"sh",
"-c",
@bastjan
bastjan / loki.py
Created December 3, 2019 15:49
Syslog-NG Python Loki Plugin
import grpc
import logproto_pb2
import logproto_pb2_grpc
import google.protobuf.timestamp_pb2
import time
print("=== loaded loki plugin ===")
netstat -apn | grep CLOSE_WAIT | awk '{ print $7 }' | sort | uniq -c | sort -nr
@bastjan
bastjan / purge.sh
Created August 22, 2018 14:11
Purge old kernels on ubuntu
#!/bin/bash
dpkg -l | grep '^rc' | awk '{print $2}' | grep 'linux-image' | xargs sudo apt purge -y
@bastjan
bastjan / string_regex.md
Created March 21, 2018 14:26
string regex without lookarounds "asd\" asd"
/"([^\\]\\"|[^"])*?"/

Sample Data:

"ASD"
"asd\" asd"
"asd\\" "asd"
"asd"
@bastjan
bastjan / titleize_mediawiki.rb
Created February 28, 2017 16:45
titleize mediwiki titles; reads from stdin
require 'titleize'
matches = []
input = ARGF.read
puts "--------------------------------------------------------"
puts "--------------------------------------------------------"
puts "--------------------------------------------------------"
@bastjan
bastjan / pg_search_corrupt_field.sql
Created February 21, 2017 13:12
find corrupted database fields in postgres
DO $f$
declare
read_data TEXT;
bad_id INT;
begin
FOR bad_id IN SELECT id FROM <TABLE_NAME> LOOP
begin
SELECT <TABLE_FIELD>
INTO read_data
FROM <TABLE_NAME> where id = bad_id;
@bastjan
bastjan / replace_dig.rb
Created January 17, 2017 15:40
replace our Hash#dig with ruby2.3 Hash#dig
file_names = Dir.glob("**/*.rb")
file_names.each do |file_name|
content = File.read(file_name)
new_content = content.gsub(/\.dig\('([^']+)'\)/) { |function| function.gsub(/(.)\.(.)/, '\1\', \'\2' ) }
File.open(file_name, "w") {|file| file.puts new_content }
end
@bastjan
bastjan / activerecord_union_scopes.rb
Created October 31, 2016 14:02
activerecord union scopes
scope1 = Model.scope1
scope2 = Model.scope2
union = Arel::Nodes::Union.new(scope1.arel, scope2.arel)
aliased_union = Arel::Nodes::As.new(union, Model.arel_table)
Model.from(aliased_union)
@bastjan
bastjan / order_by_subquery.sql
Last active August 29, 2015 14:24
order by subquery postgres
CREATE OR REPLACE FUNCTION idx(anyarray, anyelement)
RETURNS int AS
$$
SELECT i FROM (
SELECT generate_series(array_lower($1,1),array_upper($1,1))
) g(i)
WHERE $1[i] = $2
LIMIT 1;
$$ LANGUAGE sql IMMUTABLE;