From abba44f40e183c4b6f4194b158432479fbb1a190 Mon Sep 17 00:00:00 2001 From: Sandy Walsh Date: Tue, 27 Jan 2015 10:49:01 -0800 Subject: [PATCH] Adds num-streams command Which returns the count of the number of streams matching filter criteria. Change-Id: I25799365f1185cbb9ad4f277d8e5809887fd8295 --- klugman/v1.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++-- klugman/v2.py | 2 ++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/klugman/v1.py b/klugman/v1.py index 6ef5b3a..5fb1661 100644 --- a/klugman/v1.py +++ b/klugman/v1.py @@ -15,6 +15,7 @@ import base +import json import jsonutil from docopt import docopt @@ -54,13 +55,17 @@ class Streams(object): def cmdline(self, version, cmdline): arguments = docopt(Streams.__doc__, argv=cmdline) - if version.base_args['--debug']: + debug = version.base_args['--debug'] + if debug: print arguments response = self.do_streams(version, arguments) # Handle cmdline output here, not in do_foo() raw_rows = response.json(object_hook=jsonutil.object_hook) + if debug: + print json.dumps(raw_rows, indent=2) + # TODO(sandy): This should come from the server-issued # schema at some point. keys = ['id', 'state', 'name', 'first_event', 'last_event', @@ -92,13 +97,72 @@ class Streams(object): return base.get(version.base_url, cmd, params) +class NumStreams(object): + """usage: + klugman.py num-streams [options] + + options: + --state + return streams in state + --older_than + list streams older than datetime + --younger_than + list streams younger than datetime + --trigger_name + list streams with given trigger definition + --distinguishing_traits + list stream with specific distriquishing traits + + Stream states: + collecting - collecting events + ready - ready for processing + triggered - being processed + processed - processing completed + error - pipeline processing failed + commit_error - pipeline result commit failed + + Distinguishing trait format: + "trait:value;trait:value;..." + """ + + def cmdline(self, version, cmdline): + arguments = docopt(NumStreams.__doc__, argv=cmdline) + debug = version.base_args['--debug'] + if debug: + print arguments + + response = self.do_stream_count(version, arguments) + raw_rows = response.json(object_hook=jsonutil.object_hook) + + keys = ['count'] + base.dump_response(keys, raw_rows) + + def do_stream_count(self, version, arguments): + state = arguments.get('--state') + older = arguments.get('--older_than') + younger = arguments.get('--younger_than') + trigger = arguments.get('--trigger_name') + traits = arguments.get('--distinguishing_traits') + + cmd = "streams/count" + params = base.remove_empty({'state': state, + 'older_than': older, + 'younger_than': younger, + 'trigger_name': trigger, + 'distinguishing_traits': traits}) + + return base.get(version.base_url, cmd, params) + + class V1(base.Impl): """usage: klugman.py streams [...] [options] + klugman.py num-streams [...] [options] -h, --help show command options """ def __init__(self, base_url, base_args): - cmds = {'streams': Streams()} + cmds = {'streams': Streams(), + 'num-streams': NumStreams()} super(V1, self).__init__(base_url, base_args, cmds, V1.__doc__) diff --git a/klugman/v2.py b/klugman/v2.py index fdcc117..95416a3 100644 --- a/klugman/v2.py +++ b/klugman/v2.py @@ -64,6 +64,7 @@ class V2(base.Impl): Usage: klugman.py [options] streams [...] + klugman.py num-streams [...] [options] klugman.py [options] archives [...] Options: @@ -72,5 +73,6 @@ Options: def __init__(self, base_url, base_args): cmds = {'streams': v1.Streams(), + 'num-streams': v1.NumStreams(), 'archives': Archives()} super(V2, self).__init__(base_url, base_args, cmds, V2.__doc__)