Skip to content

Commit

Permalink
Merge pull request #69 from feup-infolab/lazaroDevelop
Browse files Browse the repository at this point in the history
Lazaro develop
  • Loading branch information
lazarocosta committed May 1, 2020
2 parents 279bddb + 8501175 commit 0b7b22b
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 164 deletions.
46 changes: 23 additions & 23 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@ services:
networks:
- archgraph-docker-deployment
restart: on-failure
archgraph:
container_name: archgraph
image: feupinfolab/archgraph:latest
build:
dockerfile: Dockerfile
args:
- "SOURCE_BRANCH=master"
- "PRELOAD_GRAPH=1"
context: .
environment:
- "NEO4J_CONNECTION_STRING=bolt://neo4j:password@neo4j:7687"
- "RUN_IN_PRODUCTION=1"
ports:
- published: 4200
target: 4200
- published: 5000
target: 5000
depends_on:
- mongodb
- neo4j
networks:
- archgraph-docker-deployment
restart: on-failure
# archgraph:
# container_name: archgraph
# image: feupinfolab/archgraph:latest
# build:
# dockerfile: Dockerfile
# args:
# - "SOURCE_BRANCH=master"
# - "PRELOAD_GRAPH=1"
# context: .
# environment:
# - "NEO4J_CONNECTION_STRING=bolt://neo4j:password@neo4j:7687"
# - "RUN_IN_PRODUCTION=1"
# ports:
# - published: 4200
# target: 4200
# - published: 5000
# target: 5000
# depends_on:
# - mongodb
# - neo4j
# networks:
# - archgraph-docker-deployment
# restart: on-failure
networks:
archgraph-docker-deployment: {}
11 changes: 9 additions & 2 deletions src/GCF/decorators/OntologyClass.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import datetime
import importlib
import json
from marshmallow_jsonschema import JSONSchema


def decorator_schema(cls):
def getSchema(self):
json_schema = JSONSchema()
return json_schema.dump(self)
setattr(cls, 'getSchema', getSchema)
return cls


def ontology_class(cls):
Expand Down
5 changes: 3 additions & 2 deletions src/Models/CRM/v5_0_2/NodeEntities/E1_CRM_Entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
UniqueIdProperty,
db,
)
from src.GCF.decorators.OntologyClass import ontology_class
from src.GCF.decorators.OntologyClass import ontology_class, decorator_schema
from src.Models.CRM.v5_0_2.NodeProperties.has_value import has_value
from src.Models.CRM.v5_0_2.NodeProperties.P1_is_identified_by import P1_is_identified_by
from src.Models.CRM.v5_0_2.NodeProperties.P2_has_type import P2_has_type
Expand All @@ -21,7 +21,7 @@
)
from src.Models.DataObject.v0_0_2.SuperClass import SuperClass


@decorator_schema
class E1_CRM_EntitySchema(Schema):
uid = fields.String()
name = fields.String(required=True)
Expand Down Expand Up @@ -81,3 +81,4 @@ def __init__(self, schema=None, *args, **kwargs):
schema = E1_CRM_EntitySchema()

SuperClass.__init__(self, schema)

