Skip to content

Commit

Permalink
Allow to configure the max_time_ms parameter to use when calling mong…
Browse files Browse the repository at this point in the history
…o aggregate
  • Loading branch information
aarranz committed Dec 4, 2024
1 parent fac70e6 commit aedf7b8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ extend-ignore =
BLK100,
# See https://github.com/PyCQA/pycodestyle/issues/373
E203,
E402,
exclude =
venv
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ repos:
language_version: python3.9
- repo: https://github.com/pycqa/flake8.git
rev: 6.0.0
args: ["--ignore=E501,BLK100,E203,E402"]
hooks:
- id: flake8
additional_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion odata_server/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.0.0a23"
__version__ = "1.0.0a24"
26 changes: 19 additions & 7 deletions odata_server/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

import abnf
import pymongo
import pymongo.database
import werkzeug
from flask import Blueprint, Response, abort, request, url_for

from odata_server import edm
from odata_server import edm, settings
from odata_server.utils import (
build_response_headers,
expand_result,
Expand Down Expand Up @@ -79,7 +80,7 @@ def make_setup_state(self, app, options, first_registration=False):
and entity_set.custom_insert_business is None
):
logger.error(
"EntitySet {} is managing an entity type that contains computed properties. The logic for initializaing those computed properties has to be configured".format(
"EntitySet {} is managing an entity type that contains computed properties. The logic for initializing those computed properties has to be configured".format(
entity_set.Name
)
)
Expand Down Expand Up @@ -126,9 +127,9 @@ def make_setup_state(self, app, options, first_registration=False):
}
)
)
entity_set.annotations[
"Org.OData.Core.V1.ResourcePath"
] = entity_set.Annotations[-1]
entity_set.annotations["Org.OData.Core.V1.ResourcePath"] = (
entity_set.Annotations[-1]
)

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


def get(mongo, RootEntitySet, subject, id_value, prefers, session=None):
def get(
mongo: pymongo.database.Database,
RootEntitySet,
subject,
id_value,
prefers,
session=None,
):
anonymous = not isinstance(subject, edm.EntitySet)
qs = parse_qs(request.query_string)
EntityType = subject.entity_type
Expand Down Expand Up @@ -324,7 +332,11 @@ def get(mongo, RootEntitySet, subject, id_value, prefers, session=None):
pipeline.append({"$unwind": f"${prefix}"})

pipeline.append({"$limit": 1})
results = tuple(mongo_collection.aggregate(pipeline, session=session))
results = tuple(
mongo_collection.aggregate(
pipeline, session=session, maxTimeMs=settings.MONGO_SEARCH_MAX_TIME_MS
)
)
if len(results) == 0:
abort(404)
data = results[0]
Expand Down
3 changes: 3 additions & 0 deletions odata_server/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

MONGO_SEARCH_MAX_TIME_MS = int(os.getenv("MONGO_SEARCH_MAX_TIME_MS", "30000"))
7 changes: 3 additions & 4 deletions odata_server/utils/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@

def add_odata_annotations(data, entity_set):
key_predicate = format_key_predicate(extract_id_value(entity_set.entity_type, data))
data["@odata.id"] = "{}({})".format(
url_for("odata.{}".format(entity_set.Name), _external=True), key_predicate
)
data["@odata.etag"] = 'W/"{}"'.format(data["uuid"])
base_url = url_for("odata.{}".format(entity_set.Name), _external=True)
data["@odata.id"] = f"{base_url}({key_predicate})"
data["@odata.etag"] = f'W/"{data["uuid"]}"'
del data["uuid"]

return data

0 comments on commit aedf7b8

Please sign in to comment.