Skip to content

get_requested_site: configurable backend to get the site for an API call - #410

Merged
OmarIthawi merged 2 commits into
masterfrom
req_site_backend
Jan 27, 2022
Merged

OmarIthawi merged 2 commits into
masterfrom
req_site_backend

Conversation

@OmarIthawi

@OmarIthawi OmarIthawi commented Jan 14, 2022 •

Copy link
Copy Markdown
Contributor

Description

Dashboard needs to be able to access Figures API with privileged access by sending site's UUID parameter.

This concept is Tahoe-specific and shouldn't make it into Figures (thanks John for advocating for that). Therefor this PR adds a plugin to customize the logic we use to get_current_site()

I've added a Figures plugin repo to do that:

Using the backend

  • Install a package which has the custom backend e.g. pip install tahoe-figures-plugins

  • In the server-vars.yml configure Figures like below:

    FIGURES:
      REQUESTED_SITE_BACKEND: 'tahoe_figures_plugins.sites:get_current_site_or_by_uuid'

Backward compatibility

  • By default this PR changes nothing for both community and Tahoe installations
  • The tests works as-is for all Figures views
  • The plugin only used when configured

Benchmark

I tested the import_from_path function and the load takes 0.0069ms per call if the backend is already imported in the Python module system on a staging server.

Loading the backend for the first time (e.g. a new Python gunicorn worker) takes 0.5ms.

This seems like an acceptable delay to add on top of the each request.

Expand to see the "timeit" code used

Test module reload time with cache

import timeit

load_with_cache_setup = """
from django.conf import settings
from figures.sites import import_from_path
import importlib
"""

load_with_cache = """
sites_backend_path = settings.ENV_TOKENS['FIGURES'].get('SITES_BACKEND')
sites_backend = import_from_path(sites_backend_path)
"""

print('load_with_cache', timeit.timeit(
    setup=load_with_cache_setup,
    stmt=load_with_cache,
    number=100000,
))

# load_with_cache 0.690 sec for 100k loads

Test module reload time without cache

import timeit

load_without_cache_setup = """
from django.conf import settings
from figures.sites import import_from_path
import importlib
import openedx.core.djangoapps.appsembler.sites.utils
"""

load_without_cache = """
sites_backend_path = settings.ENV_TOKENS['FIGURES'].get('SITES_BACKEND')
importlib.reload(openedx.core.djangoapps.appsembler.sites.utils)
sites_backend = import_from_path(sites_backend_path)
"""

print('load_without_cache', timeit.timeit(
    setup=load_without_cache_setup,
    stmt=load_without_cache,
    number=10000,
))

# load_without_cache 5.033 sec for 10k loads

@OmarIthawi OmarIthawi changed the title (wip) requested site backend get_requested_site: configurable backend to get the site for an API call Jan 18, 2022
@OmarIthawi
OmarIthawi marked this pull request as ready for review January 18, 2022 06:56
Comment thread figures/sites.py

This functions makes use of the `REQUESTED_SITE_BACKEND` setting if configured, otherwise
it defaults to Django's get_current_site().

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@OmarIthawi Please add "Using the backend" instructions either here, in this module docstring or in a readme doc

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks John! I'll add the instructions to the README doc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've documented the feature in a new Configurations section. I'll merge after someone spot check the section.

It's not a complete one, but a start.

@johnbaldwin johnbaldwin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Just one comment to include this new functionality configuration instructions in the code or a readme doc. Thanks!

Description
-----------

Dashboard needs to be able to access Figures API with privileged access by sending site's UUID parameter.

This concept is Tahoe-specific and shouldn't make it into Figures (thanks John for advocating for that). Therefor this PR adds a plugin to customize the logic we use to `get_current_site()`

I've added a Figures plugin repo to do that:

 - appsembler/tahoe-figures-plugins#1

Using the backend
-----------------

 - Install a package which has the custom backend e.g. `pip install tahoe-figures-plugins`
 - In the `server-vars.yml` configure Figures like below:

   ```yaml
   FIGURES:
     REQUESTED_SITE_BACKEND: 'tahoe_figures_plugins.sites:get_current_site_or_by_uuid'
   ```

Backward compatibility
----------------------

 - By default this PR changes nothing for both community and Tahoe installations
 - The tests works as-is for all Figures views
 - The plugin only used when configured

Benchmark
---------

I tested the `import_from_path` function and the load takes 0.0069ms per call if the backend is already imported in the Python module system on a staging server.

Loading the backend for the first time (e.g. a new Python gunicorn worker) takes 0.5ms.

This seems like an acceptable delay to add on top of the each request.
 - left out few items undocumented
 - another Figures doc update is needed
@OmarIthawi
OmarIthawi requested a review from thraxil January 27, 2022 09:38
OmarIthawi added a commit that referenced this pull request Jan 27, 2022
Merging after #410 is merged.
@OmarIthawi OmarIthawi mentioned this pull request Jan 27, 2022
1 of 3 tasks
@OmarIthawi
OmarIthawi merged commit d8c76f9 into master Jan 27, 2022
@OmarIthawi
OmarIthawi deleted the req_site_backend branch January 27, 2022 13:49
OmarIthawi added a commit that referenced this pull request Jan 27, 2022
Merging after #410 is merged.
@OmarIthawi
OmarIthawi restored the req_site_backend branch January 27, 2022 15:30
@johnbaldwin
johnbaldwin deleted the req_site_backend branch March 7, 2022 20:57
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.

4 participants