[rhmessaging-commits] rhmessaging commits: r1445 - in mgmt: cumin/python/cumin and 1 other directory.

rhmessaging-commits at lists.jboss.org rhmessaging-commits at lists.jboss.org
Sun Dec 9 20:46:31 EST 2007


Author: justi9
Date: 2007-12-09 20:46:31 -0500 (Sun, 09 Dec 2007)
New Revision: 1445

Modified:
   mgmt/bin/quirk
   mgmt/cumin/python/cumin/broker.py
   mgmt/cumin/python/cumin/broker.strings
   mgmt/cumin/python/cumin/brokercluster.py
   mgmt/cumin/python/cumin/brokercluster.strings
   mgmt/cumin/python/cumin/brokergroup.py
   mgmt/cumin/python/cumin/brokergroup.strings
   mgmt/cumin/python/cumin/exchange.py
   mgmt/cumin/python/cumin/exchange.strings
   mgmt/cumin/python/cumin/page.strings
   mgmt/cumin/python/cumin/queue.py
   mgmt/cumin/python/cumin/queue.strings
   mgmt/cumin/python/cumin/widgets.py
Log:
Enhances the quirk amqp client to generate some kinds of demo data.

Adds paginators to broker, queue, and exchange views.

Simplifies the broker group list.

Changes the way the Paginator gets its total item count set.



Modified: mgmt/bin/quirk
===================================================================
--- mgmt/bin/quirk	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/bin/quirk	2007-12-10 01:46:31 UTC (rev 1445)
@@ -1,177 +1,236 @@
 #!/usr/bin/env python
 
-import sys, qpid
+import sys, qpid, time
 
 class Exchange(object):
     def __init__(self, session, name):
         self.session = session
         self.name = name
+        self.type = "direct"
 
+    def declare(self):
+        self.session.psession.exchange_declare(exchange=self.name,
+                                               type=self.type)
+
+    def exists(self):
+        return False #XXX figure out how to do this
+
 class Queue(object):
     def __init__(self, session, name):
         self.session = session
         self.name = name
 
+    def declare(self):
+        self.session.psession.queue_declare(queue=self.name)
+
+    def exists(self):
+        return False #XXX figure out how to do this
+
+    def bind(self, exchange, binding_key=None):
+        if binding_key is None:
+            binding_key = self.name
+
+        self.session.psession.queue_bind(exchange=exchange.name,
+                                         queue=self.name,
+                                         routing_key=binding_key)
+
 class Subscription(object):
-    def __init__(self, session, name, queue):
+    def __init__(self, session, queue):
         self.session = session
-        self.name = name
+        self.name = queue.name # XXX bad?
         self.queue = queue
 
