Skip to content

Instantly share code, notes, and snippets.

@mrjones-plip
Last active December 11, 2024 18:14
Show Gist options
  • Save mrjones-plip/eba1af086e3f6c52deaf9688a373b15c to your computer and use it in GitHub Desktop.
Save mrjones-plip/eba1af086e3f6c52deaf9688a373b15c to your computer and use it in GitHub Desktop.
count d_community_health_volunteer_area and count by YEAR-MONTH

CHT Core user creation count by year-month

This script takes a series of CHT Core instances and loops through each one fetching all contacts that are type d_community_health_volunteer_area. It then loops throught the results and counts the number of contacts create each month. It assumes each instance is a "County

Runing

  1. download files in this gist
  2. create a new file allInstances.js with the contents of this entry in 1password ("All eCHIS KE instances in a JS object" is the entry name)
  3. run npm install
  4. run node get-cha-counts.js
  5. results found in cha_report.csv

allInstances.js format

For an example, the script assumes this format:

export const allInstances = [
    {
        county: 'the-small-one',
        url: 'county1-cht.example.org',
        password: 'super-secret'
    },
    {
        county: 'the-big-one',
        url: 'county2-cht.example.org',
        password: 'super-d00p3r-secret'
    },
 ]
/home/mrjones/.nvm/versions/node/v22.11.0/bin/node /home/mrjones/Documents/MedicMobile/app-services-team/fetch-users/index.js
Fetching CHAs for snyamira
Fetching CHAs for samburu
Fetching CHAs for baringo
Fetching CHAs for bomet
Fetching CHAs for bungoma
Fetching CHAs for busia
Fetching CHAs for elgeyomarakwet
Fetching CHAs for embu
Fetching CHAs for homabay
Fetching CHAs for kajiado
Fetching CHAs for kericho
Fetching CHAs for kiambu
Fetching CHAs for kirinyaga
Fetching CHAs for kisii
Fetching CHAs for kwale
Fetching CHAs for laikipia
Fetching CHAs for lamu
Fetching CHAs for makueni
Fetching CHAs for mandera
Fetching CHAs for marsabit
Fetching CHAs for meru
Fetching CHAs for mombasa
Fetching CHAs for muranga
Fetching CHAs for nairobi
Fetching CHAs for nandi
Fetching CHAs for narok
Fetching CHAs for nyandarua
Fetching CHAs for taitataveta
Fetching CHAs for tanariver
Fetching CHAs for tharakanithi
Fetching CHAs for transnzoia
Fetching CHAs for uasingishu
Fetching CHAs for wajir
Fetching CHAs for nyamira
Fetching CHAs for garissa
Fetching CHAs for isiolo
Fetching CHAs for kakamega
Fetching CHAs for kilifi
Fetching CHAs for kisumu
Fetching CHAs for kitui
Fetching CHAs for machakos
Fetching CHAs for migori
Fetching CHAs for nakuru
Fetching CHAs for nyeri
Fetching CHAs for turkana
Fetching CHAs for uasingishu
Fetching CHAs for vihiga
(node:6605) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Completed lamu which is 1 out of 47 counties...
Completed nyandarua which is 2 out of 47 counties...
Completed kirinyaga which is 3 out of 47 counties...
Completed wajir which is 4 out of 47 counties...
Completed samburu which is 5 out of 47 counties...
Completed isiolo which is 6 out of 47 counties...
Completed mandera which is 7 out of 47 counties...
Completed vihiga which is 8 out of 47 counties...
Completed laikipia which is 9 out of 47 counties...
Completed tharakanithi which is 10 out of 47 counties...
Completed narok which is 11 out of 47 counties...
Completed homabay which is 12 out of 47 counties...
Completed marsabit which is 13 out of 47 counties...
Completed kajiado which is 14 out of 47 counties...
Completed taitataveta which is 15 out of 47 counties...
Completed elgeyomarakwet which is 16 out of 47 counties...
Completed machakos which is 17 out of 47 counties...
Completed uasingishu which is 18 out of 47 counties...
Completed tanariver which is 19 out of 47 counties...
Completed garissa which is 20 out of 47 counties...
Completed uasingishu which is 21 out of 47 counties...
Completed turkana which is 22 out of 47 counties...
Completed snyamira which is 23 out of 47 counties...
Completed mombasa which is 24 out of 47 counties...
Completed nyamira which is 25 out of 47 counties...
Completed muranga which is 26 out of 47 counties...
Completed embu which is 27 out of 47 counties...
Completed nandi which is 28 out of 47 counties...
Completed kisumu which is 29 out of 47 counties...
Completed kwale which is 30 out of 47 counties...
Completed nyeri which is 31 out of 47 counties...
Completed transnzoia which is 32 out of 47 counties...
Completed baringo which is 33 out of 47 counties...
Completed kitui which is 34 out of 47 counties...
Completed nakuru which is 35 out of 47 counties...
Completed kiambu which is 36 out of 47 counties...
Completed makueni which is 37 out of 47 counties...
Completed kakamega which is 38 out of 47 counties...
Completed kilifi which is 39 out of 47 counties...
Completed meru which is 40 out of 47 counties...
Completed migori which is 41 out of 47 counties...
Completed bungoma which is 42 out of 47 counties...
Completed busia which is 43 out of 47 counties...
Completed kericho which is 44 out of 47 counties...
Completed kisii which is 45 out of 47 counties...
Completed nairobi which is 46 out of 47 counties...
Report complete - see cha_report.csv
Process finished with exit code 0
const PouchDB = require('pouchdb-core');
PouchDB.plugin(require('pouchdb-adapter-http'));
PouchDB.plugin(require('pouchdb-find'));
PouchDB.plugin(require('pouchdb-mapreduce'));
// see "All eCHIS KE instances in a JS object" in 1password for contents of allInstances
const {allInstances} = require("./allInstances");
const USER = "medic";
const fetch = (url, opts) => PouchDB.fetch(url, opts);
const stringifySafe = (obj) => jsonStringifySafe(obj, null, 2, () => undefined);
const jsonStringifySafe = require('json-stringify-safe');
const fs = require('fs');
const counts = {}
counts.completed = 0;
const getUsers = async (county, url, password) => {
const SERVER_URL = "https://" + USER + ":" + password + "@" + url + "/medic";
const db = new PouchDB(SERVER_URL, { fetch });
console.log(`Fetching CHAs for ${county}`);
const response = await db.query(
'medic-client/contacts_by_type',
{
key: ["d_community_health_volunteer_area"],
include_docs: true
}
);
response.rows
.filter(r => {
// todo - not return years in future?
return !!(r.doc && r.doc.contact && r.doc.contact._id);
})
.map(r => {
const reported_date = r.doc.reported_date;
const date = new Date(reported_date);
const year_month = date.getFullYear() +
'-' +
(date.getMonth() + 1).toString().padStart(2, '0');
if(counts[county][year_month]){
counts[county][year_month]++
} else {
counts[county][year_month] = 1;
}
});
outputWhenComplete(county);
};
const fetchAll = async () => {
allInstances.map(instance =>{
counts[instance.county] = [];
getUsers(instance.county, instance.url, instance.password);
});
}
const outputWhenComplete = async (county) => {
counts.completed++;
if (counts.completed === Object.keys(allInstances).length){
const csv_rows = []
allInstances.map(instance =>{
// thanks https://stackoverflow.com/a/9658712 !
Object.keys(counts[instance.county])
.sort()
.forEach(function(v, i) {
csv_rows.push(`${instance.county},${v},${counts[instance.county][v]}`);
});
});
fs.writeFileSync('cha_report.csv', csv_rows.join('\n'));
console.log("Report complete - see cha_report.csv")
} else {
console.log("Completed",county, "which is",counts.completed, "out of", Object.keys(allInstances).length,"counties...")
}
}
fetchAll();
{
"name": "machakos-chas",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"json-stringify-safe": "^5.0.1",
"pouchdb-adapter-http": "^9.0.0",
"pouchdb-core": "^9.0.0",
"pouchdb-find": "^9.0.0",
"pouchdb-mapreduce": "^9.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment