Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ssafty-data api involved #2760

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions alembic/versions/99364b16374f_add_safety_data_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"""Add safety-data tables

Revision ID: 99364b16374f
Revises: e962054e4422
Create Date: 2024-11-15 11:03:26.435210

"""

# revision identifiers, used by Alembic.
revision = '99364b16374f'
down_revision = 'e962054e4422'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa

sd_involved_table = "safety_data_involved"
sd_accident_table = "safety_data_accident"


def downgrade():
op.drop_table(sd_involved_table) # pylint: disable=no-member
op.drop_table(sd_accident_table) # pylint: disable=no-member


def upgrade():
create_safety_data_accident_table()
create_safety_data_involved_table()

def create_safety_data_accident_table():
op.create_table( # pylint: disable=no-member
sd_accident_table,
sa.Column('accident_id', sa.BigInteger(), primary_key=True, autoincrement=False, nullable=False),
sa.Column('accident_year', sa.Integer(), primary_key=True, autoincrement=False, nullable=False),
sa.Column('provider_code', sa.Integer(), primary_key=True, autoincrement=False, nullable=False),
sa.Column('accident_month', sa.Integer()),
sa.Column('accident_timestamp', sa.TIMESTAMP()),
sa.Column('accident_type', sa.Integer(), nullable=True),
sa.Column('accident_yishuv_symbol', sa.Integer(), nullable=True),
sa.Column('day_in_week', sa.Integer(), nullable=True),
sa.Column('day_night', sa.Integer(), nullable=True),
sa.Column('location_accuracy', sa.Integer(), nullable=True),
sa.Column('multi_lane', sa.Integer(), nullable=True),
sa.Column('one_lane', sa.Integer(), nullable=True),
sa.Column('road1', sa.Integer(), nullable=True),
sa.Column('road2', sa.Integer(), nullable=True),
sa.Column('road_segment_number', sa.Integer(), nullable=True),
sa.Column('road_type', sa.Integer(), nullable=True),
sa.Column('road_width', sa.Integer(), nullable=True),
sa.Column('speed_limit', sa.Integer(), nullable=True),
sa.Column('street1', sa.Integer(), nullable=True),
sa.Column('street2', sa.Integer(), nullable=True),
sa.Column('vehicles', sa.Integer(), nullable=True),
sa.Column('latitude', sa.Float(), nullable=True),
sa.Column('longitude', sa.Float(), nullable=True),
)
for field in ['accident_year', 'accident_month', 'accident_timestamp',
'accident_type', 'accident_yishuv_symbol',
'day_night', 'multi_lane', 'one_lane',
'road1', 'road2', 'road_segment_number', 'road_type', 'road_width',
'speed_limit',
'street1', 'street2',
'latitude', 'longitude',
]:
# pylint: disable=no-member
op.create_index(op.f(f'ix_{sd_accident_table}_{field}'), sd_accident_table, [field], unique=False)

def create_safety_data_involved_table():
op.create_table( # pylint: disable=no-member
sd_involved_table,
sa.Column('_id', sa.Integer(), primary_key=True, autoincrement=False, nullable=False),
sa.Column('accident_id', sa.BigInteger(), nullable=False),
sa.Column('accident_year', sa.Integer(), nullable=False),
sa.Column('provider_code', sa.Integer(), nullable=False),
sa.Column('age_group', sa.Integer(), nullable=True),
sa.Column('injured_type', sa.Integer(), nullable=True),
sa.Column('injury_severity', sa.Integer(), nullable=True),
sa.Column('population_type', sa.Integer(), nullable=True),
sa.Column('sex', sa.Integer(), nullable=True),
sa.Column('vehicle_type', sa.Integer(), nullable=True),
)
op.create_foreign_key(f'{sd_involved_table}_accident_id_fkey', # pylint: disable=no-member
sd_involved_table, sd_accident_table,
['accident_id', 'provider_code', 'accident_year'],
['accident_id', 'provider_code', 'accident_year'],
ondelete='CASCADE')
op.create_index(op.f(f'ix_{sd_involved_table}_inv_acc'), sd_involved_table, # pylint: disable=no-member
['accident_id', 'accident_year', 'provider_code'], unique=False)
for field in ['injury_severity', 'injured_type',
'age_group', 'sex', 'population_type', 'vehicle_type']:
# pylint: disable=no-member
op.create_index(op.f(f'ix_{sd_involved_table}_{field}'), sd_involved_table, [field], unique=False)

