From 66abe151b04a0e90daabef15c6572bdf43188b7a Mon Sep 17 00:00:00 2001 From: Lea Date: Fri, 20 Sep 2024 17:57:49 +0200 Subject: [PATCH] Optimize cache -> smaller number of requests by collecting more information --- arztapi/APIHandler.py | 47 +++++++++++++++++++++++++++++++++++--- arztapi/ArztPraxisDatas.py | 1 + 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/arztapi/APIHandler.py b/arztapi/APIHandler.py index c567c79..dae758a 100644 --- a/arztapi/APIHandler.py +++ b/arztapi/APIHandler.py @@ -63,6 +63,15 @@ class APIHandler: def get_list_of_doctors(self, lat, lon, req_val_base64, therapy_types, therapy_age, therapy_setting, amount_of_weeks) -> ArztPraxisDatas: + # Use the selected therapy types in case of "Verhaltenstherapie" -> most data used for it, might overload the + # request, so other therapy types are missing + if "V" in therapy_types: + selected_codes = therapy_types + + # Collect all three other therapy types in case of missing V + else: + selected_codes = ["A", "S", "T"] + # Data object as built by the original website, some fields might not be plausible or known (since there is no # API documentation itself available self.json_data = { @@ -81,7 +90,7 @@ class APIHandler: { "title": "Psychotherapie: Verfahren", "fieldName": "ptv", - "selectedCodes": therapy_types, + "selectedCodes": selected_codes }, { "title": "Psychotherapie: Altersgruppe", @@ -115,6 +124,9 @@ class APIHandler: self.get_list_of_doctors_from_api(lat, lon, req_val_base64, therapy_types, therapy_age, therapy_setting) self.set_current_doctor_information_data_in_cache() + # Filter for the relevant therapy times before processing + self.filter_for_therapy_types(therapy_types) + def get_list_of_doctors_from_api(self, lat, lon, req_val_base64, therapy_types, therapy_age, therapy_setting) -> ArztPraxisDatas: """ @@ -140,8 +152,6 @@ class APIHandler: "req-val": req_val_base64, } - - response = requests.post(api_path, headers=headers, json=self.json_data) # Check for HTTP errors response.raise_for_status() @@ -163,6 +173,37 @@ class APIHandler: current_date = datetime.now() self._cache[str(self.json_data)] = {"timestamp": current_date, "data": self.phone_times} + def filter_for_therapy_types(self, therapy_types): + """ + The idea is to get as much data as possible from the API at once to minimize the number of API calls. + For some cases, more data than actually necessary is cached and this is a filter for it. + :param therapy_types: Desired therapy types + :return: + """ + + # Mapping of the selected codes to the actual therapy type + mapping = { + "V": "Verhaltenstherapie", + "T": "Tiefenpsychologisch fundierte Psychotherapie", + "A": "Analytische Psychotherapie", + "S": "Systemische Therapie" + } + + # Store relevant phone times + relevant_phone_times = [] + + for data in self.phone_times.arztPraxisDatas: + # Relevant therapy type stored in psy + settings = data.psy + # Multiple therapy types might be available + for setting in settings: + # Check if the therapy type which is desired is available + if any(mapping[code] in setting for code in therapy_types): + relevant_phone_times.append(data) + + # Update with relevant phone times + self.phone_times.arztPraxisDatas = relevant_phone_times + def get_general_doctor_information(self) -> List[DoctorInformation]: """ Transform and filter data to more usable format: Check for phone times and collect general doctor information diff --git a/arztapi/ArztPraxisDatas.py b/arztapi/ArztPraxisDatas.py index d635c12..82dd467 100644 --- a/arztapi/ArztPraxisDatas.py +++ b/arztapi/ArztPraxisDatas.py @@ -45,6 +45,7 @@ class ArztPraxisData(BaseModel): keineSprechzeiten: bool ag: List[Ag] tsz: List[Tsz] + psy: List[str] class ArztPraxisDatas(BaseModel):