From 4337604286557f03d7b1450c2bf31e68e14f5499 Mon Sep 17 00:00:00 2001 From: nitusima Date: Tue, 16 Jul 2024 14:04:04 +0300 Subject: [PATCH] . --- code/dihlibs/data/df_update_table.sql | 20 +++++++ .../data/dhis_templates/category_options.json | 27 ++++++++++ .../data/dhis_templates/data_element.json | 22 ++++++++ .../dihlibs/data/dhis_templates/data_set.json | 53 +++++++++++++++++++ code/dihlibs/db.py | 3 ++ code/dihlibs/functions.py | 8 +-- 6 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 code/dihlibs/data/df_update_table.sql create mode 100644 code/dihlibs/data/dhis_templates/category_options.json create mode 100644 code/dihlibs/data/dhis_templates/data_element.json create mode 100644 code/dihlibs/data/dhis_templates/data_set.json diff --git a/code/dihlibs/data/df_update_table.sql b/code/dihlibs/data/df_update_table.sql new file mode 100644 index 0000000..6209591 --- /dev/null +++ b/code/dihlibs/data/df_update_table.sql @@ -0,0 +1,20 @@ +WITH temp AS ( + SELECT * FROM ( VALUES + {values} + ) AS t({columns}) +) +UPDATE {tablename} AS u_table +SET + {set_columns} +FROM temp +WHERE u_table.{id_column}=temp.{id_column} ; +WITH temp AS ( + SELECT * FROM ( VALUES + {values} + ) AS t({columns}) +) +INSERT INTO {tablename} ({columns}) +SELECT {update_columns} +FROM temp +LEFT JOIN {tablename} AS u_table ON u_table.{id_column} = temp.{id_column} +WHERE u_table.{id_column} IS NULL; \ No newline at end of file diff --git a/code/dihlibs/data/dhis_templates/category_options.json b/code/dihlibs/data/dhis_templates/category_options.json new file mode 100644 index 0000000..8c579c9 --- /dev/null +++ b/code/dihlibs/data/dhis_templates/category_options.json @@ -0,0 +1,27 @@ +[{ + "shortName": "{change}", + "id": "{change}", + "displayShortName": "{change}", + "displayName": "{change}", + "name": "{change}", + "externalAccess": false, + "publicAccess": "rwr-----", + "userGroupAccesses": [], + "userAccesses": [], + "access": { + "manage": true, + "externalize": true, + "write": true, + "read": true, + "update": true, + "delete": true, + "data": { + "write": true, + "read": true + } + }, + "favorites": [], + "dimensionItemType": "CATEGORY_OPTION", + "favorite": false, + "isDefault": false +}] diff --git a/code/dihlibs/data/dhis_templates/data_element.json b/code/dihlibs/data/dhis_templates/data_element.json new file mode 100644 index 0000000..9750c77 --- /dev/null +++ b/code/dihlibs/data/dhis_templates/data_element.json @@ -0,0 +1,22 @@ +[{ + "id": "{id}", + "name": "{name}", + "shortName": "{short_name}", + "aggregationType": "SUM", + "domainType": "AGGREGATE", + "publicAccess": "rw------", + "externalAccess": false, + "valueType": "INTEGER_ZERO_OR_POSITIVE", + "zeroIsSignificant": true, + "favorite": false, + "dimensionItemType": "DATA_ELEMENT", + "optionSetValue": false, + "access": { + "read": true, + "update": true, + "externalize": true, + "delete": true, + "write": true, + "manage": true + } +}] diff --git a/code/dihlibs/data/dhis_templates/data_set.json b/code/dihlibs/data/dhis_templates/data_set.json new file mode 100644 index 0000000..80d51cd --- /dev/null +++ b/code/dihlibs/data/dhis_templates/data_set.json @@ -0,0 +1,53 @@ +{ + "name": "{name}", + "translations": [], + "externalAccess": false, + "publicAccess": "rw------", + "userGroupAccesses": [], + "userAccesses": [], + "access": { + "manage": true, + "externalize": true, + "write": true, + "read": true, + "update": true, + "delete": true, + "data": { + "write": true, + "read": true + } + }, + "favorites": [], + "shortName": "{short_name}", + "dimensionItemType": "REPORTING_RATE", + "legendSets": [], + "periodType": "Monthly", + "dataInputPeriods": [], + "dataSetElements": [], + "indicators": [], + "compulsoryDataElementOperands": [], + "sections": [], + "mobile": false, + "version": 7, + "expiryDays": 0, + "timelyDays": 15, + "notifyCompletingUser": false, + "interpretations": [], + "openFuturePeriods": 0, + "openPeriodsAfterCoEndDate": 0, + "fieldCombinationRequired": false, + "validCompleteOnly": false, + "noValueRequiresComment": false, + "skipOffline": false, + "dataElementDecoration": false, + "renderAsTabs": false, + "renderHorizontally": false, + "compulsoryFieldsCompleteOnly": false, + "formType": "DEFAULT", + "displayName": "{name}", + "favorite": false, + "displayShortName": "{short_name}", + "displayFormName": "{short_name", + "id": "{id}", + "attributeValues": [], + "organisationUnits": [] } diff --git a/code/dihlibs/db.py b/code/dihlibs/db.py index 2db4477..9745b5c 100644 --- a/code/dihlibs/db.py +++ b/code/dihlibs/db.py @@ -110,6 +110,9 @@ def select_part_matview(self, sql_file): def quote_columns_names(self, names): return [f'"{n}"' if " " in n else n for n in names] + def ssh_upate_table_df(self, df, tablename, id_column="id"): + return self.ssh_run( self.upate_table_df,df,tablename,id_column) + def upate_table_df(self, df, tablename, id_column="id"): db_columns = self.quote_columns_names(df.columns) columns = ",".join(db_columns) diff --git a/code/dihlibs/functions.py b/code/dihlibs/functions.py index ec1e38f..551ed2a 100644 --- a/code/dihlibs/functions.py +++ b/code/dihlibs/functions.py @@ -83,13 +83,13 @@ def change(item): return walk(obj, change) def get_month(delta): - ve = 1 if delta > 0 else -1 - x = datetime.today() + ve * relativedelta(months=abs(delta)) + sign = 1 if delta > 0 else -1 + x = datetime.today() + sign * relativedelta(months=abs(delta)) return x.replace(day=1).strftime("%Y-%m-01") def days_delta(delta): - ve = 1 if delta > 0 else -1 - x = datetime.today() + ve * relativedelta(days=abs(delta)) + sign = 1 if delta > 0 else -1 + x = datetime.today() + sign * relativedelta(days=abs(delta)) return x.strftime(r"%Y-%m-%d") def file_binary(file_name):