python-eigen-ingenuity-nocli 0.5.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. eigeningenuity/__init__.py +29 -0
  2. eigeningenuity/assetmodel.py +433 -0
  3. eigeningenuity/auth.py +178 -0
  4. eigeningenuity/core/__init__.py +236 -0
  5. eigeningenuity/elastic.py +171 -0
  6. eigeningenuity/events.py +88 -0
  7. eigeningenuity/historian.py +786 -0
  8. eigeningenuity/historianmulti.py +481 -0
  9. eigeningenuity/settings.py +87 -0
  10. eigeningenuity/smartdash.py +103 -0
  11. eigeningenuity/sql.py +132 -0
  12. eigeningenuity/util.py +548 -0
  13. modelbuilder/__init__.py +7 -0
  14. modelbuilder/src/__init__.py +0 -0
  15. modelbuilder/src/assetmodelbuilder.py +164 -0
  16. modelbuilder/src/assetmodelcsvwriter.py +31 -0
  17. modelbuilder/src/assetmodelutilities.py +51 -0
  18. modelbuilder/src/configmanager.py +500 -0
  19. modelbuilder/src/connectors/__init__.py +0 -0
  20. modelbuilder/src/connectors/boltconnector.py +108 -0
  21. modelbuilder/src/connectors/dbconnectors.py +40 -0
  22. modelbuilder/src/connectors/genericconnector.py +30 -0
  23. modelbuilder/src/connectors/httpsconnector.py +132 -0
  24. modelbuilder/src/csvcontentsmanager.py +135 -0
  25. modelbuilder/src/csvwriter.py +305 -0
  26. modelbuilder/src/fileanalyser.py +91 -0
  27. modelbuilder/src/filemanager.py +400 -0
  28. modelbuilder/src/messages.py +61 -0
  29. modelbuilder/src/processors/__init__.py +0 -0
  30. modelbuilder/src/processors/cypherfileprocessor.py +73 -0
  31. modelbuilder/src/processors/labelmanager.py +133 -0
  32. modelbuilder/src/processors/nodefileprocessor.py +282 -0
  33. modelbuilder/src/processors/propertymanager.py +129 -0
  34. modelbuilder/src/processors/relationshipfileprocessor.py +285 -0
  35. modelbuilder/src/processors/relationshipmanager.py +31 -0
  36. modelbuilder/src/queries/__init__.py +0 -0
  37. modelbuilder/src/queries/cypherbuilder.py +323 -0
  38. modelbuilder/src/queries/query.py +82 -0
  39. modelbuilder/src/queries/typedefs.py +180 -0
  40. modelbuilder/src/versionmanager.py +356 -0
  41. python_eigen_ingenuity_nocli-0.5.6.dist-info/METADATA +30 -0
  42. python_eigen_ingenuity_nocli-0.5.6.dist-info/RECORD +44 -0
  43. python_eigen_ingenuity_nocli-0.5.6.dist-info/WHEEL +5 -0
  44. python_eigen_ingenuity_nocli-0.5.6.dist-info/top_level.txt +2 -0