-        # XXX what all does this do?  it seems to declare things
-
-        # XXX what is the destination arg for?
-
-        # XXX from reading the spec, "destination" seems less
-        # appropriate than "subscription name" (which is what the spec
-        # ch. 25 docs say it is)
-
-        session.csession.message_subscribe(queue="test",
+        session.psession.message_subscribe(queue=queue.name,
                                            destination=self.name)
 
-        session.csession.message_flow(self.name, 0, 0xFFFFFFFF)
-        session.csession.message_flow(self.name, 1, 0xFFFFFFFF)
+        session.psession.message_flow(self.name, 0, 0xFFFFFFFF)
+        session.psession.message_flow(self.name, 1, 0xFFFFFFFF)
 
-        self.client_queue = session.client.queue(self.name)
+        self.client_queue = session.client.pclient.queue(self.name)
 
     def get(self):
-        m = Message()
+        m = self.session.message()
         m.content = self.client_queue.get(timeout=10).content
         return m
 
 class Message(object):
-    def __init__(self, body=""):
-        self.content = qpid.content.Content(body)
+    def __init__(self, session):
+        self.session = session
+
+    def set(self, payload):
+        self.content = qpid.content.Content(payload)
         self.content["content_type"] = "text/plain"
+        
+    def send(self, dest, routing_key=None):
+        if dest.__class__ is Queue:
+            self.content["routing_key"] = dest.name
+            self.session.psession.message_transfer(destination="",
+                                                   content=self.content)
+        elif dest.__class__ is Exchange:
+            if routing_key is None:
+                raise Exception("Routing key not set")
 
-    def set_routing_key(self, key):
-        self.content["routing_key"] = key
+            self.session.psession.message_transfer(destination=dest.name,
+                                                   content=self.content)
+        else:
+            raise Exception("Unknown destination object")
 
-    def get_routing_key(self):
-        try:
-            return self.content["routing_key"]
-        except KeyError:
-            pass
-
     def __str__(self):
         return self.content.body
 
+class Client(object):
+    def __init__(self, host, port):
+        self.pclient = qpid.client.Client(host, port)
+
+    def session(self):
+        return Session(self)
+
+    def login(self, user, password):
+        self.pclient.start({"LOGIN": user, "PASSWORD": password})
+
 class Session(object):
-    def __init__(self, client, csession):
+    def __init__(self, client):
         self.client = client
-        self.csession = csession
+        self.psession = client.pclient.session()
 
     def open(self):
-        self.csession.open()
+        self.psession.open()
 
     def close(self):
-        self.csession.close()
+        self.psession.close()
 
-    def declare(self, object):
-        if object.__class__ is Queue:
-            # XXX blows up without queue=
-            self.csession.queue_declare(queue=object.name)
-        elif object.__class__ is Exchange:
-            self.csession.exchange_declare(exchange=object.name)
-        else:
-            raise Exception()
+    def exchange(self, name):
+        return Exchange(self, name)
 
-    def bind(self, queue, exchange, binding_key=None):
-        if binding_key is None:
-            binding_key = queue.name
+    def queue(self, name):
+        return Queue(self, name)
 
-        self.csession.queue_bind(exchange=exchange.name,
-                                 queue=queue.name,
-                                 routing_key=binding_key)
+    def subscribe(self, queue):
+        return Subscription(self, queue)
 
-    def publish(self, message, object):
-        if object.__class__ is Exchange:
-            self.csession.message_transfer(destination=object.name,
-                                           content=message.content)
-        elif object.__class__ is Queue:
-            # XXX maybe this shouldn't be conditional
-            if message.get_routing_key() is None:
-                message.set_routing_key(object.name)
+    def message(self):
+        return Message(self)
 
-            self.csession.message_transfer(destination="",
-                                           content=message.content)
-        else:
-            raise Exception()
+class TestCommand(object):
+    def __init__(self, name):
+        self.name = name
 
-def direct_with_explicit_exchange(host, port):
-    client = qpid.client.Client(host, port)
-    client.start({"LOGIN": "guest", "PASSWORD": "guest"})
+    def run(self, client):
+        session = client.session()
+        session.open()
 
-    session = Session(client, client.session())
-    session.open()
+        try:
+            q = session.queue("quirk.test")
 
-    try:
-        q = Queue(session, "test")
-        e = Exchange(session, "amq.direct")
-        s = Subscription(session, "s", q)
+            if not q.exists():
+                q.declare()
 
-        session.declare(q)
-        session.bind(q, e)
+            s = session.subscribe(q)
 
-        for i in range(0, 10):
-            print i,
+            for i in range(0, 10):
+                print i,
+                m = session.message()
+                m.set("message %i" % i)
+                m.send(q)
+                print "Sent", m
 
-            m = Message("Test message " + str(i))
+            for i in range(0, 10):
+                print i,
+                m = s.get()
+                print "Received", m
+        finally:
+            session.close()
 
-            # XXX make this an arg to publish, instead?
-            m.set_routing_key(q.name)
+class BenchCommand(object):
+    def __init__(self, name):
+        self.name = name
 
-            session.publish(m, e)
+    def run(self, client):
+        session = client.session()
+        session.open()
 
-            print "."
+        try:
+            q = session.queue("quirk.bench")
 
-        for i in range(0, 10):
-            print i,
+            if not q.exists():
+                q.declare()
 
-            m = s.get()
+            s = session.subscribe(q)
 
-            print m
-    finally:
-        session.close()
+            i = 0
 
-def direct_with_implicit_exchange(host, port):
-    client = qpid.client.Client(host, port)
-    client.start({"LOGIN": "guest", "PASSWORD": "guest"})
+            while True:
+                m = session.message()
+                m.set(str(i))
+                m.send(q)
 
-    # Now, simpler, using the default exchange:
+                if i % 1000 == 0:
+                    print ".",
+                
+                i += 1
+        finally:
+            session.close()
 
-    session = Session(client, client.session())
-    session.open()
+class DemoCommand(object):
+    def __init__(self, name):
+        self.name = name
 
-    try:
-        q = Queue(session, "test")
-        s = Subscription(session, "s", q)
+    def run(self, client):
+        session = client.session()
+        session.open()
 
-        session.declare(q)
+        qs = list()
+        es = list()
 
-        for i in range(0, 10):
-            print i,
-            m = Message("m%i" % i)
-            session.publish(m, q)
-            print "Sent", m
+        try:
+            for i in range(0, 30):
+                name = "demo%02i" % (i + 1)
 
-        for i in range(0, 10):
-            print i,
-            m = s.get()
-            print "Received", m
-    finally:
-        session.close()
+                q = session.queue(name)
+                q.declare()
 
+                qs.append(q)
+
+                e = session.exchange(name)
+                e.declare()
+
+                es.append(e)
+        finally:
+            session.close()
+
+def usage():
+    print "Usage: quirk COMMAND [IP:PORT]"
+    sys.exit(2)
+
+commands = dict()
+commands["test"] = TestCommand("test")
+commands["bench"] = BenchCommand("bench")
+commands["demo"] = DemoCommand("demo")
+
 if __name__ == "__main__":
-    if len(sys.argv) != 2:
-        print "Usage: quirk IP:PORT"
-        sys.exit(2)
+    if len(sys.argv) < 2:
+        usage();
 
-    addr = sys.argv[1].split(":")
+    command = sys.argv[1]
 
-    if len(addr) > 1:
-        host, port = (addr[0], int(addr[1]))
-    else:
-        host, port = (addr[0], 5672)
+    if command not in commands:
+        print "Unknown command '%s'" % command
+        usage()
+    
+    try:
+        addr = sys.argv[2].split(":")
 
-    direct_with_implicit_exchange(host, port)
+        if len(addr) > 1:
+            host, port = addr[0], int(addr[1])
+        else:
+            host, port = addr[0], 5672
+    except IndexError:
+        host, port = "localhost", 5672
+
+    client = Client(host, port)
+    client.login("guest", "guest")
+
+    try:
+        commands[command].run(client)
+    except KeyboardInterrupt:
+        pass

Modified: mgmt/cumin/python/cumin/broker.py
===================================================================
--- mgmt/cumin/python/cumin/broker.py	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/broker.py	2007-12-10 01:46:31 UTC (rev 1445)
@@ -38,7 +38,7 @@
         self.add_parameter(self.submit)
         self.add_form_parameter(self.submit)
         
-        self.paginator = self.BrokerPaginator(app, "page")
+        self.paginator = Paginator(app, "page")
         self.add_child(self.paginator)
 
     def get_title(self, session, model):
@@ -69,6 +69,8 @@
                     broker.addBrokerGroup(group)
 
             self.page().set_redirect_url(session, session.marshal())
+        else:
+            self.paginator.set_count(session, BrokerRegistration.select().count())
 
     def render_action_param_name(self, session, broker):
         return self.action.path()
@@ -128,10 +130,6 @@
 
     class BrokerSetGroupInput(BrokerGroupInput):
         pass
-    
-    class BrokerPaginator(Paginator):
-        def get_object(self, session, model):
-            return list(BrokerRegistration.select()) #XXX ugh
 
 class BrokerFrame(CuminFrame):
     def __init__(self, app, name):

Modified: mgmt/cumin/python/cumin/broker.strings
===================================================================
--- mgmt/cumin/python/cumin/broker.strings	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/broker.strings	2007-12-10 01:46:31 UTC (rev 1445)
@@ -2,6 +2,8 @@
 <form id="{id}" method="post" action="?">
   <!-- <select onchange="document.getElementById('{id}.submit').submit()"> -->
 
+  <div style="float: right; position: relative; top: -2em;">{page}</div>
+
   <div class="sactions">
     <h2>Act on Selected Brokers:</h2>
 

Modified: mgmt/cumin/python/cumin/brokercluster.py
===================================================================
--- mgmt/cumin/python/cumin/brokercluster.py	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/brokercluster.py	2007-12-10 01:46:31 UTC (rev 1445)
@@ -30,8 +30,7 @@
         return fmt_olink(branch, cluster)
 
     def render_item_config(self, session, cluster):
-        count = len(cluster.brokers)
-        return "%i broker%s" % (count, ess(count))
+        return len(cluster.brokers)
 
     def render_item_status(self, session, cluster):
         writer = Writer()

Modified: mgmt/cumin/python/cumin/brokercluster.strings
===================================================================
--- mgmt/cumin/python/cumin/brokercluster.strings	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/brokercluster.strings	2007-12-10 01:46:31 UTC (rev 1445)
@@ -20,7 +20,7 @@
 <table class="mobjects">
   <tr>
     <th>Name</th>
-    <th>Configuration</th>
+    <th>Brokers</th>
     <th>Status</th>
   </tr>
 

Modified: mgmt/cumin/python/cumin/brokergroup.py
===================================================================
--- mgmt/cumin/python/cumin/brokergroup.py	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/brokergroup.py	2007-12-10 01:46:31 UTC (rev 1445)
@@ -28,8 +28,7 @@
         return fmt_olink(branch, group)
 
     def render_item_config(self, session, group):
-        count = len(group.brokers)
-        return "%i broker%s" % (count, ess(count))
+        return len(group.brokers)
 
     def render_item_status(self, session, group):
         writer = Writer()

Modified: mgmt/cumin/python/cumin/brokergroup.strings
===================================================================
--- mgmt/cumin/python/cumin/brokergroup.strings	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/brokergroup.strings	2007-12-10 01:46:31 UTC (rev 1445)
@@ -3,17 +3,10 @@
   <li><a class="nav" href="{group_add_href}">Add Broker Group</a></li>
 </ul>
 
-<div class="sactions">
-  <h2>Act on Selected Groups:</h2>
-  <button>Shutdown</button>
-  <button>Remove</button>
-</div>
-
 <table class="mobjects">
   <tr>
-    <th><input type="checkbox"/></th>
     <th>Name</th>
-    <th>Configuration</th>
+    <th>Brokers</th>
     <th>Status</th>
   </tr>
 
@@ -22,7 +15,6 @@
 
 [BrokerGroupSet.item_html]
 <tr>
-  <td><input type="checkbox"/></td>
   <td>{item_link}</td>
   <td>{item_config}</td>
   <td>{item_status}</td>

Modified: mgmt/cumin/python/cumin/exchange.py
===================================================================
--- mgmt/cumin/python/cumin/exchange.py	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/exchange.py	2007-12-10 01:46:31 UTC (rev 1445)
@@ -39,15 +39,22 @@
         self.unit = UnitSwitch(app, "unit")
         self.add_child(self.unit)
     
+        self.paginator = Paginator(app, "page")
+        self.add_child(self.paginator)
+
     def get_title(self, session, vhost):
         return "Exchanges %s" % fmt_count(len(vhost.exchanges))
 
+    def do_process(self, session, vhost):
+        self.paginator.set_count(session, len(vhost.exchanges))
+
     def render_unit_plural(self, session, vhost):
         return self.unit.get(session) == "b" and "Bytes" or "Msgs."
 
     def do_get_items(self, session, vhost):
         if vhost:
-            return sorted_by(vhost.exchanges)
+            start, end = self.paginator.get_bounds(session)
+            return vhost.exchanges[start:end]
 
     def render_item_link(self, session, exchange):
         branch = session.branch()

Modified: mgmt/cumin/python/cumin/exchange.strings
===================================================================
--- mgmt/cumin/python/cumin/exchange.strings	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/exchange.strings	2007-12-10 01:46:31 UTC (rev 1445)
@@ -12,6 +12,7 @@
 }
 
 [ExchangeSet.html]
