-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: batch insert to db using partitions, omitting processed files
- Loading branch information
Showing
9 changed files
with
149 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,21 +1,43 @@ | ||
import logging | ||
from typing import TypeAlias | ||
|
||
from pyspark import Row | ||
from sqlalchemy import create_engine, select | ||
from sqlalchemy.orm import sessionmaker | ||
from sqlalchemy.dialects.postgresql import insert as pg_insert | ||
|
||
from pe_analyzer.db.models import FileMetadata | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
FilePath: TypeAlias = str | ||
|
||
|
||
class Database: | ||
def __init__(self, db_url): | ||
self.engine = create_engine(db_url) | ||
self.SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=self.engine) | ||
|
||
def get_processed_files(self): | ||
def get_not_processed_files(self, file_paths: list[FilePath]) -> list: | ||
with self.SessionLocal() as session: | ||
return [row.path for row in session.execute(select(FileMetadata)).scalars()] | ||
# Get all the processed files within the list of file_paths | ||
stmt = select(FileMetadata.path).where(FileMetadata.path.in_(file_paths)) | ||
processed_files = session.scalars(stmt).all() | ||
logger.info(f"Found {len(processed_files)} processed files.") | ||
# Exclude the ones that are already processed | ||
return list(set(file_paths).difference(set(processed_files))) | ||
|
||
def save_metadata(self, metadata_list: list[Row]): | ||
with self.SessionLocal() as session: | ||
metadata_dicts = [] | ||
for metadata in metadata_list: | ||
file_metadata = FileMetadata(**metadata.asDict()) | ||
session.merge(file_metadata) | ||
metadata_dict = metadata.asDict() | ||
if metadata_dict["error"]: | ||
# TODO: output errors somewhere else | ||
logger.warning(f"Error processing {metadata_dict['path']}: {metadata_dict['error']}") | ||
del metadata_dict["error"] | ||
metadata_dicts.append(metadata_dict) | ||
|
||
stmt = pg_insert(FileMetadata).values(metadata_dicts) | ||
session.execute(stmt) | ||
session.commit() |
31 changes: 31 additions & 0 deletions
31
pe_analyzer/db/migrations/versions/a17cab59bdaa_index_on_path.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""index on path | ||
Revision ID: a17cab59bdaa | ||
Revises: 4b50f5396ee1 | ||
Create Date: 2024-07-27 19:52:52.207756 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "a17cab59bdaa" | ||
down_revision: Union[str, None] = "4b50f5396ee1" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_constraint("file_metadata_path_key", "file_metadata", type_="unique") | ||
op.create_index(op.f("ix_file_metadata_path"), "file_metadata", ["path"], unique=True) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f("ix_file_metadata_path"), table_name="file_metadata") | ||
op.create_unique_constraint("file_metadata_path_key", "file_metadata", ["path"]) | ||
# ### end Alembic commands ### |
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
FROM bitnami/spark:3.5.1 | ||
|
||
USER root | ||
RUN pip install boto3 pefile | ||
RUN pip install boto3 pefile sqlalchemy psycopg2-binary | ||
USER 1001 |