Skip to content

Commit

Permalink
Merge pull request #2729 from data-for-change/2728-add-is-included-to…
Browse files Browse the repository at this point in the history
…_prod

Add and Update is_included, add translations to accident_count_by_day_night_widget
  • Loading branch information
atalyaalon authored Nov 3, 2024
2 parents fde7199 + ac895ca commit 70051ab
Show file tree
Hide file tree
Showing 18 changed files with 456 additions and 366 deletions.
12 changes: 12 additions & 0 deletions anyway/backend_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ def labels(cls):
}


class DayNight(Enum):
DAY = 1
NIGHT = 5

@classmethod
def labels(cls):
return {
DayNight.DAY: "Day",
DayNight.NIGHT: "Night",
}


class CrossCategory(Enum):
UNKNOWN = 0
NONE = 1
Expand Down
3 changes: 3 additions & 0 deletions anyway/infographics_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def __missing__(self, key):
_("Collision with an animal")
_("Damage caused by a falling load off a vehicle")

_("Day")
_("Night")

_("professional_driver") # "נהג מקצועי"
_("private_vehicle_driver") # "נהג פרטי"
_("other_driver") # "לא ידוע"
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def generate_items(self) -> None:
resolution=self.request_params.resolution
)

def is_included(self) -> bool:
accidents_count = sum(item['count'] for item in self.items)
return accidents_count > 0

@staticmethod
def get_accident_count_by_accident_type(location_info, start_time, end_time, resolution):
all_accident_type_count = get_accidents_stats(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def generate_items(self) -> None:
)
self.items = format_2_level_items(res2, None, AccidentSeverity)

def is_included(self) -> bool:
accidents_count = sum(item['value'] for entry in self.items for item in entry['series'])
return accidents_count > 0

@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
location_text = get_location_text(request_params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Dict
from flask_babel import _


from anyway.backend_constants import DayNight
@register
class AccidentCountByDayNightWidget(AllLocationsWidget):
name: str = "accident_count_by_day_night"
Expand All @@ -21,20 +21,29 @@ def __init__(self, request_params: RequestParams):
)

def generate_items(self) -> None:
self.items = get_accidents_stats(
all_accident_day_night_count = get_accidents_stats(
table_obj=AccidentMarkerView,
filters=self.request_params.location_info,
group_by="day_night_hebrew",
count="day_night_hebrew",
group_by="day_night",
count="day_night",
start_time=self.request_params.start_time,
end_time=self.request_params.end_time,
resolution=self.request_params.resolution,
)
all_items = []
for item in all_accident_day_night_count:
at: DayNight = DayNight(item["day_night"])
item["day_night"] = at.get_label()
all_items.append(item)
res = sorted(all_items, key=lambda x: x["count"], reverse=True)
return res

@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": _(location_text)}
for item in items["data"]["items"]:
item["day_night"] = _(item["day_night"])
return items


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def generate_items(self) -> None:
resolution=self.request_params.resolution,
)

def is_included(self) -> bool:
return self.items["total_accidents_count"] > 0

@staticmethod
def get_accident_count_by_severity(location_info, start_time, end_time, resolution):
count_by_severity = get_accidents_stats(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def generate_items(self) -> None:
)
self.items = format_2_level_items(res2, None, InjurySeverity)

def is_included(self) -> bool:
injured_count = sum(item['value'] for entry in self.items for item in entry['series'])
return injured_count > 0

@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
location_text = get_location_text(request_params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def generate_items(self) -> None:
self.request_params.end_time,
)

def is_included(self) -> bool:
return self.items["total_injured_count"] > 0

@staticmethod
def get_injured_count_by_severity(
resolution: BE_CONST.ResolutionCategories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ def localize_items(request_params: RequestParams, items: Dict) -> Dict:
items["meta"]["information"] = _("Injured count per age group and injurey severity. The graph shows all injury severities: fatal, severe, and light.")
return items

def is_included(self) -> bool:
killed_injured_count = sum(item['value'] for entry in self.items for item in entry['series'])
return killed_injured_count > 0

@staticmethod
def get_age_range_list() -> List[str]:
age_list = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ def generate_items(self) -> None:
structured_data_list.append({BE.LKEY: age_group, BE.VAL: count_total})
self.items = structured_data_list

def is_included(self) -> bool:
accidents_count = sum(item['value'] for item in self.items)
return accidents_count > 0

@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
if request_params.lang != "en":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def is_included(self) -> bool:
else:
raise ValueError
all_total = all_h2h + all_others # pylint: disable=E0606
return segment_h2h > 0 and (segment_h2h / segment_total) > all_h2h / all_total
return segment_h2h > 1 and (segment_h2h / segment_total) > all_h2h / all_total


# adding calls to _() for pybabel extraction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ def generate_items(self) -> None:
logging.error(f"InjuredAccidentsWithPedestriansWidget.generate_items(): {e}")
raise

def is_included(self) -> bool:
accidents_with_pedestrians_count = sum(item['value'] for entry in self.items for item in entry['series'])
return accidents_with_pedestrians_count > 0

@staticmethod
def localize_items(request_params: RequestParams, items: Dict) -> Dict:
items["data"]["text"] = {
Expand Down
1 change: 0 additions & 1 deletion anyway/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def is_in_cache() -> bool:
"""Whether this widget is stored in the cache"""
return True

# noinspection PyMethodMayBeStatic
def is_included(self) -> bool:
"""Whether this widget is included in the response"""
return bool(self.items)
Expand Down
Loading

0 comments on commit 70051ab

Please sign in to comment.