Skip to content
Snippets Groups Projects
Commit b4600d4f authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

query: Factorize code for getting statistics value


This was not only copied for the networking fields in commit 306bed0e,
but commit cfcea7ef fixed wrongly ordered parameters and didn't fix the
original. Either way, this patch merges the two cases again. The newly
added function is already tested through the tests for
_GetLiveNodeField.

Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent d6f3a69d
No related branches found
No related tags found
No related merge requests found
...@@ -1234,8 +1234,24 @@ def _GetLiveNodeField(field, kind, ctx, node): ...@@ -1234,8 +1234,24 @@ def _GetLiveNodeField(field, kind, ctx, node):
if not ctx.curlive_data: if not ctx.curlive_data:
return _FS_NODATA return _FS_NODATA
return _GetStatsField(field, kind, ctx.curlive_data)
def _GetStatsField(field, kind, data):
"""Gets a value from live statistics.
If the value is not found, L{_FS_UNAVAIL} is returned. If the field kind is
numeric a conversion to integer is attempted. If that fails, L{_FS_UNAVAIL}
is returned.
@param field: Live field name
@param kind: Data kind, one of L{constants.QFT_ALL}
@type data: dict
@param data: Statistics
"""
try: try:
value = ctx.curlive_data[field] value = data[field]
except KeyError: except KeyError:
return _FS_UNAVAIL return _FS_UNAVAIL
...@@ -1249,7 +1265,7 @@ def _GetLiveNodeField(field, kind, ctx, node): ...@@ -1249,7 +1265,7 @@ def _GetLiveNodeField(field, kind, ctx, node):
return int(value) return int(value)
except (ValueError, TypeError): except (ValueError, TypeError):
logging.exception("Failed to convert node field '%s' (value %r) to int", logging.exception("Failed to convert node field '%s' (value %r) to int",
value, field) field, value)
return _FS_UNAVAIL return _FS_UNAVAIL
...@@ -2509,24 +2525,7 @@ def _GetNetworkStatsField(field, kind, ctx, _): ...@@ -2509,24 +2525,7 @@ def _GetNetworkStatsField(field, kind, ctx, _):
@type ctx: L{NetworkQueryData} @type ctx: L{NetworkQueryData}
""" """
return _GetStatsField(field, kind, ctx.curstats)
try:
value = ctx.curstats[field]
except KeyError:
return _FS_UNAVAIL
if kind == QFT_TEXT:
return value
assert kind in (QFT_NUMBER, QFT_UNIT)
# Try to convert into number
try:
return int(value)
except (ValueError, TypeError):
logging.exception("Failed to convert network field '%s' (value %r) to int",
field, value)
return _FS_UNAVAIL
def _BuildNetworkFields(): def _BuildNetworkFields():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment