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

[procdockerstatsd] add missing unit conversion #7151

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions src/sonic-host-services/scripts/procdockerstatsd
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,29 @@ class ProcDockerStats(daemon_base.DaemonBase):
UNITS_B = 'B'
UNITS_KB = 'KB'
UNITS_MB = 'MB'
UNITS_GB = 'GB'
UNITS_TB = 'TB'
UNITS_MiB = 'MiB'
UNITS_GiB = 'GiB'
UNITS_TiB = 'TiB'

res = re.match(r'(\d+\.?\d*)([a-zA-Z]+)', value)
value = float(res.groups()[0])
units = res.groups()[1]
if units.lower() == UNITS_KB.lower():
value *= 1000
elif units.lower() == UNITS_MB.lower():
value *= (1000 * 1000)
value *= (1000 ** 2)
elif units.lower() == UNITS_GB.lower():
value *= (1000 ** 3)
elif units.lower() == UNITS_TB.lower():
value *= (1000 ** 4)
elif units.lower() == UNITS_MiB.lower():
value *= (1024 * 1024)
value *= (1024 ** 2)
elif units.lower() == UNITS_GiB.lower():
value *= (1024 * 1024 * 1024)
value *= (1024 ** 3)
elif units.lower() == UNITS_TiB.lower():
value *= (1024 ** 4)

return int(round(value))

Expand Down
7 changes: 6 additions & 1 deletion src/sonic-host-services/tests/procdockerstatsd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,14 @@ def test_convert_to_bytes(self):
('500MiB', 524288000),
('66.41MiB', 69635932),
('333.6MiB', 349804954),
('7.75GB', 7750000000),
('800GB', 800000000000),
('1GiB', 1073741824),
('500GiB', 536870912000),
('7.751GiB', 8322572878)
('7.751GiB', 8322572878),
('8.1TiB', 8906044184986),
('12TB', 12000000000000),
('2.25TB', 2250000000000)
Copy link
Collaborator

Choose a reason for hiding this comment

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

the test is too simple, we need to have a test that convert from the ps -e output and test the content in the database as end-to-end test.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this test is appropriate to the convert_to_bytes() method. More test cases need to be added in order to ensure full end-to-end functionality.

]

pdstatsd = procdockerstatsd.ProcDockerStats(procdockerstatsd.SYSLOG_IDENTIFIER)
Expand Down