Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle units and descriptions for empty tags #162

Merged
merged 8 commits into from
Nov 18, 2024
Merged
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
46 changes: 28 additions & 18 deletions tagreader/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,31 +442,41 @@ def get_units(self, tags: Union[str, List[str]]):
tags = [tags]
units = {}
for tag in tags:
if self.cache is not None:
r = self.cache.get_metadata(key=tag, properties="unit")
if r is not None and "unit" in r:
units[tag] = r["unit"]
if tag not in units:
unit = self.handler._get_tag_unit(tag)
if self.cache is not None and unit is not None:
self.cache.put_metadata(key=tag, value={"unit": unit})
units[tag] = unit
try:
if self.cache is not None:
r = self.cache.get_metadata(key=tag, properties="unit")
if r is not None and "unit" in r:
units[tag] = r["unit"]
if tag not in units:
unit = self.handler._get_tag_unit(tag)
if self.cache is not None and unit is not None:
self.cache.put_metadata(key=tag, value={"unit": unit})
units[tag] = unit
except Exception:
if self.search(tag) == []: # check for nonexisting string
logger.warning(f"Tag not found: {tag}")
continue
return units

def get_descriptions(self, tags: Union[str, List[str]]) -> Dict[str, str]:
if isinstance(tags, str):
tags = [tags]
descriptions = {}
for tag in tags:
if self.cache is not None:
r = self.cache.get_metadata(key=tag, properties="description")
if r is not None and "description" in r:
descriptions[tag] = r["description"]
if tag not in descriptions:
desc = self.handler._get_tag_description(tag)
if self.cache is not None and desc is not None:
self.cache.put_metadata(key=tag, value={"description": desc})
descriptions[tag] = desc
try:
if self.cache is not None:
r = self.cache.get_metadata(key=tag, properties="description")
if r is not None and "description" in r:
descriptions[tag] = r["description"]
if tag not in descriptions:
desc = self.handler._get_tag_description(tag)
if self.cache is not None and desc is not None:
self.cache.put_metadata(key=tag, value={"description": desc})
descriptions[tag] = desc
except Exception:
if self.search(tag) == []: # check for nonexisting string
logger.warning(f"Tag not found: {tag}")
continue
return descriptions

def read_tags(
Expand Down
19 changes: 11 additions & 8 deletions tagreader/web_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,10 @@ def search(
if not desc and not return_desc:
ret.append(tagname)
else:
description = self._get_tag_description(tagname)
try:
description = self._get_tag_description(tagname)
except KeyError:
description = ""
ret.append((tagname, description))

if not desc:
Expand All @@ -523,11 +526,11 @@ def _get_tag_unit(self, tag: str):
query = self.generate_get_unit_query(tag)
url = urljoin(self.base_url, "TagInfo")
data = self.fetch(url, params=query)
try:
attr_data = data["data"]["tags"][0]["attrData"]
except KeyError as e:
logger.error(f"Error. I got this: {data}")
raise e
# try:
attr_data = data["data"]["tags"][0]["attrData"]
# except KeyError as e:
# logger.error(f"Error. I got this: {data}")
# raise e
unit = ""
for a in attr_data:
if a["g"] == "Units":
Expand Down Expand Up @@ -559,8 +562,8 @@ def _get_tag_description(self, tag: str):
try:
data = self.fetch(url, params=query)
desc = data["data"]["tags"][0]["attrData"][0]["samples"][0]["v"]
except KeyError:
desc = ""
# except KeyError:
# desc = ""
except JSONDecodeError:
desc = ""
return desc
Expand Down
64 changes: 30 additions & 34 deletions tests/test_AspenHandlerREST_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@

VERIFY_SSL = False if is_AZURE_PIPELINE else get_verify_ssl()

SOURCE = "SNA"
TAG = "ATCAI"
SOURCE = "TRB"
TAG = "xxx"
FAKE_TAG = "so_random_it_cant_exist"
START_TIME = datetime(2023, 5, 1, 10, 0, 0)
STOP_TIME = datetime(2023, 5, 1, 11, 0, 0)
SAMPLE_TIME = timedelta(seconds=60)
Expand Down Expand Up @@ -77,44 +78,31 @@ def test_verify_connection(aspen_handler: AspenHandlerWeb) -> None:


def test_search_tag(client: IMSClient) -> None:
res = client.search(tag="so_specific_it_cannot_possibly_exist", desc=None)
res = client.search(tag=FAKE_TAG, desc=None)
assert 0 == len(res)

res = client.search(tag="ATCAI", desc=None)
assert res == [("ATCAI", "Sine Input")]
res = client.search(tag="AverageCPUTimeVals", desc=None)
assert res == [("AverageCPUTimeVals", "Average CPU Time")]

res = client.search(tag="ATCM*", desc=None)
assert 5 <= len(res)

[taglist, desclist] = zip(*res)
assert "ATCMIXTIME1" in taglist
assert desclist[taglist.index("ATCMIXTIME1")] == "MIX TANK 1 TIMER"

res = client.search(tag="ATCM*", desc=None)
assert 5 <= len(res)
res = client.search(tag="Aspen*", desc=None, return_desc=False)
assert len(res) < 5
assert isinstance(res, list)
assert isinstance(res[0], tuple)

res = client.search("AspenCalcTrigger1", desc=None)
assert res == [("AspenCalcTrigger1", "")]

res = client.search("ATC*", "Sine*")
assert res == [("ATCAI", "Sine Input")]
with pytest.raises(ValueError):
_ = client.search(desc="Sine Input") # noqa
assert isinstance(res[0], str)

res = client.search(tag="ATCM*", return_desc=False)
assert 5 <= len(res)
res = client.search(tag="Aspen*", desc=None)
assert len(res) < 5
assert isinstance(res, list)
assert isinstance(res[0], str)
assert isinstance(res[0], tuple)

res = client.search("AspenCalcTrigger1")
assert res == [("AspenCalcTrigger1", "")]
res = client.search("AspenCalcTrigger1", desc=None)
assert res == [("AspenCalcTrigger1", "")]

res = client.search("ATC*", "Sine*")
assert res == [("ATCAI", "Sine Input")]
res = client.search("AverageCPUTimeVals", "*CPU*")
assert res == [("AverageCPUTimeVals", "Average CPU Time")]
with pytest.raises(ValueError):
_ = client.search(desc="Sine Input") # noqa

with pytest.raises(ValueError):
res = client.search("")
Expand All @@ -126,17 +114,25 @@ def test_search_tag(client: IMSClient) -> None:


def test_read_unknown_tag(client: IMSClient) -> None:
df = client.read(
tags=["so_random_it_cant_exist"], start_time=START_TIME, end_time=STOP_TIME
)
df = client.read(tags=[FAKE_TAG], start_time=START_TIME, end_time=STOP_TIME)
assert len(df.index) == 0
df = client.read(
tags=[TAG, "so_random_it_cant_exist"], start_time=START_TIME, end_time=STOP_TIME
)
df = client.read(tags=[TAG, FAKE_TAG], start_time=START_TIME, end_time=STOP_TIME)
assert len(df.index) > 0
assert len(df.columns == 1)


def test_get_units(client: IMSClient) -> None:
d = client.get_units(FAKE_TAG)
assert isinstance(d, dict)
assert len(d.items()) == 0


def test_get_desc(client: IMSClient) -> None:
d = client.get_descriptions(FAKE_TAG)
assert isinstance(d, dict)
assert len(d.items()) == 0


def test_query_sql(client: IMSClient) -> None:
# The % causes WC_E_SYNTAX error in result. Tried "everything" but no go.
# Leaving it for now.
Expand Down
Loading