Skip to content

Commit eac4b7a

Browse files
author
Aitor Gómez Goiri
committed
Adding additional method get_all_measures to StatusReceiver class.
1 parent 962aebe commit eac4b7a

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

rstatus/communication.py

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,52 @@ def store(self):
4141

4242
class StatusReceiver(RedisClient):
4343
def __init__(self, redis_connection, status_keys):
44+
"""
45+
:param redis_connection:
46+
:param status_keys: Features which will be taken into account.
47+
:return:
48+
"""
4449
super(StatusReceiver, self).__init__(redis_connection)
4550
self._status_keys = status_keys
46-
self.regex = re.compile("/", re.VERBOSE)
51+
self._regex = re.compile("/", re.VERBOSE)
4752

4853
def _put_in_dict(self, dictio, machine_id, feature_name, value):
4954
if machine_id not in dictio:
5055
dictio[machine_id] = {}
5156
dictio[machine_id][feature_name] = value
5257

53-
def get_last_measures(self, machine_ids=None):
58+
def _get_whatever_measures(self, method_to_call, machine_ids=None):
59+
"""
60+
:param machine_ids:
61+
:param number_measures: If None all the measures will be returned.
62+
:param method_to_call: Method from this class to be called to obtain the desired value from Redis DB.
63+
:return:
64+
"""
5465
measures = {}
5566
for status_key in self._status_keys:
5667
if machine_ids is None:
5768
possible_features = self._conn.keys("*/%s" % status_key)
5869
for feature in possible_features:
59-
value = self._conn.lpop(feature) # get last value
60-
machine_id, feature_name = self.regex.split(feature)
61-
self._put_in_dict(measures, machine_id, feature_name, value)
70+
values = method_to_call(feature)
71+
machine_id, feature_name = self._regex.split(feature)
72+
self._put_in_dict(measures, machine_id, feature_name, values)
6273
else:
6374
for machine_id in machine_ids:
64-
value = self._conn.lpop("%s/%s" % (machine_id, status_key)) # get last value
65-
self._put_in_dict(measures, machine_id, status_key, value)
75+
values = method_to_call("%s/%s" % (machine_id, status_key)) # get last value
76+
self._put_in_dict(measures, machine_id, status_key, values)
6677
return measures
78+
79+
def get_last_measures(self, machine_ids=None):
80+
return self._get_whatever_measures(self._conn.lpop, machine_ids)
81+
82+
def _get_all_measures_by_feature(self, feature):
83+
l = self._conn.llen(feature)
84+
return self._conn.lrange(feature, 0, l)
85+
86+
def get_all_measures(self, machine_ids=None):
87+
"""
88+
:param machine_ids:
89+
:param number_measures: If None all the measures will be returned.
90+
:return:
91+
"""
92+
return self._get_whatever_measures(self._get_all_measures_by_feature, machine_ids)

0 commit comments

Comments
 (0)