-
Notifications
You must be signed in to change notification settings - Fork 16
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
Feat/hw tool state pattern #134
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I left some comments that I think it can be improved.
I also would like to know why we are using # type: ignore
in a lot of places of the code.
import logging | ||
from time import sleep | ||
from typing import Any, Dict, Optional, Tuple | ||
from typing import Any, Dict, List, Optional, Tuple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typing Dict
and List
are deprecated. It's recommendable using plain list
and dict
if len(self._stored.missing_resources) == 0: # type: ignore | ||
return False | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this would be equivalent?
if len(self._stored.missing_resources) == 0: # type: ignore | |
return False | |
return True | |
return len(self._stored.missing_resources) > 0 |
if len(self._stored.failed_strategies) == 0: # type: ignore | ||
return False | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if len(self._stored.failed_strategies) == 0: # type: ignore | |
return False | |
return True | |
return len(self._stored.failed_strategies) > 0 | |
if ( | ||
len(self._stored.missing_resources) > 0 # type: ignore | ||
or len(self._stored.failed_strategies) > 0 # type: ignore | ||
): | ||
return False | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can use state_missing_resource
and state_failed_install_strategies
and not repeat the logic
if ( | |
len(self._stored.missing_resources) > 0 # type: ignore | |
or len(self._stored.failed_strategies) > 0 # type: ignore | |
): | |
return False | |
return True | |
return self.state_missing_resource or self.state_failed_install_strategies |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the meaning is different here, but I know what you want.
health = self.exporter.check_health() | ||
if not health: | ||
logger.warning("Exporter health check - failed.") | ||
return health |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use walrus notation
health = self.exporter.check_health() | |
if not health: | |
logger.warning("Exporter health check - failed.") | |
return health | |
if not health := self.exporter.check_health(): | |
logger.warning("Exporter health check - failed.") | |
return health |
config={}, | ||
exporter_installed=False, | ||
installed=False, | ||
hw_white_list=[], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think it's preferable using accept_list
or allow_list
than white_list
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This terms is been used in whole project now. We can use another PR to replace it.
|
||
if not installed: | ||
logger.error(msg) | ||
self._log_stored() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why here we don't have the same pattern of generating the message and logging as it happens on state_msg_missing_resources
and state_msg_failed_install_strategies
?
@property | ||
def state_msg_missing_resources(self) -> str: | ||
"""Message for missing resources.""" | ||
return f"Missing resources: {list(self._stored.missing_resources)}" # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe using .join
as the comment bellow
if len(failed_checks) > 0: | ||
return False, f"Fail strategy checks: {failed_checks}" | ||
return True, "" | ||
if ok: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
it's not a good variable name and it's not used on other places. I prefer checking directly like if strategy.check():
"""Install the exporter.""" | ||
logger.info("Installing %s.", EXPORTER_NAME) | ||
success = self.template.render_config(port=port, level=level, redfish_creds=redfish_creds) | ||
success = self.template.render_config( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why we are using twice and overriding the variable success
. If this fails it will not be checked and logged
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couple of small comments, but for functionality point of view it looks good to me.
def _log_stored(self) -> None: | ||
"""Logger print _stored information.""" | ||
msg = ( | ||
"Hw tool state: " # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need to use type: ignore here?
"""Logger print _stored information.""" | ||
msg = ( | ||
"Hw tool state: " # type: ignore | ||
f"hw_white_list: {list(self._stored.hw_white_list)}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really needed to convert list to list?
>>> a = [1, 2, 3]
>>> f"without: {a} and with {list(a)}"
'without: [1, 2, 3] and with [1, 2, 3]'
f" installed_strategies: {list(self._stored.installed_strategies)}" | ||
f" failed_strategies: {list(self._stored.failed_strategies)}" | ||
) | ||
logger.info(msg) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will suggest to use lazy formatting instead of f-string, e.g. logger.log("some list: %s", some_list)
.
len(self._stored.missing_resources) > 0 # type: ignore | ||
or len(self._stored.failed_strategies) > 0 # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why we need to have this type ignore again? The StoredState
is not providing type hinting? If yes, could we report an issue about it (I check there is none).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, StoredState
is not providing type hinting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to report an issue. Those type: ignore
are enjoying :).
if ( | ||
len(self._stored.missing_resources) > 0 # type: ignore | ||
or len(self._stored.failed_strategies) > 0 # type: ignore | ||
): | ||
return False | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if ( | |
len(self._stored.missing_resources) > 0 # type: ignore | |
or len(self._stored.failed_strategies) > 0 # type: ignore | |
): | |
return False | |
return True | |
return not ( | |
len(self._stored.missing_resources) > 0 # type: ignore | |
or len(self._stored.failed_strategies) > 0 # type: ignore | |
) |
if len(self._stored.missing_resources) == 0: # type: ignore | ||
return False | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if len(self._stored.missing_resources) == 0: # type: ignore | |
return False | |
return True | |
return not len(self._stored.missing_resources) == 0 # type: ignore |
if strategy.name not in hw_white_list: | ||
continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should provide some log message for skipped strategy, same in install function.
return | ||
|
||
port = self.model.config.get("exporter-port", "10000") | ||
level = self.model.config.get("exporter-log-level", "INFO") | ||
logger.info("Get redfish creds." "") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logger.info("Get redfish creds." "") | |
logger.info("Get redfish creds.") |
Draft PR without unit test.
_stored
and use it for conditional expression_stored
[EC] this PR should cover