diff --git a/.coverage b/.coverage index 8ccad4cf530884d0d7e09dae50f7d7ef537febf1..ad95739423016a104fb28ab751d20e77ca1b4504 100644 Binary files a/.coverage and b/.coverage differ diff --git a/server/main.py b/server/main.py index c7a1691335048990fa94fdb1bffcfa6e0eace208..6c55584e77bb74be07dcd0d18272a62d8b02c0b2 100644 --- a/server/main.py +++ b/server/main.py @@ -11,7 +11,7 @@ from server.scheduler import update_scheduler from server.consts import LAKE_RELATIONS_PATH from map_handler.get_lake_relation import get_map_data_handler from map_handler.input_new_data import input_new_Lidar_data -from map_handler.update_measurements import update_measurements, addTestData +from map_handler.update_measurements import update_measurements_handler, addTestData app = Flask(__name__) terminate_server = 0 @@ -82,7 +82,7 @@ class IceHTTP(BaseHTTPRequestHandler): lake_name = unquote(lake_name_param) # Decode url param if lake_name_param: - update_measurements(self, lake_name) + update_measurements_handler(self, lake_name) else: self.send_response(400) self.send_header('Content-type', 'application/json') diff --git a/server/map_handler/__pycache__/add_new_lake.cpython-311.pyc b/server/map_handler/__pycache__/add_new_lake.cpython-311.pyc index 8dbe5c4c5269ae0daa3a59b6220662c181919014..8d3750ebdf174db610c11549b96840a0d5519932 100644 Binary files a/server/map_handler/__pycache__/add_new_lake.cpython-311.pyc and b/server/map_handler/__pycache__/add_new_lake.cpython-311.pyc differ diff --git a/server/map_handler/__pycache__/update_measurements.cpython-311.pyc b/server/map_handler/__pycache__/update_measurements.cpython-311.pyc index 37757e8e863f0fd6f0235db5e18ff3def83dc6d6..b2894fcaa2808ca853647bccd7b4c454f79b8146 100644 Binary files a/server/map_handler/__pycache__/update_measurements.cpython-311.pyc and b/server/map_handler/__pycache__/update_measurements.cpython-311.pyc differ diff --git a/server/map_handler/add_new_lake.py b/server/map_handler/add_new_lake.py index 04f44b727196dea9c8e120ae87485ababa1afd49..58deadc6807b21d146ad2f9c739c73a3eac3c292 100644 --- a/server/map_handler/add_new_lake.py +++ b/server/map_handler/add_new_lake.py @@ -129,10 +129,8 @@ def cut_map(cursor, lake_name: str, cell_size_in_km: float = 0.5) -> (int, str): return 200, feature_collection except FileNotFoundError as e: - print(f"Failed to find the map file: {e}") return 404, f"Failed to find the map file: {e}" except Exception as e: - print(f"Error in adding new map: {e}") return 500, f"Error in adding new map: {e}" diff --git a/server/map_handler/lake_relations/test_lake_div.json b/server/map_handler/lake_relations/test_lake_div.json deleted file mode 100644 index 653440158e2399415647e8d73637c1c6a78a00fb..0000000000000000000000000000000000000000 --- a/server/map_handler/lake_relations/test_lake_div.json +++ /dev/null @@ -1 +0,0 @@ -{"test-field1": "test-value1", "test-field2": ["test-value2", "testvalue3"]} \ No newline at end of file diff --git a/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-311-pytest-8.2.0.pyc b/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-311-pytest-8.2.0.pyc index a74c8c7b03b7928d80c0ec1baad0a0ff2cfdca0a..ec3abd0b1224921af2ad0122bc5699c6d54a97f7 100644 Binary files a/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-311-pytest-8.2.0.pyc and b/server/map_handler/unit_tests/__pycache__/test_add_new_lake.cpython-311-pytest-8.2.0.pyc differ diff --git a/server/map_handler/unit_tests/__pycache__/test_update_measurements.cpython-311-pytest-8.2.0.pyc b/server/map_handler/unit_tests/__pycache__/test_update_measurements.cpython-311-pytest-8.2.0.pyc new file mode 100644 index 0000000000000000000000000000000000000000..0f76e4a0ea22b4d3828864abe9ad776e18e7da51 Binary files /dev/null and b/server/map_handler/unit_tests/__pycache__/test_update_measurements.cpython-311-pytest-8.2.0.pyc differ diff --git a/server/map_handler/unit_tests/test_add_new_lake.py b/server/map_handler/unit_tests/test_add_new_lake.py index 6b3fd67f5377ee6bb0fc1f7cb44b67fc01c9cdb3..a80b333e99d6657fe7a2196c8296a864141cf7ba 100644 --- a/server/map_handler/unit_tests/test_add_new_lake.py +++ b/server/map_handler/unit_tests/test_add_new_lake.py @@ -1,3 +1,4 @@ +import os import json from shapely.geometry import Polygon, LineString @@ -37,12 +38,18 @@ def test_write_json_to_file() -> None: "testvalue3" ], } + test_path = LAKE_RELATIONS_PATH + '/' + test_lake_name + '_div.json' # Call the function that is being tested write_json_to_file(test_lake_name, test_data) # Try to read the data from the newly created file - with open(LAKE_RELATIONS_PATH + '/' + test_lake_name + '_div.json', 'r') as f: + with open(test_path, 'r') as f: result = json.load(f) assert result == test_data + + # Remove the test file + os.remove(test_path) + + diff --git a/server/map_handler/unit_tests/test_update_measurements.py b/server/map_handler/unit_tests/test_update_measurements.py new file mode 100644 index 0000000000000000000000000000000000000000..e56f20bf0cdcc8458c002c564f9ac9add5a1e15c --- /dev/null +++ b/server/map_handler/unit_tests/test_update_measurements.py @@ -0,0 +1,9 @@ +from server.map_handler.update_measurements import update_measurements + + +def test_update_measurements() -> None: + test_lake_name = "test_lake" + + status_code, _ = update_measurements(test_lake_name) + + assert status_code == 404 diff --git a/server/map_handler/update_measurements.py b/server/map_handler/update_measurements.py index 2e07a875b4a54dd1f90a44049a65895423083ca7..ed3cd94a85a3e7450eaf08da83a91f8819ee2331 100644 --- a/server/map_handler/update_measurements.py +++ b/server/map_handler/update_measurements.py @@ -7,15 +7,29 @@ from server.consts import LAKE_RELATIONS_PATH from server.ModelFromNVE.icemodellingscripts.getIceThicknessLakes import get_raw_dates, ice_prognosis_raw_data -def update_measurements(self, lake_name: str): +def update_measurements_handler(self, lake_name: str): + status_code, measurement_data = update_measurements(lake_name) + + self.send_response(status_code) + self.send_header("Content-type", "application/json") + self.end_headers() + + self.wfile.write(measurement_data.encode('utf-8')) + + +def update_measurements(lake_name: str) -> (int, str): """ Retrieves LiDar data for a given lake, and adds weather data to each subdivision. Parameters: - self (BaseHTTPRequestHandler): A instance of a BaseHTTPRequestHandler lake_name (str): The name of the requested lake """ try: + # Return immediately if an invalid lake name was provided + if not os.path.exists(LAKE_RELATIONS_PATH + lake_name + "_div.json"): + print("The system lake does not exist") + return 404, f"{lake_name} does not exists in the system" + # Define file path to lidar data file file_path = os.path.join(LAKE_RELATIONS_PATH, lake_name + '_lidar_data.json') @@ -115,27 +129,13 @@ def update_measurements(self, lake_name: str): with open(LAKE_RELATIONS_PATH + lake_name.lower() + '_measurements.json', 'w') as f: json.dump(measurements, f, indent=4) - if self is not None: - # Convert list of dictionaries to JSON - response_data = json.dumps(measurements, indent=4) - # Set headers - self.send_response(200) - self.send_header("Content-type", "application/json") - self.end_headers() - - # Write processed data to response object - self.wfile.write(response_data.encode('utf-8')) + # Convert list of dictionaries to JSON + response_data = json.dumps(measurements, indent=4) + # Set headers + return 200, response_data except Exception as e: - print(f"Error in updating measurements: {e}") - - if self is not None: - # Set headers - self.send_response(500) - self.send_header("Content-type", "application/json") - self.end_headers() - - self.wfile.write(f"Error in updating measurements: {e}".encode('utf-8')) + return 500, f"Error in updating measurements: {e}".encode('utf-8') def fill_remaining_subdivisions(lake_name: str, processed_ids: list): @@ -171,7 +171,7 @@ def fill_remaining_subdivisions(lake_name: str, processed_ids: list): if len(ice_stats) > 0 and len(ice_stats[0]) > 0: total_ice_thickness = ice_stats[0]['Total ice (m)'] accuracy = 1 - else: # Initialise empty ice stats + else: # Initialise empty ice stats ice_stats = { "Date": "NA", "Slush ice (m)": 0, @@ -252,7 +252,6 @@ def addTestData(self, lake_name: str): # Create 10 subdivisions for each measurement, with randomized coordinates and thicknesses for subdiv_id in range(30): - subdivision = { "SubdivID": sub_div_id, "MinThickness": round(random.uniform(4, 20), 1), diff --git a/server/scheduler.py b/server/scheduler.py index 970987bd6d074aa609e4ddf10f28be0d2a777a67..6c50b0948dc7ddfafc19d0a05b68806b36774c7f 100644 --- a/server/scheduler.py +++ b/server/scheduler.py @@ -2,14 +2,14 @@ import json import time import schedule -from map_handler.update_measurements import update_measurements +from map_handler.update_measurements import update_measurements_handler from server.consts import LAKE_RELATIONS_PATH def update_all_measurements(lake_names: list): """Loops through all lake names and calls get_measurements() on each lake""" for lake in lake_names: - update_measurements(None, lake) + update_measurements_handler(None, lake) def update_scheduler():