Host show api endpoint is slow for non-admin user
Description of problem:
When a non-admin user fetches host details, it takes much more time than for admin user. Something similar like:
but just for getting host info, not editing.
When applied upstream patch from bz1598855 to Sat 6.4.1, no improvement in performance was noticed. So it is a separate issue (or not sufficiently general patch covering both scenarios).
Version-Release number of selected component (if applicable):
6.4.1, seen also on 6.3
Steps to Reproduce:
1. Have a Satellite with some Hosts (I think the count does not matter)
2. Have a non-admin user with all rights but not Admin checkbox.
time curl -X GET -s -k -u nonadmin:password https://$(hostname -f)/api/v2/hosts/529 > /dev/null
4. Select Admin checkbox for the user.
5. Run the curl command again.
3. shows several times higher time than 5.
3. shows comparable time like 5. Some higher value is understandable, but not several times more.
Previously, `Taxonomy.expend` was used to get the taxonomies for the
user or the current taxonomy, then all taxonomies were iterated over
to get the subtree ids.
However, when `Taxonomy.current` is `nil`, we already get
all subtree ids since `expend` calls `my_taxonomy` which return the
subtrees already. Instead, we can directly call the
`taxonomy_and_subtree_id` method to get the ids. In the case of admin
user, we don't need a taxonomy condition for `nil` taxonomies as that
should return all taxonomies.
When `Taxonomy.current` is present, we can call the `subtree_ids` method
on it directly without needing to wrap it in an array and iterate over
In the case of `nil` current taxonomies, this can potentially save
hundreds of useless database queries to check subtree ids for all the
taxonomies a user has access to. For example, in my setup every call to
`/api/hosts/$id` triggered 20 queries per taxonomy the user is assigned
to (due to the permission section of the `show.json.rabl` view).