Skip to content

Commit

Permalink
move widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
EliorGigi committed Jan 6, 2024
1 parent d062a15 commit 989f0f3
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 50 deletions.
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,9 @@ def get_accident_count_by_accident_type(location_info, 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": _("Number of accidents by accident type"),
"subtitle": f'{_("in segment")} {_(request_params.location_info["road_segment_name"])}'}
"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 Down Expand Up @@ -52,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": _("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,9 @@ 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": _("Accidents by time"),
"subtitle": f'{_("in segment")} {_(request_params.location_info["road_segment_name"])}'}
"subtitle": _(location_text)}
return items


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,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 +49,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 @@ -6,19 +6,20 @@
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 +33,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 +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": _("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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
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.widget_utils import get_accidents_stats, join_strings, get_location_text
from anyway.widgets.widget_utils import get_accidents_stats, join_strings, get_location_text, get_involved_marker_view_location_filters
from anyway.backend_constants import BE_CONST
from flask_babel import _

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

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

@staticmethod
def get_injured_count_by_severity(resolution, location_info, start_time, end_time):
filters = {}
def get_injured_count_by_severity(request_params):
filters = get_involved_marker_view_location_filters(request_params)
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=filters,
group_by="injury_severity",
count="injury_severity",
start_time=start_time,
end_time=end_time,
start_time=request_params.start_time,
end_time=request_params.end_time,
)
found_severities = [d["injury_severity"] for d in count_by_severity]
items = {}
total_injured_count = 0
start_year = start_time.year
end_year = end_time.year
start_year = request_params.start_time.year
end_year = request_params.end_time.year
for sev in InjurySeverity:
if sev.value not in found_severities:
count_by_severity.append({"injury_severity": sev.value, "count": 0})
Expand Down
5 changes: 0 additions & 5 deletions anyway/widgets/road_segment_widgets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,12 @@
accident_count_by_road_light_widget,
accident_count_pedestrians_per_vehicle_street_vs_all_widget,
accident_severity_by_cross_location_widget,
accidents_heat_map_widget,
head_on_collisions_comparison_widget,
pedestrian_injured_in_junctions_widget,
accident_count_by_hour_widget,
accident_count_by_driver_type_widget,
accident_count_by_day_night_widget,
accident_type_vehicle_type_road_comparison_widget,
accident_count_by_accident_year_widget,
accident_count_by_accident_type_widget,
accident_count_by_car_type_widget,
injured_count_by_accident_year_widget,
motorcycle_accidents_vs_all_accidents_widget,
suburban_crosswalk_widget,
fatal_accident_yoy_same_month,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def generate_items(self) -> None:

@staticmethod
def count_accidents_by_driver_type(request_params):
filters = get_injured_filters(request_params.location_info)
filters = get_injured_filters(request_params)
filters["involved_type"] = [
consts.InvolvedType.DRIVER.value,
consts.InvolvedType.INJURED_DRIVER.value,
Expand Down
22 changes: 16 additions & 6 deletions anyway/widgets/widget_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,25 @@ def gen_entity_labels(entity: Type[LabeledCode]) -> dict:
res[label] = _(label)
return res


def get_injured_filters(location_info):
new_filters = {}
for curr_filter, curr_values in location_info.items():
def get_involved_marker_view_location_filters(request_params : RequestParams):
filters = {}
if request_params.resolution == BE_CONST.ResolutionCategories.STREET:
filters["involve_yishuv_name"] = request_params.location_info.get("yishuv_name")
filters["street1_hebrew"] = request_params.location_info.get("street1_hebrew")
elif request_params.resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
filters["road1"] = request_params.location_info.get("road1")
filters["road_segment_name"] = request_params.location_info.get("road_segment_name")

return filters


def get_injured_filters(request_params : RequestParams):
new_filters = get_involved_marker_view_location_filters(request_params)
for curr_filter, curr_values in request_params.location_info.items():
if curr_filter in ["region_hebrew", "district_hebrew", "yishuv_name"]:
new_filter_name = "accident_" + curr_filter
new_filters[new_filter_name] = curr_values
else:
new_filters[curr_filter] = curr_values

new_filters["injury_severity"] = [1, 2, 3, 4, 5]
return new_filters

Expand Down

0 comments on commit 989f0f3

Please sign in to comment.