<commit_before>from websocketserver.handlers.ErrorHandler import *
from py2neo import Graph


class GetGraphHandler(HandleInterface):
    """Class to handle sending whole graph to client."""
    _payload = ""
    _request = ""

    def __init__(self, request, payload):
        self._payload = payload
        self._request = request

    def handle(self, socket: WebSocketServerProtocol):
        nodes = "["
        edges = "["
        graph = {}
        jsonmsg = {}
        neo4j = Graph()

        graphid = self._payload
        if graphid == "":
            ErrorHandler("No graph specified", "").handle(socket)
            return

        # noinspection PyBroadException
        try:
            query = "START n=node(*) MATCH n WHERE n.graphid='" \
                    + graphid + "' RETURN n"
            for record in neo4j.cypher.execute(query):
                nodes += "{"
                for key in record[0].properties:
                    if key == "graphid":
                        continue
                    nodes += "\"" + key + "\":\"" + record[0].properties[key] \
                             + "\","
                nodes = nodes[:-1]
                nodes += "},"
            nodes = nodes[:-1]
            nodes += "]"
            query = "START n=node(*) MATCH (n {graphid:'" + graphid \
                    + "'})-[r{graphid:'" + graphid + "'}]->(m{graphid:'" \
                    + graphid + "'}) RETURN r"
            for record in neo4j.cypher.execute(query):
                edges += "{"
                for key in record[0].properties:
                    if key == "graphid":
                        continue
                    edges += "\"" + key + "\":\"" + record[0].properties[key] \
                             + "\","
                edges += "\"from\":\"" + \
                         record[0].start_node.properties["id"] + \
                         "\",\"to\":\"" + record[0].end_node.properties["id"] \
                         + "\"},"
            edges = edges[:-1]
            edges += "]"
        except Exception:
            print("Unable to connect to neo4j")
            ErrorHandler(self._request, "Unable to connect to neo4j", "").handle(socket)
        return

        if nodes == edges:
            ErrorHandler("Graph not found", graphid).handle(socket)
            return

        graph["nodes"] = json.loads(nodes)
        graph["edges"] = json.loads(edges)

        jsonmsg["message_id"] = "".join(
            random.choice(string.ascii_uppercase + string.digits) for _ in
            range(0, 15))
        jsonmsg["sender_id"] = "server"
        jsonmsg["time"] = int(time.time())
        jsonmsg["request"] = "response"
        jsonmsg["status"] = "success"
        jsonmsg["error"] = ""
        jsonmsg["payload"] = graph
        message = {}
        message["client_request_type"] = self._request
        jsonmsg["message"] = message

        socket.sendMessage(json.dumps(jsonmsg,
                                      separators=(',', ':')).encode('utf8'))<commit_msg>indent error in return caused getgraph to return nothing<commit_after>from websocketserver.handlers.ErrorHandler import *
from py2neo import Graph


class GetGraphHandler(HandleInterface):
    """Class to handle sending whole graph to client."""
    _payload = ""
    _request = ""

    def __init__(self, request, payload):
        self._payload = payload
        self._request = request

    def handle(self, socket: WebSocketServerProtocol):
        nodes = "["
        edges = "["
        graph = {}
        jsonmsg = {}
        neo4j = Graph()

        graphid = self._payload
        if graphid == "":
            ErrorHandler("No graph specified", "").handle(socket)
            return

        # noinspection PyBroadException
        try:
            query = "START n=node(*) MATCH n WHERE n.graphid='" \
                    + graphid + "' RETURN n"
            for record in neo4j.cypher.execute(query):
                nodes += "{"
                for key in record[0].properties:
                    if key == "graphid":
                        continue
                    nodes += "\"" + key + "\":\"" + record[0].properties[key] \
                             + "\","
                nodes = nodes[:-1]
                nodes += "},"
            nodes = nodes[:-1]
            nodes += "]"
            query = "START n=node(*) MATCH (n {graphid:'" + graphid \
                    + "'})-[r{graphid:'" + graphid + "'}]->(m{graphid:'" \
                    + graphid + "'}) RETURN r"
            for record in neo4j.cypher.execute(query):
                edges += "{"
                for key in record[0].properties:
                    if key == "graphid":
                        continue
                    edges += "\"" + key + "\":\"" + record[0].properties[key] \
                             + "\","
                edges += "\"from\":\"" + \
                         record[0].start_node.properties["id"] + \
                         "\",\"to\":\"" + record[0].end_node.properties["id"] \
                         + "\"},"
            edges = edges[:-1]
            edges += "]"
        except Exception:
            print("Unable to connect to neo4j")
            ErrorHandler(self._request, "Unable to connect to neo4j", "").handle(socket)
            return

        if nodes == edges:
            ErrorHandler(self._request, "Graph not found", graphid).handle(socket)
            return

        graph["nodes"] = json.loads(nodes)
        graph["edges"] = json.loads(edges)

        jsonmsg["message_id"] = "".join(
            random.choice(string.ascii_uppercase + string.digits) for _ in
            range(0, 15))
        jsonmsg["sender_id"] = "server"
        jsonmsg["time"] = int(time.time())
        jsonmsg["request"] = "response"
        jsonmsg["status"] = "success"
        jsonmsg["error"] = ""
        jsonmsg["payload"] = graph
        message = {}
        message["client_request_type"] = self._request
        jsonmsg["message"] = message

        socket.sendMessage(json.dumps(jsonmsg,
                                      separators=(',', ':')).encode('utf8'))
{'fraction_non_alphanumeric': 0.1474129515131793, 'fraction_numerical': 0.006182883176049463, 'mean_word_length': 1.4696665327440739, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7059586', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1600, 'n_words': 352}