-
Notifications
You must be signed in to change notification settings - Fork 53
feat(taxon-tiles): improve performance and scope to collection #8584
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
Open
grantfitzsimmons
wants to merge
6
commits into
main
Choose a base branch
from
issue-303
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bcdc6c7
feat: fix slow tiles
grantfitzsimmons af36f50
feat: add index for fetching current objects in the collection
grantfitzsimmons e37db2c
test: fix taxon tree def
grantfitzsimmons 6fd2e8e
fix: remove unnecessary index
grantfitzsimmons 17f7ab7
fix(taxon-tiles): prevent multiple rows per CO
grantfitzsimmons 485992c
Merge branch 'main' into issue-303
grantfitzsimmons 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
Empty file.
This file contains hidden or 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,71 @@ | ||
| from specifyweb.specify.models import Collection, Determination, Taxon, Taxontreedefitem | ||
| from specifyweb.specify.tests.test_api import ApiTests | ||
|
|
||
| from specifyweb.backend.barvis.views import get_taxon_bar_data | ||
|
|
||
|
|
||
| class TaxonBarTests(ApiTests): | ||
| def setUp(self): | ||
| super().setUp() | ||
| self.discipline.taxontreedef = self.taxontreedef | ||
| self.discipline.save() | ||
| self.taxontreedef.discipline = self.discipline | ||
| self.taxontreedef.save() | ||
|
|
||
| root_rank = Taxontreedefitem.objects.create( | ||
| name='Root', rankid=0, treedef=self.taxontreedef | ||
| ) | ||
| species_rank = Taxontreedefitem.objects.create( | ||
| name='Species', rankid=220, parent=root_rank, treedef=self.taxontreedef | ||
| ) | ||
| self.root = Taxon.objects.create( | ||
| name='Root', rankid=0, definition=self.taxontreedef, | ||
| definitionitem=root_rank, | ||
| ) | ||
| self.taxon = Taxon.objects.create( | ||
| name='Test species', rankid=220, parent=self.root, | ||
| definition=self.taxontreedef, definitionitem=species_rank, | ||
| ) | ||
|
|
||
| def test_returns_current_determination_counts_without_changing_rows(self): | ||
| collection_2 = Collection.objects.create( | ||
| catalognumformatname='test', | ||
| collectionname='TestCollection2', | ||
| isembeddedcollectingevent=False, | ||
| discipline=self.discipline, | ||
| ) | ||
| Determination.objects.create( | ||
| collectionobject=self.collectionobjects[0], taxon=self.taxon, | ||
| collectionmemberid=self.collection.id, iscurrent=True, | ||
| createdbyagent=self.agent, modifiedbyagent=self.agent, | ||
| ) | ||
| Determination.objects.create( | ||
| collectionobject=self.collectionobjects[1], taxon=self.taxon, | ||
| collectionmemberid=self.collection.id, iscurrent=True, | ||
| createdbyagent=self.agent, modifiedbyagent=self.agent, | ||
| ) | ||
| Determination.objects.bulk_create([ | ||
| Determination( | ||
| collectionobject=self.collectionobjects[0], taxon=self.taxon, | ||
| collectionmemberid=self.collection.id, iscurrent=True, | ||
| createdbyagent=self.agent, modifiedbyagent=self.agent, | ||
| ), | ||
| ]) | ||
| Determination.objects.create( | ||
| collectionobject=self.collectionobjects[2], taxon=self.taxon, | ||
| collectionmemberid=self.collection.id, iscurrent=False, | ||
| createdbyagent=self.agent, modifiedbyagent=self.agent, | ||
| ) | ||
| Determination.objects.create( | ||
| collectionobject=self.collectionobjects[2], taxon=self.taxon, | ||
| collectionmemberid=collection_2.id, iscurrent=True, | ||
| createdbyagent=self.agent, modifiedbyagent=self.agent, | ||
| ) | ||
|
|
||
| self.assertCountEqual( | ||
| get_taxon_bar_data(self.collection), | ||
| [ | ||
| (self.root.id, 0, None, 'Root', 0), | ||
| (self.taxon.id, 220, self.root.id, 'Test species', 2), | ||
| ], | ||
| ) |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,60 +1,46 @@ | ||
| from django.db.models import Count, IntegerField, OuterRef, Subquery | ||
| from django.db.models.functions import Coalesce | ||
| from django.http import HttpResponse | ||
| from django.db.models import Count, Q | ||
|
|
||
| from specifyweb.middleware.general import require_GET | ||
| from specifyweb.specify.views import login_maybe_required | ||
| from specifyweb.specify.api.filter_by_col import filter_by_collection | ||
| from specifyweb.specify.api.serializers import toJson | ||
| from specifyweb.specify.models import Taxon | ||
| from specifyweb.specify.models import Determination, Taxon | ||
| from specifyweb.specify.views import login_maybe_required | ||
|
|
||
| from django.db import connection | ||
|
|
||
| def get_taxon_bar_data(collection): | ||
| """Return taxon tile rows with a correlated current-count lookup.""" | ||
| current_determination_counts = ( | ||
| Determination.objects | ||
| .filter( | ||
| taxon_id=OuterRef('pk'), | ||
| collectionmemberid=collection.id, | ||
| iscurrent=True, | ||
| ) | ||
| .values('taxon_id') | ||
| .annotate(count=Count('collectionobject', distinct=True)) | ||
| .values('count') | ||
| ) | ||
| taxons = Taxon.objects.annotate( | ||
| current_determination_count=Coalesce( | ||
| Subquery( | ||
| current_determination_counts, | ||
| output_field=IntegerField(), | ||
| ), | ||
| 0, | ||
| ) | ||
| ).values_list( | ||
| 'id', 'rankid', 'parent_id', 'name', 'current_determination_count' | ||
| ) | ||
| return list(filter_by_collection(taxons, collection)) | ||
|
|
||
|
|
||
| @require_GET | ||
| @login_maybe_required | ||
| def taxon_bar(request): | ||
| # "Returns the data for creating a taxon tiles visualization." | ||
| # cursor = connection.cursor() | ||
| # cursor.execute(""" | ||
| # SELECT t.TaxonID, | ||
| # t.RankID, | ||
| # t.ParentID, | ||
| # t.Name, | ||
| # (SELECT COUNT(*) FROM determination d WHERE t.TaxonID = d.TaxonID AND d.IsCurrent = 1) | ||
| # FROM taxon t | ||
| # WHERE t.TaxonTreeDefID = %s | ||
| # """, [request.specify_collection.discipline.taxontreedef_id]) | ||
|
|
||
| # Implementing the previous SQL query in Django ORM: | ||
| taxons = ( | ||
| Taxon.objects.annotate( | ||
| current_determination_count=Count( | ||
| 'determinations', filter=Q(determinations__iscurrent=True)) | ||
| ) | ||
| .values_list("id", "rankid", "parent_id", "name", "current_determination_count") | ||
| """Return the data for creating a taxon tiles visualization.""" | ||
| return HttpResponse( | ||
| toJson(get_taxon_bar_data(request.specify_collection)), | ||
| content_type='application/json', | ||
| ) | ||
| filtered_taxons = filter_by_collection(taxons, request.specify_collection) | ||
| result = toJson(list(filtered_taxons)) | ||
|
|
||
| # SELECT d.TaxonID, COUNT(DISTINCT d.CollectionObjectID), t.ParentID | ||
| # FROM determination d | ||
| # INNER JOIN taxon t ON t.TaxonID = d.TaxonID | ||
| # WHERE d.CollectionMemberID = %s | ||
| # AND d.IsCurrent = 1 | ||
| # GROUP BY d.TaxonId | ||
| # ORDER BY d.TaxonId | ||
| # """, [request.specify_collection.id]) | ||
| # result = toJson(cursor.fetchall()) | ||
| # session = Session() | ||
| # query = session.query( | ||
| # Determination.TaxonID, | ||
| # func.count(distinct(Determination.CollectionObjectID)), | ||
| # Taxon.ParentID) \ | ||
| # .join(Taxon, Determination.TaxonID == Taxon.taxonId) \ | ||
| # .filter(Determination.collectionMemberId == request.specify_collection.id) \ | ||
| # .filter(Determination.isCurrent == True) \ | ||
| # .group_by(Determination.TaxonID).order_by(Determination.TaxonID) | ||
|
|
||
| # result = toJson(list(query)) | ||
| # session.close() | ||
| return HttpResponse(result, content_type='application/json') | ||
18 changes: 18 additions & 0 deletions
18
specifyweb/specify/migrations/0049_determination_current_taxon_index.py
This file contains hidden or 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,18 @@ | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('specify', '0048_taxontreedefitem_parent_context_delete'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddIndex( | ||
| model_name='determination', | ||
| index=models.Index( | ||
| fields=['collectionmemberid', 'iscurrent', 'taxon'], | ||
| name='DetColMemCurrentTaxonIDX', | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.