Saturday, January 13, 2018

Clojure: generate Fibonacci sequence

((fn [n] (map second (take n (iterate (fn [[a b]] [b (+ a b)]) [0 1])))) 6)
(1 1 2 3 5 8)

Clojure: build map from value and keys sequence

((fn [defval vect] (reduce (fn [m k] (conj m {k defval})) {} vect)) 0 [:a :b])
{:a 0, :b 0}
or
((fn [defval vect] (zipmap vect (repeat defval))) 0 [:a :b :c])
{:a 0, :b 0, :c 0}

Clojure: inc map values

Source: Braveclojure
(reduce (fn [m [k v]] (assoc m k (inc v))) {} {:a 1 :b 2})
{:a 2, :b 3}
NB: In this example, reduce treats the argument {:a 1 :b 2} as a sequence of vectors, like ([:a 1] [:b 2])

or, with reduce-kv
(reduce-kv (fn [m k v] (assoc m k (inc v))) {} {:a 1 :b 2})
{:a 2, :b 3}

Wednesday, November 29, 2017

Clojure: vector 2 map

(reduce #(assoc %1 %2 1) {} ["apn1" "apn2" "apn3"])
{"apn1" 1, "apn2" 1, "apn3" 1}

Thursday, November 23, 2017

byobu: avoid terminal scrolling at each status update

Did not find a proper way to avoid terminal scrolling to the bottom when the byobu status is updated.
So, increased the tmux status-interval with a big enough value to not be bothered.
vi ~/.byoburc/.tmux.conf
set -g status-interval 86400

Friday, November 3, 2017

PostgreSQL: Table dependencies via Foreign Keys

Source: How to list tables affected by cascading delete
WITH RECURSIVE t AS (
    SELECT
        c.oid AS origin_id,
        c.oid::regclass::text AS origin_table,
        c.oid AS referencing_id,
        c.oid::regclass::text AS referencing_table,
        c2.oid AS referenced_id,
        c2.oid::regclass::text AS referenced_table,
        ARRAY[c.oid::regclass,c2.oid::regclass] AS chain
    FROM pg_catalog.pg_constraint AS co
    INNER JOIN pg_catalog.pg_class AS c ON c.oid = co.conrelid
    INNER JOIN pg_catalog.pg_class AS c2 ON c2.oid = co.confrelid
--     Add this line as an input parameter if you want to make a one-off query
--     WHERE c.oid::regclass::text = 'YOUR TABLE'
    UNION ALL
    SELECT
        t.origin_id,
        t.origin_table,
        t.referenced_id AS referencing_id,
        t.referenced_table AS referencing_table,
        c3.oid AS referenced_id,
        c3.oid::regclass::text AS referenced_table,
        t.chain || c3.oid::regclass AS chain
    FROM pg_catalog.pg_constraint AS co
    INNER JOIN pg_catalog.pg_class AS c3 ON c3.oid = co.confrelid
    INNER JOIN t ON t.referenced_id = co.conrelid
    WHERE
        -- prevent infinite recursion by pruning paths where the last entry in
        -- the path already appears somewhere else in the path
        NOT (
            ARRAY[ t.chain[array_upper(t.chain, 1)] ] -- an array containing the last element
            <@                                        -- "is contained by"
            t.chain[1:array_upper(t.chain, 1) - 1]    -- a slice of the chain,
                                                      -- from element 1 to n-1
        )
)
SELECT  origin_table,
        referenced_table,
        array_upper(chain,1) AS "depth",
        array_to_string(chain,',') as chain
FROM t
where referenced_table ~ 'shared_db'

Git diff between current branch and master

Source: Git diff between current branch and master but not including unmerged master commits
git diff master...

Linux: Alert when running process finishes

Source: Alert when running process finishes
(while kill -0 $pid; do sleep 1; done) && echo "finished"

PostgreSQL: Running SQL scripts with psql

Source: Running SQL scripts with psql
PGOPTIONS='--client-min-messages=warning' psql -X -q -a -1 -v ON_ERROR_STOP=1 --pset pager=off -d mydb -f dump.sql

Thursday, October 12, 2017

PostgreSQL: Display materialized view definition

select definition from pg_matviews where matviewname = 'mv_jersey_tap_data_3months_lag';

PostgreSQL: Find numeric rows w/o digit after decimal point

Source: Fetch records that are non zero after the decimal point in PostgreSQL
SELECT count(*) FROM t WHERE amount != trunc(amount)

Wednesday, October 11, 2017

Delete files older than file

Source: Delete files older than specific file
find __dir__ -type f ! -newer __file__ -delete

Wednesday, August 30, 2017

PostgreSQL: dump and restore one table

pg_dump --host db2.prod.vty.mbqt --port 5432 --username postgres --no-owner --no-password --format custom --encoding UTF8 --verbose --file /tmp/jerome/jerome_test.custom --table public.jerome_test --data-only dwhmbqt
Then
pg_restore --host db2.prod.vty.mbqt --port 5432 --username postgres --no-owner --no-password --dbname dwhmbqt --data-only --verbose /tmp/jerome/jerome_test.custom

PostgreSQL: tables without PK

Source: http://petereisentraut.blogspot.fr/2009/07/how-to-find-all-tables-without-primary.html
-- Tables of db2 without PK
SELECT table_catalog, table_schema, table_name
    FROM information_schema.tables
    WHERE (table_catalog, table_schema, table_name) NOT IN
          (SELECT table_catalog, table_schema, table_name
               FROM information_schema.table_constraints
               WHERE constraint_type = 'PRIMARY KEY')
      AND table_schema NOT IN ('information_schema', 'pg_catalog') and table_schema ~ '_db$' and table_schema not in ('prov_db', 'queue_db', 'ota_db', 'sla_db');

PostgreSQL: find non-empty tables

Source: https://stackoverflow.com/questions/24734489/show-all-not-empty-tables-in-postgres
SELECT n.nspname, c.relname                                                                                                                                                                           
FROM pg_class c
INNER JOIN pg_namespace n ON (n.oid = c.relnamespace)
WHERE c.reltuples != 0 AND c.relkind = 'r' and nspname ~ '_db$'

Thursday, June 8, 2017

Internet lost while VPN Cisco and wired to wifi

The situation is the following:
  • Wired to LAN on Windows 7 
  • VPN Cisco AnyConnect connected 
  • Internet OK

then

Change LAN connection from wired to WIFI => Internet lost unless disconnecting VPN

Solution 1: 
Reboot : )

Solution 2: 

  • Disconnect Cisco VPN 
  • Windows Device Manager | Network adapters 
  • Right click on wireless adapter
  • Select Disable

Then

  • Right click on wireless adapter
  • Select Enable
  • Reconnect Cisco VPN