Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 2373- move widgets to all locations #2512

Merged
merged 21 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
2bdeeb6
move widgets to all locations
EliorGigi Dec 20, 2023
0e6e9f3
merge chagnes from dev and subtitle adjustments
EliorGigi Dec 22, 2023
f1c9c20
remove import
EliorGigi Dec 22, 2023
bc25edb
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 22, 2023
ac3e4d4
remove newsflash infographics dir
EliorGigi Dec 22, 2023
3772b81
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 22, 2023
ab74a2b
skip wall test
EliorGigi Dec 22, 2023
2bcd172
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 22, 2023
090f220
fix widget location
EliorGigi Dec 22, 2023
9d19879
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 22, 2023
9737275
fix black issues
EliorGigi Dec 22, 2023
e529fef
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 22, 2023
695390b
remove unused imprted datetime
EliorGigi Dec 22, 2023
5fb7c90
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 22, 2023
253e4c4
Revert "skip wall test"
EliorGigi Dec 22, 2023
b7c2069
Revert "fix black issues"
EliorGigi Dec 22, 2023
8abb211
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 22, 2023
a4f6304
apply black reformatting
EliorGigi Dec 22, 2023
2e6d656
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 22, 2023
8c09afc
skip scrape_sanity_online_walla
EliorGigi Dec 29, 2023
88c6f71
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
EliorGigi Dec 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions anyway/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
get_downloaded_data,
DEFAULT_LIMIT_REQ_PARAMETER,
DEFAULT_OFFSET_REQ_PARAMETER,
DEFAULT_NUMBER_OF_YEARS_AGO
DEFAULT_NUMBER_OF_YEARS_AGO,
)
from anyway.views.schools.api import (
schools_description_api,
Expand Down Expand Up @@ -321,6 +321,7 @@ def schools():
else:
return Response("Method Not Allowed", 405)


@app.route("/markers", methods=["GET"])
def markers():
logging.debug("getting markers")
Expand Down Expand Up @@ -357,6 +358,7 @@ def markers():
accident_markers, rsa_markers, discussions, is_thin, total_records=result.total_records
)