24 changes: 23 additions & 1 deletion anyway/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@
Streets,
Comment,
TelegramForwardedMessages,
NewsFlash
NewsFlash,
Sex,
RoadLight,
RoadType,
AgeGroup,
)
from anyway.request_params import get_request_params_from_request_values
from anyway.views.news_flash.api import (
Expand Down Expand Up @@ -1551,3 +1555,21 @@ def test_roles_func():


app.add_url_rule("/api/test_roles", endpoint=None, view_func=test_roles, methods=["GET"])


@app.route("/involved", methods=["GET"])
def safety_data_test():
from anyway.views.safety_data import sd_utils as sdu
from anyway.views.safety_data import involved_query as ac

iq = ac.InvolvedQuery()
try:
res = iq.get_data()
return Response(json.dumps(res, default=str), mimetype="application/json")
except ValueError as e:
logging.exception(e)
return abort(http_client.BAD_REQUEST)
except Exception as e:
logging.exception(e)
return abort(http_client.INTERNAL_SERVER_ERROR)

41 changes: 41 additions & 0 deletions anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3081,3 +3081,44 @@ class TelegramForwardedMessages(Base):
message_id = Column(String(), primary_key=True)
newsflash_id = Column(BigInteger(), nullable=False)
group_sent = Column(String(), nullable=False)


class SDInvolved(Base):
__tablename__ = "safety_data_involved"
_id = Column(Integer(), primary_key=True)
accident_id = Column(BigInteger())
accident_year = Column(Integer())
provider_code = Column(Integer())
age_group = Column(Integer(), nullable=True)
injured_type = Column(Integer(), nullable=True)
injury_severity = Column(Integer(), nullable=True)
population_type = Column(Integer(), nullable=True)
sex = Column(Integer(), nullable=True)
vehicle_type = Column(Integer(), nullable=True)


class SDAccident(Base):
__tablename__ = "safety_data_accident"
accident_id = Column(Integer(), primary_key=True, nullable=False)
accident_year = Column(Integer(), primary_key=True, nullable=False)
provider_code = Column(Integer(), primary_key=True, nullable=False)
accident_month = Column(Integer())
accident_timestamp = Column(DateTime, default=None, index=True)
accident_type = Column(Integer(), nullable=True)
accident_yishuv_symbol = Column(Integer(), nullable=True)
day_in_week = Column(Integer(), nullable=True)
day_night = Column(Integer(), nullable=True)
location_accuracy = Column(Integer(), nullable=True)
multi_lane = Column(Integer(), nullable=True)
one_lane = Column(Integer(), nullable=True)
road1 = Column(Integer(), nullable=True)
road2 = Column(Integer(), nullable=True)
road_segment_number = Column(Integer(), nullable=True)
road_type = Column(Integer(), nullable=True)
road_width = Column(Integer(), nullable=True)
speed_limit = Column(Integer(), nullable=True)
street1 = Column(Integer(), nullable=True)
street2 = Column(Integer(), nullable=True)
vehicles = Column(Integer(), nullable=True)
latitude = Column(Float(), nullable=True)
longitude = Column(Float(), nullable=True)
4 changes: 4 additions & 0 deletions anyway/parsers/cbs/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
from anyway.db_views import VIEWS
from anyway.app_and_db import db
from anyway.parsers.cbs.s3 import S3DataRetriever
from anyway.views.safety_data import sd_utils

street_map_type: Dict[int, List[dict]]

Expand Down Expand Up @@ -1115,6 +1116,9 @@ def main(batch_size, source, load_start_year=None):
logging.debug("Finished Creating Hebrew DB Tables")
recreate_table_for_location_extraction()
logging.debug("Finished Recreating tables for location extraction")
logging.debug("Loading safety data tables")
sd_utils.sd_load_data()
logging.debug("Completed load of safety data tables")
except Exception as ex:
print("Traceback: {0}".format(traceback.format_exc()))
raise CBSParsingFailed(message=str(ex))
Expand Down
Loading
Loading