Skip to content

Commit

Permalink
Merge branch 'feat-2373-MoveWidgetsToAllLocations' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
EliorGigi committed Dec 22, 2023
2 parents 8abb211 + a4f6304 commit 2e6d656
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 66 deletions.
34 changes: 26 additions & 8 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 @@ -1196,7 +1198,10 @@ def get(self):
parser = reqparse.RequestParser()
parser.add_argument("news_flash_id", type=int, help="News flash id")
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)
)
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,32 @@ def generate_items(self) -> None:
self.request_params.resolution,
self.request_params.location_info,
self.request_params.start_time,
self.request_params.end_time
self.request_params.end_time,
)

@staticmethod
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')
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')
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,
table_obj=InvolvedMarkerView,
filters=filters,
group_by="injury_severity",
count="injury_severity",
start_time=start_time,
end_time=end_time,
)
found_severities = [d["injury_severity"] for d in count_by_severity]
items = {}
Expand All @@ -73,49 +73,50 @@ def get_injured_count_by_severity(resolution, location_info, start_time, end_tim
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 @@ -125,24 +126,24 @@ 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": _(subtitle),
"transcription": InjuredCountBySeverityWidget.get_transcription(request_params=request_params,
items=items["data"]["items"])
"transcription": InjuredCountBySeverityWidget.get_transcription(
request_params=request_params, items=items["data"]["items"]
),
}
return items



_("injured/killed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@

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, get_location_text
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from anyway.backend_constants import BE_CONST as BE
from anyway.request_params import RequestParams
from anyway.widgets.all_locations_widgets.killed_and_injured_count_per_age_group_widget_utils import (
KilledAndInjuredCountPerAgeGroupWidgetUtils
KilledAndInjuredCountPerAgeGroupWidgetUtils,
)
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(AllLocationsWidget):
name: str = "killed_and_injured_count_per_age_group"
Expand All @@ -37,8 +38,5 @@ 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": _("Injury per age group"),
"subtitle": _(location_text)
}
items["data"]["text"] = {"title": _("Injury per age group"), "subtitle": _(location_text)}
return items
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,18 @@ class KilledAndInjuredCountPerAgeGroupWidgetUtils:
def filter_and_group_injured_count_per_age_group(
request_params: RequestParams,
) -> Dict[str, Dict[int, int]]:

start_time = request_params.start_time
end_time = request_params.end_time
if request_params.resolution == BE_CONST.ResolutionCategories.STREET:
involve_yishuv_name = request_params.location_info['yishuv_name']
street1_hebrew = request_params.location_info['street1_hebrew']
involve_yishuv_name = request_params.location_info["yishuv_name"]
street1_hebrew = request_params.location_info["street1_hebrew"]
cache_key = (involve_yishuv_name, street1_hebrew, start_time, end_time)

elif request_params.resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
road_number = request_params.location_info["road1"]
road_segment = request_params.location_info["road_segment_name"]
cache_key = (road_number, road_segment, start_time, end_time)

if cache_dict.get(cache_key):
return cache_dict.get(cache_key)

Expand Down Expand Up @@ -100,9 +99,17 @@ def create_query_for_killed_and_injured_count_per_age_group(
end_time, start_time, location_info, resolution
) -> BaseQuery:
if resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
location_filter = ((InvolvedMarkerView.road1 == location_info["road1"]) | (InvolvedMarkerView.road2 == location_info["road1"])) & (InvolvedMarkerView.road_segment_name == location_info["road_segment_name"])
location_filter = (
(InvolvedMarkerView.road1 == location_info["road1"])
| (InvolvedMarkerView.road2 == location_info["road1"])
) & (InvolvedMarkerView.road_segment_name == location_info["road_segment_name"])
elif resolution == BE_CONST.ResolutionCategories.STREET:
location_filter = (InvolvedMarkerView.involve_yishuv_name == location_info["yishuv_name"]) & ((InvolvedMarkerView.street1_hebrew == location_info["street1_hebrew"]) | (InvolvedMarkerView.street2_hebrew == location_info["street1_hebrew"]))
location_filter = (
InvolvedMarkerView.involve_yishuv_name == location_info["yishuv_name"]
) & (
(InvolvedMarkerView.street1_hebrew == location_info["street1_hebrew"])
| (InvolvedMarkerView.street2_hebrew == location_info["street1_hebrew"])
)

query = (
db.session.query(InvolvedMarkerView)
Expand Down
13 changes: 7 additions & 6 deletions anyway/widgets/widget_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ def newsflash_has_location(newsflash: NewsFlash):
and newsflash.road_segment_name
) or (resolution == BE_CONST.ResolutionCategories.STREET.value and newsflash.street1_hebrew)

def get_location_text(request_params : RequestParams) -> str :
in_str = _("in")
if request_params.resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
return f'{_("in segment")} {_(request_params.location_info["road_segment_name"])}'
elif request_params.resolution == BE_CONST.ResolutionCategories.STREET:
return f'{_("in street")} {request_params.location_info["street1_hebrew"]} {in_str}{request_params.location_info["yishuv_name"]}'

def get_location_text(request_params: RequestParams) -> str:
in_str = _("in")
if request_params.resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD:
return f'{_("in segment")} {_(request_params.location_info["road_segment_name"])}'
elif request_params.resolution == BE_CONST.ResolutionCategories.STREET:
return f'{_("in street")} {request_params.location_info["street1_hebrew"]} {in_str}{request_params.location_info["yishuv_name"]}'
2 changes: 1 addition & 1 deletion tests/test_news_flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def assert_all_equal(items_actual, items_expected):
for k in to_dict(expected):
assert (i, getattr(actual, k)) == (i, getattr(expected, k))


@pytest.mark.skip
def test_scrape_walla():
# Reuters is marked differently than Walla's authors
items_expected = [
Expand Down

0 comments on commit 2e6d656

Please sign in to comment.