Skip to content

Commit

Permalink
Keep extra supported programs data
Browse files Browse the repository at this point in the history
  • Loading branch information
dborowiecki committed Jul 1, 2024
1 parent e5ebfcd commit 672e1ac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def _add_supported_program_info_to_df(self, df):
add_info = udf(
lambda supported_programs: json.dumps(
{
entry["code"]: code_id_dict.get(entry["code"], None)
entry["code"]: {"id": code_id_dict.get(entry["code"], None)}
for entry in supported_programs
if entry["code"] in code_id_dict
}
Expand Down Expand Up @@ -249,8 +249,14 @@ def format_payload_f(self):
"enabled": enabled,
"openLmisAccessible": enabled,
"supportedPrograms": [
{"id": program_id}
for program_id in json.loads(supported_programs).values()
{
"id": data['id'],
"supportActive": data.get('supportActive', True),
"supportLocallyFulfilled": data.get('supportLocallyFulfilled', False),
"supportStartDate": data.get("supportStartDate")

}
for data in json.loads(supported_programs).values() if 'id' in data.keys()
],
}
)
Expand Down Expand Up @@ -387,6 +393,7 @@ def compare_for_any_change(df, col1, col2):
result_df = result_df.filter(col("any_change") == True)[
["payload", "existing_facilities.id"]
]

for row in result_df.collect():
self._update_request(row)

Expand Down
15 changes: 12 additions & 3 deletions sigeca_data_import_microservice/app/domain/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ def get_all(self):
class FacilityResourceRepository(BaseResourceRepository):
def get_all(self):
query = """(SELECT f.*,
(case when count(p.code)= 0 then '{}'::jsonb
else jsonb_object_agg(coalesce(p.code, 'undefined'), p.id)
end) as supported_programs
(CASE
WHEN count(p.code) = 0 THEN '{}'::jsonb
ELSE jsonb_object_agg(
coalesce(p.code, 'undefined'),
jsonb_build_object(
'id', p.id,
'supportActive', sp.active,
'supportLocallyFulfilled', sp.locallyfulfilled,
'supportStartDate', sp.startdate
)
)
END) AS supported_programs
FROM referencedata.facilities f
LEFT JOIN referencedata.supported_programs sp ON sp.facilityid = f.id
LEFT JOIN referencedata.programs p ON sp.programid = p.id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

logger = logging.getLogger(__name__)


class JDBCReader:
def __init__(self, config: Any):
"""
Expand Down Expand Up @@ -45,7 +46,9 @@ def setup_ssh_tunnel(self):
local_bind_address=("127.0.0.1", self.config.get("local_bind_port", 5432)),
)
self.tunnel.start()
logger.info(f"SSH Tunnel established on local port {self.tunnel.local_bind_port}")
logger.info(
f"SSH Tunnel established on local port {self.tunnel.local_bind_port}"
)

def close_ssh_tunnel(self):
if self.tunnel:
Expand All @@ -65,7 +68,7 @@ def read_data(self, query) -> DataFrame:
)
else:
local_jdbc_url = self.config["jdbc_url"]

data_frame = (
self.spark.read.format("jdbc")
.option("url", local_jdbc_url)
Expand Down

0 comments on commit 672e1ac

Please sign in to comment.