Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion python-lib/osisoft_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def __init__(self, server_url, auth_type, username, password, is_ssl_check_disab
self.is_debug_mode = is_debug_mode
self.debug_level = None
self.network_timer = network_timer
self.cache = {}

def get_auth(self, auth_type, username, password):
if auth_type == "basic":
Expand Down Expand Up @@ -931,7 +932,49 @@ def traverse(self, path_elements):
item = self.extract_item_with_name(json_response, attribute)
return item

def traverse_and_cache(self, path_elements, path_attributes, tree):
def traverse_and_cache(self, ex_path_elements, ex_path_attributes, tree):
path_elements = ex_path_elements.copy()
path_attributes = ex_path_attributes.copy()
full_path_elements = path_elements.copy() + path_attributes.copy()
if tree.exists(full_path_elements):
sending_back = tree.get(full_path_elements)
return sending_back
if path_attributes:
attribute_to_search = path_attributes.pop()
cached_item = self.traverse_and_cache(path_elements, path_attributes, tree)
last_know_url = cached_item.get("url") + "/attributes"
headers = self.get_requests_headers()
# json_response = self.cached_get(url=last_know_url, headers=headers, params={}, error_source="recursive traverse_and_cache")
json_response = self.get(url=last_know_url, headers=headers, params={}, error_source="recursive traverse_and_cache")
for item in json_response.get(OSIsoftConstants.API_ITEM_KEY, []):
item_name = item.get("Name")
tree.put(path_elements + path_attributes + [item_name], get_item_details(item))
item = self.extract_item_with_name(json_response, attribute_to_search)
# tree.put(path_elements + path_attributes + [attribute_to_search], get_item_details(item))
return get_item_details(item)

element_to_search = path_elements.pop()
cached_item = self.traverse_and_cache(path_elements, [], tree)
last_know_url = cached_item.get("url") + "/elements"
headers = self.get_requests_headers()
# json_response = self.cached_get(url=last_know_url, headers=headers, params={}, error_source="recursive traverse_and_cache")
json_response = self.get(url=last_know_url, headers=headers, params={}, error_source="recursive traverse_and_cache")
for item in json_response.get(OSIsoftConstants.API_ITEM_KEY, []):
item_name = item.get("Name")
tree.put(path_elements + [item_name], get_item_details(item))
item = self.extract_item_with_name(json_response, element_to_search)
# tree.put(path_elements + [element_to_search], get_item_details(item))
return get_item_details(item)

def cached_get(self, url, headers, params, error_source):
if url in self.cache:
return self.cache.get(url)
else:
json_response = self.get(url=url, headers=headers, params=params, error_source=error_source)
self.cache[url] = json_response
return json_response

def traverse_and_cache_2(self, path_elements, path_attributes, tree):
full_path_elements = path_elements.copy() + path_attributes.copy()
if tree.exists(full_path_elements):
# this path has already been retrieved, so skip
Expand Down
5 changes: 5 additions & 0 deletions python-lib/osisoft_plugin_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@ def get_tree(self):
return self.tree

def get_record(self, index):
if index is None:
return None
if index < len(self.index):
return self.index[index]
return None
Expand All @@ -726,6 +728,9 @@ def print(self):
print("Tree {}".format(self.tree))
print("Tree content {}".format(self.index))

def size(self):
return len(self.index)


def recursive_tree_rebuild(dictionary, records, counter=None):
counter = counter or -1
Expand Down