-
Notifications
You must be signed in to change notification settings - Fork 63
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
[Core] Port 11686 Reduce cases of mass deletion #1363
Open
yaelibarg
wants to merge
5
commits into
main
Choose a base branch
from
PORT-11686-investigate-options-to-reduce-cases-of-mass-deletion-from-integration-transfer-go
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+100
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
43c64bc
created a check for mass deletion and added checks
yaelibarg eb02460
lint fix
yaelibarg 1964b59
added entityDeletionThreshold flag to the mapping model
yaelibarg 7791d8c
fixed name of flag
yaelibarg 94fc83b
Merge branch 'main' into PORT-11686-investigate-options-to-reduce-cas…
Tankilevitch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -78,6 +78,7 @@ async def delete_diff( | |||||||||||
self, | ||||||||||||
entities: EntityDiff, | ||||||||||||
user_agent_type: UserAgentType, | ||||||||||||
entity_deletion_threshold: float = 0.9, | ||||||||||||
) -> None: | ||||||||||||
diff = get_port_diff(entities["before"], entities["after"]) | ||||||||||||
|
||||||||||||
|
@@ -87,10 +88,21 @@ async def delete_diff( | |||||||||||
kept_entities = diff.created + diff.modified | ||||||||||||
|
||||||||||||
logger.info( | ||||||||||||
f"Determining entities to delete ({len(diff.deleted)}/{len(kept_entities)})" | ||||||||||||
f"Determining entities to delete ({len(diff.deleted)}/{len(kept_entities)})", | ||||||||||||
deleting_entities=len(diff.deleted), | ||||||||||||
keeping_entities=len(kept_entities), | ||||||||||||
) | ||||||||||||
Comment on lines
+93
to
94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
await self._safe_delete(diff.deleted, kept_entities, user_agent_type) | ||||||||||||
delete_rate = len(diff.deleted) / len(entities["before"]) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets use another parameter name |
||||||||||||
if delete_rate <= entity_deletion_threshold: | ||||||||||||
await self._safe_delete(diff.deleted, kept_entities, user_agent_type) | ||||||||||||
else: | ||||||||||||
logger.info( | ||||||||||||
f"Skipping deletion of entities with delete rate {delete_rate}", | ||||||||||||
delete_rate=delete_rate, | ||||||||||||
deleting_entities=len(diff.deleted), | ||||||||||||
total_entities=len(entities), | ||||||||||||
) | ||||||||||||
|
||||||||||||
async def upsert( | ||||||||||||
self, entities: list[Entity], user_agent_type: UserAgentType | ||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
port_ocean/tests/core/handlers/entities_state_applier/test_applier.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from unittest.mock import Mock, patch | ||
import pytest | ||
from port_ocean.core.handlers.entities_state_applier.port.applier import ( | ||
HttpEntitiesStateApplier, | ||
) | ||
from port_ocean.core.models import Entity | ||
from port_ocean.core.ocean_types import EntityDiff | ||
from port_ocean.clients.port.types import UserAgentType | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_delete_diff_no_deleted_entities() -> None: | ||
applier = HttpEntitiesStateApplier(Mock()) | ||
entities = EntityDiff( | ||
before=[Entity(identifier="1", blueprint="test")], | ||
after=[Entity(identifier="1", blueprint="test")], | ||
) | ||
|
||
with patch.object(applier, "_safe_delete") as mock_safe_delete: | ||
await applier.delete_diff(entities, UserAgentType.exporter) | ||
|
||
mock_safe_delete.assert_not_called() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_delete_diff_below_threshold() -> None: | ||
applier = HttpEntitiesStateApplier(Mock()) | ||
entities = EntityDiff( | ||
before=[ | ||
Entity(identifier="1", blueprint="test"), | ||
Entity(identifier="2", blueprint="test"), | ||
Entity(identifier="3", blueprint="test"), | ||
], | ||
after=[ | ||
Entity(identifier="1", blueprint="test"), | ||
Entity(identifier="2", blueprint="test"), | ||
], | ||
) | ||
|
||
with patch.object(applier, "_safe_delete") as mock_safe_delete: | ||
await applier.delete_diff(entities, UserAgentType.exporter) | ||
|
||
mock_safe_delete.assert_called_once() | ||
assert len(mock_safe_delete.call_args[0][0]) == 1 | ||
assert mock_safe_delete.call_args[0][0][0].identifier == "3" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_delete_diff_above_default_threshold() -> None: | ||
applier = HttpEntitiesStateApplier(Mock()) | ||
entities = EntityDiff( | ||
before=[ | ||
Entity(identifier="1", blueprint="test"), | ||
Entity(identifier="2", blueprint="test"), | ||
Entity(identifier="3", blueprint="test"), | ||
], | ||
after=[], | ||
) | ||
|
||
with patch.object(applier, "_safe_delete") as mock_safe_delete: | ||
await applier.delete_diff(entities, UserAgentType.exporter) | ||
|
||
mock_safe_delete.assert_not_called() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_delete_diff_custom_threshold_above_threshold_not_deleted() -> None: | ||
applier = HttpEntitiesStateApplier(Mock()) | ||
entities = EntityDiff( | ||
before=[ | ||
Entity(identifier="1", blueprint="test"), | ||
Entity(identifier="2", blueprint="test"), | ||
], | ||
after=[], | ||
) | ||
|
||
with patch.object(applier, "_safe_delete") as mock_safe_delete: | ||
await applier.delete_diff( | ||
entities, UserAgentType.exporter, entity_deletion_threshold=0.5 | ||
) | ||
|
||
mock_safe_delete.assert_not_called() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 default here should be
Null
and the value will be passedThere 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 it will be clearer to communicate it through precentage e.g. 90 rather than 0.9, wdyt?