Beispiel #1
0
	def test_output_query_as_json(self):
		file = StringIO()
		cypher.execute_and_output_as_json("start n=node(0) return n", self.graph_db, out=file)
		self.assertEqual("""\
[
\t{"n": "(0)"}
]
""", file.getvalue())
def get_node_complexquery(qtype, qval):
    """
  Run one of the pre-configured Cypher queries against the neo4j database.

  See the complexqueries.cfg file for all the pre-configured queries. Most of
  the pre-configured queries are limited to just a query but there is also an
  option to add a post-query block of code that will further transform the
  query results. Here is an example that takes the given post value and looks 
  up posts that are tagged the same then transforms the results so that they
  are sorted by tag count and limited to the first 20 tag sets:

    [similarly.tagged]
    query: start startQuestion=node:Posts(postId="{val}")
           match startQuestion -[:tagged]-> (tag) <-[:tagged]- (endQuestion)
           return endQuestion.postId as questionId, collect(tag.tagName) as tags 
           order by questionId
    exec:result = map(lambda r: [r[0], r[1].replace('[', '').replace(']', '').replace(', ', ',').split(',')], result[0])
         result.sort(lambda x,y: cmp(len(x[1]), len(y[1])))
         result.reverse()
         del result[20:]
         result = map(lambda r: {'questionId':r[0], 'tags':r[1]}, result)
         result = json.dumps(result)

  The result of an exec code block is expected to be in JSON format.

  neo4j note: This code is an example of executing a Cypher query.

  @param qtype:       the type of query being made, must match an entry in 
                      the complexqueries.cfg file
  @param qval:        the value to introduce into the query
  """

    start_ts = time.time()
    raw_query = config.get(qtype, "query")
    if raw_query is not None:
        query = raw_query.format(val=util.simple_sanitize(qval))

        result = None
        if config.has_option(qtype, "exec"):
            result = cypher.execute(query, graph_db)
            exec(config.get(qtype, "exec"))
        else:
            temp = StringIO.StringIO()
            cypher.execute_and_output_as_json(query, graph_db, temp)
            result = temp.getvalue()
            temp.close()

        resp = make_response(
            '{{"value":"{value}", "result":{result}, "exec_time":{exec_time}}}'
            .format(value=qval,
                    result=result,
                    exec_time=(time.time() - start_ts)))
        resp.headers['Content-type'] = 'application/json'
        return resp
    else:
        abort(404)
Beispiel #3
0
    def test_output_query_as_json(self):
        file = StringIO()
        cypher.execute_and_output_as_json("start n=node(0) return n",
                                          self.graph_db,
                                          out=file)
        self.assertEqual("""\
[
\t{"n": "(0)"}
]
""", file.getvalue())
def get_node_complexquery(qtype, qval):
  """
  Run one of the pre-configured Cypher queries against the neo4j database.

  See the complexqueries.cfg file for all the pre-configured queries. Most of
  the pre-configured queries are limited to just a query but there is also an
  option to add a post-query block of code that will further transform the
  query results. Here is an example that takes the given post value and looks 
  up posts that are tagged the same then transforms the results so that they
  are sorted by tag count and limited to the first 20 tag sets:

    [similarly.tagged]
    query: start startQuestion=node:Posts(postId="{val}")
           match startQuestion -[:tagged]-> (tag) <-[:tagged]- (endQuestion)
           return endQuestion.postId as questionId, collect(tag.tagName) as tags 
           order by questionId
    exec:result = map(lambda r: [r[0], r[1].replace('[', '').replace(']', '').replace(', ', ',').split(',')], result[0])
         result.sort(lambda x,y: cmp(len(x[1]), len(y[1])))
         result.reverse()
         del result[20:]
         result = map(lambda r: {'questionId':r[0], 'tags':r[1]}, result)
         result = json.dumps(result)

  The result of an exec code block is expected to be in JSON format.

  neo4j note: This code is an example of executing a Cypher query.

  @param qtype:       the type of query being made, must match an entry in 
                      the complexqueries.cfg file
  @param qval:        the value to introduce into the query
  """

  start_ts = time.time()
  raw_query = config.get(qtype, "query")
  if raw_query is not None:
    query = raw_query.format(val=util.simple_sanitize(qval))

    result = None
    if config.has_option(qtype, "exec"):
      result = cypher.execute(query, graph_db)
      exec(config.get(qtype, "exec"))
    else:
      temp = StringIO.StringIO()
      cypher.execute_and_output_as_json(query, graph_db, temp)
      result = temp.getvalue()
      temp.close()

    resp = make_response('{{"value":"{value}", "result":{result}, "exec_time":{exec_time}}}'.format(value=qval, result=result, exec_time=(time.time()-start_ts)))
    resp.headers['Content-type'] = 'application/json'
    return resp
  else:
    abort(404)