Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sketch tool to push data elememts to dhis2 #288

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dhis2/config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[API]
endpoint_url = {{destination dhis2 instance}}
username = {{username}}
password = ************
25 changes: 25 additions & 0 deletions dhis2/data_elements.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name,shortName,aggregationType,domainType,periodOffset,valueType,dimensionItemType
Number of BCG vaccinations administered,eir_BCG,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of OPV 0 vaccinations administered,eir_OPV0,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of OPV 1 vaccinations administered,eir_OPV1,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of OPV 2 vaccinations administered,eir_OPV2,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of OPV 3 vaccinations administered,eir_OPV3,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of IPV vaccinations administered,eir_IPV,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of DTP-HepB-Hib 1 vaccinations administered,eir_Penta1,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of DTP-HepB-Hib 2 vaccinations administered,eir_Penta2,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of DTP-HepB-Hib 3 vaccinations administered,eir_Penta3,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of PCV 1 vaccinations administered,eir_PCV1,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of PCV 2 vaccinations administered,eir_PCV2,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of Rota 1 vaccinations administered,eir_Rota1,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of Rota 2 vaccinations administered,eir_Rota2,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of Measles 1 vaccinations administered,eir_MR1,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of Measles 2 vaccinations administered,eir_MR2,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of HPV 1 vaccinations administered,eir_HPV1,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of HPV 2 vaccinations administered,eir_HPV2,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of Vitamin A vaccinations administered to children between 6 & 11 months,eir_vitA_6_11_months,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of Vitamin A vaccinations administered to children between 12 & 59 months,eir_vitA_12_59_months,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
The number of children who missed out on a vaccination(s) for the reporting period/Total number of children,eir_missed_stockout,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
The number of children who have completed all doses of the vaccines as per the full vaccination schedule/Total number of children registered in the application within the same age range,eir_fully_vaccinated,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of reported adverse events after receiving a vaccine dose/The total number of doses administered to patients of the product.,eir_AEFI,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
The number of first doses of measles containing vaccine administered during reporting period minus the number of last doses of measles containing vaccine during the report period/Number of first doses of measles containing vaccine administered during the reporting period.,eir_dropout_MR1_MR2,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
Number of administrations of Penta1 (DTP1+HepB1+Hib1) administered during reporting period minus the number of administrations of Penta3 (DTP3+HepB3+Hib3) administered during report period/Number of doses of Penta 1 (DTP1+HepB1+Hib1) administered.,eir_dropout_Penta1_Penta3,SUM,AGGREGATE,0,NUMBER,DATA_ELEMENT
67 changes: 67 additions & 0 deletions dhis2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import csv
import json
import requests
from configparser import ConfigParser
from requests.auth import HTTPBasicAuth

# Read the config file to get API credentials and endpoint URL
def read_config(config_file='config.ini'):
config = ConfigParser()
config.read(config_file)
endpoint_url = config.get('API', 'endpoint_url')
username = config.get('API', 'username')
password = config.get('API', 'password')
return endpoint_url, username, password


# # Function to read CSV and convert to valid JSON format
# def csv_to_json(csv_file):
# data_list = []
# with open(csv_file, mode='r') as file:
# csv_reader = csv.reader(file)
# headers = next(csv_reader) # First row as headers
# for row in csv_reader:
# data_list.append(dict(zip(headers, row)))
#
# # Convert list of dictionaries to a JSON string with double quotes
# json_data = json.dumps(data_list, indent=4)
# return json_data

# Read CSV and format it into JSON
def csv_to_json(csv_file):
data_list = []
with open(csv_file, mode='r') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
data_list.append(row)
#json_data = json.dumps(data_list, indent=4)
return data_list

# Post data to the endpoint using Basic Auth
def post_data(endpoint_url, username, password, data_entry):
headers = {'Content-Type': 'application/json'}
post_payload = json.dumps(data_entry) # Convert each dictionary to JSON before sending
print(f"This is the request body: {post_payload}")
response = requests.post(endpoint_url, auth=HTTPBasicAuth(username, password), headers=headers, data=post_payload)
return response

# Main function to execute the logic
def main():
# Read config
endpoint_url, username, password = read_config()

# Convert CSV to JSON
csv_file = 'data_elements.csv' # Path to your CSV file
json_data = csv_to_json(csv_file)
print(json_data)
# Post each row to the endpoint
for entry in json_data:
#print(f"This is a single entry: {entry}")
response = post_data(endpoint_url, username, password, entry)
if response.status_code == 201:
print(f"Success: {entry}")
else:
print(f"Failed to post: {entry}. \n Fail Status Code: {response.status_code}")

if __name__ == "__main__":
main()