@app.route("/markers_by_yishuv_symbol", methods=["GET"])
def markers_by_yishuv_symbol():
logging.debug("getting markers by yishuv symbol")
Expand Down Expand Up @@ -1194,9 +1196,12 @@ def get(self):
Returns infographics-data API
"""
parser = reqparse.RequestParser()
parser.add_argument("id", type=int, help="News flash id")
parser.add_argument("news_flash_id", type=int, help="News flash id")
atalyaalon marked this conversation as resolved.
Show resolved Hide resolved
parser.add_argument(
"years_ago", type=int, default=DEFAULT_NUMBER_OF_YEARS_AGO, help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years"
"years_ago",
type=int,
default=DEFAULT_NUMBER_OF_YEARS_AGO,
help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years",
)
parser.add_argument("lang", type=str, default="he", help="Language")

Expand Down Expand Up @@ -1254,7 +1259,10 @@ def gps_to_cbs_location():
idbl_parser = reqparse.RequestParser()
idbl_parser.add_argument("road_segment_id", type=int, help="Road Segment id")
idbl_parser.add_argument(
"years_ago", type=int, default=DEFAULT_NUMBER_OF_YEARS_AGO, help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years"
"years_ago",
type=int,
default=DEFAULT_NUMBER_OF_YEARS_AGO,
help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years",
)
idbl_parser.add_argument("lang", type=str, default="he", help="Language")

Expand Down Expand Up @@ -1455,6 +1463,7 @@ def post(self):
"road_segment_id", type=int, required=True, help="road segment id"
)


@api.route("/api/streets")
@api.expect(get_streets_parser)
class GetAllStreetsOfYishuv(Resource):
Expand Down Expand Up @@ -1499,17 +1508,26 @@ def put(self, id):


download_data_parser = reqparse.RequestParser()
download_data_parser.add_argument("format", type=str, default="csv",
help="Format for downloaded data (.csv/.xlsx)")
download_data_parser.add_argument("years_ago", type=int, default=DEFAULT_NUMBER_OF_YEARS_AGO,
help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years")
download_data_parser.add_argument(
"format", type=str, default="csv", help="Format for downloaded data (.csv/.xlsx)"
)
download_data_parser.add_argument(
"years_ago",
type=int,
default=DEFAULT_NUMBER_OF_YEARS_AGO,
help=f"Number of years back to consider accidents. Default is {DEFAULT_NUMBER_OF_YEARS_AGO} years",
)
"""
Download accidents data with regards to news flash/location
"""


@api.route("/api/download-data", methods=["GET"])
class DownloadData(Resource):
@api.doc("download data")
@api.expect(parser)
def get(self):
args = download_data_parser.parse_args()
return get_downloaded_data(args.get('format', 'csv'), args.get('years_ago', DEFAULT_NUMBER_OF_YEARS_AGO))
return get_downloaded_data(
args.get("format", "csv"), args.get("years_ago", DEFAULT_NUMBER_OF_YEARS_AGO)
)
2 changes: 1 addition & 1 deletion anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ def set_critical(
suburban_road_killed_value=3,
urban_severe_value=2,
):
from anyway.widgets.road_segment_widgets.injured_count_by_severity_widget import (
from anyway.widgets.all_locations_widgets.injured_count_by_severity_widget import (
InjuredCountBySeverityWidget,
)
from anyway.request_params import get_latest_accident_date
Expand Down
5 changes: 4 additions & 1 deletion anyway/widgets/all_locations_widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from . import (
accident_count_by_severity_widget,
injured_count_by_severity_widget,
most_severe_accidents_widget,
most_severe_accidents_table_widget,
seriously_injured_killed_in_bicycles_scooter_widget
seriously_injured_killed_in_bicycles_scooter_widget,
killed_and_injured_count_per_age_group_stacked_widget,
killed_and_injured_count_per_age_group_widget
)
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
from typing import Dict

from anyway.request_params import RequestParams
from anyway.backend_constants import InjurySeverity
from anyway.models import InvolvedMarkerView
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
from anyway.widgets.widget import register
from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
from anyway.widgets.widget_utils import get_accidents_stats, join_strings
from anyway.widgets.widget_utils import get_accidents_stats, join_strings, get_location_text
from anyway.backend_constants import BE_CONST
from flask_babel import _


@register
class InjuredCountBySeverityWidget(RoadSegmentWidget):
class InjuredCountBySeverityWidget(AllLocationsWidget):
name: str = "injured_count_by_severity"
files = [__file__]

Expand All @@ -22,25 +21,31 @@ def __init__(self, request_params: RequestParams):

def generate_items(self) -> None:
self.items = InjuredCountBySeverityWidget.get_injured_count_by_severity(
self.request_params.location_info["road1"],
self.request_params.location_info["road_segment_name"],
self.request_params.resolution,
self.request_params.location_info,
self.request_params.start_time,
self.request_params.end_time,
)

@staticmethod
def get_injured_count_by_severity(road, segment, start_time, end_time):
def get_injured_count_by_severity(resolution, location_info, start_time, end_time):
filters = {}
filters["injury_severity"] = [
InjurySeverity.KILLED.value,
InjurySeverity.SEVERE_INJURED.value,
InjurySeverity.LIGHT_INJURED.value,
]

if resolution == BE_CONST.ResolutionCategories.STREET:
filters["involve_yishuv_name"] = location_info.get("yishuv_name")
filters["street1_hebrew"] = location_info.get("street1_hebrew")
elif resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
filters["road1"] = location_info.get("road1")
filters["road_segment_name"] = location_info.get("road_segment_name")

count_by_severity = get_accidents_stats(
table_obj=InvolvedMarkerView,
filters={
"injury_severity": [
InjurySeverity.KILLED.value,
InjurySeverity.SEVERE_INJURED.value,
InjurySeverity.LIGHT_INJURED.value,
],
"road1": road,
"road_segment_name": segment,
},
filters=filters,
group_by="injury_severity",
count="injury_severity",
start_time=start_time,
Expand Down Expand Up @@ -68,49 +73,50 @@ def get_injured_count_by_severity(road, segment, start_time, end_time):
items["total_injured_count"] = total_injured_count
return items


@staticmethod
def get_transcription(request_params: RequestParams, items: Dict):
total_injured_count = items.get("total_injured_count")
if total_injured_count == 0:
return ''
return ""
severity_light_count = items.get("light_injured_count")
if severity_light_count == 0:
severity_light_count_text = ''
severity_light_count_text = ""
elif severity_light_count == 1:
severity_light_count_text = _("one light injured")
else:
severity_light_count_text = f'{severity_light_count} ' + _("light injured plural")
severity_light_count_text = f"{severity_light_count} " + _("light injured plural")
severity_severe_count = items.get("severe_injured_count")
if severity_severe_count == 0:
severity_severe_count_text = ''
severity_severe_count_text = ""
elif severity_severe_count == 1:
severity_severe_count_text = _("one severe injured")
else:
severity_severe_count_text = f'{severity_severe_count} '+ _("severe injured plural")
severity_severe_count_text = f"{severity_severe_count} " + _("severe injured plural")
killed_count = items.get("killed_count")
if killed_count == 0:
killed_count_text = ''
killed_count_text = ""
elif killed_count == 1:
killed_count_text = _("one killed")
else:
killed_count_text = f'{killed_count} ' + _("killed plural")
killed_count_text = f"{killed_count} " + _("killed plural")
if request_params.resolution == BE_CONST.ResolutionCategories.STREET:
text = "{in_yishuv_keyword} {yishuv_name} {in_street_keyword} {street_name} ".format(
in_yishuv_keyword=_("in yishuv"),
yishuv_name=_(request_params.location_info.get('yishuv_name')),
yishuv_name=_(request_params.location_info.get("yishuv_name")),
in_street_keyword=_("in street"),
street_name=_(request_params.location_info.get('street1_hebrew')),
street_name=_(request_params.location_info.get("street1_hebrew")),
)
elif request_params.resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
text = "{in_road_keyword} {road_num} {in_segment_keyword} {segment_name} ".format(
in_road_keyword=_("in road"),
road_num=request_params.location_info.get('road1'),
road_num=request_params.location_info.get("road1"),
in_segment_keyword=_("in segment"),
segment_name=_(request_params.location_info.get('road_segment_name')),
segment_name=_(request_params.location_info.get("road_segment_name")),
)
else:
raise Exception(f"cannot convert to hebrew for resolution : {request_params.resolution.get('resolution')}")
raise Exception(
f"cannot convert to hebrew for resolution : {request_params.resolution.get('resolution')}"
)
text += "{between_years_keyword} {start_year} - {end_year}, {injured_killed_keyword} {injured_num} {people_phrase}, {out_of_them_keywoard} ".format(
between_years_keyword=_("between the years"),
start_year=request_params.start_time.year,
Expand All @@ -120,19 +126,22 @@ def get_transcription(request_params: RequestParams, items: Dict):
people_phrase=_("people from car accidents"),
out_of_them_keywoard=_("out of them"),
)
text += join_strings([killed_count_text, severity_severe_count_text, severity_light_count_text],
sep_a=" ,",
sep_b=_(" and "))
text += join_strings(
[killed_count_text, severity_severe_count_text, severity_light_count_text],
sep_a=" ,",
sep_b=_(" and "),
)
return text


@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
subtitle = get_location_text(request_params)
items["data"]["text"] = {
"title": _("Number of Injuries in accidents by severity"),
"subtitle": _(request_params.location_info['road_segment_name']),
"transcription": InjuredCountBySeverityWidget.get_transcription(request_params=request_params,
items=items["data"]["items"])
"subtitle": _(subtitle),
"transcription": InjuredCountBySeverityWidget.get_transcription(
request_params=request_params, items=items["data"]["items"]
),
}
return items

Expand All @@ -144,4 +153,4 @@ def localize_items(request_params: RequestParams, items: Dict) -> Dict:
_("one severe injured")
_("severe injured plural")
_("one light injured")
_("light injured plural")
_("light injured plural")
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@

from anyway.backend_constants import InjurySeverity, BE_CONST as BE
from anyway.request_params import RequestParams
from anyway.widgets.road_segment_widgets.killed_and_injured_count_per_age_group_widget_utils import (
from anyway.widgets.all_locations_widgets.killed_and_injured_count_per_age_group_widget_utils import (
KilledAndInjuredCountPerAgeGroupWidgetUtils,
AGE_RANGE_DICT,
)
from anyway.widgets.road_segment_widgets import killed_and_injured_count_per_age_group_widget_utils
from anyway.widgets.all_locations_widgets import killed_and_injured_count_per_age_group_widget_utils

from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
from anyway.widgets.widget import register
from anyway.widgets.widget_utils import add_empty_keys_to_gen_two_level_dict, gen_entity_labels
from anyway.widgets.widget_utils import (
add_empty_keys_to_gen_two_level_dict,
gen_entity_labels,
get_location_text,
)

INJURY_ORDER = [InjurySeverity.LIGHT_INJURED, InjurySeverity.SEVERE_INJURED, InjurySeverity.KILLED]
MAX_AGE = 200


@register
class KilledInjuredCountPerAgeGroupStackedWidget(RoadSegmentWidget):
class KilledInjuredCountPerAgeGroupStackedWidget(AllLocationsWidget):
name: str = "killed_and_injured_count_per_age_group_stacked"
files = [__file__, killed_and_injured_count_per_age_group_widget_utils.__file__]

Expand Down Expand Up @@ -48,9 +52,10 @@ def generate_items(self) -> None:

@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
location_text = get_location_text(request_params)
items["data"]["text"] = {
"title": _("Killed and injury stacked per age group"),
"subtitle": _(request_params.location_info["road_segment_name"]),
"subtitle": _(location_text),
"labels_map": gen_entity_labels(InjurySeverity),
}
return items
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

from anyway.backend_constants import BE_CONST as BE
from anyway.request_params import RequestParams
from anyway.widgets.road_segment_widgets.killed_and_injured_count_per_age_group_widget_utils import (
KilledAndInjuredCountPerAgeGroupWidgetUtils
from anyway.widgets.all_locations_widgets.killed_and_injured_count_per_age_group_widget_utils import (
KilledAndInjuredCountPerAgeGroupWidgetUtils,
)
from anyway.widgets.road_segment_widgets import killed_and_injured_count_per_age_group_widget_utils
from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
from anyway.widgets.all_locations_widgets import killed_and_injured_count_per_age_group_widget_utils
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
from anyway.widgets.widget import register
from anyway.widgets.widget_utils import get_location_text


@register
class KilledInjuredCountPerAgeGroupWidget(RoadSegmentWidget):
class KilledInjuredCountPerAgeGroupWidget(AllLocationsWidget):
name: str = "killed_and_injured_count_per_age_group"
files = [__file__, killed_and_injured_count_per_age_group_widget_utils.__file__]

Expand All @@ -35,8 +37,6 @@ def generate_items(self) -> None:

@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
items["data"]["text"] = {
"title": _("Injury per age group"),
"subtitle": f'{_("in segment")} {_(request_params.location_info["road_segment_name"])}',
}
location_text = get_location_text(request_params)
items["data"]["text"] = {"title": _("Injury per age group"), "subtitle": _(location_text)}
return items
Loading
Loading