Skip to content

Commit

Permalink
Merge pull request #78 from feup-infolab/lazaroDevelop
Browse files Browse the repository at this point in the history
Lazaro develop
  • Loading branch information
lazarocosta committed May 27, 2020
2 parents a7a341c + b2454ba commit 513984a
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 77 deletions.
14 changes: 13 additions & 1 deletion conf/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ nvm use v10
echo "Starting archgraph server at $ROOT_DIR"
cd "$ROOT_DIR"

python "$ROOT_DIR/src/Routes/routes.py" --neo4j="$NEO4J_CONNECTION_STRING" --mongodb="$MONGODB_CONNECTION_STRING" &
if [[ "$NEO4J_CONNECTION_STRING" != "" ]]; then
echo "Neo4j Server at $NEO4J_CONNECTION_STRING";
fi

if [[ "$MONGODB_CONNECTION_STRING" != "" ]]; then
echo "MongoDB Server at $MONGODB_CONNECTION_STRING";
fi

if [[ "$CUSTOM_HOST_FOR_SERVER_BIND" != "" ]]; then
echo "Flask Server binding to host with address $CUSTOM_HOST_FOR_SERVER_BIND";
fi

python "$ROOT_DIR/src/Routes/routes.py" --neo4j="$NEO4J_CONNECTION_STRING" --mongodb="$MONGODB_CONNECTION_STRING" --host="$CUSTOM_HOST_FOR_SERVER_BIND" &
SERVER_PID=$!
cd "$ROOT_DIR/frontend" || ( echo "folder missing " && exit 1 )
if [[ "$RUN_IN_PRODUCTION" != "" ]] ; then
Expand Down
8 changes: 5 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ services:
mongodb:
container_name: archgraph-mongodb
image: mongo:latest
command: ["--bind_ip_all"]
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: rootpassword
Expand Down Expand Up @@ -32,11 +33,12 @@ services:
dockerfile: Dockerfile
args:
SOURCE_BRANCH: master
PRELOAD_GRAPH: "true"
PRELOAD_GRAPH: "1"
context: .
environment:
- "NEO4J_CONNECTION_STRING=bolt://neo4j:password@neo4j:7687"
- "MONGODB_CONNECTION_STRING=mongodb://root:rootpassword@mongodb:27017"
- "NEO4J_CONNECTION_STRING=bolt://neo4j:password@archgraph-neo4j:7687"
- "MONGODB_CONNECTION_STRING=mongodb://root:rootpassword@archgraph-mongodb:27017"
- "CUSTOM_HOST_FOR_SERVER_BIND=0.0.0.0"
- "RUN_IN_PRODUCTION=1"
ports:
- published: 4200
Expand Down
5 changes: 2 additions & 3 deletions src/Models/CRM/v5_0_2/NodeEntities/E1_CRM_Entity.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from marshmallow import Schema, fields
from neomodel import (
One,
RelationshipTo,
StringProperty,
StructuredNode,
UniqueIdProperty,
db, ZeroOrOne,
ZeroOrOne,
)
from src.GCF.decorators.OntologyClass import ontology_class, decorator_schema
from src.Models.CRM.v5_0_2.NodeProperties.has_value import has_value
Expand All @@ -32,7 +31,7 @@ class E1_CRM_EntitySchema(Schema):
P137_exemplifies = fields.List(fields.Nested("src.Models.CRM.v5_0_2.NodeEntities.E55_Type.E55_TypeSchema"))
P48_has_preferred_identifier = fields.Nested("src.Models.CRM.v5_0_2.NodeEntities.E42_Identifier.E42_IdentifierSchema")
P139_has_alternative_form = fields.List(fields.Nested("src.Models.CRM.v5_0_2.NodeEntities.E41_Appellation.E41_AppellationSchema"))
P1_is_identified_by = fields.Nested("src.Models.CRM.v5_0_2.NodeEntities.E55_Type.E55_TypeSchema")
P1_is_identified_by = fields.Nested("src.Models.CRM.v5_0_2.NodeEntities.E41_Appellation.E41_AppellationSchema")
has_value = fields.List(
fields.Nested("src.Models.DataObject.v0_0_2.DataObject.DataObjectSchema")
)
Expand Down
2 changes: 1 addition & 1 deletion src/Models/DataObject/v0_0_2/RegexString.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, schema=None, *args, **kwargs):

