Debian Codegraph @

beanie/odm/settings/timeseries.py

raw
1 from enum import Enum
2 from typing import Annotated, Any
3
4 from pydantic import BaseModel, Field
5
6
7 class Granularity(str, Enum):
8 """
9 Time Series Granuality
10 """
11
12 seconds = "seconds"
13 minutes = "minutes"
14 hours = "hours"
15
16
17 class TimeSeriesConfig(BaseModel):
18 """
19 Time Series Collection config
20 """
21
22 time_field: str
23 meta_field: str | None = None
24 granularity: Granularity | None = None
25 bucket_max_span_seconds: int | None = None
26 bucket_rounding_second: Annotated[
27 int | None,
28 Field(
29 deprecated="This field is deprecated in favor of 'bucket_rounding_seconds'.",
30 ),
31 ] = None
32 bucket_rounding_seconds: int | None = None
33 expire_after_seconds: int | None = None
34
35 def build_query(self, collection_name: str) -> dict[str, Any]:
36 res: dict[str, Any] = {"name": collection_name}
37 timeseries: dict[str, Any] = {"timeField": self.time_field}
38
39 if self.meta_field is not None:
40 timeseries["metaField"] = self.meta_field
41 if self.granularity is not None:
42 timeseries["granularity"] = self.granularity
43 if self.bucket_max_span_seconds is not None:
44 timeseries["bucketMaxSpanSeconds"] = self.bucket_max_span_seconds
45
46 # Use new field if present, otherwise fallback to deprecated one without triggering warning
47 rounding = self.bucket_rounding_seconds
48 if rounding is None:
49 # Avoid deprecation warning by checking __dict__ directly
50 rounding = self.__dict__.get("bucket_rounding_second")
51
52 if rounding is not None:
53 timeseries["bucketRoundingSeconds"] = rounding
54
55 res["timeseries"] = timeseries
56
57 if self.expire_after_seconds is not None:
58 res["expireAfterSeconds"] = self.expire_after_seconds
59
60 return res