def get(self, user_id): req = "MATCH (find:user {user_id: %d}) RETURN find" % user_id result = neo4j.query_neo4j(req) try: return makeResponse(result.single()['find'].properties, 200) except ResultError: return makeResponse("ERROR : Cannot find user with uid: %d" % user_id, 204)
def get(self): req = "MATCH (:post) RETURN count(*) AS nb_posts" result = neo4j.query_neo4j(req) try: return makeResponse(result.single()['nb_posts'], 200) except ResultError: return makeResponse("ERROR", 500)
def get(self, parent_tag_id): req = "MATCH (t:tag {tag_id : %d})<-[:IS_CHILD]-(:tag) RETURN count(*) AS nb_child" % parent_tag_id result = neo4j.query_neo4j(req) try: return makeResponse(result.single()['nb_child'], 200) except ResultError: return makeResponse("ERROR", 500)
def get(self, author_id): req = "MATCH (author:user {user_id : %d})-[:AUTHORSHIP]->(:post) RETURN count(*) AS nb_posts" % author_id result = neo4j.query_neo4j(req) try: return makeResponse(result.single()['nb_posts'], 200) except ResultError: return makeResponse("ERROR", 500)
def get(self, user_id): req = "MATCH (a:annotation)<-[:AUTHORSHIP]-(:user {user_id: %d}) RETURN count(a) AS nb_annotations" % user_id result = neo4j.query_neo4j(req) try: return makeResponse(result.single()['nb_annotations'], 200) except ResultError: return makeResponse("ERROR", 500)
def get(self, public_gid, layout): private_gid = self.gid_stack[public_gid] if not os.path.isfile("%s%s.tlp" % (config['exporter']['tlp_path'], private_gid)): return makeResponse("Unknow graph id : %s" % public_gid) tulip_graph = tlp.loadGraph("%s%s.tlp" % (config['exporter']['tlp_path'], private_gid)) tulip_graph.applyLayoutAlgorithm(layout) return makeResponse(getJson(tulip_graph), 200)
def get(self, comment_id): req = "MATCH (a:annotation)-[:ANNOTATES]->(:comment {comment_id: %d}) RETURN count(a) AS nb_annotations" % comment_id result = neo4j.query_neo4j(req) try: return makeResponse(result.single()['nb_annotations'], 200) except ResultError: return makeResponse("ERROR", 500)
def get(self, tag_id1, tag_id2): req = "MATCH (find:tag {tag_id: %d}) RETURN find" % tag_id1 result = neo4j.query_neo4j(req) tag1 = result.single()['find'].properties req = "MATCH (find:tag {tag_id: %d}) RETURN find" % tag_id2 result = neo4j.query_neo4j(req) tag2 = result.single()['find'].properties response = {} if tag1['tag_id'] <= tag2['tag_id']: response['tag_src'] = tag1 response['tag_dst'] = tag2 else: response['tag_src'] = tag2 response['tag_dst'] = tag1 req = "match (t1: tag {tag_id: %d})<-[:REFERS_TO]-(a: annotation)-[:ANNOTATES]->(e) " % tag_id1 req += "match (e)<-[:ANNOTATES]-(a2: annotation)-[:REFERS_TO]->(t2: tag {tag_id: %d}) " % tag_id2 req += "match (e)<-[:AUTHORSHIP]-(u: user) " req += "return distinct t1.tag_id, CASE e.post_id when null then e.comment_id else e.post_id end as id, CASE e.post_id WHEN null THEN 'comment' ELSE 'post' END as entity_type, e.timestamp as timestamp, e.label as label, u.user_id as user_id, u.label as user_label, t2.tag_id ORDER BY e.timestamp DESC" result = neo4j.query_neo4j(req) tags = [] for record in result: tags.append({'id': record['id'], "entity_type": record['entity_type'], "timestamp": record['timestamp'], 'label': record['label'], 'user_id': record['user_id'], 'user_label': record['user_label']}) response['list'] = tags try: tags except ResultError: return makeResponse("ERROR",500) return makeResponse(response, 200)
def get(self, comment_id): req = "MATCH (find:comment {comment_id: %d}) RETURN find" % comment_id result = neo4j.query_neo4j(req) try: return makeResponse(result.single()['find'].properties, 200) except ResultError: return makeResponse( "ERROR : Cannot find comment with cid: %d" % comment_id, 204)
def get(self, tag_id): result = neo4j.query_neo4j( "MATCH (find:tag {tag_id: %d}) RETURN find" % tag_id) try: return makeResponse(result.single()['find'].properties, 200) except ResultError: return makeResponse( "ERROR : Cannot find tag with tid: %d" % tag_id, 204)
def get(self, post_id): result = neo4j.query_neo4j( "MATCH (find:post {post_id: %d}) RETURN find" % post_id) try: return makeResponse(result.single()['find'].properties, 200) except ResultError: return makeResponse( "ERROR : Cannot find post with pid: %d" % post_id, 204)
def get(self, annot_id): result = neo4j.query_neo4j( "MATCH (find:annotation {annotation_id: %d}) RETURN find" % annot_id) try: return makeResponse(result.single()['find'].properties, 200) except ResultError: return makeResponse( "ERROR : Cannot find annotation with id: %d" % annot_id, 204)
def get(self, timestamp1, timestamp2, limit): req = "match (n)<-[:ANNOTATES]-(a:annotation)-[:REFERS_TO]->(t:tag) where n.timestamp > %d and n.timestamp < %d and ('post' in labels(n) or 'comment' in labels(n)) return t.tag_id as id, t.label as label, count(t) as count order by count desc limit %d" % (timestamp1, timestamp2, limit) result = neo4j.query_neo4j(req) tags=[] for record in result: tags.append({'id': record['id'], "label": record['label'], "count": record['count']}) try: return makeResponse(tags, 200) except ResultError: return makeResponse("ERROR", 500)
def get(self, user_id): # Get user properties req = "MATCH (find:user {user_id: %d}) RETURN find" % user_id result = neo4j.query_neo4j(req) user = result.single()['find'].properties # Get user's posts req = "MATCH (find:user {user_id: %d})" % user_id req += " MATCH (find)-[:AUTHORSHIP]->(p:post)" req += ' RETURN p.post_id AS post_id, p.label AS post_label, p.timestamp AS timestamp ORDER BY p.timestamp DESC' result = neo4j.query_neo4j(req) posts = [] posts_id = [] for record in result: try: if record['post_id'] and record['post_id'] not in posts_id: post = {} post['post_id'] = record['post_id'] post['label'] = record['post_label'] post['timestamp'] = record['timestamp'] posts.append(post) posts_id.append(post['post_id']) except KeyError: pass # Get user's comments req = "MATCH (find:user {user_id: %d})" % user_id req += " MATCH (find)-[:AUTHORSHIP]->(c:comment)" req += " OPTIONAL MATCH (c)-[:COMMENTS]->(p:post)" req += ' RETURN c.comment_id AS comment_id, c.label AS comment_label, c.timestamp AS timestamp, p.post_id AS comment_parent_post_id, p.label AS comment_parent_post_label ORDER BY c.timestamp DESC' result = neo4j.query_neo4j(req) comments_id = [] comments = [] for record in result: try: if record['comment_id'] and record['comment_id'] not in comments_id: comment = {} comment['comment_id'] = record['comment_id'] comment['label'] = record['comment_label'] comment['timestamp'] = record['timestamp'] comment['comment_parent_post_id'] = record['comment_parent_post_id'] comment['comment_parent_post_label'] = record['comment_parent_post_label'] comments.append(comment) comments_id.append(comment['comment_id']) except KeyError: pass try: user except NameError: return makeResponse("ERROR : Cannot find user with user_id: %d" % user_id, 204) user['posts'] = posts user['comments'] = comments return makeResponse(user, 200)
def get(self): # todo change status response = {"status": "ok", "version": "0000000000000", "percentRamUsage": psutil.virtual_memory()[2], "percentDiskUsage": psutil.disk_usage('/')[3]} req = "MATCH (n) RETURN max(n.timestamp) AS version" result = neo4j.query_neo4j(req) try: response['version'] = result.single()['version'] except ResultError: return makeResponse("ERROR : Cannot load latest timestamp", 204) return makeResponse(response, 200)
def get(self, public_gid, layout): private_gid = self.gid_stack[public_gid] if not os.path.isfile("%s%s.tlp" % (config['exporter']['tlp_path'], private_gid)): return makeResponse("Unknow graph id : %s" % public_gid) tulip_graph = tlp.loadGraph( "%s%s.tlp" % (config['exporter']['tlp_path'], private_gid)) tulip_graph.applyLayoutAlgorithm(layout) path = tempfile.mkstemp() ds = tlp.getDefaultPluginParameters("SIGMA JSON Export", tulip_graph) tlp.exportGraph("SIGMA JSON Export", tulip_graph, path[1], ds) return makeResponse(path[1], 200, True)
def get(self, tag_id): req = "MATCH (t1: tag {tag_id: %d})--(a1: annotation)-[:ANNOTATES]->(e:post)<-[:ANNOTATES]-(a2:annotation)--(t2: tag) where t1<>t2 return t2, count(e) as count order by count desc" % tag_id result = neo4j.query_neo4j(req) tags = [] for record in result: tag = record['t2'].properties if record['count']: tag['count'] = record['count'] tags.append(tag) try: return makeResponse(tags, 200) except ResultError: return makeResponse("ERROR", 500)
def get(self, annotation_id): req = "MATCH (find:annotation {annotation_id: %d})" % annotation_id req += "MATCH (find)-[:ANNOTATES]->(x)" req += 'RETURN find, labels(x) as test' result = neo4j.query_neo4j(req) #annotation = result.single()['find'].properties annotateComment = False for record in result: annotation = record['find'].properties try: if "comment" in record["test"]: annotateComment = True except KeyError: return makeResponse( "ERROR : Impossible to identify 'entity_type' for annotation with aid: %d" % annotation_id, 205) if annotateComment: req = "MATCH (find:annotation {annotation_id: %d})-[:REFERS_TO]->(t: tag) " % annotation_id req += "MATCH (find)-[:ANNOTATES]->(c:comment)" req += "MATCH (c)<-[:AUTHORSHIP]-(u:user)" req += 'RETURN find, c.comment_id as entity_id, c.title as entity_title, c.timestamp as entity_timestamp, u.user_id as user_id, u.name as user_name, "comment" as entity_type, t.tag_id as tag_id, t.label as tag_label ORDER BY c.timestamp DESC' else: req = "MATCH (find:annotation {annotation_id: %d})-[:REFERS_TO]->(t: tag) " % annotation_id req += "MATCH (find)-[:ANNOTATES]->(p:post)" req += "MATCH (p)<-[:AUTHORSHIP]-(u:user)" req += 'RETURN find, p.post_id as entity_id, p.title as entity_title, p.timestamp as entity_timestamp, u.user_id as user_id, u.name as user_name, "post" as entity_type, t.tag_id as tag_id, t.label as tag_label ORDER BY p.timestamp DESC' result = neo4j.query_neo4j(req) for record in result: try: annotation['user_id'] = record['user_id'] annotation['user_name'] = record['user_name'] annotation['entity_id'] = record['entity_id'] annotation['entity_title'] = record['entity_title'] annotation['entity_timestamp'] = record['entity_timestamp'] annotation['entity_type'] = record['entity_type'] annotation['tag_id'] = record['tag_id'] annotation['tag_label'] = record['tag_label'] except KeyError: return makeResponse( "ERROR : Cannot find annotation with aid: %d" % annotation_id, 203) try: annotation except NameError: return makeResponse( "ERROR : Cannot find annotation with aid: %d" % annotation_id, 204) return makeResponse(annotation, 200)
def get(self, tag_id1, tag_id2, start_date, end_date): req = "match (t1: tag {tag_id: %d})<-[:REFERS_TO]-(a: annotation)-[:ANNOTATES]->(e) " % tag_id1 req += "match (e)<-[:ANNOTATES]-(a2: annotation)-[:REFERS_TO]->(t2: tag {tag_id: %d}) " % tag_id2 req += "match (e)<-[:AUTHORSHIP]-(u: user) " req += "where e.timestamp >= %d and e.timestamp <= %d " % (start_date, end_date) req += "return distinct t1.tag_id, CASE e.post_id when null then e.comment_id else e.post_id end as id, CASE e.post_id when null then 'comment' else 'post' end as entity_type, e.timestamp as timestamp, e.label as label, u.user_id as user_id, u.label as user_label, t2.tag_id ORDER BY e.timestamp DESC" result = neo4j.query_neo4j(req) tags = [] for record in result: tags.append({'id': record['id'], "entity_type": record['entity_type'], "timestamp": record['timestamp'], 'label': record['label'], 'user_id': record['user_id'], 'user_label': record['user_label']}) try: return makeResponse(tags,200) except ResultError: return makeResponse("ERROR",500)
def get(self): req = "MATCH (find:annotation) -[:ANNOTATES]-> (:post) RETURN find.annotation_id AS annotation_id, find.quote AS quote" result = neo4j.query_neo4j(req) annots = [] for record in result: annots.append({'annotation_id': record['annotation_id'], "quote": record['quote']}) return makeResponse(annots, 200)
def get(self): importer = ImportFromJson(True) #req= requests.get(config['importer_edgeryders']['json_users_path']) #json_file = req.json() #importer.create_users(json_file) req = requests.get(config['importer_edgeryders']['json_posts_path']) if req.status_code != 200: print("Error req posts: " + str(req.status_code)) else: json_file = req.json() importer.create_posts(json_file) req = requests.get(config['importer_edgeryders']['json_comments_path']) if req.status_code != 200: print("Error req comments: " + str(req.status_code)) else: json_file = req.json() importer.create_comments(json_file) req = requests.get(config['importer_edgeryders']['json_tags_path']) if req.status_code != 200: print("Error req tags: " + str(req.status_code)) else: json_file = req.json() importer.create_tags(json_file) req = requests.get( config['importer_edgeryders']['json_annotations_path']) if req.status_code != 200: print("Error req annotations: " + str(req.status_code)) else: json_file = req.json() importer.create_annotations(json_file) return makeResponse(importer.end_import(), 200)
def get(self): importer = ImportFromJson(False) # no user update # first update tag req = requests.get(config['importer_edgeryders']['json_tags_path']) json_file = req.json() importer.create_tags(json_file) # then the rest updateList = ['post', 'comment', 'annotation'] for elem in updateList: req = "MATCH (n:" + elem + ") RETURN max(n.timestamp) AS max" result = neo4j.query_neo4j(req) try: most_recent = time.gmtime(int(result.single()['max']) / 1000) except ResultError: print("Problem from neo4j request.") since_str = time.strftime('%Y%m%d', most_recent) req = requests.get(config['importer_edgeryders']['json_' + elem + 's_path'] + "?since=" + since_str) json_file = req.json() if elem == 'post': importer.create_posts(json_file) if elem == 'comment': importer.create_comments(json_file) if elem == 'annotation': importer.create_annotations(json_file) return makeResponse(importer.end_import(), 200)
def get(self, returnValue=True): # Full graph if 'complete' in self.gid_stack.keys(): os.remove('%s%s.tlp' % (config['exporter']['tlp_path'], self.gid_stack.pop("complete"))) creator = CreateFullTlp() complete_gid = uuid.uuid4().urn[9:] creator.create(complete_gid) self.gid_stack.update({"complete": complete_gid}) # User Graph if 'usersToUsers' in self.gid_stack.keys(): os.remove('%s%s.tlp' % (config['exporter']['tlp_path'], self.gid_stack.pop("usersToUsers"))) creator = CreateUserTlp() users_gid = uuid.uuid4().urn[9:] creator.create(users_gid) self.gid_stack.update({"usersToUsers": users_gid}) # Comment And Post Graph if 'commentAndPost' in self.gid_stack.keys(): os.remove('%s%s.tlp' % (config['exporter']['tlp_path'], self.gid_stack.pop("commentAndPost"))) creator = CreateTlp() commentPost_gid = uuid.uuid4().urn[9:] creator.createWithout(["user"], commentPost_gid) self.gid_stack.update({"commentAndPost": commentPost_gid}) if returnValue: return makeResponse(True)
def get(self): req = "MATCH (p:post) <-[:AUTHORSHIP]- (u: user) RETURN p.post_id AS post_id,p.label AS post_label, u.user_id AS user_id, u.label AS user_label, p.timestamp AS timestamp ORDER BY timestamp DESC LIMIT 5" result = neo4j.query_neo4j(req) posts = [] for record in result: posts.append({'post_id': record['post_id'], "post_label": record['post_label'], "user_id": record['user_id'], "user_label": record['user_label'], "timestamp": record['timestamp']}) return makeResponse(posts, 200)
def post(self): args = parser.parse_args() print(args) req = requests.get(config['importer_edgeryders']['json_users_path']) json_file = req.json() # args['file'].save("%s" % config['importer']['json_users_path']) return makeResponse(True, 200)
def get(self, parent_tag_id): req = "MATCH (parent:tag {tag_id : %d})<-[:IS_CHILD]-(child:tag) RETURN child" % parent_tag_id result = neo4j.query_neo4j(req) tags = [] for record in result: tags.append(record['child'].properties) return makeResponse(tags, 200)
def get(self, graph, type, id): parser.add_argument('max_size', type=int) args = parser.parse_args() public_gid = repr(int(time.time())) + uuid.uuid4().urn[19:] private_gid = uuid.uuid4().urn[9:] if graph == "complete": try: private_source = self.gid_stack[graph] except KeyError: creator = CreateFullTlp() pgid = uuid.uuid4().urn[9:] creator.create(pgid) self.gid_stack.update({"complete": pgid}) private_source = self.gid_stack[graph] else: private_source = graph if (not os.path.exists( "%s%s.tlp" % (config['exporter']['tlp_path'], private_source))): creator = CreatePostCommentTagTlp(0, int(time.time()) * 1000, True) creator.create() if args['max_size']: createDOI(private_source, private_gid, type, id, args['max_size']) else: createDOI(private_source, private_gid, type, id) checkTlpFiles(self.gid_stack) self.gid_stack.update({public_gid: private_gid}) return makeResponse({'gid': public_gid})
def get(self): parser.add_argument('uid', action='append') args = parser.parse_args() if args['uid']: req = "MATCH (n:post_type)<-[r:TYPE_IS]-(p:post) " req += addTimeFilter() for user in args['uid']: req += "OPTIONAL MATCH (n)<-[r%s:TYPE_IS]-(p:post)<-[]-(u%s:user {uid: %s}) " % ( user, user, user) req += "RETURN n, count(r) AS nb_posts" for user in args['uid']: req += ", count(r%s) AS u%s_posts" % (user, user) else: req = "MATCH (n:post_type)<-[r:TYPE_IS]-(p:post) " req += addTimeFilter() req += "RETURN n, count(r) AS nb_posts" result = neo4j.query_neo4j(req) labels = [] data = [[]] if args['uid']: for user in args['uid']: data.append([]) for record in result: labels.append(record['n'].properties['name']) data[0].append(record['nb_posts']) if args['uid']: count = 1 for user in args['uid']: data[count].append(record['u%s_posts' % user]) count += 1 return makeResponse({'labels': labels, 'data': data}, 200)
def get(self, post_type): req = "MATCH (find:post {type: '%s'}) RETURN find" % post_type req += addargs() result = neo4j.query_neo4j(req) posts = [] for record in result: posts.append(record['find'].properties) return makeResponse(posts, 200)
def get(self, author_id): req = "MATCH (author:user {user_id: %d})-[:AUTHORSHIP]->(p:post) RETURN p" % author_id req += addargs() result = neo4j.query_neo4j(req) posts = [] for record in result: posts.append(record['p'].properties) return makeResponse(posts, 200)