Skip to content

Commit aedf7b8

Browse files
committed
Allow to configure the max_time_ms parameter to use when calling mongo aggregate
1 parent fac70e6 commit aedf7b8

File tree

6 files changed

+30
-12
lines changed

6 files changed

+30
-12
lines changed

.flake8

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ extend-ignore =
77
BLK100,
88
# See https://github.com/PyCQA/pycodestyle/issues/373
99
E203,
10+
E402,
11+
exclude =
12+
venv

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ repos:
1717
language_version: python3.9
1818
- repo: https://github.com/pycqa/flake8.git
1919
rev: 6.0.0
20+
args: ["--ignore=E501,BLK100,E203,E402"]
2021
hooks:
2122
- id: flake8
2223
additional_dependencies:

odata_server/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.0.0a23"
1+
__version__ = "1.0.0a24"

odata_server/flask.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
import abnf
1010
import pymongo
11+
import pymongo.database
1112
import werkzeug
1213
from flask import Blueprint, Response, abort, request, url_for
1314

14-
from odata_server import edm
15+
from odata_server import edm, settings
1516
from odata_server.utils import (
1617
build_response_headers,
1718
expand_result,
@@ -79,7 +80,7 @@ def make_setup_state(self, app, options, first_registration=False):
7980
and entity_set.custom_insert_business is None
8081
):
8182
logger.error(
82-
"EntitySet {} is managing an entity type that contains computed properties. The logic for initializaing those computed properties has to be configured".format(
83+
"EntitySet {} is managing an entity type that contains computed properties. The logic for initializing those computed properties has to be configured".format(
8384
entity_set.Name
8485
)
8586
)
@@ -126,9 +127,9 @@ def make_setup_state(self, app, options, first_registration=False):
126127
}
127128
)
128129
)
129-
entity_set.annotations[
130-
"Org.OData.Core.V1.ResourcePath"
131-
] = entity_set.Annotations[-1]
130+
entity_set.annotations["Org.OData.Core.V1.ResourcePath"] = (
131+
entity_set.Annotations[-1]
132+
)
132133

133134
resource_path = edm.get_annotation(
134135
entity_set, "Org.OData.Core.V1.ResourcePath"
@@ -273,7 +274,14 @@ def parse_prefer_header(value, version="4.0"):
273274
return data
274275

275276

276-
def get(mongo, RootEntitySet, subject, id_value, prefers, session=None):
277+
def get(
278+
mongo: pymongo.database.Database,
279+
RootEntitySet,
280+
subject,
281+
id_value,
282+
prefers,
283+
session=None,
284+
):
277285
anonymous = not isinstance(subject, edm.EntitySet)
278286
qs = parse_qs(request.query_string)
279287
EntityType = subject.entity_type
@@ -324,7 +332,11 @@ def get(mongo, RootEntitySet, subject, id_value, prefers, session=None):
324332
pipeline.append({"$unwind": f"${prefix}"})
325333

326334
pipeline.append({"$limit": 1})
327-
results = tuple(mongo_collection.aggregate(pipeline, session=session))
335+
results = tuple(
336+
mongo_collection.aggregate(
337+
pipeline, session=session, maxTimeMs=settings.MONGO_SEARCH_MAX_TIME_MS
338+
)
339+
)
328340
if len(results) == 0:
329341
abort(404)
330342
data = results[0]

odata_server/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os
2+
3+
MONGO_SEARCH_MAX_TIME_MS = int(os.getenv("MONGO_SEARCH_MAX_TIME_MS", "30000"))

odata_server/utils/flask.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@
77

88
def add_odata_annotations(data, entity_set):
99
key_predicate = format_key_predicate(extract_id_value(entity_set.entity_type, data))
10-
data["@odata.id"] = "{}({})".format(
11-
url_for("odata.{}".format(entity_set.Name), _external=True), key_predicate
12-
)
13-
data["@odata.etag"] = 'W/"{}"'.format(data["uuid"])
10+
base_url = url_for("odata.{}".format(entity_set.Name), _external=True)
11+
data["@odata.id"] = f"{base_url}({key_predicate})"
12+
data["@odata.etag"] = f'W/"{data["uuid"]}"'
1413
del data["uuid"]
1514

1615
return data

0 commit comments

Comments
 (0)