From 60a0f309d52959a07998f724ad2b22f22649edae Mon Sep 17 00:00:00 2001 From: ageryck Date: Tue, 17 Sep 2024 18:53:13 +0300 Subject: [PATCH] sketch tool to push data elememts to dhis2 --- dhis2/config.ini | 4 +++ dhis2/data_elements.csv | 25 +++++++++++++++ dhis2/main.py | 67 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 dhis2/config.ini create mode 100644 dhis2/data_elements.csv create mode 100644 dhis2/main.py diff --git a/dhis2/config.ini b/dhis2/config.ini new file mode 100644 index 00000000..efa54f31 --- /dev/null +++ b/dhis2/config.ini @@ -0,0 +1,4 @@ +[API] +endpoint_url = {{destination dhis2 instance}} +username = {{username}} +password = ************ diff --git a/dhis2/data_elements.csv b/dhis2/data_elements.csv new file mode 100644 index 00000000..36ddd196 --- /dev/null +++ b/dhis2/data_elements.csv @@ -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 diff --git a/dhis2/main.py b/dhis2/main.py new file mode 100644 index 00000000..ad89d426 --- /dev/null +++ b/dhis2/main.py @@ -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()