def intercepts_create_single_log(log=None): print '====create single log node====' cypher = secure_graph1.cypher # Find all activities, but really just one in this case log_cursor = mongo3.db.logs.find({"_id": ObjectId(log)}) json_log = json.dumps(log_cursor[0], default=json_util.default) # Create a new python dictionary from the json_log, we'll call it log_dict log_dict = json.loads(json_log) ### # Business logic for USER_NODE starts here, uses data from above. ### user_id = log_dict.get('user').get('$oid') user_node = get_user_node(user_id=user_id) ### # Business logic for getting EXPERIENCE_NODE starts here, uses data from above. ### experience_id = log_dict.get('firstExperience').get('$oid') experience_node = get_experience_node(experience_id=experience_id) ### # Business logic for LOG_NODE starts here, uses data from above. ### new_log_node = cnr_user_logged_log(new_user_node=user_node, log_dict=log_dict) ### # Business logic for SUBLOG_NODE starts here, uses data from above. ### # List of all dictionary types sublog_list = ['physic', 'emotion', 'academic', 'commune', 'ether'] for sublog_name in sublog_list: # This method also creates a new sublog, and builds a relationship # to the user (and adds the word nodes)! cnr_log_contains_sub( new_user_node=user_node, new_log_node=new_log_node, log_dict=log_dict, sublog_array_name=sublog_name, node_title=sublog_name.title() + 'Log', ) # Create a new relationship for the experience/log experience_contains_log = Relationship(experience_node, "CONTAINS", new_log_node) secure_graph1.create(experience_contains_log) return 'success'
def cnr_log_contains_sub(new_user_node=None, new_log_node=None, log_dict=None, sublog_array_name=None, node_title=None): ## Only do the iteration step if there is a word to add if log_dict.get(sublog_array_name + 'ArrayLength') > 0: new_sub_log_node = cnr_user_described_sublog( new_user_node=new_user_node, new_log_node=new_log_node, log_dict=log_dict, sublog_array_name=sublog_array_name, node_title=node_title, ) log_contains_sub = Relationship(new_log_node, "SUB_CONTAINS", new_sub_log_node) secure_graph1.create(log_contains_sub)
def cnr_user_described_sublog(new_user_node=None, new_log_node=None, log_dict=None, sublog_array_name=None, node_title=None): # Create a new sublog node new_sub_log_node = Node(node_title, parentLogName=log_dict.get('name'), parentLogId=log_dict.get('_id').get('$oid'), privacy=log_dict.get('privacy'), archived=log_dict.get('archived'), wordLength=log_dict.get(sublog_array_name + 'ArrayLength'), content=log_dict.get(sublog_array_name + 'Content'), nodeType='sublog', ) for word in log_dict.get(sublog_array_name + 'Array'): new_word_node = Node("Word", name=word, characters=len(word), nodeType='word',) log_has_word = Relationship(new_log_node, "HAS", new_word_node) secure_graph1.create(log_has_word) sublog_has_word = Relationship(new_sub_log_node, "HAS", new_word_node) secure_graph1.create(sublog_has_word) user_spoke_word = Relationship(new_user_node, "SPOKE", new_word_node) secure_graph1.create(user_spoke_word) user_described_sublog = Relationship(new_user_node, "DESCRIBED", new_sub_log_node) secure_graph1.create(user_described_sublog) return new_sub_log_node
def intercepts_create_event_supplement(): # Create events with the following attributes... # logCount, highestValue, totals for each category, winningCategoryName cypher = secure_graph1.cypher # TODO: Make this method execute differently if there is a user_id # For example, if a user_id, make it so that user has updated event nodes # All distinct events for each give user for event_record in cypher.execute("MATCH (u)-[r:LOGGED]->(n:Log) RETURN DISTINCT n.year, n.month, n.day, u.user_id"): sums = cypher.execute("MATCH (u)-[r:LOGGED]->(n:Log) where n.year = " + str(event_record[0]) + " and n.month = " + str(event_record[1]) + " and n.day = " + str(event_record[2]) + " and u.user_id = '" + event_record[3] + "' " + "RETURN sum(n.physicArrayLength), sum(n.emotionArrayLength), sum(n.academicArrayLength), sum(n.communeArrayLength), sum(n.etherArrayLength), u.user_id, count(n)")[0] # Find the position of the max values in the list winningIndexes = [] sum_list = [sums[0], sums[1], sums[2], sums[3], sums[4]] max_value = max(sum_list) for idx,s in enumerate(sum_list): if s >= max_value: winningIndexes.append(idx) new_event_node = Node("Event", user = sums[5], ymd=str(event_record[0]) + '-' + str(event_record[1]) + '-' + str(event_record[2]), year=event_record[0], month=event_record[1], day=event_record[2], physicArrayLengthSum = sums[0], emotionArrayLengthSum = sums[1], academicArrayLengthSum = sums[2], communeArrayLengthSum = sums[3], etherArrayLengthSum = sums[4], winningIndexes = winningIndexes, logCount = sums[6], ) for log_record in cypher.execute("MATCH (u)-[r:LOGGED]->(n:Log) where n.year = " + str(event_record[0]) + " and n.month = " + str(event_record[1]) + " and n.day = " + str(event_record[2]) + " and u.user_id = '" + event_record[3] + "' " + " " + "RETURN n"): for log_node in log_record: event_includes_log = Relationship(new_event_node, "INCLUDES", log_node) secure_graph1.create(event_includes_log) return 'success'
def intercepts_create_single_experience(experience=None): print '====create single experience node====' cypher = secure_graph1.cypher # Find all activities, but really just one in this case experience_cursor = mongo3.db.experiences.find({"_id": ObjectId(experience)}) json_experience = json.dumps(experience_cursor[0], default=json_util.default) # Create a new python dictionary from the json_experience, we'll call it experience_dict experience_dict = json.loads(json_experience) print experience_dict ### # Business logic for USER_NODE starts here, uses data from above. ### user_id = experience_dict.get('user').get('$oid') user_node = get_user_node(user_id=user_id) ### # Business logic for getting ACTIVITY_NODE starts here, uses data from above. ### activity_id = experience_dict.get('firstActivity').get('$oid') activity_node_one = get_activity_node(activity_id=activity_id) activity_id = experience_dict.get('secondActivity').get('$oid') activity_node_two = get_activity_node(activity_id=activity_id) ### # Business logic for EXPERIENCE_NODE starts here, uses data from above. ### new_experience_node = cnr_user_experienced_experience(new_user_node=user_node, experience_dict=experience_dict) # Create a new relationship for the activity/experience activity_contains_experience = Relationship(activity_node_one, "CONTAINS", new_experience_node) secure_graph1.create(activity_contains_experience) activity_contains_experience = Relationship(activity_node_two, "CONTAINS", new_experience_node) secure_graph1.create(activity_contains_experience) return 'success'
def update_experience_node(new_user_node=None, experience_dict=None): ''' To get the experience and change the node MATCH (n { experience_id: '56ea1cf43a34fc7711ae330e' }) SET n.name = 'Sleeping' RETURN n To get the activty and all of its word nodes MATCH (n { experience_id: '56ea1cf43a34fc7711ae330e' })-[r:HAS]-(w) RETURN n,w To delete the word nodes of an experience MATCH (n { experience_id: '56ea1cf43a34fc7711ae330e' })-[r:HAS]-(w) DETACH DELETE w ''' cypher = secure_graph1.cypher _match = 'MATCH (n { experience_id: "' + experience_dict.get('_id').get('$oid') + '" })' _set = ' SET n.name="' + experience_dict.get('name') + '"' _set += ' SET n.privacy="' + str(experience_dict.get('privacy')) + '"' _set += ' SET n.archived="' + str(experience_dict.get('archived')) + '"' _set += ' SET n.pronoun="' + experience_dict.get('pronoun') + '"' _set += ' SET n.word_length="' + str(experience_dict.get('descriptionArrayLength')) + '"' _return = ' RETURN n' # This the query that updates the node cypher.execute(_match + _set + _return) # Get the updated experience node updated_experience_node = get_experience_node(experience_dict.get('_id').get('$oid')) # Detach all the relationships and delete all the words associated with the experience cypher.execute('MATCH (n { experience_id: "' + experience_dict.get('_id').get('$oid') + '" })-[r:HAS]-(w) DETACH DELETE w') for word in experience_dict.get('descriptionArray'): new_word_node = Node("Word", name=word, characters=len(word), nodeType='word',) experience_has_word = Relationship(updated_experience_node, "HAS", new_word_node) secure_graph1.create(experience_has_word) user_spoke_word = Relationship(new_user_node, "SPOKE", new_word_node) secure_graph1.create(user_spoke_word) return updated_experience_node
def update_activity_node(new_user_node=None, activity_dict=None): ''' To get the activity and change the node MATCH (n { activity_id: '56edf2da6b43ff691627efd8' }) SET n.name = 'Sleeping' RETURN n To get the activty and all of its word nodes MATCH (n { activity_id: '56edf2da6b43ff691627efd8' })-[r:HAS]-(w) RETURN n,w To delete the word nodes of an activty MATCH (n { activity_id: '56edf2da6b43ff691627efd8' })-[r:HAS]-(w) DETACH DELETE w ''' cypher = secure_graph1.cypher _match = 'MATCH (n { activity_id: "' + activity_dict.get('_id').get('$oid') + '" })' _set = ' SET n.name="' + activity_dict.get('name') + '"' _set += ' SET n.privacy="' + str(activity_dict.get('privacy')) + '"' _set += ' SET n.archived="' + str(activity_dict.get('archived')) + '"' _set += ' SET n.word_length="' + str(activity_dict.get('descriptionArrayLength')) + '"' _return = ' RETURN n' # This the query that updates the node cypher.execute(_match + _set + _return) # Get the updated activity node updated_activity_node = get_activity_node(activity_dict.get('_id').get('$oid')) # Detach all the relationships and delete all the words associated with the activity cypher.execute('MATCH (n { activity_id: "' + activity_dict.get('_id').get('$oid') + '" })-[r:HAS]-(w) DETACH DELETE w') for word in activity_dict.get('descriptionArray'): new_word_node = Node("Word", name=word, characters=len(word), nodeType='word',) activity_has_word = Relationship(updated_activity_node, "HAS", new_word_node) secure_graph1.create(activity_has_word) user_spoke_word = Relationship(new_user_node, "SPOKE", new_word_node) secure_graph1.create(user_spoke_word) return updated_activity_node
def cnr_user_logged_log(new_user_node=None, log_dict=None): milliDate = log_dict.get('created').get('$date') date = datetime.datetime.fromtimestamp(milliDate/1000.0) # Create a new log node new_log_node = Node("Log", name=log_dict.get('name'), log_id=log_dict.get('_id').get('$oid'), privacy=log_dict.get('privacy'), archived=log_dict.get('archived'), physicArrayLength=log_dict.get('physicArrayLength'), emotionArrayLength=log_dict.get('emotionArrayLength'), academicArrayLength=log_dict.get('academicArrayLength'), communeArrayLength=log_dict.get('communeArrayLength'), etherArrayLength=log_dict.get('etherArrayLength'), physicContent=log_dict.get('physicContent'), emotionContent=log_dict.get('emotionContent'), academicContent=log_dict.get('academicContent'), communeContent=log_dict.get('communeContent'), etherContent=log_dict.get('etherContent'), milliDate=milliDate, year=date.year, month=date.month, day=date.day, hour=date.hour, minute=date.minute, second=date.second, nodeType='log', ) ## You might be wondering, where are the words for a log... see the 'cnr_user_described_sublog' user_logged_log = Relationship(new_user_node, "LOGGED", new_log_node) secure_graph1.create(user_logged_log) return new_log_node
def cnr_user_did_activity(new_user_node=None, activity_dict=None): # Create a new activity node new_activity_node = Node("Activity", name=activity_dict.get('name'), activity_id=activity_dict.get('_id').get('$oid'), privacy=activity_dict.get('privacy'), archived=activity_dict.get('archived'), word_length=activity_dict.get('descriptionArrayLength'), nodeType='activity', ) for word in activity_dict.get('descriptionArray'): new_word_node = Node("Word", name=word, characters=len(word), nodeType='word',) activity_has_word = Relationship(new_activity_node, "HAS", new_word_node) secure_graph1.create(activity_has_word) user_spoke_word = Relationship(new_user_node, "SPOKE", new_word_node) secure_graph1.create(user_spoke_word) user_did_activity = Relationship(new_user_node, "DID", new_activity_node) secure_graph1.create(user_did_activity) return new_activity_node
def cnr_user_experienced_experience(new_user_node=None, experience_dict=None): # Create a new experience node new_experience_node = Node("Experience", name=experience_dict.get('name'), experience_id=experience_dict.get('_id').get('$oid'), privacy=experience_dict.get('privacy'), archived=experience_dict.get('archived'), pronoun=experience_dict.get('pronoun'), word_length=experience_dict.get('descriptionArrayLength'), nodeType='experience', ) for word in experience_dict.get('descriptionArray'): new_word_node = Node("Word", name=word, characters=len(word), nodeType='word',) experience_has_word = Relationship(new_experience_node, "HAS", new_word_node) secure_graph1.create(experience_has_word) user_spoke_word = Relationship(new_user_node, "SPOKE", new_word_node) secure_graph1.create(user_spoke_word) user_experienced_experience = Relationship(new_user_node, "EXPERIENCED", new_experience_node) secure_graph1.create(user_experienced_experience) return new_experience_node
def intercepts_create_records(): # Clear the database secure_graph1.delete_all() user_cursor = mongo3.db.users.find({}) #find all users #### # For every user create a node #### for user in user_cursor: json_user = json.dumps(user, default=json_util.default) user_dict = json.loads(json_user) # Create a bunch of user nodes new_user_node = Node("User", email=user_dict.get('email'), user_id=user_dict.get('_id').get('$oid'), nodeType='user', ) #### user = user_dict.get('_id').get('$oid') activity_cursor = mongo3.db.activities.find({"user": ObjectId(user)}) #find all activities for a user #### # For every activity create a node #### for activity in activity_cursor: json_activity = json.dumps(activity, default=json_util.default) # Create a new python dictionary from the json_activity, we'll call it activity_dict activity_dict = json.loads(json_activity) # Output is a new_activity_node new_activity_node = cnr_user_did_activity( new_user_node=new_user_node, activity_dict=activity_dict ) #### activity = activity_dict.get('_id').get('$oid') experience_cursor = mongo3.db.experiences.find({"firstActivity": ObjectId(activity)}) #find all experiences for an activity #### # For every experience create a node #### for experience in experience_cursor: json_experience = json.dumps(experience, default=json_util.default) # Create a new python dictionary from the json_experience, we'll call it experience_dict experience_dict = json.loads(json_experience) # Output is a new_experience_node new_experience_node = cnr_user_experienced_experience( new_user_node=new_user_node, experience_dict=experience_dict ) # Create a new relationship for the activity/experience activity_contains_experience = Relationship(new_activity_node, "CONTAINS", new_experience_node) secure_graph1.create(activity_contains_experience) #### experience = experience_dict.get('_id').get('$oid') log_cursor = mongo3.db.logs.find({"firstExperience": ObjectId(experience)}) #find all logs for an experience #### # For every log create a node #### for log in log_cursor: json_log = json.dumps(log, default=json_util.default) # Create a new python dictionary from the json_experience, we'll call it log_dict log_dict = json.loads(json_log) new_log_node = cnr_user_logged_log( new_user_node=new_user_node, log_dict=log_dict, ) # List of all dictionary types sublog_list = ['physic', 'emotion', 'academic', 'commune', 'ether'] for sublog_name in sublog_list: # This method also creates a new sublog, and builds a relationship # to the user (and adds the word nodes)! cnr_log_contains_sub( new_user_node=new_user_node, new_log_node=new_log_node, log_dict=log_dict, sublog_array_name=sublog_name, node_title=sublog_name.title() + 'Log', ) experience_contains_log = Relationship(new_experience_node, "CONTAINS", new_log_node) secure_graph1.create(experience_contains_log) return 'success'