summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/boost_fail/json_parser_read_104900.hpp (renamed from lib/json_parser_read_104900.hpp)0
-rw-r--r--lib/json_parser.cpp2
-rw-r--r--lib/top_block_query.cpp59
-rw-r--r--python/gras/GRAS_HierBlock.i8
-rw-r--r--tests/query_test.py32
5 files changed, 58 insertions, 43 deletions
diff --git a/lib/json_parser_read_104900.hpp b/lib/boost_fail/json_parser_read_104900.hpp
index f7c6515..f7c6515 100644
--- a/lib/json_parser_read_104900.hpp
+++ b/lib/boost_fail/json_parser_read_104900.hpp
diff --git a/lib/json_parser.cpp b/lib/json_parser.cpp
index 4a75f3d..d66297f 100644
--- a/lib/json_parser.cpp
+++ b/lib/json_parser.cpp
@@ -5,7 +5,7 @@
//why the fuck does no OS ever patch boost when there is a bug
//https://svn.boost.org/trac/boost/ticket/6785
#if BOOST_VERSION == 104900
-#include "json_parser_read_104900.hpp"
+#include "boost_fail/json_parser_read_104900.hpp"
#endif
//--------- end bullshit --------------//
diff --git a/lib/top_block_query.cpp b/lib/top_block_query.cpp
index 671168c..9d8afbe 100644
--- a/lib/top_block_query.cpp
+++ b/lib/top_block_query.cpp
@@ -9,6 +9,8 @@
using namespace gras;
+using namespace boost::property_tree;
+
ThreadPool get_active_thread_pool(void);
struct GetStatsReceiver : Theron::Receiver
@@ -26,31 +28,31 @@ struct GetStatsReceiver : Theron::Receiver
std::vector<GetStatsMessage> messages;
};
-static boost::property_tree::ptree query_blocks(ElementImpl *self, const boost::property_tree::ptree &)
+static ptree query_blocks(ElementImpl *self, const ptree &)
{
- boost::property_tree::ptree root;
- boost::property_tree::ptree e;
+ ptree root;
+ ptree e;
BOOST_FOREACH(Apology::Worker *worker, self->executor->get_workers())
{
BlockActor *block = dynamic_cast<BlockActor *>(worker);
- boost::property_tree::ptree prop_e;
+ ptree prop_e;
typedef std::pair<std::string, PropertyRegistryPair> PropRegistryKVP;
BOOST_FOREACH(const PropRegistryKVP &p, block->property_registry)
{
- boost::property_tree::ptree prop_attrs;
+ ptree prop_attrs;
if (p.second.setter)
{
- boost::property_tree::ptree type;
+ ptree type;
type.put_value(p.second.setter->type().name());
prop_attrs.push_back(std::make_pair("setter", type));
}
if (p.second.getter)
{
- boost::property_tree::ptree type;
+ ptree type;
type.put_value(p.second.getter->type().name());
prop_attrs.push_back(std::make_pair("getter", type));
}
- boost::property_tree::ptree block_attrs;
+ ptree block_attrs;
block_attrs.push_back(std::make_pair(p.first, prop_attrs));
prop_e.push_back(std::make_pair("props", block_attrs));
}
@@ -60,14 +62,13 @@ static boost::property_tree::ptree query_blocks(ElementImpl *self, const boost::
return root;
}
-static boost::property_tree::ptree query_stats(ElementImpl *self, const boost::property_tree::ptree &query)
+static ptree query_stats(ElementImpl *self, const ptree &query)
{
-
//parse list of block ids needed in this query
std::vector<std::string> block_ids;
if (query.count("blocks") != 0)
{
- BOOST_FOREACH(const boost::property_tree::ptree::value_type &v, query.get_child("blocks"))
+ BOOST_FOREACH(const ptree::value_type &v, query.get_child("blocks"))
{
block_ids.push_back(v.second.get<std::string>(""));
}
@@ -91,7 +92,7 @@ static boost::property_tree::ptree query_stats(ElementImpl *self, const boost::p
while (outstandingCount) outstandingCount -= receiver.Wait(outstandingCount);
//create root level node
- boost::property_tree::ptree root;
+ ptree root;
root.put("now", time_now());
root.put("tps", time_tps());
@@ -115,11 +116,11 @@ static boost::property_tree::ptree query_stats(ElementImpl *self, const boost::p
}
//iterate through blocks
- boost::property_tree::ptree blocks;
+ ptree blocks;
BOOST_FOREACH(const GetStatsMessage &message, receiver.messages)
{
const BlockStats &stats = message.stats;
- boost::property_tree::ptree block;
+ ptree block;
block.put("tps", time_tps());
block.put("stats_time", message.stats_time);
block.put("init_time", stats.init_time);
@@ -134,9 +135,9 @@ static boost::property_tree::ptree query_stats(ElementImpl *self, const boost::p
block.put("total_time_output", stats.total_time_output);
block.put("actor_queue_depth", stats.actor_queue_depth);
#define my_block_ptree_append(l) { \
- boost::property_tree::ptree e; \
+ ptree e; \
for (size_t i = 0; i < stats.l.size(); i++) { \
- boost::property_tree::ptree t; t.put_value(stats.l[i]); \
+ ptree t; t.put_value(stats.l[i]); \
e.push_back(std::make_pair("", t)); \
} \
block.push_back(std::make_pair(#l, e)); \
@@ -159,18 +160,30 @@ static boost::property_tree::ptree query_stats(ElementImpl *self, const boost::p
return root;
}
-boost::property_tree::ptree json_to_ptree(const std::string &s);
-std::string ptree_to_json(const boost::property_tree::ptree &p);
+static ptree query_props(ElementImpl *self, const ptree &query)
+{
+ ptree root;
+ const std::string block_id = query.get<std::string>("block");
+ const std::string prop_key = query.get<std::string>("key");
+ const std::string action = query.get<std::string>("action");
+ const std::string value = query.get<std::string>("value");
+ //TODO :-)
+ return root;
+}
+
+ptree json_to_ptree(const std::string &s);
+std::string ptree_to_json(const ptree &p);
std::string TopBlock::query(const std::string &args)
{
//convert json args into property tree
- const boost::property_tree::ptree query_args_pt = json_to_ptree(args);
+ const ptree query = json_to_ptree(args);
//dispatch based on path arg
- std::string path = query_args_pt.get<std::string>("path");
- boost::property_tree::ptree result;
- if (path == "/blocks.json") result = query_blocks(this->get(), query_args_pt);
- if (path == "/stats.json") result = query_stats(this->get(), query_args_pt);
+ std::string path = query.get<std::string>("path");
+ ptree result;
+ 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);
return ptree_to_json(result);
}
diff --git a/python/gras/GRAS_HierBlock.i b/python/gras/GRAS_HierBlock.i
index 65eb8d1..563dadd 100644
--- a/python/gras/GRAS_HierBlock.i
+++ b/python/gras/GRAS_HierBlock.i
@@ -174,6 +174,14 @@ class TopBlock(TopBlockPython):
def disconnect(self, *args):
return internal_connect__(TopBlockPython.disconnect, self, *args)
+ def query(self, args):
+ if isinstance(args, str): return TopBlockPython.query(self, args)
+ else:
+ import json
+ query_str = json.dumps(args)
+ result_str = TopBlockPython.query(self, query_str)
+ return json.loads(result_str)
+
class HierBlock(HierBlockPython):
def __init__(self, name="Hier"):
HierBlockPython.__init__(self, name)
diff --git a/tests/query_test.py b/tests/query_test.py
index 1874eb2..ec8fade 100644
--- a/tests/query_test.py
+++ b/tests/query_test.py
@@ -4,7 +4,6 @@ import unittest
import gras
import numpy
from demo_blocks import *
-import json
class MyBlock(gras.Block):
def __init__(self):
@@ -45,25 +44,22 @@ class QueryTest(unittest.TestCase):
self.assertEqual(vec_sink.get_vector(), (0, 9, 8, 7, 6))
#query the block list
- blocks_json = self.tb.query('{"path": "/blocks.json"}')
- print blocks_json
- blocks_python = json.loads(blocks_json)
- print blocks_python
- self.assertEqual(len(blocks_python['blocks']), 2)
+ blocks_result = self.tb.query(dict(path="/blocks.json"))
+ self.assertEqual(len(blocks_result['blocks']), 2)
#pick a block to query below:
- block_id = blocks_python['blocks'].keys()[0]
+ block_id = blocks_result['blocks'].keys()[0]
#query the stats
- stats_json = self.tb.query(str('{"path": "/stats.json", "blocks": ["%s"]}'%block_id))
- print stats_json
- stats_python = json.loads(stats_json)
- print stats_python
- self.assertTrue('tps' in stats_python)
- self.assertTrue('now' in stats_python)
+ stats_result = self.tb.query(dict(
+ path="/stats.json",
+ blocks=[block_id],
+ ))
+ self.assertTrue('tps' in stats_result)
+ self.assertTrue('now' in stats_result)
#found the block we asked for
- self.assertTrue(block_id in stats_python['blocks'])
+ self.assertTrue(block_id in stats_result['blocks'])
def test_props(self):
vec_source = VectorSource(numpy.uint32, [0, 9, 8, 7, 6])
@@ -72,11 +68,9 @@ class QueryTest(unittest.TestCase):
self.tb.connect(vec_source, block, vec_sink)
self.tb.run()
- blocks_json = self.tb.query('{"path": "/blocks.json"}')
- print blocks_json
- blocks_python = json.loads(blocks_json)
- print blocks_python
- self.assertEqual(len(blocks_python['blocks']), 3)
+ #query the block list
+ blocks_result = self.tb.query(dict(path="/blocks.json"))
+ self.assertEqual(len(blocks_result['blocks']), 3)
if __name__ == '__main__':
unittest.main()