Repository: data-for-change/anyway

Issue:
Add front collision count relative to other accidents count to infographics api
Add front collision count relative to other accidents count to infographics api


Discussion:
@BarVolunteering please add the image as well
This issue is about implementing the 5th widget(marked in red)
![image](https://user-images.githubusercontent.com/58947331/80024521-e0ad7f80-84e7-11ea-8571-d20eb66556d3.png)


Resolving patch:
diff --git a/anyway/constants.py b/anyway/constants.py
--- a/anyway/constants.py
+++ b/anyway/constants.py
@@ -23,6 +23,12 @@ class Constants(object):
 
     ALL_AGE_GROUPS_LIST = list(range(1, AGE_GROUPS_NUMBER + 1)) + [99]
 
+    # This is a type for the 'accident_severity' table field name
+    ACCIDENT_SEVERITY_DEADLY = 1
+
+    # This is a type for the 'road_type' table field name
+    ROAD_TYPE_NOT_IN_CITY_NOT_IN_INTERSECTION = 4
+
     # other global constants (python only)
     DEFAULT_NUMBER_OF_YEARS_AGO = 5
 
diff --git a/anyway/infographics_utils.py b/anyway/infographics_utils.py
--- a/anyway/infographics_utils.py
+++ b/anyway/infographics_utils.py
@@ -346,6 +346,59 @@ def get_news_flash_location_text(news_flash_id):
     logging.debug('{}'.format(res))
     return res
 
+
+def extract_news_flash_obj(news_flash_id):
+    news_flash_obj = db.session.query(NewsFlash).filter(
+        NewsFlash.id == news_flash_id).first()
+
+    if not news_flash_obj:
+        logging.warning('Could not find news flash id {}'.format(news_flash_id))
+        return None
+
+    return news_flash_obj
+
+
+def sum_road_accidents_by_specific_type(road_data, field_name):
+    dict_merge = defaultdict(int)
+    for accident_data in road_data:
+        if accident_data['accident_type'] == field_name:
+            dict_merge[field_name] += accident_data['count']
+        else:
+            dict_merge['תאונות אחרות'] += accident_data['count']
+    return dict_merge
+
+
+def convert_roads_fatal_accidents_to_frontend_view(data_dict):
+    data_list = []
+    for key, value in data_dict.items():
+        data_list.append({'desc': key, 'count': value})
+    return data_list
+
+
+def get_head_to_head_stat(news_flash_id, start_time, end_time):
+    news_flash_obj = extract_news_flash_obj(news_flash_id)
+    road_data = {}
+    filter_dict = {'road_type': CONST.ROAD_TYPE_NOT_IN_CITY_NOT_IN_INTERSECTION,
+                   'accident_severity': CONST.ACCIDENT_SEVERITY_DEADLY}
+    all_roads_data = get_accidents_stats(table_obj=AccidentMarkerView,
+                                         filters=filter_dict,
+                                         group_by='accident_type_hebrew', count='accident_type_hebrew',
+                                         start_time=start_time, end_time=end_time)
+
+    if news_flash_obj.road1 and news_flash_obj.road_segment_name:
+        filter_dict.update({'road1': news_flash_obj.road1, 'road_segment_name': news_flash_obj.road_segment_name})
+        road_data = get_accidents_stats(table_obj=AccidentMarkerView,
+                                        filters=filter_dict,
+                                        group_by='accident_type_hebrew', count='accident_type_hebrew',
+                                        start_time=start_time, end_time=end_time)
+
+    road_data_dict = sum_road_accidents_by_specific_type(road_data, 'התנגשות חזית בחזית')
+    all_roads_data_dict = sum_road_accidents_by_specific_type(all_roads_data, 'התנגשות חזית בחזית')
+
+    return {'specific_road_segment_fatal_accidents': convert_roads_fatal_accidents_to_frontend_view(road_data_dict),
+            'all_roads_fatal_accidents': convert_roads_fatal_accidents_to_frontend_view(all_roads_data_dict)}
+
+
 def create_infographics_data(news_flash_id, number_of_years_ago):
     output = {}
     try:
@@ -410,12 +463,13 @@ def create_infographics_data(news_flash_id, number_of_years_ago):
                                 'latitude': gps['lat']})
     output['widgets'].append(street_view.serialize())
 
-
-    # vision zero
-    vision_zero = Widget(name='vision_zero',
-                         rank=5,
-                         items=['vision_zero_2_plus_1'])
-    output['widgets'].append(vision_zero.serialize())
+    # head to head accidents
+    head_on_collisions_comparison = Widget(name='head_on_collisions_comparison',
+                                    rank=5,
+                                    items=get_head_to_head_stat(news_flash_id=news_flash_id,
+                                                                start_time=start_time,
+                                                                end_time=end_time))
+    output['widgets'].append(head_on_collisions_comparison.serialize())
 
     # accident_type count
     accident_count_by_accident_type = Widget(name='accident_count_by_accident_type',
@@ -517,4 +571,10 @@ def create_infographics_data(news_flash_id, number_of_years_ago):
                                          items=data_of_injured_count_per_age_group)
     output['widgets'].append(injured_count_per_age_group.serialize())
 
+    # vision zero
+    vision_zero = Widget(name='vision_zero',
+                         rank=15,
+                         items=['vision_zero_2_plus_1'])
+    output['widgets'].append(vision_zero.serialize())
+
     return Response(json.dumps(output, default=str), mimetype="application/json")


Test patch: