python-eigen-ingenuity 0.4.35b2__py3-none-any.whl → 0.4.36__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.
- eigeningenuity/assetmodel.py +1 -1
- eigeningenuity/historianmulti.py +2 -2
- eigeningenuity/util.py +1 -1
- modelbuilder/src/assetmodelcsvwriter.py +1 -0
- modelbuilder/src/assetmodelutilities.py +16 -86
- modelbuilder/src/configmanager.py +68 -79
- modelbuilder/src/connectors/httpsconnector.py +11 -13
- modelbuilder/src/csvcontentsmanager.py +84 -38
- modelbuilder/src/csvwriter.py +29 -65
- modelbuilder/src/fileanalyser.py +91 -0
- modelbuilder/src/filemanager.py +40 -29
- modelbuilder/src/processors/__init__.py +0 -0
- modelbuilder/src/processors/cypherfileprocessor.py +73 -0
- modelbuilder/src/processors/labelmanager.py +133 -0
- modelbuilder/src/processors/nodefileprocessor.py +266 -0
- modelbuilder/src/processors/propertymanager.py +87 -0
- modelbuilder/src/processors/relationshipfileprocessor.py +283 -0
- modelbuilder/src/processors/relationshipmanager.py +31 -0
- modelbuilder/src/queries/cypherbuilder.py +3 -15
- modelbuilder/src/queries/typedefs.py +6 -3
- modelbuilder/src/versionmanager.py +13 -11
- {python_eigen_ingenuity-0.4.35b2.dist-info → python_eigen_ingenuity-0.4.36.dist-info}/METADATA +1 -1
- python_eigen_ingenuity-0.4.36.dist-info/RECORD +46 -0
- python_eigen_ingenuity-0.4.35b2.dist-info/RECORD +0 -38
- {python_eigen_ingenuity-0.4.35b2.dist-info → python_eigen_ingenuity-0.4.36.dist-info}/WHEEL +0 -0
- {python_eigen_ingenuity-0.4.35b2.dist-info → python_eigen_ingenuity-0.4.36.dist-info}/entry_points.txt +0 -0
- {python_eigen_ingenuity-0.4.35b2.dist-info → python_eigen_ingenuity-0.4.36.dist-info}/top_level.txt +0 -0
|
File without changes
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import assetmodelutilities as amu
|
|
2
|
+
from queries.query import Neo4jQuery
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class CypherFileProcessor:
|
|
6
|
+
|
|
7
|
+
def __init__(self, filename, config, inline_query=False):
|
|
8
|
+
|
|
9
|
+
self.filename = filename
|
|
10
|
+
self.inline_query = inline_query
|
|
11
|
+
|
|
12
|
+
self.def_path = config.get_default_path()
|
|
13
|
+
self.validate_only = config.in_validation_mode()
|
|
14
|
+
self.summarise = config.in_summary_mode()
|
|
15
|
+
|
|
16
|
+
def process_cypher_file(self):
|
|
17
|
+
|
|
18
|
+
queries = []
|
|
19
|
+
try:
|
|
20
|
+
cypher_file_name = amu.find_file(self.def_path, self.filename)
|
|
21
|
+
cypher_file = open(cypher_file_name, 'r', encoding='UTF8')
|
|
22
|
+
cypher_list = list(cypher_file)
|
|
23
|
+
file_queries = 0
|
|
24
|
+
next_query = ''
|
|
25
|
+
cypher_file_size = len(cypher_list)
|
|
26
|
+
entry_count = 0
|
|
27
|
+
while entry_count < cypher_file_size:
|
|
28
|
+
this_entry = cypher_list[entry_count].strip()
|
|
29
|
+
if not this_entry.startswith('#') and not this_entry == '\n':
|
|
30
|
+
next_query += this_entry + '\n'
|
|
31
|
+
if not this_entry.endswith(',') and not this_entry.endswith('\\'):
|
|
32
|
+
while next_query.endswith('\n'):
|
|
33
|
+
next_query = next_query[0:-1]
|
|
34
|
+
query = Neo4jQuery('cypher', next_query.replace('\\', '').strip())
|
|
35
|
+
next_query = ''
|
|
36
|
+
if self.validate_only and not self.summarise:
|
|
37
|
+
print(query.text)
|
|
38
|
+
queries.append(query)
|
|
39
|
+
file_queries += 1
|
|
40
|
+
entry_count += 1
|
|
41
|
+
|
|
42
|
+
# Process any leftover query at the end of the file
|
|
43
|
+
# This shouldn't normally happen, but will if the last entry ends with a comma or backslash
|
|
44
|
+
# Any trailing backslash is removed, so the query may be OK but will be missing any continuation clause(s)
|
|
45
|
+
# But a trailing comma will be left - result is a query that will generate an error if executed!
|
|
46
|
+
# This is done intentionally as the query in the input file is incomplete
|
|
47
|
+
if next_query != '':
|
|
48
|
+
query = Neo4jQuery('cypher', next_query[0:-1].replace('\\', '').strip())
|
|
49
|
+
if self.validate_only and not self.summarise:
|
|
50
|
+
print(query.text)
|
|
51
|
+
queries.append(query)
|
|
52
|
+
file_queries += 1
|
|
53
|
+
|
|
54
|
+
cypher_file.close()
|
|
55
|
+
except:
|
|
56
|
+
print(f'{self.filename} not found')
|
|
57
|
+
|
|
58
|
+
return queries
|
|
59
|
+
|
|
60
|
+
def process_query_list(self):
|
|
61
|
+
|
|
62
|
+
queries = []
|
|
63
|
+
query = Neo4jQuery('query', self.filename)
|
|
64
|
+
if self.validate_only and not self.summarise:
|
|
65
|
+
print(query.text)
|
|
66
|
+
queries.append(query)
|
|
67
|
+
return queries
|
|
68
|
+
|
|
69
|
+
def get_filename(self):
|
|
70
|
+
return self.filename
|
|
71
|
+
|
|
72
|
+
def is_inline_query(self):
|
|
73
|
+
return self.inline_query
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import assetmodelutilities as amu
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def validate_label_string(label_string):
|
|
5
|
+
# The input string can both comma and colon separated.
|
|
6
|
+
# We will return a string of colon separated labels, but without a leading colon
|
|
7
|
+
# Labels that do not conform to Neo4j naming rules will be back-ticked (`)
|
|
8
|
+
# For example:
|
|
9
|
+
# fruit --> fruit
|
|
10
|
+
# apple,tree --> apple:tree
|
|
11
|
+
# birds&bees --> `birds&bees`
|
|
12
|
+
# one,two:!three --> one:two:!three
|
|
13
|
+
|
|
14
|
+
result = ''
|
|
15
|
+
for a_label in label_string.replace(',', ':').split(':'):
|
|
16
|
+
# Labels may be prefixed with '!' or '!!'. Check for these before validating
|
|
17
|
+
if a_label.startswith('!!'):
|
|
18
|
+
result += ':!!' + amu.validate(a_label[2:])
|
|
19
|
+
elif a_label.startswith('!'):
|
|
20
|
+
result += ':!' + amu.validate(a_label[1:])
|
|
21
|
+
elif a_label:
|
|
22
|
+
result += ':' + amu.validate(a_label)
|
|
23
|
+
|
|
24
|
+
# Remove the leading ':' from the result because we don't want it
|
|
25
|
+
if result.startswith(':'):
|
|
26
|
+
result = result[1:]
|
|
27
|
+
|
|
28
|
+
return result
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def validate_labels(labels):
|
|
32
|
+
# We either get a string, or a list of strings.
|
|
33
|
+
# Process as appropriate and return a string or list of strings to match input
|
|
34
|
+
if isinstance(labels, str):
|
|
35
|
+
return validate_label_string(labels)
|
|
36
|
+
else:
|
|
37
|
+
return [validate_label_string(i) for i in labels]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def validate_label_string_to_list(labels):
|
|
41
|
+
# Validate a single input string as normal
|
|
42
|
+
# But return the result as a list of the individual labels (without colons) instead of the combined string
|
|
43
|
+
validated_string = validate_label_string(labels)
|
|
44
|
+
validated_list = [i for i in validated_string.split(':') if i != '']
|
|
45
|
+
return list(set(validated_list))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def sort_labels(required_labels, labels):
|
|
49
|
+
# So this is where the real magic happens
|
|
50
|
+
# Combine the various sets of input labels into 4 output lists
|
|
51
|
+
# merge_labels - labels used in the MERGE clause
|
|
52
|
+
# new_labels - labels added to SET clause
|
|
53
|
+
# remove labels - labels that are removed from a node via a REMOVE clause
|
|
54
|
+
# relationship labels - used in the MATCH clause to find the start/end nodes of a relationship (not used for node updates)
|
|
55
|
+
|
|
56
|
+
# First off, make a list of all the unique labels given to us
|
|
57
|
+
# Note the string(s) in the input may contain more than one label, hence the split by colon
|
|
58
|
+
unique_labels = []
|
|
59
|
+
for label in labels:
|
|
60
|
+
for split_label in label.replace(',', ':').split(':'):
|
|
61
|
+
if split_label and split_label not in unique_labels:
|
|
62
|
+
unique_labels.append(split_label)
|
|
63
|
+
|
|
64
|
+
# Now the first sort
|
|
65
|
+
wanted_label_list = [] # Labels that are required
|
|
66
|
+
unwanted_label_list = [] # Labels that are not wanted (i.e. prefixed with ! in the required label list)
|
|
67
|
+
remove_label_list = [] # Labels that are to be removed (i.e. prefixed with ! in the .csv file)
|
|
68
|
+
for label in unique_labels:
|
|
69
|
+
if label.startswith('!!'):
|
|
70
|
+
wanted_label_list.append(label[2:])
|
|
71
|
+
elif not ('!'+label in required_labels or label.startswith('!')):
|
|
72
|
+
wanted_label_list.append(label)
|
|
73
|
+
elif label.startswith('!'):
|
|
74
|
+
remove_label_list.append(label[1:])
|
|
75
|
+
else:
|
|
76
|
+
unwanted_label_list.append('!' + label)
|
|
77
|
+
|
|
78
|
+
return wanted_label_list, unwanted_label_list, remove_label_list
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def sort_node_labels(required_labels, input_labels):
|
|
82
|
+
wanted_label_list, unwanted_label_list, remove_label_list = sort_labels(required_labels, input_labels)
|
|
83
|
+
# MERGE clause: required labels without !
|
|
84
|
+
merge_labels = [i for i in required_labels if (not ('!' + i in unwanted_label_list) and not (i.startswith('!')))]
|
|
85
|
+
# Labels to add via the SET clause: specified in the csv file
|
|
86
|
+
new_labels = [i for i in wanted_label_list if i not in merge_labels]
|
|
87
|
+
|
|
88
|
+
return merge_labels, new_labels, remove_label_list
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def sort_relationship_labels(required_labels, input_labels):
|
|
92
|
+
wanted_label_list, unwanted_label_list, remove_label_list = sort_labels(required_labels, input_labels)
|
|
93
|
+
# Labels used in the relationship MATCH queries
|
|
94
|
+
relationship_labels = [i for i in required_labels + wanted_label_list if (not ('!' + i in unwanted_label_list) and not (i in remove_label_list) and not (i.startswith('!')))]
|
|
95
|
+
|
|
96
|
+
return relationship_labels
|
|
97
|
+
|
|
98
|
+
def sort_output_labels(node_labels, version_labels):
|
|
99
|
+
# We need the specifed node labels, but not any version labels
|
|
100
|
+
# This means that any version nodes will not be processed
|
|
101
|
+
input_labels = node_labels + ['!' + label for label in version_labels if not label.startswith('!')]
|
|
102
|
+
# Use the same sort algorithm, but we don't use any required labels
|
|
103
|
+
wanted_label_list, unwanted_label_list, remove_label_list = sort_labels('', input_labels)
|
|
104
|
+
|
|
105
|
+
return wanted_label_list, remove_label_list
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def strip_quotes(quote_list):
|
|
109
|
+
stripped_list = [i[1:-1].replace('``', '`') if i.startswith('`') and i.endswith('`') else i for i in quote_list]
|
|
110
|
+
return stripped_list
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def merge_labels(self, required_labels, unrequired_labels, remove_labels):
|
|
114
|
+
merged_labels = [i for i in self.required_labels + required_labels if (not ('!' + i in unrequired_labels) and not (i in remove_labels) and not (i.startswith('!')))]
|
|
115
|
+
return merged_labels
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def combine_required_labels(config_labels, override_labels):
|
|
119
|
+
valid_config_labels = validate_labels(config_labels)
|
|
120
|
+
valid_override_labels = validate_label_string_to_list(override_labels)
|
|
121
|
+
required_labels = [i for i in valid_config_labels if (not(i.startswith('!') and ('!'+i not in valid_config_labels)) and ('!'+i not in valid_override_labels))] + [i for i in valid_override_labels if (not(i.startswith('!')) and ('!'+i not in valid_override_labels))]
|
|
122
|
+
unwanted_labels = [i for i in valid_config_labels if (i.startswith('!') and (i[1:] not in valid_override_labels) and ('!' + i not in valid_override_labels))] + [i for i in valid_override_labels if i.startswith('!') and not i.startswith('!!')]
|
|
123
|
+
|
|
124
|
+
combined_list = list(set(required_labels + unwanted_labels))
|
|
125
|
+
if '' in combined_list:
|
|
126
|
+
combined_list.remove('')
|
|
127
|
+
|
|
128
|
+
return combined_list
|
|
129
|
+
|
|
130
|
+
def validate_output_labels(label_list):
|
|
131
|
+
output_list = ['`'+label+'`' if label.startswith('!') else label for label in label_list]
|
|
132
|
+
return ':'.join(output_list)
|
|
133
|
+
|
|
@@ -0,0 +1,266 @@
|
|
|
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
|
+
|
|
9
|
+
|
|
10
|
+
class NodeFileProcessor:
|
|
11
|
+
|
|
12
|
+
def __init__(self, filename, config, contents):
|
|
13
|
+
self.filename = filename
|
|
14
|
+
self.contents = contents
|
|
15
|
+
|
|
16
|
+
self.primary_property = config.get_primary_property()
|
|
17
|
+
self.add_system_properties = not config.get_no_system_properties()
|
|
18
|
+
|
|
19
|
+
self.required_labels = config.get_required_labels()
|
|
20
|
+
self.required_properties = config.get_required_properties_dict()
|
|
21
|
+
self.unrequired_properties = config.get_unrequired_properties_list()
|
|
22
|
+
self.unique_id_property = config.get_unique_id_property()
|
|
23
|
+
self.creation_time_property = config.get_creation_time_property()
|
|
24
|
+
self.update_time_property = config.get_update_time_property()
|
|
25
|
+
|
|
26
|
+
self.label_alias = config.get_label_alias()
|
|
27
|
+
self.node_alias = config.get_node_alias()
|
|
28
|
+
|
|
29
|
+
self.validate_only = config.in_validation_mode()
|
|
30
|
+
self.summarise = config.in_summary_mode()
|
|
31
|
+
|
|
32
|
+
self.allow_blanks = config.get_allow_blanks()
|
|
33
|
+
self.batch_rate = config.get_batch_rate()
|
|
34
|
+
self.start_from = config.get_start_from()
|
|
35
|
+
self.last_row = config.get_end_at()
|
|
36
|
+
|
|
37
|
+
self.node_types = amu.node_types
|
|
38
|
+
self.label_types = amu.label_types
|
|
39
|
+
self.all_types = amu.all_types
|
|
40
|
+
|
|
41
|
+
def get_last_row(self, num_rows):
|
|
42
|
+
if self.last_row < 0:
|
|
43
|
+
last_row = num_rows
|
|
44
|
+
else:
|
|
45
|
+
last_row = min(num_rows, self.last_row)
|
|
46
|
+
return last_row
|
|
47
|
+
|
|
48
|
+
def process_node_file(self):
|
|
49
|
+
|
|
50
|
+
# For each line in the file, build a cypher query to MERGE a node
|
|
51
|
+
# Each query is added to a list of queries, which are returned to the caller. They are NOT actioned here
|
|
52
|
+
|
|
53
|
+
num_data_rows = self.contents.get_row_count()
|
|
54
|
+
node_columns = self.contents.get_column_numbers_list(self.node_alias, self.node_types)
|
|
55
|
+
label_columns = self.contents.get_column_numbers_list(self.label_alias, self.label_types)
|
|
56
|
+
property_columns = self.contents.get_other_column_numbers_list(self.node_alias + self.label_alias, self.all_types)
|
|
57
|
+
|
|
58
|
+
queries = []
|
|
59
|
+
|
|
60
|
+
num_rows = num_data_rows - self.start_from + 1
|
|
61
|
+
this_row = self.start_from-1
|
|
62
|
+
last_row = self.get_last_row(num_data_rows)
|
|
63
|
+
property_ref = 'n'
|
|
64
|
+
property_ref_dot = property_ref + '.'
|
|
65
|
+
|
|
66
|
+
while this_row < last_row:
|
|
67
|
+
validated_csv_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, label_columns))
|
|
68
|
+
merge_labels, new_labels, remove_labels = lm.sort_node_labels(self.required_labels, validated_csv_labels)
|
|
69
|
+
properties = self.contents.get_property_values_dict(this_row, property_columns, self.allow_blanks)
|
|
70
|
+
if len(self.required_properties) > 0:
|
|
71
|
+
properties = {**self.required_properties, **properties}
|
|
72
|
+
# Remove any properties we don't want
|
|
73
|
+
for property in self.unrequired_properties:
|
|
74
|
+
try:
|
|
75
|
+
properties.pop(property)
|
|
76
|
+
except:
|
|
77
|
+
pass
|
|
78
|
+
for k in list(properties):
|
|
79
|
+
if k.startswith(':'):
|
|
80
|
+
properties.pop(k)
|
|
81
|
+
else:
|
|
82
|
+
properties[property_ref_dot + k] = properties.pop(k)
|
|
83
|
+
|
|
84
|
+
for primary_property in self.contents.get_property_values_list(this_row, node_columns, False, self.primary_property):
|
|
85
|
+
time_now = amu.get_formatted_time_now()
|
|
86
|
+
|
|
87
|
+
# Build a simple MERGE query using queries
|
|
88
|
+
query_text = (QueryBuilder()
|
|
89
|
+
.merge()
|
|
90
|
+
.node(labels=merge_labels, ref_name=property_ref, properties=Properties(primary_property))
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
# Add in System properties if required
|
|
94
|
+
if self.add_system_properties:
|
|
95
|
+
new_uuid = str(uuid.uuid4())
|
|
96
|
+
create_time = property_ref_dot + self.creation_time_property + '=datetime("' + time_now + '")'
|
|
97
|
+
id_property = property_ref_dot + self.unique_id_property
|
|
98
|
+
update_id = id_property + '= CASE WHEN ' + id_property + ' IS null THEN "' + new_uuid + '" ELSE ' + id_property + ' END,' +\
|
|
99
|
+
property_ref_dot + self.update_time_property + '=datetime("' + time_now + '")'
|
|
100
|
+
query_text = query_text.on_create().set_literal(create_time).set_literal(update_id)
|
|
101
|
+
|
|
102
|
+
if len(new_labels) > 0:
|
|
103
|
+
query_text = query_text.set_labels(ref_name=property_ref, labels=new_labels)
|
|
104
|
+
if len(remove_labels) > 0:
|
|
105
|
+
query_text = query_text.remove_literal(f'{property_ref}:{":".join(remove_labels)}')
|
|
106
|
+
if len(properties) > 0:
|
|
107
|
+
query_text = query_text.set(properties=properties)
|
|
108
|
+
|
|
109
|
+
query_text = query_text.toStr()
|
|
110
|
+
|
|
111
|
+
query = Neo4jQuery('node', query_text, time_now, merge_labels, new_labels, primary_property, properties)
|
|
112
|
+
|
|
113
|
+
if self.validate_only and not self.summarise:
|
|
114
|
+
print(query.text)
|
|
115
|
+
|
|
116
|
+
queries.append(query)
|
|
117
|
+
|
|
118
|
+
this_row += 1
|
|
119
|
+
messages.message_no_cr(f'Generating queries... {100 * (this_row - self.start_from + 1) / num_rows:.0f}%{chr(13)}')
|
|
120
|
+
|
|
121
|
+
messages.message('', False)
|
|
122
|
+
return queries
|
|
123
|
+
|
|
124
|
+
def process_node_file_batch(self):
|
|
125
|
+
|
|
126
|
+
# For each line in the file, build a cypher query to MERGE a node
|
|
127
|
+
# Each query is added to a list of queries, which are returned to the caller. They are NOT actioned here
|
|
128
|
+
|
|
129
|
+
num_data_rows = self.contents.get_row_count()
|
|
130
|
+
node_columns = self.contents.get_column_numbers_list(self.node_alias, self.node_types)
|
|
131
|
+
label_columns = self.contents.get_column_numbers_list(self.label_alias, self.label_types)
|
|
132
|
+
property_columns = self.contents.get_other_column_numbers_list(self.node_alias + self.label_alias, self.all_types)
|
|
133
|
+
|
|
134
|
+
queries = []
|
|
135
|
+
|
|
136
|
+
num_rows = num_data_rows - self.start_from + 1
|
|
137
|
+
this_row = self.start_from-1
|
|
138
|
+
last_row = self.get_last_row(num_data_rows)
|
|
139
|
+
dict_size = 0
|
|
140
|
+
property_ref = 'n'
|
|
141
|
+
property_ref_dot = property_ref + '.'
|
|
142
|
+
payload_ref = 'payload'
|
|
143
|
+
payload_ref_dot = payload_ref + '.'
|
|
144
|
+
|
|
145
|
+
time_now = amu.get_formatted_time_now()
|
|
146
|
+
|
|
147
|
+
# For best optimisation, we need to create a batch query for all the update with the same labels
|
|
148
|
+
# So we will create a dictionary of queries based on the labels for each query
|
|
149
|
+
# 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
|
|
150
|
+
# When we have worked through all the input and grouped the queries by label combination, we will create the queries
|
|
151
|
+
|
|
152
|
+
batch_keys = []
|
|
153
|
+
query_dictionary = {}
|
|
154
|
+
id_property = property_ref_dot + self.unique_id_property
|
|
155
|
+
creation_property = property_ref_dot + self.creation_time_property
|
|
156
|
+
|
|
157
|
+
while this_row < last_row:
|
|
158
|
+
validated_csv_labels = lm.validate_labels(self.contents.get_column_values_list(this_row, label_columns))
|
|
159
|
+
merge_labels, new_labels, remove_labels = lm.sort_node_labels(self.required_labels, validated_csv_labels)
|
|
160
|
+
row_properties = self.contents.get_property_values_dict(this_row, property_columns, allow_blanks=self.allow_blanks)
|
|
161
|
+
|
|
162
|
+
for primary_property in self.contents.get_property_values_list(this_row, node_columns, False, self.primary_property):
|
|
163
|
+
if len(self.required_properties) > 0:
|
|
164
|
+
properties = {**self.required_properties, **row_properties}
|
|
165
|
+
else:
|
|
166
|
+
properties = row_properties.copy()
|
|
167
|
+
# Remove any properties we don't want
|
|
168
|
+
for property in self.unrequired_properties:
|
|
169
|
+
try:
|
|
170
|
+
properties.pop(property)
|
|
171
|
+
except:
|
|
172
|
+
pass
|
|
173
|
+
properties = {**properties, **primary_property}
|
|
174
|
+
|
|
175
|
+
for k in list(properties):
|
|
176
|
+
if k.startswith(':'):
|
|
177
|
+
properties.pop(k)
|
|
178
|
+
|
|
179
|
+
query_data = [properties, time_now]
|
|
180
|
+
|
|
181
|
+
# So now we know all about this particular update. Let's put it in the query dictionary
|
|
182
|
+
# We'll group these by unique combinations of labels and primary key type
|
|
183
|
+
# Have to use a list since a dictionary key can't be a dictionary
|
|
184
|
+
dictionary_key = [new_labels, merge_labels, remove_labels, list(primary_property)[0].split(':')[0]]
|
|
185
|
+
try:
|
|
186
|
+
dictionary_index = batch_keys.index(dictionary_key)
|
|
187
|
+
query_dictionary[dictionary_index].append(query_data)
|
|
188
|
+
except:
|
|
189
|
+
dictionary_index = len(batch_keys)
|
|
190
|
+
query_dictionary[dictionary_index] = [query_data]
|
|
191
|
+
batch_keys.append(dictionary_key)
|
|
192
|
+
dict_size += 1
|
|
193
|
+
|
|
194
|
+
this_row += 1
|
|
195
|
+
messages.message_no_cr(f'Analysing file... {100 * (this_row - self.start_from + 1) / num_rows:.0f}%{chr(13)}')
|
|
196
|
+
|
|
197
|
+
messages.message('', False)
|
|
198
|
+
|
|
199
|
+
# Right, so now we have a dictionary of updates grouped by labels. Let's create the queries
|
|
200
|
+
query_count = 0
|
|
201
|
+
for new_labels, updates in query_dictionary.items():
|
|
202
|
+
num_updates_for_this_label = len(updates)
|
|
203
|
+
num_batches = int((num_updates_for_this_label-1)/self.batch_rate) + 1
|
|
204
|
+
|
|
205
|
+
batch_num = 1
|
|
206
|
+
while batch_num <= num_batches:
|
|
207
|
+
if batch_num == num_batches:
|
|
208
|
+
# Last batch, so
|
|
209
|
+
batch_size = 1 + (num_updates_for_this_label-1) % self.batch_rate
|
|
210
|
+
else:
|
|
211
|
+
batch_size = self.batch_rate
|
|
212
|
+
|
|
213
|
+
this_size = 0
|
|
214
|
+
payload = []
|
|
215
|
+
while this_size < batch_size:
|
|
216
|
+
# Combine all the update data...
|
|
217
|
+
update = updates[(batch_num-1)*self.batch_rate + this_size]
|
|
218
|
+
update_properties = update[0] or {}
|
|
219
|
+
if self.add_system_properties:
|
|
220
|
+
update_properties[self.update_time_property] = update[1]
|
|
221
|
+
payload.append(update_properties)
|
|
222
|
+
this_size += 1
|
|
223
|
+
query_count += 1
|
|
224
|
+
|
|
225
|
+
formatted_payload = []
|
|
226
|
+
property_count = 0
|
|
227
|
+
for a_payload in payload:
|
|
228
|
+
formatted_payload.append(Properties(a_payload).format())
|
|
229
|
+
property_count += len(a_payload)
|
|
230
|
+
|
|
231
|
+
# Build a MERGE query using queries
|
|
232
|
+
merge_labels = batch_keys[new_labels][1]
|
|
233
|
+
remove_labels = batch_keys[new_labels][2]
|
|
234
|
+
if batch_keys[new_labels][0]:
|
|
235
|
+
merge_labels += batch_keys[new_labels][0]
|
|
236
|
+
primary_property = batch_keys[new_labels][3]
|
|
237
|
+
query_text = (QueryBuilder()
|
|
238
|
+
.unwind_list_as(formatted_payload, payload_ref)
|
|
239
|
+
.merge()
|
|
240
|
+
.node_batch(labels=list(set(merge_labels)), ref_name=property_ref, properties=primary_property+':'+payload_ref_dot+primary_property)
|
|
241
|
+
.set_literal(property_ref + ' += ' + payload_ref)
|
|
242
|
+
)
|
|
243
|
+
if len(remove_labels) > 0:
|
|
244
|
+
query_text = query_text.remove_literal(f'{property_ref}:{":".join(remove_labels)}')
|
|
245
|
+
if self.add_system_properties:
|
|
246
|
+
query_text = query_text.set_literal(id_property + '= CASE WHEN ' + id_property + ' IS null THEN randomUUID() ELSE ' + id_property + ' END')
|
|
247
|
+
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')
|
|
248
|
+
|
|
249
|
+
query_text = query_text.toStr()
|
|
250
|
+
query = Neo4jQuery('batch node', query_text, time_now, batch_size=this_size, property_count=property_count)
|
|
251
|
+
queries.append(query)
|
|
252
|
+
messages.message_no_cr(f'Generating queries... {100 * query_count / dict_size:.0f}%{chr(13)}')
|
|
253
|
+
|
|
254
|
+
if self.validate_only and not self.summarise:
|
|
255
|
+
print(query.text)
|
|
256
|
+
|
|
257
|
+
batch_num += 1
|
|
258
|
+
|
|
259
|
+
messages.message('', False)
|
|
260
|
+
return queries
|
|
261
|
+
|
|
262
|
+
def get_filename(self):
|
|
263
|
+
return self.filename
|
|
264
|
+
|
|
265
|
+
def is_inline_query(self):
|
|
266
|
+
return False
|
|
@@ -0,0 +1,87 @@
|
|
|
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
|
+
def sort_properties(properties):
|
|
48
|
+
validated = {}
|
|
49
|
+
unwanted = []
|
|
50
|
+
retain = []
|
|
51
|
+
if properties:
|
|
52
|
+
for property in properties:
|
|
53
|
+
for prop in property.strip().split(','):
|
|
54
|
+
# prop has the format key[[:format]:|=value]
|
|
55
|
+
split_prop = prop.replace('=', ':').split(':')
|
|
56
|
+
# Remove leading ! as they are not needed in the key
|
|
57
|
+
valid_key = amu.validate(remove_leading_chars(split_prop[0], '!'))
|
|
58
|
+
if split_prop[0].startswith('!!'):
|
|
59
|
+
retain.append(valid_key)
|
|
60
|
+
else:
|
|
61
|
+
# If we have a format and/or value we have a new property to add
|
|
62
|
+
# ...unless it starts with ! in which case it belongs in the unwanted list (format/value are ignored)
|
|
63
|
+
if len(split_prop) > 1 and not split_prop[0].startswith('!'):
|
|
64
|
+
if len(split_prop) == 3:
|
|
65
|
+
# We have both format and value, so add format to the key so value is properly formatted later
|
|
66
|
+
validated[valid_key+':'+split_prop[1]] = split_prop[2]
|
|
67
|
+
else:
|
|
68
|
+
# Treat second item as the value, because key:format (without a :value) is not supported
|
|
69
|
+
validated[valid_key] = split_prop[1]
|
|
70
|
+
else:
|
|
71
|
+
# Add to the unwanted list
|
|
72
|
+
unwanted.append(valid_key)
|
|
73
|
+
|
|
74
|
+
return validated, unwanted, retain
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def combine_required_properties(config_properties, override_properties):
|
|
78
|
+
valid_config_properties, unwanted_config_properties, retain_config_properties = sort_properties(config_properties)
|
|
79
|
+
valid_override_properties, unwanted_override_properties, retain_override_properties = sort_properties(override_properties)
|
|
80
|
+
|
|
81
|
+
config_properties = {i: valid_config_properties[i] for i in valid_config_properties if i not in unwanted_override_properties}
|
|
82
|
+
override_properties = {i: valid_override_properties[i] for i in valid_override_properties}
|
|
83
|
+
required_properties = {**config_properties, **override_properties}
|
|
84
|
+
|
|
85
|
+
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]
|
|
86
|
+
|
|
87
|
+
return required_properties, unwanted_properties
|