-
Notifications
You must be signed in to change notification settings - Fork 67
covid_hosp improvements to address and investigate long update running times #1083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2919a58
Move logger module to acquisition.common
krivard a55cc10
Convert covid_hosp to use structured logger
krivard 158d8aa
[covid_hosp:f] Fix geocode acquisition and running time.
krivard c1db533
Merge remote-tracking branch 'origin/dev' into krivard/covid_hosp-fac…
krivard 221e53d
Move logger construction into Database base class
krivard 2544fa3
Apply suggestions from code review
krivard 5d8f3fc
Merge branch 'krivard/covid_hosp-facility-running-time' of github.com…
krivard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,7 +124,7 @@ def contains_revision(self, revision): | |
for (result,) in cursor: | ||
return bool(result) | ||
|
||
def insert_metadata(self, publication_date, revision, meta_json): | ||
def insert_metadata(self, publication_date, revision, meta_json, logger=False): | ||
"""Add revision metadata to the database. | ||
|
||
Parameters | ||
|
@@ -135,6 +135,8 @@ def insert_metadata(self, publication_date, revision, meta_json): | |
Unique revision string. | ||
meta_json : str | ||
Metadata serialized as a JSON string. | ||
logger structlog.Logger [optional; default False] | ||
Logger to receive messages | ||
""" | ||
|
||
with self.new_cursor() as cursor: | ||
|
@@ -152,7 +154,7 @@ def insert_metadata(self, publication_date, revision, meta_json): | |
(%s, %s, %s, %s, %s, NOW()) | ||
''', (self.table_name, self.hhs_dataset_id, publication_date, revision, meta_json)) | ||
|
||
def insert_dataset(self, publication_date, dataframe): | ||
def insert_dataset(self, publication_date, dataframe, logger=False): | ||
"""Add a dataset to the database. | ||
|
||
Parameters | ||
|
@@ -161,6 +163,8 @@ def insert_dataset(self, publication_date, dataframe): | |
Date when the dataset was published in YYYYMMDD format. | ||
dataframe : pandas.DataFrame | ||
The dataset. | ||
logger structlog.Logger [optional; default False] | ||
Logger to receive messages. | ||
""" | ||
dataframe_columns_and_types = [ | ||
x for x in self.columns_and_types.values() if x.csv_name in dataframe.columns | ||
|
@@ -181,18 +185,38 @@ def nan_safe_dtype(dtype, value): | |
sql = f'INSERT INTO `{self.table_name}` (`id`, `{self.publication_col_name}`, {columns}) ' \ | ||
f'VALUES ({value_placeholders})' | ||
id_and_publication_date = (0, publication_date) | ||
if logger: | ||
logger.info('updating values', count=len(dataframe.index)) | ||
n = 0 | ||
many_values = [] | ||
with self.new_cursor() as cursor: | ||
for _, row in dataframe.iterrows(): | ||
for index, row in dataframe.iterrows(): | ||
values = [] | ||
for c in dataframe_columns_and_types: | ||
values.append(nan_safe_dtype(c.dtype, row[c.csv_name])) | ||
cursor.execute(sql, | ||
id_and_publication_date + | ||
tuple(values) + | ||
tuple(i.csv_name for i in self.additional_fields)) | ||
many_values.append(id_and_publication_date + | ||
tuple(values) + | ||
tuple(i.csv_name for i in self.additional_fields)) | ||
n += 1 | ||
# insert in batches because one at a time is slow and all at once makes | ||
# the connection drop :( | ||
if n % 5_000 == 0: | ||
try: | ||
cursor.executemany(sql, many_values) | ||
many_values = [] | ||
except Exception as e: | ||
if logger: | ||
logger.info('error on insert', index=index, values=values) | ||
logger.error(e) | ||
krivard marked this conversation as resolved.
Show resolved
Hide resolved
|
||
raise e | ||
# insert final batch | ||
if many_values: | ||
cursor.executemany(sql, many_values) | ||
|
||
# deal with non/seldomly updated columns used like a fk table (if this database needs it) | ||
if hasattr(self, 'AGGREGATE_KEY_COLS'): | ||
if logger: | ||
logger.info('updating keys') | ||
ak_cols = self.AGGREGATE_KEY_COLS | ||
|
||
# restrict data to just the key columns and remove duplicate rows | ||
|
@@ -219,13 +243,15 @@ def nan_safe_dtype(dtype, value): | |
ak_table = self.table_name + '_key' | ||
# assemble full SQL statement | ||
ak_insert_sql = f'INSERT INTO `{ak_table}` ({ak_cols_str}) VALUES ({values_str}) AS v ON DUPLICATE KEY UPDATE {ak_updates_str}' | ||
if logger: | ||
logger.info("database query", sql=ak_insert_sql) | ||
|
||
# commit the data | ||
with self.new_cursor() as cur: | ||
cur.executemany(ak_insert_sql, ak_data) | ||
|
||
|
||
def get_max_issue(self): | ||
def get_max_issue(self, logger=False): | ||
"""Fetch the most recent issue. | ||
|
||
This is used to bookend what updates we pull in from the HHS metadata. | ||
|
@@ -242,4 +268,6 @@ def get_max_issue(self): | |
for (result,) in cursor: | ||
if result is not None: | ||
return pd.Timestamp(str(result)) | ||
if logger: | ||
logger.info("get_max_issue", msg="no matching results in meta table; returning 1900/1/1 epoch") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe a |
||
return pd.Timestamp("1900/1/1") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.