# -*- test-case-name: vumi.blinkenlights.tests.test_heartbeat -*-

"""
Storage Schema:

 Systems (set):            key = systems
 Workers in system (set):  key = system:$SYSTEM_ID:workers
 Worker attributes (hash): key = worker:$WORKER_ID:attrs
 Worker hostinfo (hash):   key = worker:$WORKER_ID:hosts
 Worker Issue (hash):      key = worker:$WORKER_ID:issue
"""

from vumi.persist.redis_base import Manager


# some redis key-making functions.
#
# These functions are reused by the monitoring dash,
# which uses another redis client interface
# Its a lot simpler for now to make these toplevel functions.

def attr_key(worker_id):
    return "worker:%s:attrs" % worker_id


def hostinfo_key(worker_id):
    return "worker:%s:hosts" % worker_id


def issue_key(worker_id):
    return "worker:%s:issue" % worker_id


class Storage(object):
    """
    TxRedis interface for the heartbeat monitor. Basically only
    supports mutating operations since the monitor does not do
    any reads
    """

    def __init__(self, redis):
        self._redis = redis
        self.manager = redis

    @Manager.calls_manager
    def set_systems(self, system_ids):
        """ delete existing system ids and replace with new ones """
        key = "systems"
        yield self._redis.delete(key)
        for sys_id in system_ids:
            yield self._redis.sadd(key, sys_id)

    @Manager.calls_manager
    def set_system_workers(self, system_id, worker_ids):
        """ delete existing worker ids and replace with new ones """
        key = "system:%s:workers" % system_id
        yield self._redis.delete(key)
        # not sure how to make sadd() accept varags :-(
        for wkr_id in worker_ids:
            yield self._redis.sadd(key, wkr_id)

    @Manager.calls_manager
    def set_worker_attrs(self, worker_id, attrs):
        key = attr_key(worker_id)
        yield self._redis.hmset(key, attrs)

    @Manager.calls_manager
    def set_worker_hostinfo(self, worker_id, hostinfo):
        key = hostinfo_key(worker_id)
        yield self._redis.hmset(key, hostinfo)

    @Manager.calls_manager
    def delete_worker_hostinfo(self, worker_id):
        key = hostinfo_key(worker_id)
        yield self._redis.delete(key)

    @Manager.calls_manager
    def set_worker_issue(self, worker_id, issue):
        key = issue_key(worker_id)
        yield self._redis.hmset(key, issue)

    @Manager.calls_manager
    def delete_worker_issue(self, worker_id):
        key = issue_key(worker_id)
        yield self._redis.delete(key)

    @Manager.calls_manager
    def open_or_update_issue(self, worker_id, issue):
        key = issue_key(worker_id)
        # add these fields if they do not already exist
        yield self._redis.hsetnx(key, 'issue_type', issue['issue_type'])
        yield self._redis.hsetnx(key, 'start_time', issue['start_time'])
        # update current proc count
        yield self._redis.hset(key, 'procs_count', issue['procs_count'])

@@ -34,16 +34,26 @@
 nlights.
+heartbeat.
 tests.te
@@ -55,25 +55,23 @@
 ts.test_
-heartbeat
+monitor
  -*-%0A%0A%22%22
@@ -369,53 +369,8 @@
 %22%22%0A%0A
-from vumi.persist.redis_base import Manager%0A%0A
 %0A# s
@@ -1046,35 +1046,8 @@
 is%0A%0A
-    @Manager.calls_manager%0A
     
@@ -1174,38 +1174,32 @@
 ystems%22%0A        
-yield 
 self._redis.dele
@@ -1244,38 +1244,32 @@
 ds:%0A            
-yield 
 self._redis.sadd
@@ -1287,35 +1287,8 @@
 d)%0A%0A
-    @Manager.calls_manager%0A
     
@@ -1455,38 +1455,32 @@
 stem_id%0A        
-yield 
 self._redis.dele
@@ -1581,38 +1581,32 @@
 ds:%0A            
-yield 
 self._redis.sadd
@@ -1624,35 +1624,8 @@
 d)%0A%0A
-    @Manager.calls_manager%0A
     
@@ -1704,38 +1704,32 @@
 ker_id)%0A        
-yield 
 self._redis.hmse
@@ -1747,35 +1747,8 @@
 s)%0A%0A
-    @Manager.calls_manager%0A
     
@@ -1837,38 +1837,32 @@
 ker_id)%0A        
-yield 
 self._redis.hmse
@@ -1883,35 +1883,8 @@
 o)%0A%0A
-    @Manager.calls_manager%0A
     
@@ -1966,38 +1966,32 @@
 ker_id)%0A        
-yield 
 self._redis.dele
@@ -2003,35 +2003,8 @@
 y)%0A%0A
-    @Manager.calls_manager%0A
     
@@ -2084,38 +2084,32 @@
 ker_id)%0A        
-yield 
 self._redis.hmse
@@ -2127,35 +2127,8 @@
 e)%0A%0A
-    @Manager.calls_manager%0A
     
@@ -2204,38 +2204,32 @@
 ker_id)%0A        
-yield 
 self._redis.dele
@@ -2241,35 +2241,8 @@
 y)%0A%0A
-    @Manager.calls_manager%0A
     
@@ -2382,38 +2382,32 @@
 y exist%0A        
-yield 
 self._redis.hset
@@ -2449,38 +2449,32 @@
 type'%5D)%0A        
-yield 
 self._redis.hset
@@ -2564,14 +2564,8 @@
     
-yield 
 self

Remove @Manager.calls_manager adaptors from heartbeat/storage.py