appsembler / figures

Reporting and data retrieval app for Open edX
MIT License
44 stars 37 forks source link

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

Closed OmarIthawi closed 2 years ago

OmarIthawi commented 2 years ago

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

Backward compatibility

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** ```py 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** ```py 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 ```