Skip to content

Instantly share code, notes, and snippets.

View sravfeyn's full-sized avatar

Sravan Reddy sravfeyn

View GitHub Profile
import io, csv
from dimagi.utils.chunked import chunked
from custom.icds_reports.tasks import _get_docs_in_ucr
from custom.icds_reports.const import DASHBOARD_DOMAIN
from corehq.util.log import send_HTML_email
from corehq.apps.es.forms import FormES
UCR_MAPPING = {
"XFormInstance": {
"static-usage_forms": [
from corehq.pillows.mappings.xform_mapping import XFORM_INDEX
from corehq.pillows.mappings.reportxform_mapping import REPORT_XFORM_INDEX_INFO
from corehq.pillows.mappings.user_mapping import USER_INDEX
from pillowtop.models import KafkaCheckpoint
def copy_checkpoints(pillow_name):
for p in KafkaCheckpoint.objects.filter(checkpoint_id__startswith=pillow_name).all():
checkpoint_id = "{}-{}-{}-{}".format(
pillow_name, XFORM_INDEX, REPORT_XFORM_INDEX_INFO.index, USER_INDEX)
p.pk = None
In [31]: def iter_sql(form_ids):
...: with TimingContext() as timer:
...: for f in FormAccessors('icds-cas').iter_forms(form_ids):
...: f.to_json()
...: return timer
...:
In [32]: def iter_es(form_ids):
...: with TimingContext() as timer:
...: list(FormES().form_ids(form_ids).run().hits)
from datetime import datetime, timedelta, time
ALLOWABLE_DIFFERENCE = 10000000 # 1M
MIN_DAYS_APART = 3
def split_dates(from_date, num_dates):
dates = [from_date]
to = datetime.now().date()
total_size = get_data_size(from_date, to)
group_size = total_size / num_dates
@sravfeyn
sravfeyn / .py
Created May 15, 2019 11:44
Get mobile workers in all domains under a BillingAccount
from corehq.apps.accounting.models import BillingAccount, Subscription, DomainUserHistory
def get_subscribed_domains(account_id):
act = BillingAccount.objects.get(id=account_id)
domains = []
for sub in Subscription.visible_objects.filter(account=act).all():
domains = domains + [s.subscriber.domain for s in sub.account.subscription_set.filter(plan_version=sub.plan_version)]
return set(domains)
@sravfeyn
sravfeyn / gist:fb7a04e85e458c37649c295c8323d3f8
Created February 8, 2019 10:52
dd_pg_connections_by_client.py
import subprocess
import socket
def send_pg_connection_count():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
out = subprocess.Popen("PGPASSWORD={{postgres_users.commcare.password}} psql -h localhost -p 6432 -U {{postgres_users.commcare.username}} pgbouncer -c 'show clients' | cut -d'|' -f 5 | tail -n +4 | sort | uniq -c", shell=True, stdout=subprocess.PIPE)
output = out.stdout.read()
lines = output.split('\n')
lines = lines[1:-3]
for line in lines:
-------------------------------------------------------------------------
Sort (cost=429351.94..429504.45 rows=61004 width=3448) (actual time=157.890..157.908 rows=2 loops=1)
Sort Key: cte._cte_ordering
Sort Method: quicksort Memory: 26kB
CTE cte
-> Recursive Union (cost=0.55..332787.08 rows=61004 width=281) (actual time=0.050..157.786 rows=2 loops=1)
-> Index Scan using locations_sqllocation_parent_id on locations_sqllocation (cost=0.55..836.32 rows=174 width=281) (actual time=0.031..0.050 rows=2 loops=1)
Index Cond: (parent_id = 903423)
-> Merge Join (cost=32947.09..33073.07 rows=6083 width=281) (actual time=157.673..157.673 rows=0 loops=1)
Merge Cond: (cte_1.id = locations_sqllocation_1.parent_id)
Sort (cost=32261397.90..32271171.84 rows=3909574 width=3448) (actual time=123230.226..123230.244 rows=2 loops=1)
Sort Key: cte._cte_ordering
Sort Method: quicksort Memory: 26kB
CTE cte
-> Recursive Union (cost=0.55..14356761.83 rows=3909574 width=281) (actual time=0.391..123229.962 rows=2 loops
=1)
-> Index Scan using locations_sqllocation_parent_id on locations_sqllocation (cost=0.55..836.32 rows=174 width=281) (actual time=0.356..0.376 rows=2 loops=1)
Index Cond: (parent_id = 903423)
-> Hash Join (cost=1385266.53..1427773.40 rows=390940 width=281) (actual time=123229.508..123229.508 rows=0 loops=1)
Hash Cond: (cte_1.id = locations_sqllocation_1.parent_id)
def ucr_summary(domain):
from corehq.apps.userreports.models import DataSourceConfiguration, StaticDataSourceConfiguration
deid_fields = ["hh_gps_location","phone_number","contact_phone_number","date_primary_admit","aadhar_number","mcts_id","dob","raw_aadhar_string","mcp_id","rch_id","date_death","date_last_private_admit","bank_account_number","referral_reached_date","time_birth","phone_number","contact_phone_number","date_primary_admit","aadhar_number","mcts_id","dob","raw_aadhar_string","mcp_id","rch_id","date_death","date_last_private_admit","bank_account_number","referral_reached_date","time_birth","phone_number","helpdesk_phone_number","dob_aww","contact_phone_number","ls_name","ls_phone_number","dimagi_username","email"]
data_sources = list(DataSourceConfiguration.by_domain(domain)) + list(StaticDataSourceConfiguration.by_domain(domain))
def get_deid_field(indicator):
if 'name' in indicator.get('column_id'):
return 'name'
expression = str(indicator.get('expres
@sravfeyn
sravfeyn / audioInVellum.js
Last active July 5, 2016 12:09
Audio playback in Formbuilder
/*
This script will let you listen to audio recording of formbuilder questions by simply clicking on the question text (without you having to open the audio link in a new tab for each question). This is helpful while reviewing audio recordings for correctness.
# How to use
- Open your form in formbuilder
- Right Click anywhere and chose 'Inspect'. On chrome a developer tools tab will open
- In developers tools, click on console
- Edit the script below as following and copy it onto console