From 41c8e89795675cd0989aee8e88647abd941c000c Mon Sep 17 00:00:00 2001 From: Josh Blum Date: Tue, 11 Jun 2013 00:32:47 -0700 Subject: flow: added query flat flow dot format --- Apology | 2 +- lib/top_block_query.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ query/__init__.py | 11 +++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Apology b/Apology index ee3ab69..ab6fc76 160000 --- a/Apology +++ b/Apology @@ -1 +1 @@ -Subproject commit ee3ab696bd5c93281ea0f15579389c23bcdee7b0 +Subproject commit ab6fc7615056dd5b6c737e1f4005250fe0281039 diff --git a/lib/top_block_query.cpp b/lib/top_block_query.cpp index e1ade67..5f22e21 100644 --- a/lib/top_block_query.cpp +++ b/lib/top_block_query.cpp @@ -3,6 +3,7 @@ #include "element_impl.hpp" #include "gras_impl/query_common.hpp" #include +#include #include #include #include @@ -199,6 +200,48 @@ static ptree query_props(ElementImpl *self, const ptree &query) return root; } +static std::string query_flows(ElementImpl *self, const ptree &query) +{ + std::string buff; + buff += "digraph flat_flows {\n"; + buff += "rankdir=LR;\n"; + buff += "node [shape=record];\n"; + + BOOST_FOREACH(Apology::Worker *w, self->executor->get_workers()) + { + BlockActor *actor = dynamic_cast(w->get_actor()); + std::string in_ports_str, out_ports_str; + for (size_t i = 0; i < w->get_num_inputs(); i++) + { + if (i) in_ports_str += " | "; + in_ports_str += str(boost::format(" %u") % i % i); + } + if (in_ports_str.size()) in_ports_str = "{" + in_ports_str + "} | "; + for (size_t i = 0; i < w->get_num_outputs(); i++) + { + if (i) out_ports_str += " | "; + out_ports_str += str(boost::format(" %u") % i % i); + } + if (out_ports_str.size()) out_ports_str = " | {" + out_ports_str + "}"; + buff += str(boost::format("%s [shape=record,label=\"{ %s %s %s }\"];\n") + % actor->GetAddress().AsString() % in_ports_str % actor->data->block->get_uid() % out_ports_str + ); + } + + BOOST_FOREACH(const Apology::Flow &flow, self->executor->get_flat_flows()) + { + buff += str(boost::format("%s:out%u -> %s:in%u;\n") + % dynamic_cast(flow.src.elem)->get_actor()->GetAddress().AsString() + % flow.src.index + % dynamic_cast(flow.dst.elem)->get_actor()->GetAddress().AsString() + % flow.dst.index + ); + } + + buff += "}\n"; + return buff; +} + std::string TopBlock::query(const std::string &args) { //convert json args into property tree @@ -207,6 +250,7 @@ std::string TopBlock::query(const std::string &args) //dispatch based on path arg std::string path = query.get("path"); ptree result; + if (path == "/flows.dot") return query_flows(this->get(), query); if (path == "/blocks.json") result = query_blocks(this->get(), query); if (path == "/stats.json") result = query_stats(this->get(), query); if (path == "/props.json") result = query_props(this->get(), query); diff --git a/query/__init__.py b/query/__init__.py index dc3339c..28c11a4 100644 --- a/query/__init__.py +++ b/query/__init__.py @@ -26,6 +26,17 @@ class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): args = server_registry[s.server] path = o.path + if path == "/flow.png": + s.send_response(200) + s.send_header("Content-type", "image/png") + s.end_headers() + dot = args['top_block'].query(json.dumps(dict(path='/flows.dot'))) + import subprocess + open("/tmp/dot.dot", 'w').write(dot) + subprocess.check_call(["dot", "-T", "png", "-o", "/tmp/dot.png", "/tmp/dot.dot"]) + s.wfile.write(open("/tmp/dot.png").read()) + return + #handle json requests if path.endswith('.json'): s.send_response(200) -- cgit