-
Notifications
You must be signed in to change notification settings - Fork 29
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
OCPERT-9 Cache slack user/group IDs in runtime #357
Conversation
…the slack server Signed-off-by: Jayashree Huttanagoudar <[email protected]>
/cc @rioliu-rh |
oar/core/notification.py
Outdated
@@ -14,7 +14,7 @@ | |||
|
|||
|
|||
logger = logging.getLogger(__name__) | |||
|
|||
in_memory_cache = dict() |
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.
@jhuttana it's better to declare cache object as field of SlackClient
, the class can be initialized in other module
oar/core/notification.py
Outdated
@@ -265,11 +265,15 @@ def get_user_id_by_email(self, email): | |||
str: slack user id | |||
""" | |||
email = self.transform_email(email) | |||
|
|||
if in_memory_cache.get(email) is not None: | |||
return "<@%s>" % in_memory_cache.get(email) |
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.
@jhuttana this line has duplicate pattern declaration, suggest to declare local var userid
, fill the var by if/else logic
userid = None
if email in cache_dict:
userid = cache_dict.get(email)
else:
// api call
userid = resp["user"]["id"]
cache_dict[email] = userid
return "<@%s>" % userid
it looks more efficient
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.
also, we can add debug level log here to indicate the contact is retrieved from cache. so if you execute the cmd with option -v/--debug
, the logic can be traced.
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.
Sure
oar/core/notification.py
Outdated
@@ -287,6 +291,8 @@ def get_group_id_by_name(self, name): | |||
group id: slack group id | |||
""" | |||
ret_id = "" | |||
if in_memory_cache.get(name) is not 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.
@jhuttana same concern as above
Thanks for the comments Rio. I will address these comments. |
Signed-off-by: Jayashree Huttanagoudar <[email protected]>
oar/core/notification.py
Outdated
@@ -238,6 +237,8 @@ def send_email(self, to_addrs, subject, content): | |||
|
|||
|
|||
class SlackClient: | |||
cache_dict = dict() |
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.
@jhuttana cache object should be moved to class constructor __init__
like self.cache = {}
, all subsequent calls need to be changed as well. e.g. self.cache[xx]=y
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.
That would be perfect. I will do that.
…cache variable to the constructor Signed-off-by: Jayashree Huttanagoudar <[email protected]>
Signed-off-by: Jayashree Huttanagoudar <[email protected]>
oar/core/notification.py
Outdated
@@ -239,6 +238,7 @@ def send_email(self, to_addrs, subject, content): | |||
|
|||
class SlackClient: | |||
def __init__(self, bot_token): | |||
self.cache_dict = dict() |
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.
can we do code formatting, put all the local vars together
self.a = xx
self.b = yy
you can use formatter ms-python.black-formatter
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 check that and try to apply
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.
For me it looks like declaring and initializing the runtime cache variable inside the constructor is fine (though it is referenced in two different methods). I don't see any difference in moving its declaration to the respective methods. For eg:
#Case-1: Declare and initialize the variable in constructor
class test:
def __init__(self):
self.attr1 = dict()
def a(self):
self.attr1 = {1:'one'}
print(self.attr1)
def b(self):
self.attr1 = {2:'two'}
print(self.attr1)
#Case-2: Declare and initialize the variable in respective methods
class test1:
def __init__(self):
print("Inside test1 class")
def aa(self):
self.attr1 = dict()
self.attr1 = {1:'one'}
print(self.attr1)
def bb(self):
self.attr1 = dict()
self.attr1 = {2:'Two'}
print(self.attr1)
#For Case-1
t = test()
t.a()
t.b()
#For Case-2
tt = test1()
tt.aa()
tt.bb()
From your comment my understanding is that you are suggesting to go for Case-2 in the above example.
Though Case-1 and Case-2 are trying to achieve the same thing , Case-1 looks more efficient to me.
If my understanding is wrong could you please clarify what exactly are you suggesting?
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 just want to make the code looks clean, put the all the local var declaration code together.
self.cache_dict = dict() | |
if not bot_token: | |
raise NotificationException("slack bot token is not available") | |
self.client = WebClient(token=bot_token) | |
self.cache_dict = dict() | |
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.
Ooops sorry !! I totally understood it in a different way :)
I will do that.
oar/core/notification.py
Outdated
|
||
logger.debug(f"Cached email ids: {self.cache_dict}") |
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.
@jhuttana we should not output all the cached contacts at func level. it will cause trace log flood, my suggestion is add debug log entry for cache put/get ops. then the user can see which contact is cached and which one is retrieved from cache
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 I am not getting it.
Do you mean to change it as: logger.debug(f"Cached email ids: {self.cache_dict.get(email)}")
?
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 email in cache:
// get userid from cache
logger.debug(f"slack user id of {email} is retrieved from cached")
else:
// get userid from api call
// put the userid to cache object
logger.debug(f"slack user id of {email} is cached")
oar/core/notification.py
Outdated
|
||
if not ret_id: | ||
raise NotificationException( | ||
f"cannot find slack group id by name {name}") | ||
|
||
logger.debug(f"Cached group ids: {self.cache_dict}") |
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.
@jhuttana same concern as above https://github.com/openshift/release-tests/pull/357/files#r1848102656
…rom the cache or added to the cache Signed-off-by: Jayashree Huttanagoudar <[email protected]>
Signed-off-by: Jayashree Huttanagoudar <[email protected]>
I think you need to create a new unit test to run funcs release-tests/tests/test_notification.py Line 24 in 35262b0
enabled debug logging: release-tests/tests/test_artifacts.py Lines 9 to 13 in 35262b0
|
Sure I will give it a try |
Signed-off-by: Jayashree Huttanagoudar <[email protected]>
/retest |
2 similar comments
/retest |
/retest |
you need to enable debug level logging in module |
Sure will do that |
if unit test is pass, we can merge this PR |
Signed-off-by: Jayashree Huttanagoudar <[email protected]>
Verify the unittest case
|
Signed-off-by: Jayashree Huttanagoudar <[email protected]>
@jhuttana: all tests passed! Full PR test history. Your PR dashboard. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here. |
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: rioliu-rh The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Could you please review the changes?