-
Notifications
You must be signed in to change notification settings - Fork 0
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
Linking tags and log records table #84
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
backend/app/models/log_record_tag.py
Outdated
class LogRecordTag(db.Model): | ||
__tablename__ = "log_record_tag" | ||
|
||
log_record_tag_id = db.Column(db.Integer, primary_key=True, nullable=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for consistency, I mentioned we should either do <table_name>_id or just id. I'm leaning towards switching everything to id. What are your thoughts?
The only reason for this switch is because the resident table has id and resident_id which is a little confusing at first.
backend/app/models/tag.py
Outdated
@@ -12,6 +12,7 @@ class Tag(db.Model): | |||
status = db.Column( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thinking this filename should be tags instead of tag (all of our models use plural instead of singular except user, which should also be updated)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should also be applied to the log_record_tags file
backend/app/models/tag.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realizing this now but name should probably be unique. There should be some validation (both in the database as a table constraint and the routes layer to validate that the name is unique).
backend/app/models/tag.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, maybe status isn't the best approach... it's creating unused data in the db which isn't a big deal in the grand scheme of things but its not ideal...
what if we remove the status column entirely, and do a delete similar to how log_records are handled? First, delete all the occurences of tag_id in the new table, log_record_tag, then delete the tag from its table? This would make sure we don't have unused data in the db and makes it easier on the user (we would've needed to show unused tags to the user as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the one complaint about this approach was that any log record with a specific tag would lose that tag, but we can throw a warning to the admin and indicate to them that every log with the selected tag will lose that tag
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this new approach would need to be handled on the API side so it doesn't need to be addressed here but we should have something concrete for #61
@@ -86,9 +99,9 @@ def filter_by_date_range(self, date_range): | |||
return f"\ndatetime>='{start_date}' AND datetime<='{end_date}'" | |||
|
|||
def filter_by_tags(self, tags): | |||
sql_statement = f"\n'{tags[0]}'=ANY (tags)" | |||
sql_statement = f"\n'{tags[0]}'=ANY (tag_names)" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when filtering, I'm noticing log records appear when they shouldn't. In this example, this is the query param: filters={"tags":["tagA", "TagB"]}
Here is the response:
{
"log_records": [
{
"attn_to": 1,
"attn_to_first_name": "Safwaan",
"attn_to_last_name": "Chowdhury",
"building": "Building D",
"datetime": "2023-06-13 15:15:40.849777-04:00",
"employee_first_name": "Safwaan",
"employee_id": 1,
"employee_last_name": "Chowdhury",
"flagged": false,
"log_id": 8,
"note": "This is a note",
"resident_first_name": "Saf",
"resident_last_name": "Waan",
"tags": [
"tagA"
]
},
{
"attn_to": 1,
"attn_to_first_name": "Safwaan",
"attn_to_last_name": "Chowdhury",
"building": "Building D",
"datetime": "2023-06-13 15:04:12.437682-04:00",
"employee_first_name": "Safwaan",
"employee_id": 1,
"employee_last_name": "Chowdhury",
"flagged": false,
"log_id": 7,
"note": "This is a note",
"resident_first_name": "Saf",
"resident_last_name": "Waan",
"tags": [
"tagA",
"tagB"
]
}
],
"num_results": 2
}
The first log record shouldn't appear, yes it does have tag A, but it doesn't have tag B. We should only show log records that have both tag A and tag B, and other tags if any. For example, suppose another log record existed with A, B, C- that log record should appear if A and B are filtered for.
Sorry if I explained it poorly the first time 😖
Future comment - investigate use of JSON_AGG for grouping tag row aggregation |
Visit the preview URL for this PR (updated for commit d1edb69): https://blueprintsupportivehousing--pr84-connor-linking-tags-ggf6u95y.web.app (expires Sat, 11 Nov 2023 00:18:27 GMT) 🔥 via Firebase Hosting GitHub Action 🌎 Sign: f6bcdba7452bf82a6ec1a299c37d1bdff7870d09 |
…ithub.com/uwblueprint/supportive-housing into connor/linking-tags-and-log-records-table
GitHub Issue link
Linking Tags and Log Records Table
Implementation description
log_record_tag
was used to link the existinglog_records
table and newtags
tableSteps to test
Setup
flask db upgrade
on the branch to ensure your local environment includes the new migrationsdocker exec -it SHOW-backend /bin/bash -c "flask db upgrade"
and insert the following tags:INSERT into tags (name, status) VALUES ('tagA', 'Active');
INSERT into tags (name, status) VALUES ('tagB, 'Active');
INSERT into tags (name, status) VALUES ('tagC', 'Active');
Create log record functionality
localhost:8080/log_records
with:{ "attn_to": 1, "building": "Building A", "datetime": "2023-03-25 08:34:56-04:00", "employee_id": 1, "flagged": false, "note": "This is a note", "resident_first_name": "Saf", "resident_last_name": "Waan", "tags": ["wrong"] }
select * from log_record_tag;
andselect * from log_records;
to ensure nothing was made"tags": ["tagA"]
,"tags": ["tagA", "tagB"]
, and"tags": ["tagA", "tagB", "tagC"]
. Once again, check the two tables in the previous step after every insert to ensure everything is being updated properlyGet log records functionality
http://localhost:8080/log_records?filters={"tags":["tagA"]}
should give you all three logshttp://localhost:8080/log_records?filters={"tags":["tagB"]}
should give you two logshttp://localhost:8080/log_records?filters={"tags":["tagC"]}
should give you one loghttp://localhost:8080/log_records?filters={"tags":["safwaan"]}
should give you no logshttp://localhost:8080/log_records?filters={"tags":["tagB", "tagC"]}
should give you two logsUpdate log records functionality
http://localhost:8080/log_records/{id}
where id is the id of your log record you want to update). For the body just send along the same one you used in the create section and change the tags as follows (after every request,select * from log_record_tag;
to make sure things were updated properly ):log_record_tag
table to be empty for that log IDtagA
,tagB
, andtagC
should be validDelete log records functionality
http://localhost:8080/log_records/{id}
where id is the id of your log record you want to update). Once this is successful, the corresponding entries in thelog_record_tag
table should also be deletedWhat should reviewers focus on?
log_records = db.relationship("LogRecords", secondary="log_record_tag", back_populates="tags")
. We should probably look into updating this eventually as it makes managing both tables a lot less painfulChecklist