+<div class="rfloat">{page}</div>
 {unit}
 
 <table class="mobjects">

Modified: mgmt/cumin/python/cumin/page.strings
===================================================================
--- mgmt/cumin/python/cumin/page.strings	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/page.strings	2007-12-10 01:46:31 UTC (rev 1445)
@@ -566,6 +566,10 @@
   color: #999;
 }
 
+.rfloat {
+  float: right;
+}
+
 [CuminPage.javascript]
 var cumin;
 

Modified: mgmt/cumin/python/cumin/queue.py
===================================================================
--- mgmt/cumin/python/cumin/queue.py	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/queue.py	2007-12-10 01:46:31 UTC (rev 1445)
@@ -21,9 +21,15 @@
         self.unit = UnitSwitch(app, "unit")
         self.add_child(self.unit)
 
+        self.paginator = Paginator(app, "page")
+        self.add_child(self.paginator)
+
     def get_title(self, session, vhost):
         return "Queues (%s)" % len(vhost.queues)
 
+    def do_process(self, session, vhost):
+        self.paginator.set_count(session, len(vhost.queues))
+
     def render_unit_singular(self, session, vhost):
         return self.unit.get(session) == "b" and "Byte" or "Msg."
 
@@ -32,7 +38,8 @@
 
     def do_get_items(self, session, vhost):
         if vhost:
