Skip to content

Instantly share code, notes, and snippets.

View amitchhajer's full-sized avatar

Amit Chhajer amitchhajer

View GitHub Profile
@amitchhajer
amitchhajer / Count Code lines
Created January 5, 2013 11:08
Count number of code lines in git repository per user
git ls-files -z | xargs -0n1 git blame -w | perl -n -e '/^.*\((.*?)\s*[\d]{4}/; print $1,"\n"' | sort -f | uniq -c | sort -n
SELECT *, pg_size_pretty(total_bytes) AS total
, pg_size_pretty(index_bytes) AS index
, pg_size_pretty(toast_bytes) AS toast
, pg_size_pretty(table_bytes) AS table
FROM (
SELECT *, total_bytes-index_bytes-coalesce(toast_bytes,0) AS table_bytes FROM (
SELECT c.oid,nspname AS table_schema, relname AS table_name
, c.reltuples AS row_estimate
, pg_total_relation_size(c.oid) AS total_bytes
SELECT
current_database(), schemaname, tablename, /*reltuples::bigint, relpages::bigint, otta,*/
round(avg((CASE WHEN otta=0 THEN 0.0 ELSE sml.relpages::float/otta END)::numeric),1) AS tbloat,
round(avg(CASE WHEN relpages < otta THEN 0 ELSE bs*(sml.relpages-otta)::BIGINT END), 0) AS wastedbytes
FROM (
SELECT
schemaname, tablename, cc.reltuples, cc.relpages, bs,
CEIL((cc.reltuples*((datahdr+ma-
(CASE WHEN datahdr%ma=0 THEN ma ELSE datahdr%ma END))+nullhdr2+4))/(bs-20::float)) AS otta,
@amitchhajer
amitchhajer / add_row.sh
Created March 31, 2017 06:18 — forked from kristjan/add_row.sh
Google Spreadsheet row insertion example
# $auth from auth.sh
# $spreadsheet_id from get_spreadsheets.sh
# $worksheet_id from get_worksheets.sh
curl \
--header "Authorization: GoogleLogin auth=$auth" \
--header 'Content-Type: application/atom+xml' \
-d @data.xml \
-X POST \
"https://spreadsheets.google.com/feeds/list/$spreadsheet_id/$worksheet_id/private/full"
# Example data in data.xml
@amitchhajer
amitchhajer / Proxy
Created December 28, 2012 06:20
Tunnelling using ssh
ssh -C2qTnN -D 8080 username@xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx is the ip which allows ssh access) (can change the port from 8080 to as per requirement)
You are connected to the proxy. To use the proxy, go the one of your browser
and change the proxy settings to use SOCKS protocol and localhost (127.0.0.1) with port 8080.
@amitchhajer
amitchhajer / git_branches_specific_user
Last active January 2, 2016 09:18
Get all the branches in git for specific user
git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n | grep 'user'
@amitchhajer
amitchhajer / delete_duplicate.mysql
Created July 20, 2013 14:29
Delete duplicate records from database leaving one of them.
delete from my_table mt1, my_table mt2 where mt1.fieldX = mt2.fieldX and mt1.fieldY = mt2.fieldY and mt1.primary_key > mt2.primary_key
import sublime
import sublime_plugin
class NumberCommand(sublime_plugin.TextCommand):
def run(self, edit):
selection = self.view.sel()
for region in selection:
try:
value = int(self.view.substr(region))
self.view.replace(edit, region, str(self.op(value)))
@amitchhajer
amitchhajer / mysql formatted result
Created January 10, 2013 13:00
Formatted query result mysql
After into mysql cli
use command " pager less -SFX " and then use mysql cli normally
You can use arrow keys as well as pageup/down keys.
@amitchhajer
amitchhajer / php datetime
Created December 28, 2012 06:25
Validating php date and date time
Validating a date from format yyyy-mm-dd
$date = DateTime::createFromFormat('Y-m-d', $generatedFrom);
if (!($date instanceof DateTime)) {
throw new ValidationError('Not valid start date');
}
Similarly other formats can be validated.