4 changes: 2 additions & 2 deletions src/Models/CRM/v5_0_2/NodeEntities/E2_Temporal_Entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class E2_Temporal_EntitySchema(E1_CRM_EntitySchema):
"src.Models.CRM.v5_0_2.NodeEntities.E52_Time_Span.E52_Time_SpanSchema"
)
P175_starts_before_or_with_the_start_of = fields.Nested(
"src.Models.CRM.v5_0_2.NodeEntities.E52_Time_Span" ".E52_Time_SpanSchema"
"src.Models.CRM.v5_0_2.NodeEntities.E52_Time_Span.E52_Time_SpanSchema"
)
P176_starts_before_the_start_of = fields.Nested(
"src.Models.CRM.v5_0_2.NodeEntities.E52_Time_Span.E52_Time_SpanSchema"
Expand All @@ -88,7 +88,7 @@ class E2_Temporal_EntitySchema(E1_CRM_EntitySchema):
"src.Models.CRM.v5_0_2.NodeEntities.E52_Time_Span.E52_Time_SpanSchema"
)
P184_ends_before_or_with_the_end_of = fields.Nested(
"src.Models.CRM.v5_0_2.NodeEntities.E52_Time_Span" ".E52_Time_SpanSchema"
"src.Models.CRM.v5_0_2.NodeEntities.E52_Time_Span.E52_Time_SpanSchema"
)
P185_ends_before_the_end_of = fields.Nested(
"src.Models.CRM.v5_0_2.NodeEntities.E52_Time_Span.E52_Time_SpanSchema"
Expand Down
21 changes: 2 additions & 19 deletions src/Models/DataObject/v0_0_2/DataObject.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

from marshmallow import Schema, fields
from neomodel import StringProperty, StructuredNode, UniqueIdProperty
from src.GCF.decorators.OntologyClass import decorator_schema
from src.Models.DataObject.v0_0_2.SuperClass import SuperClass


@decorator_schema
class DataObjectSchema(Schema):
uid = fields.String()
name = fields.String(required=True)
Expand All @@ -22,21 +23,3 @@ def __init__(self, schema=None, *args, **kwargs):




var = {
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"PersonNameSchema": {
"properties": {
"name": {"title": "name", "type": "string"},
"person_name": {"title": "person_name", "type": "string"},
"stringValue": {"title": "stringValue", "type": "string"},
"uid": {"title": "uid", "type": "string"},
},
"required": ["name", "person_name", "stringValue"],
"type": "object",
"additionalProperties": False,
}
},
"$ref": "#/definitions/PersonNameSchema",
}
26 changes: 17 additions & 9 deletions src/Models/DataObject/v0_0_2/SuperClass.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def get_property_from_entity(self, property_name):
class_name = self.__class__.__name__ + "Schema"
return schema_node['definitions'][class_name]['properties'][property_name]

def get_superclasses_name(self):
return list(set(self.labels()))

