Skip to content

Commit 1538731

Browse files
committed
Implement defaults and limits
1 parent d99decc commit 1538731

File tree

5 files changed

+433
-0
lines changed

5 files changed

+433
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from __future__ import annotations
2+
3+
from abc import ABC, abstractmethod
4+
from typing import Any
5+
6+
7+
class ConfigEndpoints(ABC):
8+
@property
9+
@abstractmethod
10+
def defaults(self) -> DefaultsEndpoints:
11+
pass
12+
13+
@property
14+
@abstractmethod
15+
def limits(self) -> LimitsEndpoints:
16+
pass
17+
18+
19+
class DefaultsEndpoints(ABC):
20+
@abstractmethod
21+
def set(
22+
self,
23+
key: str,
24+
value: Any,
25+
username: str | None = None,
26+
) -> None:
27+
"""
28+
Configure a new default configuration value.
29+
30+
Parameters:
31+
key : str
32+
The configuration key for which the default value is being set.
33+
value : Any
34+
The value to set as the default for the given key.
35+
username : str | None, default=None
36+
If set, the configuration will be set for the given user.
37+
38+
Returns: None
39+
"""
40+
pass
41+
42+
@abstractmethod
43+
def list(
44+
self,
45+
username: str | None = None,
46+
key: str | None = None,
47+
) -> dict[str, Any]:
48+
"""
49+
List configured default configuration values.
50+
51+
Parameters:
52+
key : str | None (default=None)
53+
List only the default value for the given key.
54+
username : str | None, default=None
55+
List only default values for the given user.
56+
57+
Returns: dict[str, Any]
58+
A dictionary containing the default configuration values.
59+
"""
60+
pass
61+
62+
63+
class LimitsEndpoints(ABC):
64+
@abstractmethod
65+
def set(
66+
self,
67+
key: str,
68+
value: Any,
69+
username: str | None = None,
70+
) -> None:
71+
"""
72+
Configure a new limit for a configuration value.
73+
74+
Parameters:
75+
key : str
76+
The configuration key for which the limit is being set.
77+
value : Any
78+
The value to set as the limit for the given key.
79+
username : str | None, default=None
80+
If set, the limit will be set for the given user.
81+
82+
Returns: None
83+
"""
84+
pass
85+
86+
@abstractmethod
87+
def list(
88+
self,
89+
username: str | None = None,
90+
key: str | None = None,
91+
) -> dict[str, Any]:
92+
"""
93+
List configured configuration limits.
94+
95+
Parameters:
96+
key : str | None (default=None)
97+
List only the limits for the given key.
98+
username : str | None, default=None
99+
List only liomits for the given user.
100+
101+
Returns: dict[str, Any]
102+
A dictionary containing the configuration limits.
103+
"""
104+
pass
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
5+
from graphdatascience.arrow_client.authenticated_flight_client import AuthenticatedArrowClient
6+
from graphdatascience.arrow_client.v2.data_mapper_utils import deserialize
7+
from graphdatascience.procedure_surface.api.config_endpoints import (
8+
ConfigEndpoints,
9+
DefaultsEndpoints,
10+
LimitsEndpoints,
11+
)
12+
from graphdatascience.procedure_surface.utils.config_converter import ConfigConverter
13+
14+
15+
class ConfigArrowEndpoints(ConfigEndpoints):
16+
def __init__(self, arrow_client: AuthenticatedArrowClient, show_progress: bool = False):
17+
self._arrow_client = arrow_client
18+
self._show_progress = show_progress
19+
20+
@property
21+
def defaults(self) -> DefaultsEndpoints:
22+
return DefaultsArrowEndpoints(self._arrow_client, self._show_progress)
23+
24+
@property
25+
def limits(self) -> LimitsEndpoints:
26+
return LimitsArrowEndpoints(self._arrow_client, self._show_progress)
27+
28+
29+
class DefaultsArrowEndpoints(DefaultsEndpoints):
30+
def __init__(self, arrow_client: AuthenticatedArrowClient, show_progress: bool = False):
31+
self._arrow_client = arrow_client
32+
self._show_progress = show_progress
33+
34+
def set(
35+
self,
36+
key: str,
37+
value: Any,
38+
username: str | None = None,
39+
) -> None:
40+
deserialize(self._arrow_client.do_action_with_retry("v2/defaults.set", {key: value}))
41+
42+
def list(
43+
self,
44+
username: str | None = None,
45+
key: str | None = None,
46+
) -> dict[str, Any]:
47+
config = ConfigConverter.convert_to_gds_config(
48+
key=key,
49+
)
50+
51+
rows = deserialize(self._arrow_client.do_action_with_retry("v2/defaults.list", config))
52+
result = {}
53+
54+
for row in rows:
55+
result[row["key"]] = row["value"]
56+
57+
return result
58+
59+
60+
class LimitsArrowEndpoints(LimitsEndpoints):
61+
def __init__(self, arrow_client: AuthenticatedArrowClient, show_progress: bool = False):
62+
self._arrow_client = arrow_client
63+
self._show_progress = show_progress
64+
65+
def set(
66+
self,
67+
key: str,
68+
value: Any,
69+
username: str | None = None,
70+
) -> None:
71+
deserialize(self._arrow_client.do_action_with_retry("v2/limits.set", {key: value}))
72+
73+
def list(
74+
self,
75+
username: str | None = None,
76+
key: str | None = None,
77+
) -> dict[str, Any]:
78+
config = ConfigConverter.convert_to_gds_config(
79+
key=key,
80+
)
81+
82+
rows = deserialize(self._arrow_client.do_action_with_retry("v2/limits.list", config))
83+
result = {}
84+
85+
for row in rows:
86+
result[row["key"]] = row["value"]
87+
88+
return result
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
from __future__ import annotations
2+
3+
from typing import Any
4+
5+
from graphdatascience.call_parameters import CallParameters
6+
from graphdatascience.procedure_surface.api.config_endpoints import (
7+
ConfigEndpoints,
8+
DefaultsEndpoints,
9+
LimitsEndpoints,
10+
)
11+
from graphdatascience.procedure_surface.utils.config_converter import ConfigConverter
12+
from graphdatascience.query_runner.query_runner import QueryRunner
13+
14+
15+
class ConfigCypherEndpoints(ConfigEndpoints):
16+
def __init__(self, query_runner: QueryRunner):
17+
self._query_runner = query_runner
18+
19+
@property
20+
def defaults(self) -> DefaultsEndpoints:
21+
return DefaultsCypherEndpoints(self._query_runner)
22+
23+
@property
24+
def limits(self) -> LimitsEndpoints:
25+
return LimitsCypherEndpoints(self._query_runner)
26+
27+
28+
class DefaultsCypherEndpoints(DefaultsEndpoints):
29+
def __init__(self, query_runner: QueryRunner):
30+
self._query_runner = query_runner
31+
32+
def set(
33+
self,
34+
key: str,
35+
value: Any,
36+
username: str | None = None,
37+
) -> None:
38+
params = {
39+
"key": key,
40+
"value": value,
41+
}
42+
43+
if username:
44+
params["username"] = username
45+
46+
params = CallParameters(**params)
47+
48+
self._query_runner.call_procedure(endpoint="gds.config.defaults.set", params=params)
49+
50+
def list(
51+
self,
52+
username: str | None = None,
53+
key: str | None = None,
54+
) -> dict[str, Any]:
55+
config = ConfigConverter.convert_to_gds_config(
56+
key=key,
57+
username=username,
58+
)
59+
60+
params = CallParameters(
61+
config=config,
62+
)
63+
64+
result = self._query_runner.call_procedure(endpoint="gds.config.defaults.list", params=params)
65+
return {row["key"]: row["value"] for _, row in result.iterrows()}
66+
67+
68+
class LimitsCypherEndpoints(LimitsEndpoints):
69+
def __init__(self, query_runner: QueryRunner):
70+
self._query_runner = query_runner
71+
72+
def set(
73+
self,
74+
key: str,
75+
value: Any,
76+
username: str | None = None,
77+
) -> None:
78+
params = {
79+
"key": key,
80+
"value": value,
81+
}
82+
83+
if username:
84+
params["username"] = username
85+
86+
params = CallParameters(**params)
87+
88+
self._query_runner.call_procedure(endpoint="gds.config.limits.set", params=params)
89+
90+
def list(
91+
self,
92+
username: str | None = None,
93+
key: str | None = None,
94+
) -> dict[str, Any]:
95+
config = ConfigConverter.convert_to_gds_config(
96+
key=key,
97+
username=username,
98+
)
99+
100+
params = CallParameters(
101+
config=config,
102+
)
103+
104+
result = self._query_runner.call_procedure(endpoint="gds.config.limits.list", params=params)
105+
return {row["key"]: row["value"] for _, row in result.iterrows()}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from typing import Generator
2+
3+
import pytest
4+
5+
from graphdatascience.arrow_client.authenticated_flight_client import AuthenticatedArrowClient
6+
from graphdatascience.procedure_surface.arrow.config_arrow_endpoints import ConfigArrowEndpoints
7+
8+
9+
@pytest.fixture
10+
def config_endpoints(arrow_client: AuthenticatedArrowClient) -> Generator[ConfigArrowEndpoints, None, None]:
11+
yield ConfigArrowEndpoints(arrow_client)
12+
13+
14+
def test_defaults_set_and_list(config_endpoints: ConfigArrowEndpoints) -> None:
15+
config_endpoints.defaults.set("test.key", 6)
16+
17+
defaults = config_endpoints.defaults.list()
18+
19+
assert ("test.key", 6) in defaults.items()
20+
21+
22+
def test_defaults_list_by_key(config_endpoints: ConfigArrowEndpoints) -> None:
23+
config_endpoints.defaults.set("test.specific.key", "specific_value")
24+
25+
specific_defaults = config_endpoints.defaults.list(key="test.specific.key")
26+
27+
assert specific_defaults == {"test.specific.key": "specific_value"}
28+
29+
30+
def test_limits_set_and_list(config_endpoints: ConfigArrowEndpoints) -> None:
31+
config_endpoints.limits.set("test.key", 6)
32+
33+
limits = config_endpoints.limits.list()
34+
35+
assert ("test.key", 6) in limits.items()
36+
37+
38+
def test_limits_list_by_key(config_endpoints: ConfigArrowEndpoints) -> None:
39+
config_endpoints.limits.set("test.specific.key", 42)
40+
41+
specific_limits = config_endpoints.limits.list(key="test.specific.key")
42+
43+
assert specific_limits == {"test.specific.key": 42}
44+
45+
46+
def test_config_endpoints_properties(config_endpoints: ConfigArrowEndpoints) -> None:
47+
"""Test that the config endpoints have the required properties."""
48+
assert hasattr(config_endpoints, "defaults")
49+
assert hasattr(config_endpoints, "limits")
50+
51+
# Verify the properties return the correct endpoint types
52+
defaults = config_endpoints.defaults
53+
limits = config_endpoints.limits
54+
55+
assert hasattr(defaults, "set")
56+
assert hasattr(defaults, "list")
57+
assert hasattr(limits, "set")
58+
assert hasattr(limits, "list")

0 commit comments

Comments
 (0)