diff --git a/api/graph.py b/api/graph.py index 1507555eba3bdaf1f3842b791a850afee8c73cdf..378057cdc31ff5fe9f76acf07be6815f441b2620 100644 --- a/api/graph.py +++ b/api/graph.py @@ -1,6 +1,8 @@ from neo4j import GraphDatabase import time +from api.graphql.graphql import Discussion + URL = 'jdbc:postgresql://db/discussion?user=postgres&password=FooBar' @@ -158,3 +160,14 @@ class Graph(object): "DETACH DELETE a" ) return result + + @staticmethod + def _load_discussion(tx, slug): + result = tx.run( + Discussion( + protocol='https', + host='dbas.cs.uni-duesseldorf.de', + port=443 + ).to_cypher(slug) + ) + return result diff --git a/api/graphql/__init__.py b/api/graphql/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/api/graphql/graphql.py b/api/graphql/graphql.py new file mode 100644 index 0000000000000000000000000000000000000000..266c7aa27cfb7bf0bef3b30df8e70098f3fd4f8e --- /dev/null +++ b/api/graphql/graphql.py @@ -0,0 +1,72 @@ +import json +import logging +import time + +import requests +from neo4j import GraphDatabase + + +class GraphQl(object): + + def __init__(self, protocol, host, port): + self.protocol = protocol + self.host = host + self.port = port + + @staticmethod + def _json_to_dict(col): + if isinstance(col, dict): + return col + elif isinstance(col, bytes): + col = col.decode("utf-8") + return json.loads(col) + + def request(self, query): + url = "{}://{}:{}/api/v2/query?q={}".format(self.protocol, self.host, self.port, query) + try: + response = requests.get(url) + except requests.exceptions.ConnectionError: + logging.error("Connection Error") + return {} + + ret = self._json_to_dict(response.content) + return ret + + +class Discussion(GraphQl): + + def __init__(self, protocol, host, port, uri, user, password): + super().__init__(protocol, host, port) + self._driver = GraphDatabase.driver(uri, auth=(user, password)) + + def close(self): + self._driver.close() + + @staticmethod + def _cypher_query(slug): + return """ + query{{ + issue(slug: "{0}"){{ + completeGraphCypher + }} + }} + """.format(slug) + + def to_cypher(self, slug): + result = super().request(query=self._cypher_query(slug)) + return result['issue']['completeGraphCypher'] + + @staticmethod + def _create_discussion(tx, query): + result = tx.run( + query + ) + return result + + def inject_to_neo(self, slug): + t1 = time.time() + query = self.to_cypher(slug) + + with self._driver.session() as session: + session.write_transaction(self._create_discussion, query) + return {"total": time.time() - t1} diff --git a/api/requirements.txt b/api/requirements.txt index 5c69b29e609207171ec4c881a7ebcff838ecd240..2ad5b7c5c3ee968925fe94a32f28f597db0d189e 100644 --- a/api/requirements.txt +++ b/api/requirements.txt @@ -1,3 +1,4 @@ Flask==0.10.1 flask-cors==3.0.7 -neo4j==1.7.1 \ No newline at end of file +neo4j==1.7.1 +requests==2.18.4 \ No newline at end of file diff --git a/api/server.py b/api/server.py index 6d5ede2433851d573d6e0b28074939638aacfa2b..14b271e62c7f95fc1a26f05704e44e06809cf0b6 100644 --- a/api/server.py +++ b/api/server.py @@ -2,20 +2,32 @@ from flask import Flask, render_template, jsonify from flask_cors import CORS from api.graph import Graph +from api.graphql.graphql import Discussion app = Flask(__name__, template_folder='.') CORS(app) @app.route('/<file>') -def root(file): +def load_html(file): return render_template('html/' + file) @app.route('/load') -def test(): +def load_graph(): return jsonify(Graph(uri='bolt://neo:7687', user='neo4j', password='neo4j').load()) +@app.route('/load/<slug>') +def load_discussion(slug): + result = Discussion(protocol='https', + host='dbas.cs.uni-duesseldorf.de', + port='443', + uri='bolt://neo:7687', + user='neo4j', + password='neo4j').inject_to_neo(slug) + return jsonify(result) + + if __name__ == '__main__': app.run(debug=True, host="0.0.0.0", port=5000)