add completed_successfully column to jobs table and update related statistics
This commit is contained in:
@@ -65,6 +65,7 @@ class Database:
|
|||||||
report_path TEXT,
|
report_path TEXT,
|
||||||
archive_size INTEGER,
|
archive_size INTEGER,
|
||||||
entry_point TEXT,
|
entry_point TEXT,
|
||||||
|
completed_successfully INTEGER NOT NULL DEFAULT 0,
|
||||||
cancel_requested INTEGER NOT NULL DEFAULT 0,
|
cancel_requested INTEGER NOT NULL DEFAULT 0,
|
||||||
worker_id TEXT,
|
worker_id TEXT,
|
||||||
lease_expires_at TEXT,
|
lease_expires_at TEXT,
|
||||||
@@ -102,6 +103,24 @@ class Database:
|
|||||||
connection.execute(
|
connection.execute(
|
||||||
"ALTER TABLE jobs ADD COLUMN owner_token TEXT NOT NULL DEFAULT ''"
|
"ALTER TABLE jobs ADD COLUMN owner_token TEXT NOT NULL DEFAULT ''"
|
||||||
)
|
)
|
||||||
|
if "completed_successfully" not in columns:
|
||||||
|
try:
|
||||||
|
connection.execute(
|
||||||
|
"ALTER TABLE jobs ADD COLUMN completed_successfully "
|
||||||
|
"INTEGER NOT NULL DEFAULT 0"
|
||||||
|
)
|
||||||
|
except sqlite3.OperationalError as error:
|
||||||
|
if "duplicate column name" not in str(error).lower():
|
||||||
|
raise
|
||||||
|
connection.execute(
|
||||||
|
"""
|
||||||
|
UPDATE jobs
|
||||||
|
SET completed_successfully = 1
|
||||||
|
WHERE completed_successfully = 0
|
||||||
|
AND archive_size IS NOT NULL
|
||||||
|
AND entry_point IS NOT NULL
|
||||||
|
"""
|
||||||
|
)
|
||||||
connection.execute(
|
connection.execute(
|
||||||
"CREATE INDEX IF NOT EXISTS jobs_owner_created_idx "
|
"CREATE INDEX IF NOT EXISTS jobs_owner_created_idx "
|
||||||
"ON jobs(owner_token, created_at DESC)"
|
"ON jobs(owner_token, created_at DESC)"
|
||||||
@@ -208,10 +227,16 @@ class Database:
|
|||||||
row = connection.execute(
|
row = connection.execute(
|
||||||
"""
|
"""
|
||||||
SELECT
|
SELECT
|
||||||
COUNT(CASE WHEN state = 'ready' THEN 1 END) AS websites_cloned,
|
COUNT(CASE WHEN completed_successfully = 1 THEN 1 END) AS websites_cloned,
|
||||||
COALESCE(SUM(bytes_downloaded), 0) AS bytes_scraped,
|
COALESCE(
|
||||||
|
SUM(CASE WHEN completed_successfully = 1 THEN bytes_downloaded ELSE 0 END),
|
||||||
|
0
|
||||||
|
) AS bytes_scraped,
|
||||||
COUNT(CASE WHEN state IN ('queued', 'running') THEN 1 END) AS active_crawls,
|
COUNT(CASE WHEN state IN ('queued', 'running') THEN 1 END) AS active_crawls,
|
||||||
COALESCE(SUM(files_written), 0) AS files_archived
|
COALESCE(
|
||||||
|
SUM(CASE WHEN completed_successfully = 1 THEN files_written ELSE 0 END),
|
||||||
|
0
|
||||||
|
) AS files_archived
|
||||||
FROM jobs
|
FROM jobs
|
||||||
"""
|
"""
|
||||||
).fetchone()
|
).fetchone()
|
||||||
@@ -557,6 +582,7 @@ class Database:
|
|||||||
artifact_path = ?, report_path = ?, archive_size = ?, entry_point = ?,
|
artifact_path = ?, report_path = ?, archive_size = ?, entry_point = ?,
|
||||||
pages_found = ?, pages_fetched = ?, assets_found = ?, assets_fetched = ?,
|
pages_found = ?, pages_fetched = ?, assets_found = ?, assets_fetched = ?,
|
||||||
bytes_downloaded = ?, files_written = ?, warnings_json = ?,
|
bytes_downloaded = ?, files_written = ?, warnings_json = ?,
|
||||||
|
completed_successfully = 1,
|
||||||
worker_id = NULL, lease_expires_at = NULL, expires_at = ?,
|
worker_id = NULL, lease_expires_at = NULL, expires_at = ?,
|
||||||
completed_at = ?, updated_at = ?
|
completed_at = ?, updated_at = ?
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
|
|||||||
@@ -73,6 +73,24 @@ def test_sqlite_lease_claims_one_queued_job(tmp_path) -> None:
|
|||||||
expires_at = datetime.fromisoformat(ready["expires_at"])
|
expires_at = datetime.fromisoformat(ready["expires_at"])
|
||||||
completed_at = datetime.fromisoformat(ready["completed_at"])
|
completed_at = datetime.fromisoformat(ready["completed_at"])
|
||||||
assert expires_at - completed_at <= timedelta(days=2)
|
assert expires_at - completed_at <= timedelta(days=2)
|
||||||
|
expected_stats = {
|
||||||
|
"websites_cloned": 1,
|
||||||
|
"bytes_scraped": 10,
|
||||||
|
"active_crawls": 0,
|
||||||
|
"files_archived": 1,
|
||||||
|
}
|
||||||
|
assert database.public_stats() == expected_stats
|
||||||
|
|
||||||
|
database.mark_deleted(job["id"])
|
||||||
|
|
||||||
|
assert database.public_stats() == expected_stats
|
||||||
|
with database.connection() as connection:
|
||||||
|
connection.execute(
|
||||||
|
"UPDATE jobs SET completed_successfully = 0 WHERE id = ?",
|
||||||
|
(job["id"],),
|
||||||
|
)
|
||||||
|
database.initialize()
|
||||||
|
assert database.public_stats() == expected_stats
|
||||||
|
|
||||||
|
|
||||||
def test_worker_cannot_cancel_without_a_user_cancellation_request(tmp_path) -> None:
|
def test_worker_cannot_cancel_without_a_user_cancellation_request(tmp_path) -> None:
|
||||||
|
|||||||
Reference in New Issue
Block a user