-
Notifications
You must be signed in to change notification settings - Fork 0
/
aiidaClient.ts
237 lines (215 loc) · 6.67 KB
/
aiidaClient.ts
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
import React from 'react'
import { QueryClient } from 'react-query'
export const queryAiidaClient = new QueryClient()
export const defaultRestUrl = 'http://127.0.0.1:5000/api/v4'
// TODO move validation to aiidaClient functions?
export const urlPattern = new RegExp('^https?:\\/\\/[^\\?]+$')
export const uuidPattern = new RegExp('^[-a-zA-Z0-9]+$')
export const AiidaSettingsContext = React.createContext({
baseUrl: defaultRestUrl
} as { baseUrl: null | string })
export interface IAiidaRestResponse {
// The base attributes for the rest response
data: any
id: null
method: 'GET' | 'POST'
path: string
query_string: string
resource_type: string
url: string
url_root: string
}
export interface IAiidaRestNode {
ctime: string
full_type: string
id: number
label: string
mtime: string
node_type: string
process_type: string
user_id: number
uuid: string
attributes: { [key: string]: any }
}
export interface IAiidaRestProcess extends IAiidaRestNode {
attributes: {
process_state:
| 'created'
| 'running'
| 'waiting'
| 'finished'
| 'excepted'
| 'killed'
process_label: string
exit_status: number
}
}
export interface IAiidaRestResponseNode extends IAiidaRestResponse {
data: { nodes: IAiidaRestNode[] }
}
export interface IAiidaRestNodeStatistics {
total: number
ctime_by_day: { [key: string]: number }
types: { [key: string]: number }
}
export interface IAiidaRestResponseNodeStatistics extends IAiidaRestResponse {
data: IAiidaRestNodeStatistics
}
export async function isConnected(baseUrl: string | null): Promise<boolean> {
if (baseUrl === null) {
return false
}
const response = await fetch(`${baseUrl}`)
return response.ok
}
export async function getNodes(
baseUrl: string | null,
nodeType: string,
page: number
): Promise<null | {
nodes: IAiidaRestNode[]
totalCount: number
perPage: number
}> {
if (baseUrl === null) {
return null
}
const perPage = 20
const nodeTypeQuery = nodeType ? `node_type=like=%22${nodeType}%%22&` : ''
// TODO better url join?
const response = await fetch(
`${baseUrl}/nodes/page/${page}?perpage=${perPage}&orderby=-mtime&${nodeTypeQuery}attributes=true&attributes_filter=process_label,process_state,exit_status`
)
// TODO handle errors
const totalCount = parseInt(response.headers.get('x-total-count') || '0')
const responseJson = (await response.json()) as IAiidaRestResponseNode
return { nodes: responseJson.data.nodes, totalCount, perPage }
}
export async function getNodeStatistics(
baseUrl: string | null
): Promise<null | IAiidaRestNodeStatistics> {
if (baseUrl === null) {
return null
}
const response = await fetch(`${baseUrl}/nodes/statistics/`)
const responseJson = (await response.json()) as IAiidaRestResponseNodeStatistics
return responseJson.data
}
export async function getNode(
baseUrl: string | null,
uuid: string | null
): Promise<null | IAiidaRestNode> {
if (baseUrl === null || !uuid) {
return null
}
if (!uuidPattern.test(uuid)) {
throw new TypeError('UUID does not match required format')
}
const response = await fetch(`${baseUrl}/nodes/${uuid}?attributes=true&extras=true`)
const responseJson = (await response.json()) as IAiidaRestResponse
return responseJson.data?.nodes === undefined
? null
: (Object.values(responseJson.data?.nodes)[0] as IAiidaRestNode)
}
export interface IAiidaRestNodeRepoListItem {
name: string
type: 'FILE' | 'DIRECTORY'
}
export interface IAiidaRestResponseNodeRepoList extends IAiidaRestResponse {
data: { repo_list: { [key: number]: IAiidaRestNodeRepoListItem } }
}
export async function getNodeRepoList(
baseUrl: string | null,
uuid: string | null
): Promise<null | IAiidaRestNodeRepoListItem[]> {
if (baseUrl === null || !uuid) {
return null
}
if (!uuidPattern.test(uuid)) {
throw new TypeError('UUID does not match required format')
}
const response = await fetch(`${baseUrl}/nodes/${uuid}/repo/list`)
const responseJson = (await response.json()) as IAiidaRestResponseNodeRepoList
return responseJson.data?.repo_list === undefined
? null
: (Object.values(responseJson.data?.repo_list) as IAiidaRestNodeRepoListItem[])
}
export interface IAiidaRestNodeLinkItem {
ctime: string
full_type: string
id: number
label: string
mtime: string
node_type: string
process_type: string
user_id: number
uuid: string
link_label: string
link_type: string
}
export interface IAiidaRestResponseNodeIncoming extends IAiidaRestResponse {
data: { incoming: { [key: number]: IAiidaRestNodeLinkItem } }
}
export async function getNodeIncoming(
baseUrl: string | null,
uuid: string | null
): Promise<null | IAiidaRestNodeLinkItem[]> {
if (baseUrl === null || !uuid) {
return null
}
if (!uuidPattern.test(uuid)) {
throw new TypeError('UUID does not match required format')
}
// TODO deal with pagination
const response = await fetch(`${baseUrl}/nodes/${uuid}/links/incoming/page/1`)
const responseJson = (await response.json()) as IAiidaRestResponseNodeIncoming
return responseJson.data?.incoming === undefined
? null
: (Object.values(responseJson.data?.incoming) as IAiidaRestNodeLinkItem[])
}
export interface IAiidaRestResponseNodeOutgoing extends IAiidaRestResponse {
data: { outgoing: { [key: number]: IAiidaRestNodeLinkItem } }
}
export async function getNodeOutgoing(
baseUrl: string | null,
uuid: string | null
): Promise<null | IAiidaRestNodeLinkItem[]> {
if (baseUrl === null || !uuid) {
return null
}
if (!uuidPattern.test(uuid)) {
throw new TypeError('UUID does not match required format')
}
// TODO deal with pagination
const response = await fetch(`${baseUrl}/nodes/${uuid}/links/outgoing/page/1`)
const responseJson = (await response.json()) as IAiidaRestResponseNodeOutgoing
return responseJson.data?.outgoing === undefined
? null
: (Object.values(responseJson.data?.outgoing) as IAiidaRestNodeLinkItem[])
}
export interface IAiidaRestGroupItem {
description: string
extras: { [key: string]: any }
id: number
label: string
time: string
type_string: string
user_id: number
uuid: string
}
export interface IAiidaRestResponseGroups extends IAiidaRestResponse {
data: { groups: { [key: number]: IAiidaRestGroupItem } }
}
export async function getGroups(
baseUrl: string | null
): Promise<null | IAiidaRestGroupItem[]> {
if (baseUrl === null) {
return null
}
// TODO deal with pagination
const response = await fetch(`${baseUrl}/groups?orderby=type_string`)
const responseJson = (await response.json()) as IAiidaRestResponseGroups
return responseJson.data?.groups === undefined
? null
: (Object.values(responseJson.data?.groups) as IAiidaRestGroupItem[])
}