-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.py
More file actions
259 lines (234 loc) · 9.05 KB
/
client.py
File metadata and controls
259 lines (234 loc) · 9.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import asyncio
from datetime import datetime, timedelta
from math import floor
from typing import Any, Generator, List, Literal, Optional
import httpx
from obelisk.asynchronous import Obelisk as AsyncObelisk
from obelisk.strategies.retry import NoRetryStrategy, RetryStrategy
from obelisk.types import (
Datapoint,
IngestMode,
ObeliskKind,
QueryResult,
TimestampPrecision,
)
class Obelisk:
"""
Component that contains all the logic to consume data from
the Obelisk API (e.g. historical data, sse).
Wraps :class:`~obelisk.asynchronous.consumer.Consumer`.
Obelisk API Documentation:
https://obelisk.docs.apiary.io/
"""
loop: asyncio.AbstractEventLoop
"""Event loop used to run interal async operations"""
async_obelisk: AsyncObelisk
"""The actual implementation this synchronous wrapper refers to"""
def __init__(
self,
client: str,
secret: str,
retry_strategy: RetryStrategy = NoRetryStrategy(),
kind: ObeliskKind = ObeliskKind.CLASSIC,
):
self.async_obelisk = AsyncObelisk(client, secret, retry_strategy, kind)
self.loop = asyncio.get_event_loop()
def fetch_single_chunk(
self,
datasets: List[str],
metrics: Optional[List[str]] = None,
fields: Optional[List[str]] = None,
from_timestamp: Optional[int] = None,
to_timestamp: Optional[int] = None,
order_by: Optional[dict[str, Any]] = None,
filter_: Optional[dict[str, Any]] = None,
limit: Optional[int] = None,
limit_by: Optional[dict[str, Any]] = None,
cursor: Optional[str] = None,
) -> QueryResult:
"""
Queries one chunk of events from Obelisk for given parameters,
does not handle paging over Cursors.
Parameters
----------
datasets : List[str]
List of Dataset IDs.
metrics : Optional[List[str]] = None
List of Metric IDs or wildcards (e.g. ``*::number``), defaults to all metrics.
fields : Optional[List[str]] = None
List of fields to return in the result set.
Defaults to `[metric, source, value]`
from_timestamp : Optional[int] = None
Limit output to events after (and including)
this UTC millisecond timestamp, if present.
to_timestamp : Optional[int] = None
Limit output to events before (and excluding)
this UTC millisecond timestamp, if present.
order_by : Optional[dict] = None
Specifies the ordering of the output,
defaults to ascending by timestamp.
See Obelisk docs for format. Caller is responsible for validity.
filter_ : Optional[dict] = None
Limit output to events matching the specified Filter expression.
See Obelisk docs, caller is responsible for validity.
limit : Optional[int] = None
Limit output to a maximum number of events.
Also determines the page size.
Default is server-determined, usually 2500.
limit_by : Optional[dict] = None
Limit the combination of a specific set of Index fields
to a specified maximum number.
cursor : Optional[str] = None
Specifies the next cursor,
used when paging through large result sets.
"""
self.async_obelisk.log.info("Starting task")
task = self.loop.create_task(
self.async_obelisk.fetch_single_chunk(
datasets,
metrics,
fields,
from_timestamp,
to_timestamp,
order_by,
filter_,
limit,
limit_by,
cursor,
)
)
self.async_obelisk.log.info("Blocking...")
return self.loop.run_until_complete(task)
def query(
self,
datasets: List[str],
metrics: Optional[List[str]] = None,
fields: Optional[List[str]] = None,
from_timestamp: Optional[int] = None,
to_timestamp: Optional[int] = None,
order_by: Optional[dict[str, Any]] = None,
filter_: Optional[dict[str, Any]] = None,
limit: Optional[int] = None,
limit_by: Optional[dict[str, Any]] = None,
) -> List[Datapoint]:
"""
Queries data from obelisk,
automatically iterating when a cursor is returned.
Parameters
----------
datasets : List[str]
List of Dataset IDs.
metrics : Optional[List[str]] = None
List of Metric IDs or wildcards (e.g. `*::number`), defaults to all metrics.
fields : Optional[List[str]] = None
List of fields to return in the result set.
Defaults to `[metric, source, value]`
from_timestamp : Optional[int] = None
Limit output to events after (and including)
this UTC millisecond timestamp, if present.
to_timestamp : Optional[int] = None
Limit output to events before (and excluding)
this UTC millisecond timestamp, if present.
order_by : Optional[dict] = None
Specifies the ordering of the output,
defaults to ascending by timestamp.
See Obelisk docs for format. Caller is responsible for validity.
filter_ : Optional[dict] = None
Limit output to events matching the specified Filter expression.
See Obelisk docs, caller is responsible for validity.
limit : Optional[int] = None
Limit output to a maximum number of events.
Also determines the page size.
Default is server-determined, usually 2500.
limit_by : Optional[dict] = None
Limit the combination of a specific set of Index fields
to a specified maximum number.
"""
task = self.loop.create_task(
self.async_obelisk.query(
datasets,
metrics,
fields,
from_timestamp,
to_timestamp,
order_by,
filter_,
limit,
limit_by,
)
)
return self.loop.run_until_complete(task)
def query_time_chunked(
self,
datasets: List[str],
metrics: List[str],
from_time: datetime,
to_time: datetime,
jump: timedelta,
filter_: Optional[dict[str, Any]] = None,
direction: Literal["asc", "desc"] = "asc",
) -> Generator[List[Datapoint], None, None]:
"""
Fetches all data matching the provided filters,
yielding one chunk at a time.
One "chunk" may require several Obelisk calls to resolve cursors.
By necessity iterates over time, no other ordering is supported.
Parameters
----------
datasets : List[str]
Dataset IDs to query from
metrics : List[str]
IDs of metrics to query
from_time : datetime.datetime
Start time to fetch from
to_time : datetime.datetime
End time to fetch until.
jump : datetime.timedelta
Size of one yielded chunk
filter_ : Optional[dict] = None
Obelisk filter, caller is responsible for correct format
direction : Literal['asc', 'desc'] = 'asc'
Yield older data or newer data first, defaults to older first.
"""
current_start = from_time
while current_start < to_time:
yield self.query(
datasets=datasets,
metrics=metrics,
from_timestamp=floor(current_start.timestamp() * 1000),
to_timestamp=floor((current_start + jump).timestamp() * 1000 - 1),
order_by={"field": ["timestamp"], "ordering": direction},
filter_=filter_,
)
current_start += jump
def send(
self,
dataset: str,
data: List[dict[str, Any]],
precision: TimestampPrecision = TimestampPrecision.MILLISECONDS,
mode: IngestMode = IngestMode.DEFAULT,
) -> httpx.Response:
"""
Publishes data to Obelisk
Parameters
----------
dataset : str
ID for the dataset to publish to
data : List[dict]
List of Obelisk-acceptable datapoints.
Exact format varies between Classic or HFS,
caller is responsible for formatting.
precision : TimestampPrecision = TimestampPrecision.MILLISECONDS
Precision used in the numeric timestamps contained in data.
Ensure it matches to avoid weird errors.
mode : IngestMode = IngestMode.DEFAULT
See docs for :class:`~obelisk.types.IngestMode`.
Raises
------
ObeliskError
When the resulting status code is not 204, an empty :exc:`~obelisk.exceptions.ObeliskError` is raised.
"""
task = self.loop.create_task(
self.async_obelisk.send(dataset, data, precision, mode)
)
return self.loop.run_until_complete(task)