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

Just to debug tests #2489

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ develop-eggs
lib
lib64

# newsflash-ifografics
anyway-newsflash-infographics/anyway-newsflash-infographics/

# Installer logs
pip-log.txt

Expand Down
36 changes: 36 additions & 0 deletions alembic/versions/7ea883c8a245_add_road_junction_km_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""add suburban junction table

Revision ID: 7ea883c8a245
Revises: 664f8a93794e
Create Date: 2023-06-04 17:43:13.170728

"""

# revision identifiers, used by Alembic.
revision = '7ea883c8a245'
down_revision = '664f8a93794e'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


# noinspection PyUnresolvedReferences
def upgrade():
op.create_table("road_junction_km",
sa.Column('road', sa.BigInteger(), nullable=False),
sa.Column('non_urban_intersection', sa.Integer(), nullable=False),
sa.Column('km', sa.Float(), nullable=False),
sa.PrimaryKeyConstraint('road', 'non_urban_intersection')
)
op.create_index('road_junction_km_idx',
"road_junction_km",
['road', 'non_urban_intersection'],
unique=True)


# noinspection PyUnresolvedReferences
def downgrade():
op.drop_index(op.f('road_junction_km_idx'), table_name="road_junction_km")
op.drop_table("road_junction_km")
2 changes: 1 addition & 1 deletion anyway/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
SQLALCHEMY_TRACK_MODIFICATIONS = True
ENTRIES_PER_PAGE = os.environ.get("ENTRIES_PER_PAGE", 1000)
SQLALCHEMY_ENGINE_OPTIONS = {}
# SQLALCHEMY_ECHO = True
SQLALCHEMY_ECHO = True
# available languages
LANGUAGES = {"en": "English", "he": "עברית"}

Expand Down
15 changes: 15 additions & 0 deletions anyway/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,21 @@ def serialize(self):
}


class RoadJunctionKM(Base):
__tablename__ = "road_junction_km"
MAX_NAME_LEN = 100
road = Column(Integer(), primary_key=True, nullable=False)
non_urban_intersection = Column(Integer(), primary_key=True, nullable=False)
km = Column(Float(), nullable=False)

def serialize(self):
return {
"road": self.road,
"non_urban_intersection": self.non_urban_intersection,
"km": self.km,
}


class RegisteredVehicle(Base):
__tablename__ = "cities_vehicles_registered"
id = Column(Integer(), primary_key=True)
Expand Down
35 changes: 35 additions & 0 deletions anyway/parsers/cbs/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
RoadSign,
RoadLight,
RoadControl,
RoadJunctionKM,
Weather,
RoadSurface,
RoadObjecte,
Expand Down Expand Up @@ -570,6 +571,7 @@ def import_accidents(provider_code, accidents, streets, roads, non_urban_interse
for _, accident in accidents.iterrows():
marker = create_marker(provider_code, accident, streets, roads, non_urban_intersection)
add_suburban_junction_from_marker(marker)
add_road_junction_km_from_marker(marker)
accidents_result.append(marker)
db.session.bulk_insert_mappings(AccidentMarker, accidents_result)
db.session.commit()
Expand Down Expand Up @@ -795,6 +797,8 @@ def import_streets_into_db():
yishuv_name_dict: Dict[Tuple[int, str], int] = {}
suburban_junctions_dict: Dict[int, dict] = {}
SUBURBAN_JUNCTION = "suburban_junction"
# (road, junction) -> km
road_junction_km_dict: Dict[Tuple[int, int], int] = {}


def load_existing_streets():
Expand Down Expand Up @@ -903,6 +907,35 @@ def add_suburban_junction_from_marker(marker: dict):
add_suburban_junction(j)


def load_existing_road_junction_km_data():
rows: List[RoadJunctionKM] = db.session.query(RoadJunctionKM).all()
tmp = {(r.road, r.non_urban_intersection): r.km for r in rows}
road_junction_km_dict.update(tmp)
logging.debug(f"Loaded road-junction-km rows: {len(tmp)}.")


def import_road_junction_km_into_db():
items = [{"road": k[0], "non_urban_intersection": k[1], "km": v} for
k, v in road_junction_km_dict.items()]
logging.debug(
f"Writing to db: {len(items)} road junction km rows"
)
db.session.query(RoadJunctionKM).delete()
db.session.bulk_insert_mappings(RoadJunctionKM, items)
db.session.commit()
logging.debug(f"Done.")


def add_road_junction_km_from_marker(marker: dict):
intersection = marker[NON_URBAN_INTERSECTION]
if intersection is not None:
k, v = (marker["road1"], intersection), marker["km"]
exists = road_junction_km_dict.get(k)
if exists is not None and exists != v:
logging.warning(f"Changed road junction km: from {exists} to {v}.")
road_junction_km_dict[k] = v


def delete_invalid_entries(batch_size):
"""
deletes all markers in the database with null latitude or longitude
Expand Down Expand Up @@ -1178,6 +1211,7 @@ def main(batch_size, source, load_start_year=None):
try:
load_existing_streets()
load_existing_suburban_junctions()
load_existing_road_junction_km_data()
total = 0
started = datetime.now()
if source == "s3":
Expand Down Expand Up @@ -1235,6 +1269,7 @@ def main(batch_size, source, load_start_year=None):

import_streets_into_db()
import_suburban_junctions_into_db()
import_road_junction_km_into_db()

fill_db_geo_data()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_infographics_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from anyway.widgets.widget_utils import format_2_level_items
from anyway.backend_constants import AccidentSeverity


# comment
class TestInfographicsUtilsCase(unittest.TestCase):
item1 = {
"2019": {
Expand Down
Loading