Skip to content

Commit 4038152

Browse files
authored
Set page limit to min of global and total limit (#19)
In cases where the requested number of datapoints is less than the default page limit, too many points may be returned. Adds a min wrapper to ensure this is not exceeded
2 parents 11a4ae1 + 69978c4 commit 4038152

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/obelisk/asynchronous/core.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def check_metric_type(self) -> Self:
126126
return self
127127

128128

129-
def serialize_comma_string(input: Any, handler: SerializerFunctionWrapHandler) -> str | None:
129+
def serialize_comma_string(
130+
input: Any, handler: SerializerFunctionWrapHandler
131+
) -> str | None:
130132
if val := handler(input):
131133
return ",".join(val)
132134
return None
@@ -140,11 +142,16 @@ class QueryParams(BaseModel):
140142
141143
Contrary to the name, this does not correlate directly to URL query parameters sent to Obelisk.
142144
"""
145+
143146
dataset: str
144-
groupBy: Annotated[list[FieldName] | None, WrapSerializer(serialize_comma_string)] = None
147+
groupBy: Annotated[
148+
list[FieldName] | None, WrapSerializer(serialize_comma_string)
149+
] = None
145150
"""List of Field Names to aggregate by as defined in Obelisk docs, None selects the server-side defaults."""
146151
aggregator: Aggregator | None = None
147-
fields: Annotated[list[FieldName] | None, WrapSerializer(serialize_comma_string)] = None
152+
fields: Annotated[
153+
list[FieldName] | None, WrapSerializer(serialize_comma_string)
154+
] = None
148155
"""List of Field Names as defined in Obelisk docs, None selects the server-side defaults."""
149156
orderBy: Annotated[list[str] | None, WrapSerializer(serialize_comma_string)] = None
150157
"""List of Field Names, with their potential prefixes and suffixes, to select ordering. None user server defaults."""
@@ -170,7 +177,9 @@ def check_datatype_needed(self) -> Self:
170177
return self
171178

172179
def to_dict(self) -> dict[str, Any]:
173-
return self.model_dump(exclude_none=True, by_alias=True, mode='json', exclude={"dataset"})
180+
return self.model_dump(
181+
exclude_none=True, by_alias=True, mode="json", exclude={"dataset"}
182+
)
174183

175184

176185
class ChunkedParams(BaseModel):
@@ -180,6 +189,7 @@ class ChunkedParams(BaseModel):
180189
for example processing weeks of data one hour at a time.
181190
This limits memory useage.
182191
"""
192+
183193
dataset: str
184194
groupBy: list[FieldName] | None = None
185195
aggregator: Aggregator | None = None
@@ -228,6 +238,7 @@ def chunks(self) -> Iterator[QueryParams]:
228238

229239
class QueryResult(BaseModel):
230240
"""The data returned by a single chunk fetch"""
241+
231242
cursor: str | None = None
232243
"""Cursors always point to the next page of data matched by filters.
233244
They are none if there is no more data, they do not consider datapoint count limits."""
@@ -327,7 +338,7 @@ async def query(self, params: QueryParams) -> list[Datapoint]:
327338
result_limit = params.limit
328339

329340
# Obelisk CORE does not actually stop emitting a cursor when done, limit serves as page limit
330-
params.limit = self.page_limit
341+
params.limit = min(self.page_limit, result_limit)
331342

332343
while True:
333344
result: QueryResult = await self.fetch_single_chunk(params)

src/obelisk/types/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class TimestampPrecision(str, Enum):
3838

3939
class Datapoint(BaseModel):
4040
"""An Obelisk Classic / HFS datapoint. May contain more or less fields"""
41+
4142
timestamp: int
4243
value: Any
4344
dataset: str | None = None
@@ -46,19 +47,19 @@ class Datapoint(BaseModel):
4647
userId: int | None = None
4748
"""This field is only used on HFS, and has a different name in some deployments."""
4849

49-
model_config = ConfigDict(
50-
extra='allow'
51-
)
50+
model_config = ConfigDict(extra="allow")
5251

5352

5453
class QueryResult(BaseModel):
5554
"""Result of a query"""
55+
5656
items: list[Datapoint]
5757
cursor: str | None = None
5858

5959

6060
class ObeliskKind(str, Enum):
6161
"""Defines which variety of Obelisk a Client should use, and provides some URLs and config information."""
62+
6263
CLASSIC = "classic"
6364
HFS = "hfs"
6465
CORE = "core"

0 commit comments

Comments
 (0)