diff options
| author | jc0b <j@jc0b.computer> | 2020-07-30 21:48:23 +0200 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2020-08-24 19:48:48 +0200 |
| commit | e73fd6a9b21dddf0ac324d30ab3e151bc7eddb98 (patch) | |
| tree | 77a9ea53832c8f97f5bc818679c1db7062b083fd /api/opendc | |
| parent | c8c6519551cef8b82b55e3e5866b35156c89e6b1 (diff) | |
Hopefully improved performance of auth endpoint at scale
Diffstat (limited to 'api/opendc')
| -rw-r--r-- | api/opendc/api/v2/prefabs/authorizations/endpoint.py | 10 | ||||
| -rw-r--r-- | api/opendc/api/v2/prefabs/authorizations/test_endpoint.py | 45 | ||||
| -rw-r--r-- | api/opendc/models/prefab.py | 4 |
3 files changed, 44 insertions, 15 deletions
diff --git a/api/opendc/api/v2/prefabs/authorizations/endpoint.py b/api/opendc/api/v2/prefabs/authorizations/endpoint.py index 9f0e2232..7b421662 100644 --- a/api/opendc/api/v2/prefabs/authorizations/endpoint.py +++ b/api/opendc/api/v2/prefabs/authorizations/endpoint.py @@ -13,15 +13,13 @@ def GET(request): user.check_exists() - prefab_collection = Prefab.get_all() - print(type(prefab_collection)) - print(prefab_collection) + own_prefabs = Prefab.get_all({'authorId' : user.get_id()}) + public_prefabs = Prefab.get_all({'visibility' : 'public'}) authorizations = { "authorizations" : []} - for prefab in prefab_collection: - if prefab['authorId'] == user.get_id() or prefab['visibility'] == "public": - authorizations["authorizations"].append(prefab) + authorizations["authorizations"].append(own_prefabs) + authorizations["authorizations"].append(public_prefabs) return Response(200, 'Successfully fetched authorizations.', authorizations) diff --git a/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py b/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py index 68b701bc..38401045 100644 --- a/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py +++ b/api/opendc/api/v2/prefabs/authorizations/test_endpoint.py @@ -1,10 +1,11 @@ from opendc.util.database import DB +from unittest.mock import Mock def test_get_authorizations(client, mocker): + DB.fetch_all = Mock() mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1'}) - mocker.patch.object(DB, - 'fetch_all', - return_value=[{ + DB.fetch_all.side_effect = [ + [{ '_id': '1', 'datetimeCreated': '000', 'datetimeLastEdited': '000', @@ -15,7 +16,7 @@ def test_get_authorizations(client, mocker): '_id': '2', 'datetimeCreated': '000', 'datetimeLastEdited': '000', - 'authorId': 2, + 'authorId': 1, 'visibility' : 'private' }, { @@ -26,12 +27,42 @@ def test_get_authorizations(client, mocker): 'visibility' : 'public' }, { - '_id': '2', + '_id': '4', + 'datetimeCreated': '000', + 'datetimeLastEdited': '000', + 'authorId': 1, + 'visibility' : 'public' + }], + [{ + '_id': '5', + 'datetimeCreated': '000', + 'datetimeLastEdited': '000', + 'authorId': 2, + 'visibility' : 'public' + }, + { + '_id': '6', 'datetimeCreated': '000', 'datetimeLastEdited': '000', 'authorId': 2, 'visibility' : 'public' - }]) - res = client.get('/api/v2/prefabs/authorizations', json={'prefab': {'name': 'test prefab'}}) + }, + { + '_id': '7', + 'datetimeCreated': '000', + 'datetimeLastEdited': '000', + 'authorId': 2, + 'visibility' : 'public' + }, + { + '_id': '8', + 'datetimeCreated': '000', + 'datetimeLastEdited': '000', + 'authorId': 2, + 'visibility' : 'public' + }] + ] + mocker.patch.object(DB, 'fetch_one', return_value={'_id': '1'}) + res = client.get('/api/v2/prefabs/authorizations') assert '200' in res.status diff --git a/api/opendc/models/prefab.py b/api/opendc/models/prefab.py index 2d20208c..3d5dcf54 100644 --- a/api/opendc/models/prefab.py +++ b/api/opendc/models/prefab.py @@ -28,5 +28,5 @@ class Prefab(Model): # OpenDC-authored objects don't necessarily have an authorId # return - def get_all(): - return DB.fetch_all({}, Prefab.collection_name) + def get_all(query): + return DB.fetch_all(query, Prefab.collection_name) |
