Skip to content

Instantly share code, notes, and snippets.

Avatar

Amit Upadhyay amitu

View GitHub Profile
View CICharField.py
class CICharField(models.CharField):
def db_type(self, connection):
assert connection
return 'CITEXT'
def from_db_value(self, value, expression, connection, context):
if value and isinstance(value, str):
value = value.decode("utf-8")
return value
View clone_schema.sql
-- Function: clone_schema(text, text)
-- DROP FUNCTION clone_schema(text, text);
CREATE OR REPLACE FUNCTION clone_schema(
source_schema text,
dest_schema text,
include_recs boolean)
RETURNS void AS
$BODY$
@amitu
amitu / index_usage_bad_ones.sql
Last active Jan 15, 2016
DB Performance - Index Usage - Bad Ones
View index_usage_bad_ones.sql
WITH query_stat AS (
SELECT
relname, 100 * idx_scan / (seq_scan + idx_scan) percent_of_times_index_used,
n_live_tup rows_in_table, idx_scan, seq_scan
FROM
pg_stat_user_tables
WHERE
seq_scan + idx_scan > 0
ORDER BY
seq_scan DESC
@amitu
amitu / manage.py
Created Nov 6, 2013
Find out why is something getting imported, as import fails in django sometimes leads to unhelpful "ImproperlyConfigured" exception
View manage.py
#!/usr/bin/env python
import sys, traceback
class ImportBlocker(object):
def find_module(self, fullname, path=None):
if "mobil" in fullname and "mobile" not in fullname:
print fullname
print traceback.print_stack()
raise Exception()
@amitu
amitu / load_keen.py
Last active Dec 24, 2015
Upload some test data to keen.io
View load_keen.py
# find the list of backup files
# for each file see the position till where we have read the file from "stack"
# for each file see if there is more in it.
# if there is more, start reading and writing to keen
# find total size of all files
# go thru in ls -t order so we always traverse them in same order
from progressbar import ProgressBar
import keen, pdb, json, time
@amitu
amitu / views.py
Created Sep 15, 2013
djangothis with labels
View views.py
from importd import d
from path import path
from datetime import datetime
from djangothis.app import dotslash, read_yaml, watchfile
from django.shortcuts import render
posts = None
labels = {}
@amitu
amitu / jdk7.sls
Created Sep 11, 2013
installing jdk7 via salt
View jdk7.sls
java7_ppa:
cmd.run:
- name: add-apt-repository ppa:webupd8team/java --yes && apt-get update
- user: root
accept-license:
cmd.run:
- name: echo oracle-java7-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
- unless: debconf-get-selections | grep -q shared/accepted-oracle-license-v1-1
- user: root
@amitu
amitu / debug.conf
Created Sep 10, 2013
Dummy plugin for logstash.
View debug.conf
input {
stdin {
type => "foo"
}
}
filter {
if [type] == "foo" {
foo {
message => "Hello world!"
}
View postgres_stats.sql
with table_stats as (
select psut.relname,
psut.n_live_tup,
1.0 * psut.idx_scan / greatest(1, psut.seq_scan + psut.idx_scan) as index_use_ratio
from pg_stat_user_tables psut
order by psut.n_live_tup desc
),
table_io as (
select psiut.relname,
sum(psiut.heap_blks_read) as table_page_read,
View luigi_example.py
import luigi
class SimpleTask(luigi.Task):
name = luigi.Parameter()
def output(self):
return luigi.LocalTarget(self.name)
def run(self):