generated from BCDA-APS/tiled-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_client.py
217 lines (183 loc) · 6.36 KB
/
http_client.py
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
"""
Simple demo tiled Python client using the http API.
[12/2 2:40 PM] Jemian, Pete R.
Maybe, instead of a Python client, I will show some specific URI queries and responses.
* [x] Find all runs in a catalog between these two ISO8601 dates.
* [x] Find run(s) which match given metadata.
* [ ] Get overall metadata from given run.
* [ ] What are the data streams in this run?
* [ ] What is the metadata for this stream?
* [ ] Get the data from the data stream named primary (the canonical main data).
* [ ] What about loose matches? Maybe not now. Might require some deeper expertise.
"""
import datetime
import requests
import pyRestTable
def requests_tiled(server, catalog, api="/api/v1/node/search", suffix="", port=8000):
uri = f"http://{server}:{port}{api}/{catalog}{suffix}"
r = requests.get(uri)
# print(f"{r.encoding=}")
# print(f"{r.headers=}")
# print(f"{r.status_code=}")
# print(f"{r.text=}")
return r.json()
def overview():
table = pyRestTable.Table()
table.labels = (
"catalog scan_id date/time duration,s plan_name title ESAF_id".split()
)
for catalog in "bdp2022 20idb_usaxs".split():
# how many runs in the catalog?
# Set limit to 1 because a 0 means ALL the runs.
response = requests_tiled("localhost", catalog, suffix="?page[limit]=1")
# print(f"{response['error']=}")
count = response["meta"]["count"]
print(f"{catalog=} has {count} runs")
# get the n most recent runs
num_runs = 20
response = requests_tiled(
"localhost",
catalog,
suffix=f"?page[offset]={count-num_runs}&page[limit]={num_runs}",
)
# print(f"{response['error']=}")
# list these runs
for run in reversed(response["data"]):
md = run["attributes"]["metadata"]
dt = md["summary"]["datetime"]
duration = md["summary"]["duration"]
esaf_id = md["start"].get("esaf_id", "")
exit_status = md["stop"]["exit_status"]
plan_name = md["summary"]["plan_name"]
scan_id = md["summary"]["scan_id"]
streams = " ".join(md["summary"]["stream_names"])
title = md["start"].get("title", "")
uid = md["start"]["uid"]
table.addRow(
(
catalog,
f"{scan_id:>7}",
dt,
f"{duration:>10.1f}",
plan_name,
title,
esaf_id,
)
)
print(table)
def table_of_runs(runs):
table = pyRestTable.Table()
table.labels = "row# scan_id plan_name start duration(s) uid7".split()
for row, run in enumerate(runs, start=1):
md = run["attributes"]["metadata"]
dt = md["summary"]["datetime"]
duration = md["summary"]["duration"]
plan_name = md["summary"]["plan_name"]
scan_id = md["summary"]["scan_id"]
uid = md["summary"]["uid"]
table.addRow(
(row, scan_id, plan_name, dt, duration, uid[:7])
)
return table
def find_runs_by_date(server, catalog, since, until, limit=20):
# Find all runs in a catalog between these two ISO8601 dates.
def to_ts(isodate):
return datetime.datetime.fromisoformat(isodate).timestamp()
tz = "US/Central"
suffix = (
"?page[offset]=0"
f"&page[limit]={limit}"
f"&filter[time_range][condition][since]={to_ts(since)}"
f"&filter[time_range][condition][until]={to_ts(until)}"
f"&filter[time_range][condition][timezone]={tz}"
"&sort=time"
)
r = requests_tiled(server, catalog, suffix=suffix)
print(f"Runs starting after '{since}' and before '{until}'")
print(table_of_runs(r["data"]))
return r
def find_by_plan_name(server, catalog, plan_name, limit=20):
# Find run(s) which match given metadata: given plan_name
case_sensitive = False
suffix = (
"?page[offset]=0"
f"&page[limit]={limit}"
"&filter[eq][condition][key]=plan_name"
f'&filter[eq][condition][value]="{plan_name}"'
"&sort=time"
)
r = requests_tiled(server, catalog, suffix=suffix)
print(f"Runs that match 'plan_name={plan_name}'")
print(table_of_runs(r["data"]))
return r
def get_run_metadata(server, catalog, uid, stream=None):
suffix = f"/{uid}"
if stream is not None:
suffix += f"/{stream}"
r = requests_tiled(
server, catalog,
api="/api/v1/node/metadata",
suffix=suffix,
)
print(r["data"]["attributes"]["metadata"])
return r["data"]["attributes"]["metadata"]
def get_run_data(server, catalog, uid, stream, data_name, data_format="json"):
r = requests_tiled(
server, catalog,
api="/api/v1/array/full",
suffix=(
f"/{uid}"
f"/{stream}"
"/data"
f"/{data_name}"
"?"
f"format={data_format}"
)
)
print(type(r))
return r
def main():
server = "localhost"
if False:
r = find_runs_by_date(
server, "20idb_usaxs", "2022-11-01 15:50", "2022-12-01 16:10"
)
r = find_runs_by_date(
server, "bdp2022", "2022-11-01 15:50", "2022-12-01 16:10"
)
if False:
r = find_by_plan_name(server, "20idb_usaxs", "tune_a2rp")
r = find_by_plan_name(server, "bdp2022", "take_image")
if False:
r = get_run_metadata(
server, "bdp2022", "00714a91-c33e-4e7b-90fd-2e8f385bebc9",
)
r = get_run_metadata(
server, "bdp2022", "00714a91-c33e-4e7b-90fd-2e8f385bebc9", "primary"
)
if False:
arr = get_run_data(
server,
"bdp2022",
"ae762f9c-4933-4aa4-a720-147f4aaab6fd",
"primary",
"adpvadet_pva1_execution_time", "json"
)
if False:
# Get the data from the data stream named primary (the canonical main data).
data_format = "json"
uid = "00714a91-c33e-4e7b-90fd-2e8f385bebc9"
r = requests_tiled(
server,
"bdp2022",
api="/api/v1/array/full",
suffix=(
f"/{uid}"
f"/{stream_name}"
"/data"
r"%format=json"
)
)
print(len(r))
if __name__ == "__main__":
main()