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

Conversation

ziv17
Copy link
Collaborator

@ziv17 ziv17 commented Jan 12, 2025

WIP, /involved api implementing Atalya's comment from 9-Jan on issue 2734.
Added load of safety_data involved and accident tables to the main.py process cbs command line.
No fine detail testing yet. Just to verify the query parameter and returned data.
You can view the API in swagger hub

@ziv17 ziv17 requested a review from atalyaalon January 12, 2025 05:29
@codecov-commenter
Copy link

codecov-commenter commented Jan 12, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 67.78846% with 67 lines in your changes missing coverage. Please review.

Project coverage is 52.89%. Comparing base (400af13) to head (91740a7).
Report is 162 commits behind head on dev.

Files with missing lines Patch % Lines
anyway/views/safety_data/involved_query.py 54.05% 51 Missing ⚠️
anyway/flask_app.py 14.28% 12 Missing ⚠️
anyway/views/safety_data/sd_utils.py 90.47% 4 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #2760      +/-   ##
==========================================
- Coverage   53.22%   52.89%   -0.34%     
==========================================
  Files         119      124       +5     
  Lines        9924    10440     +516     
==========================================
+ Hits         5282     5522     +240     
- Misses       4642     4918     +276     
Flag Coverage Δ
unittests 52.89% <67.78%> (-0.34%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ziv17 ziv17 requested a review from citizen-dror January 12, 2025 17:30
)
return Response(json.dumps(res, default=str), mimetype="application/json")

def sd_load_data():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since SDAccident and SDInvolved are related in foreign key, and we can't have an involved in DB w/o a related accident, then the deletion/insertion order should be changed to the following:

  1. delete involved
  2. delete accidents
  3. insert accidents
  4. insert involved

Otherwise I believe we can encounter foreign key errors in DB

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition, I will add temporary table, fill them, and then copy to the actual, in order to avoid downtime.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure temporary table is necessary.
Can we possible perform it all in the same transaction (like we discussed here on CBS hebrew views)?

If we have no choice, we can add temporary tables.

@@ -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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ForeignKeyConstraint should be added to table model class, just like in Involved table

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And perhaps also ForeignKey like in LocationVefiricationHistory

population: integer
```
## `/involved` Query
The query will join `data_safety_involved` and `data_safety_accident` using `accident_id`. In addition, the query will join
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using accident_id, provider_code and accident_year as foreign keys

Copy link
Collaborator

@atalyaalon atalyaalon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ziv17 all in all looks good. Well done!
I added a few comments.

In addition:

  1. I suggest using AccidentMarker instead of AccidentMarkerView. I assume that we won't have road_segment number for this table, but I suggest we'll handle this in a later PR, and for now I suggest keeping a placeholder for road_segment.
    Let me know if there are other challenges from using AccidentMarker instead of AccidentMarkerView.

  2. Do we keep data consistency for DB for these tables?
    (We had a similar discussion here)
    I think that in the case above with db.get_engine().begin() as conn: kept data consistency.
    I believe it can also be done with using db.session.commit() only after finishing deletions insertions.

  3. For better practice, I suggest you consider adding relationships for dimension tables. However I suggest adding it in a future pr, when we decide to handle the issue across our db, and not in this issue. I opened an issue for it here

@atalyaalon
Copy link
Collaborator

@ziv17 all in all looks good. Added a few comments, we can also discuss in tomorrow's meeting

@atalyaalon atalyaalon linked an issue Jan 21, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Safety Data new API
3 participants