Skip to content
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

Log on error #6

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions copietje/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from tqdm import tqdm

from copietje import Condenser
from copietje.download import add_metadata_to_db, determine_stream, SCHEMA
from copietje.download import add_metadata_to_db, determine_stream, log_error_to_db, SCHEMA
from copietje.ranking import rank


Expand Down Expand Up @@ -99,7 +99,7 @@ def download(*, context, database, target=None, limit=None, condenser=None, jobs

with context, sqlite3.connect(database) as database:
database.row_factory = sqlite3.Row
database.cursor().execute(SCHEMA)
database.cursor().executescript(SCHEMA)
# search hansken for the documents to download + minhash
documents = context.search(Term('type', 'document'), count=limit)
# issue bulk download with side effects to store all the documents and the minhashes
Expand All @@ -117,6 +117,7 @@ def download(*, context, database, target=None, limit=None, condenser=None, jobs
export.bulk(documents, target,
stream=partial(determine_stream, database=database),
side_effect=partial(add_metadata_to_db, database=database, condenser=condenser),
on_error=partial(log_error_to_db, database=database),
jobs=jobs)


Expand Down
28 changes: 27 additions & 1 deletion copietje/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
tags TEXT,
privileged_status TEXT,
minhash BLOB
)
);
CREATE TABLE IF NOT EXISTS errors (
uid TEXT,
-- equivalent to UNIXEPOCH(), which doesn't seem to be supported
ts INTEGER DEFAULT (CAST(strftime('%s', 'now') as INTEGER)),
stream TEXT,
privileged_status TEXT,
error TEXT,
-- compound primary key to allow multiple failures of the same trace uid, differentiated by timestamp
PRIMARY KEY (uid, ts)
);
"""


Expand Down Expand Up @@ -50,6 +60,22 @@ def determine_stream(trace, database=None):
return selected


def log_error_to_db(database, trace, stream, exception=None, **_):
database.cursor().execute(
"""
INSERT INTO errors (uid, stream, privileged_status, error)
VALUES (?, ?, ?, ?)
""",
(
trace.uid,
stream,
str(trace.privileged or '') or None,
str(exception) if exception else None,
)
)
database.commit()


def add_metadata_to_db(database, trace, stream, output, condenser=None, **_):
mh = None

Expand Down