expression = ""
if kwargs.get("expression") is None:
# TODO falta arranjar isto
# todo falta arranjar isto
expression = "regex/[a-zA-Z]([a-zA-Z 0-9])*$"
else:
expression = json.dumps(kwargs.get("expression"))
Expand Down
53 changes: 24 additions & 29 deletions src/Models/DataObject/v0_0_2/SuperClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ class SuperClass:
def __init__(self, schema):
self.schema = schema

"""The function returns the node schema"""
def getSchema(self):
json_schema = JSONSchema()
return json_schema.dump(self.schema)

# Json to string
"""The function returns the node information in string"""
def encodeJSON(self):
data = {}
for key, val in self.__properties__.items():
Expand All @@ -24,18 +25,21 @@ def my_converter(o):

return json.dumps(data, default=my_converter)

# string to json
"""The function returns the node information at format json"""
def decodeJSON(self):
return json.loads(self.encodeJSON())

"""The function returns the property of entity with name property_name"""
def get_property_from_entity(self, property_name):
schema_node = self.getSchema()
class_name = self.__class__.__name__ + "Schema"
return schema_node['definitions'][class_name]['properties'][property_name]

"""The function returns its superclasses names"""
def get_superclasses_name(self):
return list(set(self.labels()))

"""The function returns the schema according to the template provided"""
def get_schema_with_template(self, template):
jsonSchema = self.getSchema()
newJsonSchema = {
Expand All @@ -46,6 +50,7 @@ def get_schema_with_template(self, template):
self.__get_schema_with_template_aux(jsonSchema["definitions"], template, newJsonSchema)
return newJsonSchema

"""Auxiliary function - The function returns the schema according to the template provided"""
def __get_schema_with_template_aux(self, definitions, json_template, new_json_schema):
if isinstance(json_template, str):
entity_name = json_template
Expand All @@ -69,7 +74,7 @@ def __get_schema_with_template_aux(self, definitions, json_template, new_json_sc

properties = definitions[current_entity]['properties']

self.__get_entity_properties_without_ref(definitions[current_entity], entity)
self.__set_entity_properties_without_ref(definitions[current_entity], entity)

if isinstance(json_template, str):
return
Expand All @@ -81,7 +86,7 @@ def __get_schema_with_template_aux(self, definitions, json_template, new_json_sc

self.__get_schema_with_template_aux(definitions, next_entity, new_json_schema)

def __get_entity_properties_without_ref(self, current_entity, entity):
def __set_entity_properties_without_ref(self, current_entity, entity):
for property_name in current_entity['properties']:
property_entity = current_entity['properties'][property_name]

Expand All @@ -97,6 +102,7 @@ def __get_entity_properties_without_ref(self, current_entity, entity):
# 'type': 'string'}
# changed_property['title'] = property_entity['title']

"""The function merges between node and customer data, then saves the node"""
def merge_node(self, updated_node):
merged_node = dict(self.decodeJSON(), **updated_node)
field_type_date = self.__get_field_of_type_date()
Expand All @@ -111,30 +117,26 @@ def merge_node(self, updated_node):
self.save()
except BaseException:
return None

# array_uid = read_relationships(node_name, relationship_name)
#
# for uid in array_uid:
# node = get_node_by_uid(uid)

return True

def node_self_build(self, updated_node):
object = {'self_node': {},
"""The function returns the node with updated fields and relationships"""
def build_node(self, data):
node = {'self_node': {},
'relationships': {}}
for property in updated_node.keys():
if isinstance(updated_node[property], str):
object['self_node'][property] = updated_node[property]
elif isinstance(updated_node[property], dict):
object['relationships'][property] = []
object['relationships'][property].append(updated_node[property])
elif isinstance(updated_node[property], list):
for property in data.keys():
if isinstance(data[property], str):
node['self_node'][property] = data[property]
elif isinstance(data[property], dict):
node['relationships'][property] = []
node['relationships'][property].append(data[property])
elif isinstance(data[property], list):
relationships = []
for element in updated_node[property]:
for element in data[property]:
relationships.append(element)
object['relationships'][property] = relationships
return object
node['relationships'][property] = relationships
return node

"""The function returns entity's fields of type date"""
def __get_field_of_type_date(self):
result = []
get_schema = self.getSchema()
Expand All @@ -146,10 +148,3 @@ def __get_field_of_type_date(self):
result.append(field["title"])
return result

# def get_labels(target, base_class):
# return (
# c.__name__
# for c in target.__mro__
# if issubclass(c, base_class)
# )

22 changes: 13 additions & 9 deletions src/Routes/mongo.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import json
from datetime import datetime
from pymongo import MongoClient
import argparse
from pymongo import MongoClient, uri_parser

parser = argparse.ArgumentParser(description="Starts the archgraph server.")
parser.add_argument("--mongodb", nargs="?", help="Address of the mongodb server")
parser.add_argument("--neo4j", nargs="?", help="Address of the neo4j server")

args = parser.parse_args()
import src.Utils.ArgParser as ArgParser
args = ArgParser.parse()

if args.mongodb is not None:
MONGODB_URL = "mongodb://root:rootpassword@localhost:27017"

if args.mongodb is not None and args.mongodb != "":
# mongodb_arguments = uri_parser.parse_uri(args.mongodb)
# client = MongoClient(host=mongodb_arguments.nodelist.first(),
# port= mongodb_arguments.port,
# username=mongodb_arguments.username,
# password=mongodb_arguments.password,
# server_selection_timeout=1
# )
MONGODB_URL = args.mongodb
else:
MONGODB_URL = "mongodb://root:rootpassword@localhost:27017"

client = MongoClient(MONGODB_URL)
db = client.mydatabase
Expand Down
33 changes: 23 additions & 10 deletions src/Routes/routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
from pathlib import Path
import os, sys
import argparse


# returns the project root path (assumes that the script is started from src/Routes/routes.py)
Expand All @@ -15,10 +14,6 @@ def get_project_root():
print("Archgraph running at " + get_project_root().as_posix())
sys.path.append(get_project_root().as_posix())

parser = argparse.ArgumentParser(description="Starts the archgraph server.")
parser.add_argument("--neo4j", nargs="?", help="Address of the neo4j server")
parser.add_argument("--mongodb", nargs="?", help="Address of the mongodb server")
args = parser.parse_args()

from flask import Flask, Response, jsonify, make_response, request, send_from_directory

Expand All @@ -28,20 +23,35 @@ def get_project_root():
from src.Utils.JsonEncoder import search_cidoc, search_specific_cidoc
from src.Utils.Utils import get_node_by_uid, build_next_json, updated_node, make_result

if args.neo4j:
import src.Utils.ArgParser as ArgParser
args = ArgParser.parse()

if args.neo4j and args.neo4j != "":
config.DATABASE_URL = args.neo4j
else:
config.DATABASE_URL = "bolt://neo4j:password@localhost:7687"

from src.Routes.mongo import insert_template_in_mongo, get_all_records_from_collection, get_schema_from_mongo, \
get_templates_from_mongo_by_classes_name
from src.Routes.mongo import insert_template_in_mongo, get_all_records_from_collection, get_schema_from_mongo, get_templates_from_mongo_by_classes_name

app = Flask(__name__)

CORS(app)
cors = CORS(app, resources={r"/*": {"origins": "*"}})
app = Flask(__name__, static_url_path="")
app.debug = True

@app.before_request
def log_request_info():
app.logger.debug('Headers: %s', request.headers)
app.logger.debug('Body: %s', request.get_data())

@app.after_request
def after(response):
# todo with response
print(response.status)
print(response.headers)
print(response.get_data())
return response

@app.route("/favicon.ico")
def favicon():
Expand Down Expand Up @@ -234,6 +244,9 @@ def search_specific(class_name, query):
# json = read_file("../Utils/defaultTemplates.json")
# populate_template_collection(json)
# get_all_records_from_collection("createdTemplate")

if __name__ == "__main__":
app.run(host='127.0.0.1')
if args.host is not None and args.host != "":
app.logger.debug('Archgraph running with a custom host setting: %s', args.host)
app.run(host=args.host)
else:
app.run(host='127.0.0.1')
16 changes: 16 additions & 0 deletions src/Utils/ArgParser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import argparse


def parse():
parser = argparse.ArgumentParser(description="Starts the archgraph server.")
parser.add_argument("--mongodb", nargs="?", help="Address of the mongodb server")
parser.add_argument("--neo4j", nargs="?", help="Address of the neo4j server")
parser.add_argument("--host", nargs="?", help="Address of the neo4j server")

args = parser.parse_args()

return args


class ArgParser:
pass
Loading

0 comments on commit 513984a

Please sign in to comment.