Skip to content

Instantly share code, notes, and snippets.

@vinayvenu
vinayvenu / sql_update.sql
Created March 28, 2023 04:20
How to write a sql update script
-- Ticket 2492
-- So there is an encounter called annual visit base line, where there is a question called "Going to school" with answers as 'Yes' or 'Dropped out'
-- Earlier we didn't schedule any visits for students who were selected dropped out, but now they came back saying they want to schedule annual visit endline even when the adolescent has selected dropped out as answer
set role adsr;
select * from concept where uuid = '9705f6ad-50e1-4179-aa60-922014d7cc3c'; -- Going to school
select * from concept where uuid = 'fb1080b4-d1ec-4c87-a10d-3838ba9abc5b'; -- Dropped out
select * from encounter_type where id = 1343; -- Annual Visit - Baseline
select * from encounter_type where id = 1349; -- Annual Visit - Endline
@vinayvenu
vinayvenu / MediaZipFile.md
Last active March 22, 2023 10:01
Media Zip download file structure

Generic Structure of the media zip file

Location (Multiple levels if necessary)
	SubjectName
		ProgramName (If present)
			ProgramEncounterName (If present)
				ConceptName (Multiple concepts can be chosen. Also, a concept can have multiple images)
@vinayvenu
vinayvenu / gist:4249e6be2c682555efdb0ac65dbaab3d
Created February 17, 2023 09:45
Some sql gotchas, and performance analysis
-- The objective of this session is to look at performance,
reset role;
select * from individual where observations->>'24dabc3a-6562-4521-bd42-5fff11ea5c46' = '294';
set role ihmp;
select * from individual where observations->>'24dabc3a-6562-4521-bd42-5fff11ea5c46' = '294';-- column does not exist
-- Pitfall. Use the schema name everywhere (For avni-server, you don't have to do this for public schemas because of application.properties)
select * from public.individual where observations->>'24dabc3a-6562-4521-bd42-5fff11ea5c46' = '294';-- column does not exist
reset role;
@vinayvenu
vinayvenu / getSyncDetails.sql
Last active February 16, 2023 04:47
Improving performance of the /getSyncDetails call
-- Improving /getSyncDetails
-- Current scenario - POST /getSyncDetails call is responsible for about 15% of our workload in our server. Average response times are at 4.17 seconds with 95% at 9.75 seconds. Throughput can go upto 10-15 rpm during peak (which is pretty bad given the response times).
-- We have already stopped checking values for programEnrolment, programEncounter and encounter to make response times better. This is resulting in extra calls to these endpoints.
-- One way to handle this can be to have a view that provides the latest update date time for each kind of entity to be synced. We should expect about 100,000 rows in this new table for all organisations together. Retrieving data will be a small number of queries, and will be fast because of indexing (not done in this gist).
-- This sql provides a POC of such a solution that might work
reset role;
drop table if exists sync_statistics;
create table sync_statistics as
@vinayvenu
vinayvenu / multitenancy_and_reporting_evolution.sql
Last active February 10, 2023 06:17
Avni Sessions Part 1 - Multitenancy, Reporting evolution
-- This script lets you follow the first session of the Avni Sessions series. It is accompanied by the presentation at https://docs.google.com/presentation/d/1j6CH9xsBJ2QKt4B3JhJeklBrL5Ju1dPNYjsMVufZ1yk/edit?usp=sharing
------------------------------------
-- MULTITENANCY IN AVNI - START
------------------------------------
-- Multitenancy. We use Postgres Row-level multitenancy (https://www.postgresql.org/docs/current/ddl-rowsecurity.html) to prevent organisations from using each other's data. In Avni, we have a multi-level inheritance style structure that is enabled through a hierarchy of organisations. This hierarchy has two mechanisms.
-- Multitenancy mechanism 1 - Extending an organisation. We have parent and child organisations that share metadata (not data). Lets see this with an example - Calcutta Kids
set role calcutta_kids;
@vinayvenu
vinayvenu / bahmni_setup_backup
Created March 31, 2021 14:28
Set up backup for Bahmni
sudo su
vi /etc/systemd/system/multi-user.target.wants/openmrs.service
# Change user and group from bahmni to root
systemctl daemon-reload
# Unlink /var/run so that systemctl runs
unlink /var/run/openmrs
# By now, we should be able to run openmrs using systemctl
### This gist provides the details of fixing bahmni-reports once you have installed Bahmni.
The reports database requires a bunch of tables required by the scheduler. Even if you don't run the scheduler, the database is required for bahmni-reports to run.
### Symptoms
Go to reports, and click on a report. It take a lot of time, and ends up throwing an exception
Missing database exception on /var/log/bahmni-reports
### Solution
Create the database and the required tables.
@vinayvenu
vinayvenu / gist:3407181434cd483c26d4ef8f719218e0
Last active July 2, 2018 06:51
Stuff to remember when testing forms
Ensure counseling questions show up inline wherever possible.
Ensure all counseling uses the "Placeholder for counselling form element" concept
For program encounters, check if the questions need to be asked every time. If not, add a rule to ask just once.
@vinayvenu
vinayvenu / gist:922669a8224bb8cc614db4268eb5d54b
Created May 30, 2018 11:26
Sample log format for OpenCHS
2018-05-30 16:55:26.602 INFO 37343 --- [nio-8021-exec-6] o.o.f.security.AuthenticationFilter : Processing GET /userInfo?catchmentId=1 User: null, Organisation: Ashwini
2018-05-30 16:55:26.607 INFO 37343 --- [nio-8021-exec-6] o.o.f.security.AuthenticationFilter : Processed GET /userInfo?catchmentId=1 User: null, Organisation: Ashwini
2018-05-30 16:55:26.685 INFO 37343 --- [nio-8021-exec-7] o.o.f.security.AuthenticationFilter : Processing POST /individuals?null User: null, Organisation: Ashwini
2018-05-30 16:55:26.706 INFO 37343 --- [nio-8021-exec-7] org.openchs.web.IndividualController : Saving individual with UUID a1769544-4e7f-4ff7-9d26-9a3f9b4c7b3a
2018-05-30 16:55:26.826 INFO 37343 --- [nio-8021-exec-7] org.openchs.web.IndividualController : Saved individual with UUID a1769544-4e7f-4ff7-9d26-9a3f9b4c7b3a
2018-05-30 16:55:26.840 INFO 37343 --- [nio-8021-exec-7] o.o.f.security.AuthenticationFilter : Processed POST /individuals?null User: null, Organisation: Ashwini
2018-05-3
#!/bin/bash
service bahmni-lab stop
service openmrs stop
psql -Upostgres -c "drop database clinlims;"
cd /opt/bahmni-lab/migrations && scripts/initDB.sh bahmni-base.dump
cd /opt/bahmni-lab/migrations/liquibase/ && /opt/bahmni-lab/migrations/scripts/migrateDb.sh
service bahmni-lab start