Skip to content

Commit 7a9ea6e

Browse files
authored
Merge pull request #425 from benjaminysmith/remove_explicit_count
Remove seperate row count from csv upload
2 parents b99e9d2 + 68b6d4c commit 7a9ea6e

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

src/acquisition/covidcast/csv_to_database.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,11 @@ def upload_archive(
9797
filename). If boolean, whether to force WIP status (True) or
9898
production status (False) regardless of what the filename says
9999
100+
:return: the number of modified rows
100101
"""
101102
archive_as_successful, archive_as_failed = handlers
102-
103+
total_modified_row_count = 0
104+
103105
# iterate over each file
104106
for path, details in path_details:
105107
print('handling ', path)
@@ -127,18 +129,19 @@ def upload_archive(
127129
all_rows_valid = rows_list and all(r is not None for r in rows_list)
128130
if all_rows_valid:
129131
try:
130-
result = database.insert_or_update_bulk(rows_list)
131-
print(f"insert_or_update_bulk {filename} returned {result}")
132+
modified_row_count = database.insert_or_update_bulk(rows_list)
133+
print(f"insert_or_update_bulk {filename} returned {modified_row_count}")
132134
logger.info(
133135
"Inserted database rows",
134-
row_count = result,
136+
row_count = modified_row_count,
135137
source = source,
136138
signal = signal,
137139
geo_type = geo_type,
138140
time_value = time_value,
139141
issue = issue,
140142
lag = lag)
141-
if result is None or result: # else would indicate zero rows inserted
143+
if modified_row_count is None or modified_row_count: # else would indicate zero rows inserted
144+
total_modified_row_count += (modified_row_count if modified_row_count else 0)
142145
database.commit()
143146
except Exception as e:
144147
all_rows_valid = False
@@ -150,6 +153,9 @@ def upload_archive(
150153
archive_as_successful(path_src, filename, source)
151154
else:
152155
archive_as_failed(path_src, filename, source)
156+
157+
return total_modified_row_count
158+
153159

154160
def main(
155161
args,
@@ -180,25 +186,19 @@ def main(
180186

181187
database = database_impl()
182188
database.connect()
183-
num_starting_rows = database.count_all_rows()
184189

185190
try:
186-
upload_archive_impl(
191+
modified_row_count = upload_archive_impl(
187192
path_details,
188193
database,
189194
make_handlers(args.data_dir, args.specific_issue_date),
190195
logger,
191196
is_wip_override=wip_override)
197+
logger.info("Finished inserting database rows", row_count = modified_row_count)
198+
print('inserted/updated %d rows' % modified_row_count)
192199
finally:
193-
# no catch block so that an exception above will cause the program to fail
194-
# after the following cleanup
195-
try:
196-
num_inserted_rows = database.count_all_rows() - num_starting_rows
197-
logger.info("Finished inserting database rows", row_count = num_inserted_rows)
198-
print('inserted/updated %d rows' % num_inserted_rows)
199-
finally:
200-
# unconditionally commit database changes since CSVs have been archived
201-
database.disconnect(True)
200+
# unconditionally commit database changes since CSVs have been archived
201+
database.disconnect(True)
202202

203203
logger.info(
204204
"Ingested CSVs into database",

tests/acquisition/covidcast/test_csv_to_database.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,21 @@ def load_csv_impl(path, *args):
7373

7474
data_dir = 'data_dir'
7575
mock_database = MagicMock()
76+
mock_database.insert_or_update_bulk.return_value = 2
7677
mock_csv_importer = MagicMock()
7778
mock_csv_importer.load_csv = load_csv_impl
7879
mock_file_archiver = MagicMock()
7980
mock_logger = MagicMock()
8081

81-
upload_archive(
82+
modified_row_count = upload_archive(
8283
self._path_details(),
8384
mock_database,
8485
make_handlers(data_dir, False,
8586
file_archiver_impl=mock_file_archiver),
8687
mock_logger,
8788
csv_importer_impl=mock_csv_importer)
8889

90+
self.assertEqual(modified_row_count, 4)
8991
# verify that appropriate rows were added to the database
9092
self.assertEqual(mock_database.insert_or_update_bulk.call_count, 2)
9193
call_args_list = mock_database.insert_or_update_bulk.call_args_list

0 commit comments

Comments
 (0)