-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneo-network-docs.rb
154 lines (119 loc) · 4.14 KB
/
neo-network-docs.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
require 'bundler/inline'
require 'yaml'
gemfile do
source 'https://rubygems.org'
gem 'rake'
gem 'neo4j'
end
require 'neo4j/core/cypher_session/adaptors/http'
class NeoNetworkDocs
def self.load_data
options = {wrap_level: :proc} # Required to be able to use `Object.property` and `Object.relatedObject
neo4j_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new('http://neo4j:password@localhost:7474')
Neo4j::ActiveBase.on_establish_session { Neo4j::Core::CypherSession.new(neo4j_adaptor) }
clear_database
create_all_nodes
create_all_rels
p "data loaded successfully"
end
def self.clear_database
query("MATCH (n) DETACH DELETE n")
end
def self.create_all_nodes
data.each do |network|
network_name = get_network_name(network)
network_data = get_network_data(network)
query("CREATE CONSTRAINT ON (n:#{network_name.camelize}) ASSERT n.uuid IS UNIQUE")
node_class = Class.new(Object) do
include Neo4j::ActiveNode
include Neo4j::Timestamps
property :name, type: String
property :server, type: String
property :desc, type: String
end
node = Object.const_set(network_name.camelize, node_class)
network_data.each do |app|
current_app = node.find_or_initialize_by(name: app['name'])
current_app.assign_attributes(server: app['server']) if app['server'].present?
current_app.assign_attributes(desc: app['desc']) if app['desc'].present?
current_app.save
end
end
end
def self.create_all_rels
data.each do |network|
network_data = get_network_data(network)
network_data.each do |app|
source_app = get_node_class(get_network_name(network)).find_or_create_by!(name: app['name'])
if app.key?("relationships")
app['relationships'].each do |rel|
rel_name = rel.keys[0]
rel_class = Class.new(Object) do
include Neo4j::ActiveRel
from_class :any
to_class :any
type rel_name.upcase
end
rel.values[0].each do |target_app_name|
node_labels.each do |n|
begin
target_app = get_node_class(n).find_or_initialize_by(name: target_app_name)
rescue NameError
query("CREATE CONSTRAINT ON (n:#{n.camelize}) ASSERT n.uuid IS UNIQUE")
node = Object.const_set(n.camelize, node_class)
target_app = get_node_class(n).find_or_create_by!(name: target_app_name)
end
@target_app = target_app if target_app.created_at.present?
end
begin
custom_class = Object.const_get(rel_name.camelize)
rescue NameError
custom_class = Object.const_set(rel_name.camelize, rel_class)
end
if rel_name.scan(out_keywords_re).present?
custom_class.create(from_node: source_app, to_node: @target_app)
elsif rel_name.scan(in_keywords_re).present?
custom_class.create(from_node: @target_app, to_node: source_app)
else
p "no keywords match. add '#{rel_name}' to out_keywords or in_keywords in config.yml."
end
end
end
end
end
end
end
def self.data
data = []
Dir.glob('./networks/*.yml') do |yml_file|
data << [File.basename(yml_file, ".*") => YAML.load(File.read(yml_file))]
end
data
end
def self.config
YAML.load(File.read('./config.yml'))
end
def self.out_keywords_re
Regexp.union(config['out_keywords'])
end
def self.in_keywords_re
Regexp.union(config['in_keywords'])
end
def self.query(cypher_query)
Neo4j::ActiveBase.current_session.query(cypher_query)
end
def self.node_labels
node_labels = []
data.map { |n| node_labels << n[0].keys[0] }
node_labels
end
def self.get_network_name(network)
network[0].keys[0]
end
def self.get_network_data(network)
network[0].values[0]
end
def self.get_node_class(node_class_name)
node_class_name.camelize.constantize
end
end