This repository was archived by the owner on Apr 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcibridge.py
More file actions
220 lines (200 loc) · 6.02 KB
/
cibridge.py
File metadata and controls
220 lines (200 loc) · 6.02 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
from graphql_client import GraphQLClient
from graphql_http_client import query, mutation
import json
ws_url = "ws://localhost:8080/subscriptions"
"""
Uploads a file to cibridge and returns the data id.
param:file - Takes the path to the input file
returns: data id of the file
"""
def upload_file(file):
add_data_mutation = """
mutation{
uploadData(file:"%s", properties: null){
id
format
name
label
parentDataId
type
isModified
}
}
"""%(file)
data = json.loads(mutation(add_data_mutation).text)
dataId = data['data']['uploadData']['id']
return dataId
"""
Searches the converter algorithm id by taking the input and output data formats
param:in_data_format - input data format
param:out_data_format - output data format
returns: algorithm id if available or None
"""
def searchConverterAlgorithm(in_data_format, out_data_format):
search_converter_algorithm = """
query{
findConvertersByFormat(inFormat:"%s", outFormat:"%s"){
id
inData
outData
label
description
parentOutputData
type
remoteable
menuPath
conversion
authors
implementers
integrators
documentationUrl
reference
referenceUrl
writtenIn
}
}
"""%(in_data_format, out_data_format)
data = json.loads(query(search_converter_algorithm).text)
converter_id = data['data']['findConvertersByFormat'][0]['id']
print("converted id algo: " + converter_id)
return converter_id
"""
Fetches the array results given the output data format expected and the parent data id
param: out_data_format - output data format expected
param: parentDataId - Parent Data Id
returns: array of results with matching parentId
"""
def getResultsDataId(out_data_format, parentDataId):
q = """
query{
getData(filter:{ formats:["%s"]}){
results{
id
format
parentDataId
}
}
}
"""%(out_data_format)
queryResults = json.loads(query(q).text)
dataIds = []
for i in queryResults["data"]["getData"]["results"]:
if i["parentDataId"] == parentDataId:
dataIds.append(i["id"])
return dataIds
"""
Fetches the result given the output data format expected and the parent data id
param: out_data_format - output data format
param: parentDataId - Parent Data Id
returns: reault Data Id
"""
def getResultDataId(out_data_format, parentDataId):
q = """
query{
getData(filter:{ formats:["%s"]}){
results{
id
format
parentDataId
}
}
}
"""%(out_data_format)
queryResults = json.loads(query(q).text)
dataId = None
for i in queryResults["data"]["getData"]["results"]:
if i["parentDataId"] == parentDataId:
dataId = i["id"]
return dataId
"""
Creates an instance of the algorithm given the algorithm_id and the data_id
param: converter_id - algorithm id
param: parentDataId - data id for the algorithm
returns: instance id of the algorithm created
"""
def createAlgorithmInstance(converter_id, dataId):
create_converter_algorithm_instance = """
mutation{
createAlgorithm(algorithmDefinitionId:"%s",
dataIds:["%s"],
){
id
}
}
"""%(converter_id, dataId)
data = json.loads(mutation(create_converter_algorithm_instance).text)
converter_algo_instance = data["data"]["createAlgorithm"]["id"]
print("Converter Algorithm Instance: " + converter_algo_instance)
return converter_algo_instance
"""
Creates an instance of the algorithm given the algorithm_id, data_id and the parameters
param: converter_id - algorithm id
param: parentDataId - data id for the algorithm
param: parameters - parameters for the algorithm
returns: instance id of the algorithm created
"""
def create_algorithm_withParams(algorithmId, dataId, parameters):
create_coauthor_algorithm_instance="""
mutation{
createAlgorithm(algorithmDefinitionId:"%s",
dataIds:["%s"],
parameters:[%s]
){
id
}
}
"""%(algorithmId, dataId, parameters)
print("Query: " + create_coauthor_algorithm_instance)
responseId = json.loads(mutation(create_coauthor_algorithm_instance).text)
print(responseId)
coauthor_algo_instance = responseId["data"]["createAlgorithm"]["id"]
print("Algorithm Instance: " + coauthor_algo_instance)
return coauthor_algo_instance
"""
Runs an algorithm instance given the algorithm_instance_id.
param: converter_algo_instance - algorithm instance that has to be scheduled
returns: runs the algorithm and returns the run result
"""
def runAlgorithm(converter_algo_instance):
run_converter_algorithm = """
mutation{
runAlgorithmNow(algorithmInstanceId:"%s")
}
"""%(converter_algo_instance)
data = json.loads(mutation(run_converter_algorithm).text)
return data
"""
Creates a subscription to the algorithm given the algorithm_instance and reference to the callback method.
param: converter_algo_instance - instance of the algorithm for which the results are expected
param: callbackMethod - reference to the callback method
returns: subscription id created
"""
def subscribe_to_algorithm(converter_algo_instance, callbackMethod):
converter_algo_status_subscription = """
subscription{
algorithmInstanceUpdated(filter:{
algorithmInstanceIds: "%s"
}){
id
progress
state
}
}
"""%(converter_algo_instance)
global ws
ws = GraphQLClient(ws_url)
convereter_algo_subscription = ws.subscribe(converter_algo_status_subscription, callback=callbackMethod)
return convereter_algo_subscription
"""
Gets the local path to download result file given the dataId
param: dataId - data id
returns: local path to download
"""
def download_data(dataId):
download_query = """
query{
downloadData(dataId:"%s")
}
"""%(dataId)
data = json.loads(query(download_query).text)
return data["data"]["downloadData"]