-            return sorted_by(vhost.queues)
+            start, end = self.paginator.get_bounds(session)
+            return vhost.queues[start:end]
 
     def render_item_link(self, session, queue):
         branch = session.branch()
@@ -43,8 +50,6 @@
         return queue.name
 
     def render_item_consumers(self, session, queue):
-        return None # XXX
-        
         branch = session.branch()
         frame = self.page().show_queue(branch, queue)
         frame.show_view(branch).show_consumers(branch)
@@ -478,10 +483,10 @@
 
 class QueueConsumerSet(ItemSet):
     def get_title(self, session, queue):
-        return "Consumers" #XXX %s" % fmt_count(len(queue.consumers))
+        return "Consumers %s" % fmt_count(len(queue.consumers))
 
     def do_get_items(self, session, queue):
-        return list() #XXX sorted_by(queue.consumers)
+        return queue.consumers
 
     def render_item_name(self, session, consumer):
         return consumer.name

Modified: mgmt/cumin/python/cumin/queue.strings
===================================================================
--- mgmt/cumin/python/cumin/queue.strings	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/queue.strings	2007-12-10 01:46:31 UTC (rev 1445)
@@ -7,6 +7,7 @@
 
 [QueueSet.html]
 <form action="{href}" method="get">
+  <div class="rfloat">{page}</div>
   {unit}
 
   <div class="sactions">

Modified: mgmt/cumin/python/cumin/widgets.py
===================================================================
--- mgmt/cumin/python/cumin/widgets.py	2007-12-10 01:43:59 UTC (rev 1444)
+++ mgmt/cumin/python/cumin/widgets.py	2007-12-10 01:46:31 UTC (rev 1445)
@@ -273,8 +273,11 @@
         self.param.set_default(0)
         self.add_parameter(self.param)
 
-        self.page_size = 2
+        self.count = Attribute(app, "count")
+        self.add_attribute(self.count)
 
+        self.page_size = 15
+
     def get(self, session):
         return self.param.get(session)
 
@@ -285,8 +288,12 @@
         page = self.get(session)
         return (self.page_size * page, self.page_size * (page + 1))
 
+    def set_count(self, session, count):
+        self.count.set(session, count)
+
     def do_get_items(self, session, object):
-        return range(0, int(ceil(len(object) / float(self.page_size))))
+        count = self.count.get(session)
+        return range(0, int(ceil(count / float(self.page_size))))
 
     def render_item_class_attr(self, session, page):
         if self.get(session) == page:




More information about the rhmessaging-commits mailing list