From ec2096b11ba73f6eb2a44af2dacc9af0c53114f3 Mon Sep 17 00:00:00 2001 From: Atalya Alon Date: Wed, 18 Dec 2024 17:08:26 +0200 Subject: [PATCH 1/4] modify register order --- anyway/infographics_utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/anyway/infographics_utils.py b/anyway/infographics_utils.py index 3d0b5dd3..6eb57f53 100755 --- a/anyway/infographics_utils.py +++ b/anyway/infographics_utils.py @@ -28,9 +28,9 @@ # We need to import the modules, which in turn imports all the widgets, and registers them, even if they are not # explicitly used here # pylint: disable=unused-import -import anyway.widgets.urban_widgets -import anyway.widgets.road_segment_widgets import anyway.widgets.all_locations_widgets +import anyway.widgets.road_segment_widgets +import anyway.widgets.urban_widgets import anyway.widgets.no_location_widgets # pylint: enable=unused-import From 4018f0edc093b8b589e6fbb2c882de01f75eed94 Mon Sep 17 00:00:00 2001 From: Atalya Alon <20992625+atalyaalon@users.noreply.github.com> Date: Thu, 19 Dec 2024 15:38:09 +0200 Subject: [PATCH 2/4] Update CBS.md --- docs/Architecture/CBS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/Architecture/CBS.md b/docs/Architecture/CBS.md index 1e3c89f7..6a3bf39b 100644 --- a/docs/Architecture/CBS.md +++ b/docs/Architecture/CBS.md @@ -6,7 +6,7 @@ - **Relevant output**: unzipped CBS files saved in S3 - **Relevant Storage directory**: S3 -> Bucket: dfc-anyway-cbs - **command**: `python3 main.py scripts importemail` -- **Scheduling**: Nowadays runs in Jenkins once a week +- **Scheduling**: Nowadays runs in Airflow once a week ### CBS: pulls data from s3, processes cbs data and pushes it to CBS tables Environment Variables: AWS_ACCESS_KEY, AWS_SECRET_KEY From b513183215b627a8f2870a83d435777b73c9459e Mon Sep 17 00:00:00 2001 From: Atalya Alon <20992625+atalyaalon@users.noreply.github.com> Date: Thu, 19 Dec 2024 15:55:04 +0200 Subject: [PATCH 3/4] Update is_included in head on collisions --- .../head_on_collisions_comparison_widget.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anyway/widgets/road_segment_widgets/head_on_collisions_comparison_widget.py b/anyway/widgets/road_segment_widgets/head_on_collisions_comparison_widget.py index d7c92796..0aca0a76 100644 --- a/anyway/widgets/road_segment_widgets/head_on_collisions_comparison_widget.py +++ b/anyway/widgets/road_segment_widgets/head_on_collisions_comparison_widget.py @@ -117,7 +117,7 @@ def is_included(self) -> bool: else: raise ValueError all_total = all_h2h + all_others # pylint: disable=E0606 - return segment_h2h > 1 and (segment_h2h / segment_total) > all_h2h / all_total + return segment_h2h > 1 and (segment_h2h / segment_total) > 2 * all_h2h / all_total # adding calls to _() for pybabel extraction From 7c5f62206ca4c6af5f2999a20954b7ba33d0f513 Mon Sep 17 00:00:00 2001 From: ziv Date: Fri, 20 Dec 2024 07:20:51 +0200 Subject: [PATCH 4/4] Return 404 if building request params failed. --- anyway/flask_app.py | 4 +- anyway/request_params.py | 121 ++++++++++++++++++++------------------- 2 files changed, 64 insertions(+), 61 deletions(-) diff --git a/anyway/flask_app.py b/anyway/flask_app.py index 6d7aae5e..2c6938d4 100755 --- a/anyway/flask_app.py +++ b/anyway/flask_app.py @@ -1285,10 +1285,10 @@ def infographics_data_by_location(): output = get_infographics_mock_data() elif mock_data == "false": request_params = get_request_params_from_request_values(request.values) - output = get_infographics_data_for_location(request_params) - if not output: + if request_params is None: log_bad_request(request) return abort(http_client.NOT_FOUND) + output = get_infographics_data_for_location(request_params) else: log_bad_request(request) return abort(http_client.BAD_REQUEST) diff --git a/anyway/request_params.py b/anyway/request_params.py index 2e35ca9f..d0abc456 100644 --- a/anyway/request_params.py +++ b/anyway/request_params.py @@ -78,67 +78,70 @@ def __eq__(self, other): # todo: merge with get_request_params() def get_request_params_from_request_values(vals: dict) -> Optional[RequestParams]: - news_flash_obj = extract_news_flash_obj(vals) - news_flash_description = ( - news_flash_obj.description - if news_flash_obj is not None and news_flash_obj.description is not None - else None - ) - news_flash_title = ( - news_flash_obj.title - if news_flash_obj is not None and news_flash_obj.title is not None - else None - ) - location = get_location_from_news_flash_or_request_values(news_flash_obj, vals) - if location is None: - return None - years_ago = vals.get("years_ago", BE_CONST.DEFAULT_NUMBER_OF_YEARS_AGO) - lang = vals.get("lang", "he") - location_text = location["text"] - gps = location["gps"] - location_info = location["data"] - - if location_info is None: - return None - resolution = location_info.pop("resolution") - if resolution is None or resolution not in BE_CONST.SUPPORTED_RESOLUTIONS: - logging.error(f"Resolution empty or not supported: {resolution}.") - return None - - if all(value is None for value in location_info.values()): - return None - try: - years_ago = int(years_ago) - except (ValueError, TypeError): - return None - if years_ago < 0 or years_ago > 100: + news_flash_obj = extract_news_flash_obj(vals) + news_flash_description = ( + news_flash_obj.description + if news_flash_obj is not None and news_flash_obj.description is not None + else None + ) + news_flash_title = ( + news_flash_obj.title + if news_flash_obj is not None and news_flash_obj.title is not None + else None + ) + location = get_location_from_news_flash_or_request_values(news_flash_obj, vals) + if location is None: + return None + years_ago = vals.get("years_ago", BE_CONST.DEFAULT_NUMBER_OF_YEARS_AGO) + lang = vals.get("lang", "he") + location_text = location["text"] + gps = location["gps"] + location_info = location["data"] + + if location_info is None: + return None + resolution = location_info.pop("resolution") + if resolution is None or resolution not in BE_CONST.SUPPORTED_RESOLUTIONS: + logging.error(f"Resolution empty or not supported: {resolution}.") + return None + + if all(value is None for value in location_info.values()): + return None + + try: + years_ago = int(years_ago) + except (ValueError, TypeError): + return None + if years_ago < 0 or years_ago > 100: + return None + last_accident_date = get_latest_accident_date(table_obj=AccidentMarkerView, filters=None) + # converting to datetime object to get the date + end_time = last_accident_date.to_pydatetime().date() + start_time = datetime.date(end_time.year + 1 - years_ago, 1, 1) + + widget_specific = {} + if resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD: + widget_specific.update({"road_segment_name": location_info.get("road_segment_name")}) + + request_params = RequestParams( + years_ago=years_ago, + location_text=location_text, + location_info=location_info, + # TODO: getting a warning on resolution=resolution: "Expected type 'dict', got 'int' instead" + resolution=resolution, + gps=gps, + start_time=start_time, + end_time=end_time, + lang=lang, + news_flash_description=news_flash_description, + news_flash_title=news_flash_title, + widget_specific=widget_specific + ) + return request_params + except ValueError: + logging.exception(f"Exception while preparing request params. vals:{vals}.") return None - last_accident_date = get_latest_accident_date(table_obj=AccidentMarkerView, filters=None) - # converting to datetime object to get the date - end_time = last_accident_date.to_pydatetime().date() - start_time = datetime.date(end_time.year + 1 - years_ago, 1, 1) - - widget_specific = {} - if resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD: - widget_specific.update({"road_segment_name": location_info.get("road_segment_name")}) - - request_params = RequestParams( - years_ago=years_ago, - location_text=location_text, - location_info=location_info, - # TODO: getting a warning on resolution=resolution: "Expected type 'dict', got 'int' instead" - resolution=resolution, - gps=gps, - start_time=start_time, - end_time=end_time, - lang=lang, - news_flash_description=news_flash_description, - news_flash_title=news_flash_title, - widget_specific=widget_specific - ) - return request_params - def get_location_from_news_flash_or_request_values( news_flash_obj: Optional[NewsFlash], vals: dict