@@ -0,0 +1,282 @@
1
+ import messages
2
+ from queries.cypherbuilder import QueryBuilder
3
+ from queries.typedefs import Properties
4
+ import assetmodelutilities as amu
5
+ from queries.query import Neo4jQuery
6
+ import uuid
7
+ import processors.labelmanager as lm
8
+ import processors.propertymanager as pm
9
+
10
+
11
+ class NodeFileProcessor:
12
+
13
+ def __init__(self, filename, config, contents):
14
+ self.filename = filename
15
+ self.contents = contents
16
+
17
+ self.primary_property = config.get_primary_property()
18
+ self.add_system_properties = not config.get_no_system_properties()
19
+
20
+ self.required_labels = config.get_required_labels()
21
+ self.required_properties = config.get_required_properties_dict()
22
+ self.unrequired_properties = config.get_unrequired_properties_list()
23
+ self.unique_id_property = config.get_unique_id_property()
24
+ self.creation_time_property = config.get_creation_time_property()
25
+ self.update_time_property = config.get_update_time_property()
26
+
27
+ self.label_alias = config.get_label_alias()
28
+ self.node_alias = config.get_node_alias()
29
+
30
+ self.validate_only = config.in_validation_mode()
31
+ self.summarise = config.in_summary_mode()
32
+
33
+ self.blank_property_list = config.get_blank_property_list()
34
+ self.batch_rate = config.get_batch_rate()
35
+ self.start_from = config.get_start_from()
36
+ self.last_row = config.get_end_at()
37
+
38
+ self.node_types = amu.node_types
39
+ self.label_types = amu.label_types
40
+ self.all_types = amu.all_types
41
+
42
+ def get_last_row(self, num_rows):
43
+ if self.last_row < 0:
44
+ last_row = num_rows
45
+ else:
46
+ last_row = min(num_rows, self.last_row)
47
+ return last_row
48
+
49
+ def process_node_file(self):
50
+
51
+ # For each line in the file, build a cypher query to MERGE a node
52
+ # Each query is added to a list of queries, which are returned to the caller. They are NOT actioned here
53
+
54
+ num_data_rows = self.contents.get_row_count()
55
+ node_columns = self.contents.get_column_numbers_list(self.node_alias, self.node_types)
56
+ label_columns = self.contents.get_column_numbers_list(self.label_alias, self.label_types)
57
+ property_columns = self.contents.get_other_column_numbers_list(self.node_alias + self.label_alias, self.all_types)
58
+
59
+ queries = []
60
+
61
+ num_rows = num_data_rows - self.start_from + 1
62
+ this_row = self.start_from-1
63
+ last_row = self.get_last_row(num_data_rows)
64
+ property_ref = 'n'
65
+ property_ref_dot = property_ref + '.'
66
+
67
+ while this_row < last_row:
68
+ validated_csv_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, label_columns))
69
+ merge_labels, new_labels, remove_labels = lm.sort_node_labels(self.required_labels, validated_csv_labels)
70
+ row_properties = self.contents.get_property_values_dict(this_row, property_columns, self.blank_property_list)
71
+ properties = pm.combine_properties(self.required_properties, row_properties)
72
+ # Remove any properties we don't want
73
+ property_list = list(properties.keys())
74
+ for unwanted in self.unrequired_properties:
75
+ for property in property_list:
76
+ if property.split(':')[0] == unwanted:
77
+ try:
78
+ properties.pop(property)
79
+ except:
80
+ pass
81
+
82
+ for k in list(properties):
83
+ if k.startswith(':'):
84
+ properties.pop(k)
85
+ else:
86
+ properties[property_ref_dot + k] = properties.pop(k)
87
+
88
+ # Split out those properties that are dependent on others, so that we can add them in the right order
89
+ explicit_properties, calculated_properties = pm.split_out_functional_properties(properties)
90
+
91
+ for primary_property in self.contents.get_property_values_list(this_row, node_columns, self.primary_property):
92
+ time_now = amu.get_formatted_time_now()
93
+
94
+ # Build a simple MERGE query using queries
95
+ query_text = (QueryBuilder()
96
+ .merge()
97
+ .node(labels=merge_labels, ref_name=property_ref, properties=Properties(primary_property))
98
+ )
99
+
100
+ # Add in System properties if required
101
+ if self.add_system_properties:
102
+ new_uuid = str(uuid.uuid4())
103
+ create_time = property_ref_dot + self.creation_time_property + '=datetime("' + time_now + '")'
104
+ id_property = property_ref_dot + self.unique_id_property
105
+ update_id = id_property + '= CASE WHEN ' + id_property + ' IS null THEN "' + new_uuid + '" ELSE ' + id_property + ' END,' +\
106
+ property_ref_dot + self.update_time_property + '=datetime("' + time_now + '")'
107
+ query_text = query_text.on_create().set_literal(create_time).set_literal(update_id)
108
+
109
+ if len(new_labels) > 0:
110
+ query_text = query_text.set_labels(ref_name=property_ref, labels=new_labels)
111
+ if len(remove_labels) > 0:
112
+ query_text = query_text.remove_literal(f'{property_ref}:{":".join(remove_labels)}')
113
+ if len(explicit_properties) > 0:
114
+ query_text = query_text.set(properties=explicit_properties, property_ref=property_ref_dot)
115
+ if len(calculated_properties) > 0:
116
+ query_text = query_text.set(properties=calculated_properties, property_ref=property_ref_dot)
117
+
118
+ query_text = query_text.toStr()
119
+
120
+ query = Neo4jQuery('node', query_text, time_now, merge_labels, new_labels, primary_property, properties)
121
+
122
+ if self.validate_only and not self.summarise:
123
+ print(query.text)
124
+
125
+ queries.append(query)
126
+
127
+ this_row += 1
128
+ messages.message_no_cr(f'Generating queries... {100 * (this_row - self.start_from + 1) / num_rows:.0f}%{chr(13)}')
129
+
130
+ messages.message('', False)
131
+ return queries
132
+
133
+ def process_node_file_batch(self):
134
+
135
+ # For each line in the file, build a cypher query to MERGE a node
136
+ # Each query is added to a list of queries, which are returned to the caller. They are NOT actioned here
137
+
138
+ num_data_rows = self.contents.get_row_count()
139
+ node_columns = self.contents.get_column_numbers_list(self.node_alias, self.node_types)
140
+ label_columns = self.contents.get_column_numbers_list(self.label_alias, self.label_types)
141
+ property_columns = self.contents.get_other_column_numbers_list(self.node_alias + self.label_alias, self.all_types)
142
+
143
+ queries = []
144
+
145
+ num_rows = num_data_rows - self.start_from + 1
146
+ this_row = self.start_from-1
147
+ last_row = self.get_last_row(num_data_rows)
148
+ dict_size = 0
149
+ property_ref = 'n'
150
+ property_ref_dot = property_ref + '.'
151
+ payload_ref = 'payload'
152
+ payload_ref_dot = payload_ref + '.'
153
+
154
+ time_now = amu.get_formatted_time_now()
155
+
156
+ # For best optimisation, we need to create a batch query for all the update with the same labels
157
+ # So we will create a dictionary of queries based on the labels for each query
158
+ # Since the dictionary key can't be a list, we will create a list (of lists) and use the index in that list as the key
159
+ # When we have worked through all the input and grouped the queries by label combination, we will create the queries
160
+
161
+ batch_keys = []
162
+ query_dictionary = {}
163
+ id_property = property_ref_dot + self.unique_id_property
164
+ creation_property = property_ref_dot + self.creation_time_property
165
+
166
+ while this_row < last_row:
167
+ validated_csv_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, label_columns))
168
+ merge_labels, new_labels, remove_labels = lm.sort_node_labels(self.required_labels, validated_csv_labels)
169
+ row_properties = self.contents.get_property_values_dict(this_row, property_columns, self.blank_property_list)
170
+
171
+ for primary_property in self.contents.get_property_values_list(this_row, node_columns, self.primary_property):
172
+ properties = pm.combine_properties(self.required_properties, row_properties)
173
+ # Remove any properties we don't want
174
+ property_list = list(properties.keys())
175
+ for unwanted in self.unrequired_properties:
176
+ for property in property_list:
177
+ if property.split(':')[0] == unwanted:
178
+ try:
179
+ properties.pop(property)
180
+ except:
181
+ pass
182
+
183
+ properties = {**properties, **primary_property}
184
+
185
+ for k in list(properties):
186
+ if k.startswith(':'):
187
+ properties.pop(k)
188
+
189
+ explicit_properties, calculated_properties = pm.split_out_functional_properties(properties)
190
+
191
+ query_data = [explicit_properties, calculated_properties, time_now]
192
+
193
+ # So now we know all about this particular update. Let's put it in the query dictionary
194
+ # We'll group these by unique combinations of labels and primary key type
195
+ # Have to use a list since a dictionary key can't be a dictionary
196
+ dictionary_key = [new_labels, merge_labels, remove_labels, list(primary_property)[0].split(':')[0], calculated_properties]
197
+ try:
198
+ dictionary_index = batch_keys.index(dictionary_key)
199
+ query_dictionary[dictionary_index].append(query_data)
200
+ except:
201
+ dictionary_index = len(batch_keys)
202
+ query_dictionary[dictionary_index] = [query_data]
203
+ batch_keys.append(dictionary_key)
204
+ dict_size += 1
205
+
206
+ this_row += 1
207
+ messages.message_no_cr(f'Analysing file... {100 * (this_row - self.start_from + 1) / num_rows:.0f}%{chr(13)}')
208
+
209
+ messages.message('', False)
210
+
211
+ # Right, so now we have a dictionary of updates grouped by labels. Let's create the queries
212
+ query_count = 0
213
+ for new_labels, updates in query_dictionary.items():
214
+ num_updates_for_this_label = len(updates)
215
+ num_batches = int((num_updates_for_this_label-1)/self.batch_rate) + 1
216
+
217
+ batch_num = 1
218
+ while batch_num <= num_batches:
219
+ if batch_num == num_batches:
220
+ # Last batch, so
221
+ batch_size = 1 + (num_updates_for_this_label-1) % self.batch_rate
222
+ else:
223
+ batch_size = self.batch_rate
224
+
225
+ this_size = 0
226
+ payload = []
227
+ while this_size < batch_size:
228
+ # Combine all the update data...
229
+ update = updates[(batch_num-1)*self.batch_rate + this_size]
230
+ update_properties = update[0] or {}
231
+ func_properties = update[1] or {}
232
+ if self.add_system_properties:
233
+ update_properties[self.update_time_property] = update[2]
234
+ payload.append(update_properties)
235
+ this_size += 1
236
+ query_count += 1
237
+
238
+ formatted_payload = []
239
+ property_count = 0
240
+ for a_payload in payload:
241
+ formatted_payload.append(Properties(a_payload).format(property_ref_dot))
242
+ property_count += len(a_payload)
243
+
244
+ # Build a MERGE query using queries
245
+ merge_labels = batch_keys[new_labels][1]
246
+ remove_labels = batch_keys[new_labels][2]
247
+ if batch_keys[new_labels][0]:
248
+ merge_labels += batch_keys[new_labels][0]
249
+ primary_property = batch_keys[new_labels][3]
250
+ query_text = (QueryBuilder()
251
+ .unwind_list_as(formatted_payload, payload_ref)
252
+ .merge()
253
+ .node_batch(labels=list(set(merge_labels)), ref_name=property_ref, properties=primary_property+':'+payload_ref_dot+primary_property)
254
+ .set_literal(property_ref + ' += ' + payload_ref)
255
+ )
256
+ if len(remove_labels) > 0:
257
+ query_text = query_text.remove_literal(f'{property_ref}:{":".join(remove_labels)}')
258
+ if func_properties:
259
+ query_text = query_text.set_functional(func_properties, property_ref_dot)
260
+ property_count += len(func_properties)
261
+ if self.add_system_properties:
262
+ query_text = query_text.set_literal(id_property + '= CASE WHEN ' + id_property + ' IS null THEN randomUUID() ELSE ' + id_property + ' END')
263
+ query_text = query_text.set_literal(creation_property + '= CASE WHEN ' + creation_property + ' IS null THEN ' + property_ref_dot + self.update_time_property + ' ELSE ' + creation_property + ' END')
264
+
265
+ query_text = query_text.toStr()
266
+ query = Neo4jQuery('batch node', query_text, time_now, batch_size=this_size, property_count=property_count)
267
+ queries.append(query)
268
+ messages.message_no_cr(f'Generating queries... {100 * query_count / dict_size:.0f}%{chr(13)}')
269
+
270
+ if self.validate_only and not self.summarise:
271
+ print(query.text)
272
+
273
+ batch_num += 1
274
+
275
+ messages.message('', False)
276
+ return queries
277
+
278
+ def get_filename(self):
279
+ return self.filename
280
+
281
+ def is_inline_query(self):
282
+ return False
@@ -0,0 +1,129 @@
1
+ import assetmodelutilities as amu
2
+
3
+
4
+ def remove_leading_chars(string, chars):
5
+ if string:
6
+ while string[0] in chars:
7
+ string = string[1:]
8
+ return string
9
+
10
+
11
+ def validate_property(formatted_property):
12
+ # formatted_property might have a data type as a suffix e.g. name:str
13
+ # If it does, we need to only validate the node_key name amd not the whole string
14
+ # Return validated node_key, retaining the data type if provided
15
+ # For example:
16
+ # name:str -> name:str
17
+ # full name -> `full name`
18
+ # full name:str -> `full name`:str
19
+ validated = ''
20
+ property_name = formatted_property.split(':')[0]
21
+ if property_name:
22
+ validated += amu.validate(property_name)
23
+ # try to re-add the type prefix
24
+ try:
25
+ validated += ':' + formatted_property.split(':')[1]
26
+ except:
27
+ # but don't worry if it's missing
28
+ pass
29
+ else:
30
+ # Return what we were given
31
+ validated = formatted_property
32
+
33
+ return validated
34
+
35
+
36
+ def validate_properties(properties):
37
+ # We either get a string, or a list of strings.
38
+ # Process as appropriate and return a string or list of strings to match input
39
+ if properties:
40
+ if isinstance(properties, str):
41
+ return validate_property(properties)
42
+ else:
43
+ return [validate_property(i) for i in properties]
44
+ else:
45
+ return properties
46
+
47
+
48
+ def validate_property_keys(key_list):
49
+ validated = []
50
+ for key in key_list:
51
+ validated += [*validate_properties(i.split(':')[0] for i in key.split(','))]
52
+ return validated
53
+
54
+
55
+ def sort_properties(properties):
56
+ validated = {}
57
+ unwanted = []
58
+ retain = []
59
+ if properties:
60
+ for property in properties:
61
+ for prop in property.strip().split(','):
62
+ # prop has the format key[[:format]:|=value]
63
+ split_prop = prop.replace('=', ':').split(':')
64
+ # Remove leading ! as they are not needed in the key
65
+ valid_key = amu.validate(remove_leading_chars(split_prop[0], '!'))
66
+ if split_prop[0].startswith('!!'):
67
+ retain.append(valid_key)
68
+ else:
69
+ # If we have a format and/or value we have a new property to add
70
+ # ...unless it starts with ! in which case it belongs in the unwanted list (format/value are ignored)
71
+ if len(split_prop) > 1 and not split_prop[0].startswith('!'):
72
+ if len(split_prop) == 3:
73
+ # We have both format and value, so add format to the key so value is properly formatted later
74
+ validated[valid_key+':'+split_prop[1]] = split_prop[2]
75
+ else:
76
+ # Treat second item as the value, because key:format (without a :value) is not supported
77
+ validated[valid_key] = split_prop[1]
78
+ else:
79
+ # Add to the unwanted list
80
+ unwanted.append(valid_key)
81
+
82
+ return validated, unwanted, retain
83
+
84
+
85
+ def combine_required_properties(config_properties, override_properties):
86
+ valid_config_properties, unwanted_config_properties, retain_config_properties = sort_properties(config_properties)
87
+ valid_override_properties, unwanted_override_properties, retain_override_properties = sort_properties(override_properties)
88
+
89
+ config_properties = {i: valid_config_properties[i] for i in valid_config_properties if i not in unwanted_override_properties}
90
+ override_properties = {i: valid_override_properties[i] for i in valid_override_properties}
91
+ required_properties = {**config_properties, **override_properties}
92
+
93
+ unwanted_properties = [i for i in unwanted_override_properties] + [i for i in unwanted_config_properties if i not in valid_override_properties.keys() and i not in retain_override_properties]
94
+
95
+ return required_properties, unwanted_properties
96
+
97
+
98
+ def split_node_key(node):
99
+ split_node = node.split(':')
100
+ if len(split_node) == 1:
101
+ name = node
102
+ type = ''
103
+ else:
104
+ name = split_node[0]
105
+ type = ':' + split_node[1]
106
+ return name, type
107
+
108
+
109
+ def combine_properties(default_properties, actual_properties):
110
+ combined_properties = default_properties.copy()
111
+ for property, value in actual_properties.items():
112
+ if value != '' or property not in default_properties.keys():
113
+ # Either value has been specified in csv
114
+ # or value is blank (can only happen if --allowblanks used) and the property is NOT in the config list
115
+ # (if it is in the list, we use that rather than overriding config value with blank)
116
+ combined_properties[property] = value
117
+
118
+ return combined_properties
119
+
120
+ def split_out_functional_properties(properties):
121
+ explicit = {}
122
+ functional = {}
123
+ for key, value in properties.items():
124
+ if '{' in value and '}' in value:
125
+ functional[key] = value
126
+ else:
127
+ explicit[key] = value
128
+
129
+ return explicit, functional