Skip to content

Commit

Permalink
Merge pull request #2630 from data-for-change/dev
Browse files Browse the repository at this point in the history
merge dev into master
  • Loading branch information
tkalir authored Apr 23, 2024
2 parents d881d4e + 51313b6 commit 20516d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion anyway/telegram_accident_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def create_public_urls_for_infographics_images(folder_name):
presigned_urls[infographic_name] = url
return presigned_urls

@staticmethod

def trigger_generate_infographics_and_send_to_telegram(newsflash_id, pre_verification_chat=True):
dag_conf = {"news_flash_id": newsflash_id}
dag_conf["chat_id"] = TELEGRAM_CHANNEL_CHAT_ID if pre_verification_chat \
Expand Down
74 changes: 39 additions & 35 deletions anyway/views/news_flash/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import logging
import pandas as pd
import os

from typing import List, Optional
from http import HTTPStatus
Expand All @@ -13,7 +14,6 @@
from flask import request, Response, make_response, jsonify
from sqlalchemy import and_, not_, or_


from anyway.app_and_db import db
from anyway.backend_constants import (
BE_CONST,
Expand All @@ -31,14 +31,15 @@
from anyway.parsers import fields_to_resolution, resolution_dict
from anyway.models import AccidentMarkerView, InvolvedView
from anyway.widgets.widget_utils import get_accidents_stats
from anyway.telegram_accident_notifications import trigger_generate_infographics_and_send_to_telegram
from io import BytesIO

DEFAULT_OFFSET_REQ_PARAMETER = 0
DEFAULT_LIMIT_REQ_PARAMETER = 100
DEFAULT_NUMBER_OF_YEARS_AGO = 5

class NewsFlashQuery(BaseModel):

class NewsFlashQuery(BaseModel):
id: Optional[int]
road_number: Optional[int]
offset: Optional[int] = DEFAULT_OFFSET_REQ_PARAMETER
Expand Down Expand Up @@ -148,16 +149,16 @@ def news_flash_new(args: dict) -> List[dict]:


def gen_news_flash_query(
session,
source=None,
start_date=None,
end_date=None,
interurban_only=None,
road_number=None,
road_segment=None,
offset=None,
limit=None,
last_minutes=None
session,
source=None,
start_date=None,
end_date=None,
interurban_only=None,
road_number=None,
road_segment=None,
offset=None,
limit=None,
last_minutes=None
):
query = session.query(NewsFlash)
# get all possible sources
Expand Down Expand Up @@ -199,7 +200,7 @@ def gen_news_flash_query(
NewsFlash.accident == True,
not_(and_(NewsFlash.lat == 0, NewsFlash.lon == 0)),
not_(and_(NewsFlash.lat == None, NewsFlash.lon == None)),
)
)
).order_by(NewsFlash.date.desc())

query = query.offset(offset)
Expand Down Expand Up @@ -230,7 +231,7 @@ def gen_news_flash_query_v2(session, valid_params: dict):
NewsFlash.accident == True,
not_(and_(NewsFlash.lat == 0, NewsFlash.lon == 0)),
not_(and_(NewsFlash.lat == None, NewsFlash.lon == None)),
)
)
).order_by(NewsFlash.date.desc())
query = query.offset(valid_params["offset"])
query = query.limit(valid_params["limit"])
Expand Down Expand Up @@ -284,14 +285,14 @@ def filter_by_resolutions(query, resolutions: List[str]):
and_(
NewsFlash.resolution == BE_CONST.ResolutionCategories.SUBURBAN_ROAD.value,
NewsFlash.road_segment_name != None,
)
)
)
if "street" in resolutions:
ands.append(
and_(
NewsFlash.resolution == BE_CONST.ResolutionCategories.STREET.value,
NewsFlash.street1_hebrew != None,
)
)
)
if len(ands) > 1:
return query.filter(or_(*ands))
Expand All @@ -312,12 +313,12 @@ def normalize_query(params: dict):


def update_location_verification_history(
user_id: int,
news_flash_id: int,
prev_location: str,
prev_qualification: int,
new_location: str,
new_qualification: int,
user_id: int,
news_flash_id: int,
prev_location: str,
prev_qualification: int,
new_location: str,
new_qualification: int,
):
new_location_qualifiction_history = LocationVerificationHistory(
user_id=user_id,
Expand Down Expand Up @@ -387,7 +388,7 @@ def update_news_flash_qualifying(id):
)
else:
if ((news_flash_obj.road_segment_name is None) or (news_flash_obj.road1 is None)) and (
(news_flash_obj.yishuv_name is None) or (news_flash_obj.street1_hebrew is None)
(news_flash_obj.yishuv_name is None) or (news_flash_obj.street1_hebrew is None)
):
logging.error("try to set qualification on empty location.")
return return_json_error(Es.BR_BAD_FIELD)
Expand All @@ -406,6 +407,10 @@ def update_news_flash_qualifying(id):
new_location=new_location,
new_qualification=new_location_qualifiction,
)
if os.environ.get("FLASK_ENV") == "production" and \
new_location_qualifiction == NewsflashLocationQualification.MANUAL.value and \
old_location_qualifiction != NewsflashLocationQualification.MANUAL.value:
trigger_generate_infographics_and_send_to_telegram(id, False)
return Response(status=HTTPStatus.OK)


Expand Down Expand Up @@ -465,21 +470,20 @@ def get_downloaded_data(format, years_ago):
columns[AccidentMarkerView.x] = 'X קואורדינטה'
columns[AccidentMarkerView.y] = 'Y קואורדינטה'


related_accidents = get_accidents_stats(
table_obj=AccidentMarkerView,
columns=columns.keys(),
filters=request_params.location_info,
start_time=start_time,
end_time=end_time
)
table_obj=AccidentMarkerView,
columns=columns.keys(),
filters=request_params.location_info,
start_time=start_time,
end_time=end_time
)
accident_ids = list(related_accidents['id'].values())
accident_severities = get_accidents_stats(
table_obj=InvolvedView,
group_by=("accident_id", "injury_severity_hebrew"),
count="injury_severity_hebrew",
filters={"accident_id": accident_ids}
)
table_obj=InvolvedView,
group_by=("accident_id", "injury_severity_hebrew"),
count="injury_severity_hebrew",
filters={"accident_id": accident_ids}
)

severities_hebrew = set()
for i, accident_id in enumerate(accident_ids):
Expand Down

0 comments on commit 20516d3

Please sign in to comment.