Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
neo4j_for_dbas
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Marc Feger
neo4j_for_dbas
Commits
39d7ae13
Commit
39d7ae13
authored
6 years ago
by
Marc Feger
Browse files
Options
Downloads
Patches
Plain Diff
Add route to load graph
parent
e5f3b5e7
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
api/graph.py
+158
-0
158 additions, 0 deletions
api/graph.py
api/requirements.txt
+2
-1
2 additions, 1 deletion
api/requirements.txt
api/server.py
+8
-1
8 additions, 1 deletion
api/server.py
with
168 additions
and
2 deletions
api/graph.py
0 → 100644
+
158
−
0
View file @
39d7ae13
from
neo4j
import
GraphDatabase
import
time
URL
=
'
jdbc:postgresql://db/discussion?user=postgres&password=FooBar
'
class
Graph
(
object
):
def
__init__
(
self
,
uri
,
user
,
password
):
self
.
_driver
=
GraphDatabase
.
driver
(
uri
,
auth
=
(
user
,
password
))
def
close
(
self
):
self
.
_driver
.
close
()
def
load
(
self
):
t0
=
self
.
run
(
self
.
_delete_everything
)
t1
=
self
.
run
(
self
.
_create_index_on_statement_uid
)
t2
=
self
.
run
(
self
.
_create_statement_nodes
)
t3
=
self
.
run
(
self
.
_fill_statements_with_content
)
t4
=
self
.
run
(
self
.
_create_user_nodes
)
t5
=
self
.
run
(
self
.
_create_relations_between_users_and_statements
)
t6
=
self
.
run
(
self
.
_create_issue_nodes
)
t7
=
self
.
run
(
self
.
_connect_issues_with_statements
)
t8
=
self
.
run
(
self
.
_every_user_likes_his_position
)
t9
=
self
.
run
(
self
.
_create_random_ratings
)
t10
=
self
.
run
(
self
.
_delete_zero_ratings
)
return
{
"
delete_time
"
:
t0
,
"
index_time
"
:
t1
,
"
statement_node_time
"
:
t2
,
"
statement_content_time
"
:
t3
,
"
user_node_time
"
:
t4
,
"
statement_user_relation_time
"
:
t5
,
"
issue_node_time
"
:
t6
,
"
statement_to_issue_time
"
:
t7
,
"
user_rates_own_positions_time
"
:
t8
,
"
random_ratings_time
"
:
t9
,
"
delete_zero_ratings_time
"
:
t10
}
def
run
(
self
,
func
):
t1
=
time
.
time
()
with
self
.
_driver
.
session
()
as
session
:
session
.
write_transaction
(
func
)
return
time
.
time
()
-
t1
@staticmethod
def
_create_index_on_statement_uid
(
tx
):
result
=
tx
.
run
(
"
CREATE INDEX ON :Statement(uid)
"
)
return
result
@staticmethod
def
_create_index_on_user_uid
(
tx
):
result
=
tx
.
run
(
"
CREATE INDEX ON :Statement(uid)
"
)
return
result
@staticmethod
def
_create_statement_nodes
(
tx
):
result
=
tx
.
run
(
"
CALL apoc.load.jdbc($url,
'
statements
'
)
"
"
YIELD row
"
"
MERGE (statement:Statement{uid:row.uid, is_position:row.is_position, is_disabled:row.is_disabled})
"
,
url
=
URL
)
return
result
@staticmethod
def
_fill_statements_with_content
(
tx
):
result
=
tx
.
run
(
"
CALL apoc.load.jdbc($url,
'
textversions
'
)
"
"
YIELD row
"
"
MATCH (statement:Statement{uid:row.statement_uid})
"
"
WHERE NOT EXISTS(statement.content)
"
"
SET statement += {content:row.content}
"
,
url
=
URL
)
return
result
@staticmethod
def
_create_user_nodes
(
tx
):
result
=
tx
.
run
(
"
CALL apoc.load.jdbc($url,
'
users
'
)
"
"
YIELD row
"
"
MERGE (user:User{uid:row.uid, public_nickname:row.public_nickname})
"
,
url
=
URL
)
return
result
@staticmethod
def
_create_relations_between_users_and_statements
(
tx
):
result
=
tx
.
run
(
"
CALL apoc.load.jdbc($url,
'
textversions
'
)
"
"
YIELD row
"
"
MATCH (user:User), (statement:Statement)
"
"
WHERE user.uid = row.author_uid AND statement.uid = row.statement_uid
"
"
MERGE (user)-[:HAS_WRITTEN]->(statement)
"
,
url
=
URL
)
return
result
@staticmethod
def
_create_issue_nodes
(
tx
):
result
=
tx
.
run
(
"
CALL apoc.load.jdbc($url,
'
issues
'
)
"
"
YIELD row
"
"
MERGE (issue:Issue{uid:row.uid, title:row.title})
"
,
url
=
URL
)
return
result
@staticmethod
def
_connect_issues_with_statements
(
tx
):
result
=
tx
.
run
(
"
CALL apoc.load.jdbc($url,
'
statement_to_issue
'
)
"
"
YIELD row
"
"
MATCH (statement:Statement{uid:row.statement_uid}), (issue:Issue{uid:row.issue_uid})
"
"
MERGE (statement)-[:WRITTEN_IN]->(issue)
"
,
url
=
URL
)
return
result
@staticmethod
def
_every_user_likes_his_position
(
tx
):
result
=
tx
.
run
(
"
MATCH(user:User)-[:HAS_WRITTEN]->(statement:Statement{is_position:True})
"
"
MERGE(user)-[:LIKES{rating: 5}]->(statement)
"
)
return
result
@staticmethod
def
_create_random_ratings
(
tx
):
result
=
tx
.
run
(
"
MATCH (user:User)
"
"
WHERE NOT EXISTS((user)-[:LIKES]->())
"
"
MATCH (statement:Statement{is_position: True})
"
"
MERGE (user)-[:LIKES{rating:round(rand()*5)}]->(statement)
"
)
return
result
@staticmethod
def
_delete_zero_ratings
(
tx
):
result
=
tx
.
run
(
"
MATCH ()-[r:LIKES{rating:0.0}]->()
"
"
DETACH DELETE r
"
)
return
result
@staticmethod
def
_delete_everything
(
tx
):
result
=
tx
.
run
(
"
MATCH (a)
"
"
DETACH DELETE a
"
)
return
result
This diff is collapsed.
Click to expand it.
api/requirements.txt
+
2
−
1
View file @
39d7ae13
Flask==0.10.1
Flask==0.10.1
flask-cors==3.0.7
flask-cors==3.0.7
neo4j==1.7.1
\ No newline at end of file
This diff is collapsed.
Click to expand it.
api/server.py
+
8
−
1
View file @
39d7ae13
from
flask
import
Flask
,
render_template
from
flask
import
Flask
,
render_template
,
jsonify
from
flask_cors
import
CORS
from
flask_cors
import
CORS
from
api.graph
import
Graph
app
=
Flask
(
__name__
,
template_folder
=
'
.
'
)
app
=
Flask
(
__name__
,
template_folder
=
'
.
'
)
CORS
(
app
)
CORS
(
app
)
...
@@ -10,5 +12,10 @@ def root(file):
...
@@ -10,5 +12,10 @@ def root(file):
return
render_template
(
'
html/
'
+
file
)
return
render_template
(
'
html/
'
+
file
)
@app.route
(
'
/load
'
)
def
test
():
return
jsonify
(
Graph
(
uri
=
'
bolt://neo:7687
'
,
user
=
'
neo4j
'
,
password
=
'
neo4j
'
).
load
())
if
__name__
==
'
__main__
'
:
if
__name__
==
'
__main__
'
:
app
.
run
(
debug
=
True
,
host
=
"
0.0.0.0
"
,
port
=
5000
)
app
.
run
(
debug
=
True
,
host
=
"
0.0.0.0
"
,
port
=
5000
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment