-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowse_attributes.py
More file actions
208 lines (184 loc) · 8.03 KB
/
browse_attributes.py
File metadata and controls
208 lines (184 loc) · 8.03 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
import json
import dataiku
from osisoft_client import OSIsoftClient
from osisoft_plugin_common import get_credentials, build_select_choices, check_debug_mode
from osisoft_domain_handling import DomainHandler
def do(payload, config, plugin_config, inputs):
if "config" in config:
config = config.get("config")
if "credentials" not in config:
return {"choices": [{"label": "Requires DSS v10.0.4 or above. Please use the OSIsoft Search custom dataset instead"}]}
elif config.get("credentials") == {}:
return {"choices": [{"label": "Pick a credential"}]}
domain_handler = DomainHandler(config)
auth_type, username, password, server_url, is_ssl_check_disabled, credential_error = get_credentials(config, can_raise=False)
if credential_error:
return build_select_choices(credential_error)
if not (auth_type and username and password):
return build_select_choices("Pick a credential")
if not username or not password:
return build_select_choices(
"Incorrect credential. "
+ "Go to you profile page > Credentials > Your preset, click the edit button and fill in you username and password details."
)
if not server_url:
return build_select_choices("Fill in the server address")
is_debug_mode = check_debug_mode(config)
client = OSIsoftClient(server_url, auth_type, username, password, is_ssl_check_disabled=is_ssl_check_disabled, is_debug_mode=is_debug_mode)
parameter_name = payload.get("parameterName")
if parameter_name == "server_name":
choices = []
choices.extend(
domain_handler.ui_side_url(client.get_asset_servers(can_raise=False))
)
return build_select_choices(choices)
if parameter_name == "database_name":
choices = []
next_url = config.get("server_name")
if next_url:
choices.extend(
domain_handler.ui_side_url(
client.get_next_choices(domain_handler.client_side_url(next_url), "Self")
)
)
return build_select_choices(choices)
else:
return build_select_choices()
if parameter_name == "element_category":
choices = [{
"label": "<All>",
"value": None
}]
next_links = config.get("database_name")
if not next_links:
return build_select_choices()
next_url = next_links + "/elementcategories"
choices.extend(
client.get_next_choices(domain_handler.client_side_url(next_url), "Self", use_name_as_link=True)
)
return build_select_choices(choices)
if parameter_name == "element_template":
choices = [{
"label": "<All>",
"value": None
}]
next_links = config.get("database_name")
if not next_links:
return build_select_choices()
next_url = next_links + "/elementtemplates"
choices.extend(client.get_next_choices(domain_handler.client_side_url(next_url), "Self", use_name_as_link=True, filter={'InstanceType': 'Element'}))
choices.append({"label": "✍️ Enter manually", "value": "_DKU_manual_input"})
choices.append({"label": "🗄️ Use a variable", "value": "_DKU_variable_select"})
return build_select_choices(choices)
if parameter_name == "element_template_variable_select":
choices = []
variables = dataiku.get_custom_variables()
for variable in variables:
choices.append(
{
"label": variable,
"value": variable
}
)
if not choices:
return build_select_choices("No variable available")
return build_select_choices(choices)
if parameter_name == "attribute_category":
choices = [{
"label": "<All>",
"value": None
}]
next_links = config.get("database_name")
if not next_links:
return build_select_choices()
next_url = next_links + "/attributecategories"
next_url = domain_handler.ui_side_url(next_url)
choices.extend(client.get_next_choices(domain_handler.client_side_url(next_url), "Self", use_name_as_link=True))
return build_select_choices(choices)
if parameter_name == "element_1":
choices = []
next_url = config.get("database_name", None)
next_url = domain_handler.client_side_url(next_url)
if next_url:
choices.extend(domain_handler.ui_side_url(client.get_next_choices_as_json(next_url+"/elements", "Elements")))
return build_select_choices(choices)
else:
return build_select_choices()
for element_number in range(2, 10):
if parameter_name == "element_{}".format(element_number):
choices = []
json_string = config.get("element_{}".format(element_number - 1), "{}")
json_choice = json.loads(json_string)
next_url = json_choice.get("url")
next_url = domain_handler.client_side_url(next_url)
if next_url:
choices.extend(domain_handler.ui_side_url(client.get_next_choices_as_json(next_url, "Elements")))
return build_select_choices(choices)
else:
return build_select_choices()
if parameter_name == "attribute_1":
choices = []
json_string = get_latest_config(config)
json_string = json_string or "{}"
json_choice = json.loads(json_string)
next_url = json_choice.get("url")
next_url = domain_handler.client_side_url(next_url)
if next_url:
choices.extend(
domain_handler.ui_site_url(
client.get_next_choices(
next_url.replace("/elements", "/attributes").replace("/{}/attributes".format(client.endpoint.get_web_api_path()), "/{}/elements".format(client.endpoint.get_web_api_path())),
"Self")
)
)
return build_select_choices(choices)
else:
return build_select_choices()
if parameter_name == "analysis_1":
choices = []
json_string = get_latest_config(config)
json_string = json_string or "{}"
json_choice = json.loads(json_string)
next_url = json_choice.get("url")
next_url = domain_handler.client_side_url(next_url)
if next_url:
choices.extend(
domain_handler.ui_site_url(
client.get_next_choices(
next_url.replace("/elements", "/eventframes").replace("/{}/eventframes".format(client.endpoint.get_web_api_path()), "/{}/elements".format(client.endpoint.get_web_api_path())),
"Self"
)
)
)
return build_select_choices(choices)
else:
return build_select_choices()
if parameter_name == "data_type":
json_string = get_latest_config(config)
json_string = json_string or "{}"
json_choice = json.loads(json_string)
url_candidate = json_choice.get("url")
if url_candidate:
next_url = config.get(
"attribute_1",
url_candidate.replace("/elements", "/attributes").replace("/{}/attributes".format(client.endpoint.get_web_api_path()), "/{}/elements".format(client.endpoint.get_web_api_path()))
)
else:
return build_select_choices()
choices = []
item = client.get_item_from_url(next_url)
links = item.get("Links", {})
for link in links:
choices.append({
"label": link,
"value": links[link]
})
return build_select_choices(choices)
return build_select_choices()
def get_latest_config(config):
latest_config = None
for element_number in range(10, 1, -1):
latest_config = config.get("element_{}".format(element_number), None)
if latest_config:
return latest_config
return None