Skip to content

Commit 9bace4e

Browse files
committed
Reduce number of db calls
1 parent ea29322 commit 9bace4e

File tree

1 file changed

+14
-38
lines changed

1 file changed

+14
-38
lines changed

tagbase_server/tagbase_server/utils/processing_utils.py

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717

1818
def process_all_lines_for_global_attributes(
19-
global_attributes_lines, cur, submission_id, metadata, submission_filename, line_counter
19+
global_attributes_lines,
20+
cur,
21+
submission_id,
22+
metadata,
23+
submission_filename,
24+
line_counter,
2025
):
2126
attrbs_map = {}
2227
for line in global_attributes_lines:
@@ -29,8 +34,13 @@ def process_all_lines_for_global_attributes(
2934
else:
3035
logger.warning("Metadata line %s NOT in expected format!", line)
3136

32-
attrbs_names = ', '.join(['\'{}\''.format(attrib_name) for attrib_name in attrbs_map.keys()])
33-
attrbs_ids_query = "SELECT attribute_id, attribute_name FROM metadata_types WHERE attribute_name IN ({})".format(attrbs_names)
37+
attrbs_names = ", ".join(
38+
["'{}'".format(attrib_name) for attrib_name in attrbs_map.keys()]
39+
)
40+
attrbs_ids_query = (
41+
"SELECT attribute_id, attribute_name FROM metadata_types "
42+
"WHERE attribute_name IN ({})".format(attrbs_names)
43+
)
3444
logger.debug("Query=%s", attrbs_ids_query)
3545
cur.execute(attrbs_ids_query)
3646
rows = cur.fetchall()
@@ -51,29 +61,6 @@ def process_all_lines_for_global_attributes(
5161
post_msg_to_slack(msg)
5262

5363

54-
def process_line_for_global_attributes(
55-
line, cur, submission_id, metadata, submission_filename, line_counter
56-
):
57-
logger.debug("Processing global attribute: %s", line)
58-
tokens = line.strip()[1:].split(" = ")
59-
logger.debug("Processing token: %s", tokens)
60-
cur.execute(
61-
"SELECT attribute_id FROM metadata_types WHERE attribute_name = %s",
62-
(tokens[0],),
63-
)
64-
rows = cur.fetchall()
65-
if len(rows) == 0:
66-
msg = (
67-
f"*{submission_filename}* _line:{line_counter}_ - "
68-
f"Unable to locate attribute_name *{tokens[0]}* in _metadata_types_ table."
69-
)
70-
post_msg(msg)
71-
else:
72-
str_submission_id = str(submission_id)
73-
str_row = str(rows[0][0])
74-
metadata.append((str_submission_id, str_row, tokens[1]))
75-
76-
7764
def post_msg_to_slack(msg):
7865
logger.warning(msg)
7966
try:
@@ -104,19 +91,8 @@ def process_global_attributes(lines, cur, submission_id, metadata, submission_fi
10491
submission_id,
10592
metadata,
10693
submission_filename,
107-
processed_lines
94+
processed_lines,
10895
)
109-
# for global_attribute in global_attributes:
110-
# process_line_for_global_attributes(
111-
# global_attribute,
112-
# cur,
113-
# submission_id,
114-
# metadata,
115-
# submission_filename,
116-
# processed_lines,
117-
# )
118-
119-
# returning -1 because lines is an 0-indexed array
12096
return processed_lines - 1 if processed_lines > 0 else 0
12197

12298

0 commit comments

Comments
 (0)