Skip to content

Commit

Permalink
Merge pull request #2550 from EliorGigi/dev
Browse files Browse the repository at this point in the history
Feat 2532 - Change from SubUrbanWidget to AllLocationsWidget
  • Loading branch information
atalyaalon authored Jan 28, 2024
2 parents 018bd1f + 6b5825e commit 8f3209c
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 79 deletions.
23 changes: 17 additions & 6 deletions anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,25 +894,36 @@ def set_critical(
from anyway.widgets.all_locations_widgets.injured_count_by_severity_widget import (
InjuredCountBySeverityWidget,
)
from anyway.request_params import get_latest_accident_date
from anyway.request_params import get_latest_accident_date, LocationInfo

if self.road1 is None or self.road_segment_name is None:
if (self.road1 is None or self.road_segment_name is None) and (self.yishuv_name is None or self.street1_hebrew is None) :
return None
last_accident_date = get_latest_accident_date(table_obj=AccidentMarkerView, filters=None)
resolution = BE_CONST.ResolutionCategories(self.resolution)
end_time = last_accident_date.to_pydatetime().date()
start_time = datetime.date(end_time.year + 1 - years_before, 1, 1)
critical_values = InjuredCountBySeverityWidget.get_injured_count_by_severity(
self.road1, self.road_segment_name, start_time, end_time
)
location_info = LocationInfo()
if resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
location_info["road1"]
location_info["road_segment_name"]
elif resolution == BE_CONST.ResolutionCategories.STREET:
location_info["yishuv_name"] = self.yishuv_name
location_info["street1_hebrew"] = self.street1_hebrew

critical_values = InjuredCountBySeverityWidget.get_injured_count_by_severity(resolution, location_info, start_time, end_time)
if critical_values == {}:
return None
critical = None
resolution = BE_CONST.ResolutionCategories(self.resolution)
if resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
critical = (
(critical_values["severe_injured_count"] / suburban_road_severe_value)
+ (critical_values["killed_count"] / suburban_road_killed_value)
) >= 1
elif resolution == BE_CONST.ResolutionCategories.STREET:
critical = (
(critical_values["severe_injured_count"] / urban_severe_value)
+ (critical_values["killed_count"] / urban_severe_value)
) >= 1
self.critical = critical

def serialize(self):
Expand Down
5 changes: 5 additions & 0 deletions anyway/widgets/all_locations_widgets/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from . import (
accident_count_by_severity_widget,
injured_count_by_severity_widget,
injured_count_by_accident_year_widget,
most_severe_accidents_widget,
most_severe_accidents_table_widget,
seriously_injured_killed_in_bicycles_scooter_widget,
killed_and_injured_count_per_age_group_stacked_widget,
accidents_heat_map_widget,
accident_count_by_day_night_widget,
accident_count_by_accident_type_widget,
accident_count_by_accident_year_widget,
killed_and_injured_count_per_age_group_widget
)
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from anyway.request_params import RequestParams
from anyway.widgets.widget_utils import get_accidents_stats
from anyway.widgets.widget_utils import get_accidents_stats, get_location_text
from anyway.models import AccidentMarkerView
from anyway.widgets.widget import register
from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
from typing import Dict

# noinspection PyProtectedMember
Expand All @@ -11,7 +11,7 @@


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

Expand Down Expand Up @@ -54,8 +54,11 @@ def get_accident_count_by_accident_type(location_info, start_time, end_time):

@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
items["data"]["text"] = {"title": _("Number of accidents by accident type"),
"subtitle": f'{_("in segment")} {_(request_params.location_info["road_segment_name"])}'}
location_text = get_location_text(request_params)
items["data"]["text"] = {
"title": _("Number of accidents by accident type"),
"subtitle": _(location_text),
}
for item in items["data"]["items"]:
to_translate = item["accident_type"]
item["accident_type"] = _(to_translate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
from flask_babel import _

from anyway.backend_constants import AccidentSeverity
from anyway.infographics_dictionaries import segment_dictionary
from anyway.models import AccidentMarkerView
from anyway.request_params import RequestParams
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 (
get_accidents_stats,
gen_entity_labels,
format_2_level_items,
sort_and_fill_gaps_for_stacked_bar,
get_location_text,
)


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

Expand All @@ -30,7 +30,6 @@ def __init__(self, request_params: RequestParams):
self.information = "Fatal, severe and light accidents count in the specified years, split by accident severity"

def generate_items(self) -> None:

res1 = get_accidents_stats(
table_obj=AccidentMarkerView,
filters=self.request_params.location_info,
Expand All @@ -52,9 +51,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": _("Number of accidents, per year, split by severity"),
"subtitle": f'{_("in segment")} {_(segment_dictionary[request_params.location_info["road_segment_name"]])}',
"subtitle": _(location_text),
"labels_map": gen_entity_labels(AccidentSeverity),
}
return items
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from anyway.request_params import RequestParams
from anyway.widgets.widget_utils import get_accidents_stats
from anyway.widgets.widget_utils import get_accidents_stats, get_location_text
from anyway.models import AccidentMarkerView
from anyway.widgets.widget import register
from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget
from typing import Dict
from flask_babel import _


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

Expand All @@ -32,8 +32,8 @@ def generate_items(self) -> None:

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,41 +61,41 @@ def get_accident_count_by_severity(location_info, start_time, end_time):
def get_transcription(request_params: RequestParams, items: Dict):
total_accidents_count = items.get("total_accidents_count")
if total_accidents_count == 0:
return ''
return ""
severity_light_count = items.get("severity_light_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")
else:
severity_light_count_text = f'{severity_light_count} ' + _("light plural")
severity_light_count_text = f"{severity_light_count} " + _("light plural")
severity_severe_count = items.get("severity_severe_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")
else:
severity_severe_count_text = f'{severity_severe_count} '+ _("severe plural")
severity_severe_count_text = f"{severity_severe_count} " + _("severe plural")
severity_fatal_count = items.get("severity_fatal_count")
if severity_fatal_count == 0:
severity_fatal_count_text = ''
severity_fatal_count_text = ""
elif severity_fatal_count == 1:
severity_fatal_count_text = _("one fatal")
else:
severity_fatal_count_text = f'{severity_fatal_count} ' + _("fatal plural")
severity_fatal_count_text = f"{severity_fatal_count} " + _("fatal 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.value}")
Expand All @@ -108,24 +108,32 @@ def get_transcription(request_params: RequestParams, items: Dict):
incident_keyword=_("accidents"),
out_of_them_keywoard=_("out of them"),
)
text += join_strings([severity_fatal_count_text, severity_severe_count_text, severity_light_count_text],
sep_a=" ,",
sep_b=_(" and "))
text += join_strings(
[severity_fatal_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:
if request_params.resolution in (BE_CONST.ResolutionCategories.SUBURBAN_ROAD,
BE_CONST.ResolutionCategories.SUBURBAN_JUNCTION):
if request_params.resolution in (
BE_CONST.ResolutionCategories.SUBURBAN_ROAD,
BE_CONST.ResolutionCategories.SUBURBAN_JUNCTION,
):
is_segment = request_params.resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD
items["data"]["text"] = {
"title": _("Number of accidents by severity"),
"subtitle": _(request_params.location_info['road_segment_name']) if is_segment else
_(request_params.location_info['non_urban_intersection_hebrew']),
"transcription": AccidentCountBySeverityWidget.get_transcription(request_params=request_params,
items=items["data"]["items"])
"subtitle": _(request_params.location_info["road_segment_name"])
if is_segment
else _(request_params.location_info["non_urban_intersection_hebrew"]),
"transcription": AccidentCountBySeverityWidget.get_transcription(
request_params=request_params, items=items["data"]["items"]
),
}
items["meta"]["information"] = "{incident_description}{incident_location} {incident_time}.".format(
items["meta"][
"information"
] = "{incident_description}{incident_location} {incident_time}.".format(
incident_description=_("Fatal, severe and light accidents count in "),
incident_location=_("segment") if is_segment else _("junction"),
incident_time=_("in the selected time"),
Expand All @@ -141,11 +149,22 @@ def localize_items(request_params: RequestParams, items: Dict) -> Dict:
incidents_num=num_accidents,
incident_keyword=_("accidents"),
)
subtitle = _("{street_name}, {yishuv_name}".format(street_name=request_params.location_info["street1_hebrew"],
yishuv_name=request_params.location_info["yishuv_name"]))
items["data"]["text"] = {"title": s, "subtitle": subtitle, "transcription": AccidentCountBySeverityWidget.get_transcription(request_params=request_params,
items=items["data"]["items"])}
items["meta"]["information"] = "{incident_description}{incident_location} {incident_time}.".format(
subtitle = _(
"{street_name}, {yishuv_name}".format(
street_name=request_params.location_info["street1_hebrew"],
yishuv_name=request_params.location_info["yishuv_name"],
)
)
items["data"]["text"] = {
"title": s,
"subtitle": subtitle,
"transcription": AccidentCountBySeverityWidget.get_transcription(
request_params=request_params, items=items["data"]["items"]
),
}
items["meta"][
"information"
] = "{incident_description}{incident_location} {incident_time}.".format(
incident_description=_("Fatal, severe and light accidents count in "),
incident_location=_("street"),
incident_time=_("in the selected time"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

from anyway.request_params import RequestParams
from anyway.backend_constants import AccidentSeverity, BE_CONST
from anyway.infographics_dictionaries import segment_dictionary
from anyway.widgets.widget_utils import get_query
from anyway.widgets.widget_utils import get_query, get_location_text
from anyway.models import AccidentMarkerView
from anyway.widgets.widget import register
from anyway.widgets.road_segment_widgets.road_segment_widget import RoadSegmentWidget
from anyway.widgets.all_locations_widgets.all_locations_widget import AllLocationsWidget


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

Expand Down Expand Up @@ -49,8 +48,9 @@ def get_accidents_heat_map(filters, start_time, end_time):

@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
location_text = get_location_text(request_params)
items["data"]["text"] = {
"title": _("Fatal and severe accidents heat map"),
"subtitle": _(segment_dictionary[request_params.location_info["road_segment_name"]])
"subtitle": _(location_text),
}
return items
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
from flask_babel import _

from anyway.backend_constants import InjurySeverity
from anyway.infographics_dictionaries import segment_dictionary
from anyway.models import InvolvedMarkerView
from anyway.request_params import RequestParams
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 (
get_accidents_stats,
gen_entity_labels,
get_injured_filters,
format_2_level_items,
sort_and_fill_gaps_for_stacked_bar,
get_location_text,
)


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

Expand All @@ -32,7 +32,7 @@ def __init__(self, request_params: RequestParams):
def generate_items(self) -> None:
res1 = get_accidents_stats(
table_obj=InvolvedMarkerView,
filters=get_injured_filters(self.request_params.location_info),
filters=get_injured_filters(self.request_params),
group_by=("accident_year", "injury_severity"),
count="injury_severity",
start_time=self.request_params.start_time,
Expand All @@ -51,9 +51,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": _("Number of injured in accidents, per year, split by severity"),
"subtitle": f'{_("in segment")} {_(segment_dictionary[request_params.location_info["road_segment_name"]])}',
"subtitle": _(location_text),
"labels_map": gen_entity_labels(InjurySeverity),
}
return items
Expand Down
Loading

0 comments on commit 8f3209c

Please sign in to comment.