Skip to content

Instantly share code, notes, and snippets.

View nad2000's full-sized avatar
🇳🇿
Working from New Zealand

Radomirs Cirskis nad2000

🇳🇿
Working from New Zealand
View GitHub Profile
@nad2000
nad2000 / lfs.sh
Last active March 11, 2016 08:35
Building LFS (http://www.linuxfromscratch.org/) on a Ubuntu 12.04.2 LTS based on Linux From Scratch - Version 7.4
# Creating the $LFS directory
...
# Create lfs user and the profile for it
...
sudo su - lfs
# Downloading packet source:
mkdir -vp $LFS/sources
@nad2000
nad2000 / run_sql_script.sh
Last active December 22, 2015 06:29
One-liner loops through a PostgreSQL DB list and executes on each of them a script
for db in $(psql -t -X -h 192.168.132.111 -U postgres -d probedb -c 'SELECT erfstore_id FROM erfstores') ; do psql -X -h 192.168.132.111 -U postgres -d $db -f /build/trunk/dock/tree/customer/endace/src/bin/dbmgr/scripts/erfstoredb/da_breakdown_summary_sql.sql ; done
# Or slightly more readable:
export PGHOST=fin7000-4
export PGDATABASE=probedb
export PGUSER=postgres
cd /ginkgo/trunk/ginkgo/tree/customer/endace/src/bin/dbmgr/scripts/erfstoredb/
for db in $(psql -t -X -c 'SELECT erfstore_id FROM erfstores') erfstoredb ; do psql -X -d $db -f da_breakdown_summary_sql.sql ; done
@nad2000
nad2000 / argumentor.py
Last active December 22, 2015 19:29 — forked from Ed-von-Schleck/gist:6391140
Back ported to 2.7 (it should work with earlier versions with added argparse)
import argparse
import inspect
class Argumentor:
def __init__(self, description=None):
self._parser = argparse.ArgumentParser(description=description)
self._subparsers = self._parser.add_subparsers()
def add(self, description={}):
@nad2000
nad2000 / queries4juan.sql
Last active December 22, 2015 23:39
PostgreSQL queries for various present time series views The last query is an example of using window function over whole range (w/o PARTITION BY) with ORDER BY for creating a running total.
-- // How many events were created from date A to date B (possibly presenting data on a daily, weekly and monthly basis)
-- Replace 'week' with 'day' or 'month'
-- date_trunc takes: minute hour day week month quarter year
-- NB! TIMESTAMP '2013-09-16' == '2013-09-16 00:00:00' so BETWEEN includes only tickets sold until that date.
SELECT date_trunc('week', created_at) AS dt, count(*) AS total_events
FROM events
WHERE created_at BETWEEN TIMESTAMP '2013-06-16' AND TIMESTAMP '2013-09-16'
GROUP BY date_trunc('week', created_at)
-- // How many paid tickets were sold form date A to date B (presenting graphs on a day to day basis, weekly and monthly)
@nad2000
nad2000 / radom_string.sh
Created September 24, 2013 04:20
Random string
head -1 /dev/urandom | base64 -w0
# -*- coding: utf-8 -*-
# Original from:
# http://pyvideo.org/video/1780/transforming-code-into-beautiful-idiomatic-python
colors = ['red', 'green', 'blue', 'yellow']
names = ['apple', 'carrot', 'melon']
for color in reversed(colors):
print color
#%% DATA IMPORTING AND VISUALIZATION
from urllib2 import urlopen
from contextlib import closing
from os.path import basename
def download_file(url, name=None):
with closing(urlopen(url)) as u, open(name if name else basename(url), 'w') as f:
f.write(u.read())
@nad2000
nad2000 / bidir2unidir.sql
Last active December 30, 2015 13:59
Create unidirectional sample set (table) from bi-directional samples
-- PostgreSQL
DROP TABLE IF EXISTS uview5_uni;
CREATE TABLE uview5_uni AS
SELECT
start_time,
CASE e WHEN 1 THEN mac_addr_1 ELSE mac_addr_2 END src_mac_addr,
CASE e WHEN 1 THEN mac_addr_2 ELSE mac_addr_1 END des_mac_addr,
CASE e WHEN 1 THEN byte_count_1 ELSE byte_count_2 END byte_count,
CASE e WHEN 1 THEN packet_count_1 ELSE packet_count_2 END packet_count,
last_time,
@nad2000
nad2000 / process_pgbench_outpus.sh
Last active December 31, 2015 04:59
1) removes all lines except relevant: - iteration parameter; - tps (excluding connection time). 2) joins lines together to form CSV file.
egrep "(\*\*\*\*|excluding)" test.log | sed '/tps/ s/tps = \([.0-9]*\) .*/\1/; /uview/ s/ uview\([0-9]*\)/\1/' | sed -n '/\**/ {N;s/\n/,/;N;s/\n/,/;s/\**//p}'
## With loading into PostgreSQL:
# Create table:
psql -d 5 -c "create table test_output( id int, tps1 numeric, tps2 numeric)"
# Extract and load data:
egrep "(\*\*\*\*|excluding)" test.log | sed '/tps/ s/tps = \([.0-9]*\) .*/\1/; /uview/ s/ uview\([0-9]*\)/\1/' | sed -n '/\**/ {N;s/\n/,/;N;s/\n/,/;s/\**//p}' | psql -d 5 -c "TRUNCATE TABLE test_output; COPY test_output FROM STDIN (FORMAT csv, DELIMITER ',', HEADER false)"
@nad2000
nad2000 / 0_reuse_code.js
Created December 28, 2013 06:25
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console