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

Refactor: Metrics processor #75

Merged
merged 3 commits into from
Sep 25, 2024

Conversation

naved001
Copy link
Collaborator

This PR adds a new metrics processor class that exposes methods for merging and condensing metrics. The actual methods remain largely the same except that they restructure the data differently (more in the commit message).
I have some more refactoring planned for those 2 methods specifically, but I will do that in a separate PR.

if namespace not in self.merged_data:
self.merged_data[namespace] = {}
if pod not in self.merged_data[namespace]:
self.merged_data[namespace][pod] = {"metrics": {}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the other lines in this file which check for a key before providing an initial value can be simplified with setdefault(). Docs here.

>>> a = {'a': []}
>>> a.setdefault('a', 'blah')
[]
>>> a.setdefault('b', 'blah')
'blah'
>>> a
{'a': [], 'b': 'blah'}
>>> 

Or/And, you can make use of a defaultdict, which provides an initial value for all keys which don't exist. Docs here.

>>> from collections import defaultdict
>>> b = defaultdict(dict)
>>> b['missing_key']
{}
>>> b
defaultdict(<class 'dict'>, {'missing_key': {}})

Or a combination of both of the above. (setdefault a defaultdict)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's useful. I updated it to use setfedault in this case.

This commit takes the methods merge_metrics and condense_metrics out of utils.py
and puts them into the MetricsProcessor class.

The callers have been updated to instantiate this class and use the methods
provided by it.

The unit tests have been moved to their own module. The tests needed some
minor updates to run but otherwise have remained unchanged.
…amespace

Earlier the keys were contacted `namespace` + `pod_name`. So, this commit
changes the dictionary from `merged_data[namespace+pod_name]` to
`merged_data[namespace][pod_name]` which I think is a better way to organize
this.

The tests have been updated to work with the new structure.
@naved001 naved001 force-pushed the refactor/metrics_processor branch from c878034 to e6837e9 Compare September 24, 2024 14:43
@naved001 naved001 merged commit 0bb508e into CCI-MOC:main Sep 25, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants