query: Factorize code for getting statistics value
authorMichael Hanselmann <hansmi@google.com>
Tue, 18 Dec 2012 16:38:54 +0000 (17:38 +0100)
committerMichael Hanselmann <hansmi@google.com>
Wed, 19 Dec 2012 16:54:58 +0000 (17:54 +0100)
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: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>

lib/query.py

index 48d1b7b..396ff80 100644 (file)
@@ -1234,8 +1234,24 @@ def _GetLiveNodeField(field, kind, ctx, node):
   if not ctx.curlive_data:
     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:
-    value = ctx.curlive_data[field]
+    value = data[field]
   except KeyError:
     return _FS_UNAVAIL
 
@@ -1249,7 +1265,7 @@ def _GetLiveNodeField(field, kind, ctx, node):
     return int(value)
   except (ValueError, TypeError):
     logging.exception("Failed to convert node field '%s' (value %r) to int",
-                      value, field)
+                      field, value)
     return _FS_UNAVAIL
 
 
@@ -2509,24 +2525,7 @@ def _GetNetworkStatsField(field, kind, ctx, _):
   @type ctx: L{NetworkQueryData}
 
   """
-
-  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
+  return _GetStatsField(field, kind, ctx.curstats)
 
 
 def _BuildNetworkFields():