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

get_relations() called multiple times on pre-identified related resources #11459

Closed
whatisgalen opened this issue Sep 16, 2024 · 1 comment
Closed

Comments

@whatisgalen
Copy link
Member

whatisgalen commented Sep 16, 2024

At this line:

if len(instanceids) > 0:
            related_resources = se.search(index=RESOURCES_INDEX, id=list(instanceids))
            if related_resources:
                for resource in related_resources["docs"]:
                    relations = get_relations(
                        resourceinstanceid=resource["_id"],
                        start=0,
                        limit=0,
                    )
                    if resource["found"]:
                        resource["_source"]["total_relations"] = relations["total"]

                        for descriptor_type in ("displaydescription", "displayname"):
                            descriptor = get_localized_descriptor(
                                resource, descriptor_type
                            )
                            if descriptor:
                                resource["_source"][descriptor_type] = descriptor
                            else:
                                resource["_source"][descriptor_type] = _("Undefined")

                        ret["related_resources"].append(resource["_source"])

the get_relations method is called on a related resource instance solely to provide a "total" number and is not used again. The overhead of this method:

def get_relations(
            resourceinstanceid, start, limit, resourceinstance_graphid=None
        ):
            final_query = Q(resourceinstanceidfrom_id=resourceinstanceid) | Q(
                resourceinstanceidto_id=resourceinstanceid
            )

            if resourceinstance_graphid:
                to_graph_id_filter = Q(
                    resourceinstancefrom_graphid_id=str(self.graph_id)
                ) & Q(resourceinstanceto_graphid_id=resourceinstance_graphid)
                from_graph_id_filter = Q(
                    resourceinstancefrom_graphid_id=resourceinstance_graphid
                ) & Q(resourceinstanceto_graphid_id=str(self.graph_id))
                final_query = final_query & (to_graph_id_filter | from_graph_id_filter)

            relations = {
                "total": models.ResourceXResource.objects.filter(final_query).count(),
                "relations": models.ResourceXResource.objects.filter(final_query)[
                    start:limit
                ],
            }

            return relations  # resourceinstance_graphid = "00000000-886a-374a-94a5-984f10715e3a"

...implies an ORM query every time.

This isn't actually needed. We can likely get this total figure elsewhere, but besides that this property total_relations doesn't appear to be used, also we already called this method earlier anyway:

resource_relations = get_relations(
            resourceinstanceid=self.resourceinstanceid,
            start=start,
            limit=limit,
            resourceinstance_graphid=resourceinstance_graphid,
        )
@whatisgalen whatisgalen changed the title get_relations called multiple times on pre-identified related resources get_relations() called multiple times on pre-identified related resources Sep 16, 2024
@chiatt chiatt added this to pipeline Sep 16, 2024
@whatisgalen
Copy link
Member Author

completed by #11460

@github-project-automation github-project-automation bot moved this to ✅ Done in pipeline Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

No branches or pull requests

1 participant