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

Soft delete assets from deleted facilities/hospitals #1996

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from

Conversation

hrit2773
Copy link
Contributor

Fixes #1982 issue, enabled soft delete of assets by overriding delete method in facility model

Copy link
Member

@rithviknishad rithviknishad left a comment

Choose a reason for hiding this comment

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

No changes to the fields in the model were made. But why are there migrations for it?

@hrit2773
Copy link
Contributor Author

hrit2773 commented Mar 21, 2024

@rithviknishad I was initially using models.cascade but later I realised I need to perform soft delete so I changed it back to original ones so that's why you can see those redundant migrations.But the functionality is working. Should I remove those redundant migrations?

@hrit2773
Copy link
Contributor Author

@rithviknishad I made a second commit i removed those redundant migration history so effectively i just added the delete method in facility model

Copy link
Member

@rithviknishad rithviknishad left a comment

Choose a reason for hiding this comment

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

Custom migration needed to delete existing such assets of deleted facility.

https://docs.djangoproject.com/en/5.0/ref/migration-operations/#runpython

@rithviknishad
Copy link
Member

Also test cases needs to be added. It'd be great if you could follow the PR template 😄

@hrit2773
Copy link
Contributor Author

@rithviknishad ya sure I'll make the changes by today

@hrit2773
Copy link
Contributor Author

hrit2773 commented Mar 24, 2024

@rithviknishad I added custom migrations and also added api test case but the tests are not running in my machine because im using windows and im probably getting some connection errors with the test db like some dbus connection errors and no such bucket found. It is giving a failed test for medibase api maybe some db connection issues in windows

@hrit2773 hrit2773 marked this pull request as draft March 24, 2024 10:25
Copy link

codecov bot commented Mar 24, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 62.38%. Comparing base (51ce81e) to head (1cfed7f).
Report is 61 commits behind head on develop.

❗ Current head 1cfed7f differs from pull request most recent head 691f9e8. Consider uploading reports for the commit 691f9e8 to get more accurate results

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1996      +/-   ##
===========================================
+ Coverage    62.20%   62.38%   +0.18%     
===========================================
  Files          221      221              
  Lines        12204    12229      +25     
  Branches      1742     1746       +4     
===========================================
+ Hits          7591     7629      +38     
+ Misses        4305     4284      -21     
- Partials       308      316       +8     

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

@hrit2773 hrit2773 marked this pull request as ready for review March 24, 2024 10:50
Comment on lines 177 to 182
def delete(self, *args, **kwargs):
from care.facility.models.asset import Asset, AssetLocation

Asset.objects.filter(current_location__facility=self).update(deleted=True)
super().delete(self, *args, **kwargs)

Copy link
Member

Choose a reason for hiding this comment

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

this will skip

def delete(self, *args, **kwargs):
from care.facility.models.bed import AssetBed
AssetBed.objects.filter(asset=self).update(deleted=True)
super().delete(*args, **kwargs)

a better approach would be to make a cron job that regularly checks for deleted objects and recursively soft deletes related objects

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sainak but if you check recursively it'll slow down and degrade the performance

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sainak and that cron job would run in Linux but not in windows I guess so I can't test it instead what I can do is to delete it in an atomic way maybe

Copy link
Member

Choose a reason for hiding this comment

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

by cron job, I mean celerybeat cron task

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sainak ok how frequent the check should be every minute or every hour or every 24 hrs?

Copy link
Member

Choose a reason for hiding this comment

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

let's run it every 24 hour

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sainak okay I will do it

@hrit2773
Copy link
Contributor Author

@sainak plz review the commit...

@hrit2773
Copy link
Contributor Author

@sainak @rithviknishad i added the cron job and should i remove the tests also for soft delete that i created because now that wont work right and maybe the custom migration is also not needed because every midnight the assets will be soft deleted and should i add logging infos and warnings in the task function? plz review it and tell me if any changes required

@rithviknishad
Copy link
Member

rithviknishad commented Mar 27, 2024

@hrit2773 You can remove the custom migration since it'd be taken care by the celery task itself right.

Comment on lines 14 to 15
facilities = Facility.objects.all().values_list("name", flat=True)
Asset.objects.exclude(current_location__facility__name__in=facilities).update(
Copy link
Member

Choose a reason for hiding this comment

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

you can directly use the id instead of name

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sainak yes yes we can I'll do it

@hrit2773
Copy link
Contributor Author

@hrit2773 You can remove the custom migration since it'd be taken care by the celery task itself right.

@rithviknishad yes I'll remove it and I'll remove the api test case also because I feel that is not needed

@hrit2773
Copy link
Contributor Author

@rithviknishad @sainak made the required changes plz review these changes

Comment on lines 13 to 17
def soft_delete_assets_schedule():
facilities = Facility.objects.all().values_list("external_id", flat=True)
Asset.objects.exclude(
current_location__facility__external_id__in=facilities
).update(deleted=True)
Copy link
Member

Choose a reason for hiding this comment

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

We could simply rely on the PK instead of the UUID right? @sainak

Suggested change
def soft_delete_assets_schedule():
facilities = Facility.objects.all().values_list("external_id", flat=True)
Asset.objects.exclude(
current_location__facility__external_id__in=facilities
).update(deleted=True)
def soft_delete_assets_schedule():
facilities = Facility.objects.all().values_list("id", flat=True)
Asset.objects.exclude(
current_location__facility__id__in=facilities
).update(deleted=True)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rithviknishad we can do that also but as the filterset class also takes the external_id I considered external id just to avoid any existing wrong entry in the database to spoil the functionality otherwise we can do that

Copy link
Member

Choose a reason for hiding this comment

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

use id as we try to stick with id for internal queries

Copy link
Member

@sainak sainak Mar 30, 2024

Choose a reason for hiding this comment

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

we register tasks in care/facility/tasks/__init__.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sainak so should i delete soft_delete_assets.py file and write the same function in the init.py?

Copy link
Member

Choose a reason for hiding this comment

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

no delete the celerybeat_schecule.py and revert changes in celery_app.py

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sainak ok i will do that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sainak should i change the settings config also because how will it identify that the task should run every 24 hours. I have gone through the init.py file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should make changes in the init.py also right and in the soft_delete_assets.py i should mark the function using shared task decorator because it should identify that this task should run every 24 hours right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will make the changes by today

@hrit2773
Copy link
Contributor Author

@sainak view the changes.. and also replaced external_id with id

@hrit2773
Copy link
Contributor Author

hrit2773 commented Apr 1, 2024

@sainak can you review the changes

@hrit2773 hrit2773 requested a review from a team as a code owner April 4, 2024 08:16
@hrit2773
Copy link
Contributor Author

hrit2773 commented Apr 4, 2024

@rithviknishad @sainak lets close this issue today

@hrit2773
Copy link
Contributor Author

@sainak @vigneshhari can you review this PR

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.

Asset list- shows assets from deleted hospitals
3 participants