def get_schema_with_template(self, template):
jsonSchema = self.getSchema()
newJsonSchema = {
Expand All @@ -48,15 +51,20 @@ def __get_schema_with_template_aux(self, definitions, json_template, new_json_sc
else:
entity_name = list(json_template.keys())[0]
current_entity = entity_name + "Schema"
entity = {
'type': definitions[current_entity]['type'],
'properties': {},
'additionalProperties': definitions[current_entity]['additionalProperties'],
}
if 'required' in definitions[current_entity].keys():
entity['required'] = definitions[current_entity]['required']

new_json_schema['definitions'][current_entity] = entity
entity = {}
get_entity = new_json_schema["definitions"].get(current_entity, None)
if get_entity is not None:
entity = new_json_schema["definitions"][current_entity]
else:
entity = {
'type': definitions[current_entity]['type'],
'properties': {},
'additionalProperties': definitions[current_entity]['additionalProperties'],
}
if 'required' in definitions[current_entity].keys():
entity['required'] = definitions[current_entity]['required']

new_json_schema['definitions'][current_entity] = entity

properties = definitions[current_entity]['properties']

Expand Down
136 changes: 118 additions & 18 deletions src/Routes/mongo.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,132 @@
import json
from datetime import datetime
from src.Utils.Utils import find_name_of_class_schema_in_project


from pymongo import MongoClient

client = MongoClient(port=27017, username="root", password="rootpassword")
db = client.mydatabase
date_now = datetime.now().strftime("%Y-%m-%d, %H:%M:%S")


# def insert_into_mongo(content):
# result = {}
# if isinstance(content, type([])):
# db.reviews.insert_many(content)
# print("inserted many documents")
#
# elif isinstance(content, type({})):
# result = db.reviews.insert_one(content)
# print(result.inserted_id)
#
# cursor = db.reviews.find({})
# for document in cursor:
# print(document)
def populate_template_collection(objectjson):

for object in objectjson:
class_name = object["className"]

class_in_project = find_name_of_class_schema_in_project(class_name)
if class_in_project is None:
print(class_name + " did't found")
template = object["template"]
schema = class_in_project().getSchema()
schema_str = json.dumps(schema)
db.defaultTemplate.insert_one({"class_name": class_name, "template": template, "schema": schema_str})
return


def update_data_in_mongo(uid, data):
date = datetime.now()
date_str = date.strftime("%Y-%m-%d, %H:%M:%S")
result = db.data.find_one({"uid": uid})
if result is None:
db.data.insert_one({"uid": uid, "data": data, "timestamp": date_str})
print("created")
else:
db.data.find_one_and_replace({"_id": result["_id"]}, {"uid": uid, "data": data, "timestamp": date_str})
print("updated")


def update_schema_in_mongo(classes_name, schema):
schema_str = json.dumps(schema)
date = datetime.now()
date_str = date.strftime("%Y-%m-%d, %H:%M:%S")
message = ""
result = get_schema_from_mongo(classes_name)
if result is None:
message = "was inserted new template"
db.template.insert_one({'classes_name': classes_name, "timestamp": date_str, "schema": schema_str})
return message
elif result['match'] < len(classes_name):
db.template.insert_one({'classes_name': classes_name, "timestamp": date_str, "schema": schema_str})
message = "inserted new template"
else:
db.data.find_one_and_replace({"_id": result['_id']}, {'classes_name': classes_name, "timestamp": date_str, "schema": schema})
message = "updated template"
return message


def get_schema_from_mongo(classes_name):
if db.template.count_documents({'classes_name': {'$in': classes_name}}) == 0:
return None
else:
result = db.template.find({'classes_name': {'$in': classes_name}})

elements_match_records = {
'_id': "",
'match': 0,
'schema': {}
}
for document in result:
merged = set(classes_name) & set(document['classes_name'])
if len(merged) > elements_match_records["match"]:
elements_match_records['match'] = len(merged)
elements_match_records['schema'] = document
elements_match_records['_id'] = document['_id']
return elements_match_records


def get_record_from_collection(uid, collection):
record = db[collection].find_one({"uid": uid})
return record


def add_record_to_collection(uid, data, collection):
date = datetime.now()
date_str = date.strftime("%Y-%m-%d, %H:%M:%S")
db[collection].insert_one({"uid": uid, "data": data, "timestamp": date_str})
return

def read_file(path_file):
file = open(path_file, "r")
file_to_json = json.loads(file.read())
print(file_to_json)
return file_to_json

def get_all_records_from_collection(collection):
records = db[collection].find({})
for record in records:
print(record)
return records

def insert_into_mongo(content):
result = {}
if isinstance(content, type([])):
db.reviews.insert_many(content)
print("inserted many documents")

elif isinstance(content, type({})):
result = db.reviews.insert_one(content)
print(result.inserted_id)
def delete_collection(collection):
db[collection].delete_many({})

cursor = db.reviews.find({})
for document in cursor:
print(document)

delete_collection("data")
delete_collection("template")

def delete_databse():
db.reviews.delete_many({})
# unique = datetime.now()
# #update_data(5552,"aaa")
# class_name = ['ola', '1', '2', '3']
# db.template.insert_one({'classes_name': class_name, "timestamp": date_now, "template": "template"})
#
# class_name2 = ['ola', '3', "4"]
# db.template.insert_one({'classes_name': class_name2, "timestamp": date_now, "template": "template"})
#
# class_name_test = ["7"]
# update_template(class_name_test, "template")
#
#
# print("get all records")
# get_all_records_from_collection("template")
#get_all_records_from_collection("data")
Loading

0 comments on commit 0b7b22b

Please sign in to comment.