[31188f42] einfügen
einfügen
This commit is contained in:
@@ -77,13 +77,19 @@ class JobQueue:
|
||||
|
||||
return job
|
||||
|
||||
def retry_job_later(self, job_id, delay_seconds=60):
|
||||
def retry_job_later(self, job_id, delay_seconds=60, error_msg=None):
|
||||
next_try = datetime.utcnow() + timedelta(seconds=delay_seconds)
|
||||
with sqlite3.connect(DB_PATH) as conn:
|
||||
conn.execute(
|
||||
"UPDATE jobs SET status = 'PENDING', next_try_at = ?, updated_at = datetime('now') WHERE id = ?",
|
||||
(next_try, job_id)
|
||||
)
|
||||
if error_msg:
|
||||
conn.execute(
|
||||
"UPDATE jobs SET status = 'PENDING', next_try_at = ?, updated_at = datetime('now'), error_msg = ? WHERE id = ?",
|
||||
(next_try, str(error_msg), job_id)
|
||||
)
|
||||
else:
|
||||
conn.execute(
|
||||
"UPDATE jobs SET status = 'PENDING', next_try_at = ?, updated_at = datetime('now') WHERE id = ?",
|
||||
(next_try, job_id)
|
||||
)
|
||||
|
||||
def complete_job(self, job_id):
|
||||
with sqlite3.connect(DB_PATH) as conn:
|
||||
@@ -125,3 +131,113 @@ class JobQueue:
|
||||
pass
|
||||
results.append(r)
|
||||
return results
|
||||
|
||||
def get_account_summary(self, limit=1000):
|
||||
"""
|
||||
Groups recent jobs by ContactId/PersonId and returns a summary status.
|
||||
"""
|
||||
jobs = self.get_recent_jobs(limit=limit)
|
||||
accounts = {}
|
||||
|
||||
for job in jobs:
|
||||
payload = job.get('payload', {})
|
||||
# Try to find IDs
|
||||
c_id = payload.get('ContactId')
|
||||
p_id = payload.get('PersonId')
|
||||
|
||||
# Fallback for cascaded jobs or primary keys
|
||||
if not c_id and payload.get('PrimaryKey') and 'contact' in job['event_type'].lower():
|
||||
c_id = payload.get('PrimaryKey')
|
||||
if not p_id and payload.get('PrimaryKey') and 'person' in job['event_type'].lower():
|
||||
p_id = payload.get('PrimaryKey')
|
||||
|
||||
if not c_id and not p_id:
|
||||
continue
|
||||
|
||||
# Create a unique key for the entity
|
||||
key = f"P{p_id}" if p_id else f"C{c_id}"
|
||||
|
||||
if key not in accounts:
|
||||
accounts[key] = {
|
||||
"id": key,
|
||||
"contact_id": c_id,
|
||||
"person_id": p_id,
|
||||
"name": "Unknown",
|
||||
"last_event": job['event_type'],
|
||||
"status": job['status'],
|
||||
"created_at": job['created_at'], # Oldest job in group (since we sort by DESC)
|
||||
"updated_at": job['updated_at'], # Most recent job
|
||||
"error_msg": job['error_msg'],
|
||||
"job_count": 0,
|
||||
"duration": "0s",
|
||||
"phases": {
|
||||
"received": "completed",
|
||||
"enriching": "pending",
|
||||
"syncing": "pending",
|
||||
"completed": "pending"
|
||||
}
|
||||
}
|
||||
|
||||
acc = accounts[key]
|
||||
acc["job_count"] += 1
|
||||
|
||||
# Update duration
|
||||
try:
|
||||
# We want the absolute start (oldest created_at)
|
||||
# Since jobs are DESC, the last one we iterate through for a key is the oldest
|
||||
acc["created_at"] = job["created_at"]
|
||||
|
||||
start = datetime.strptime(acc["created_at"], "%Y-%m-%d %H:%M:%S")
|
||||
end = datetime.strptime(acc["updated_at"], "%Y-%m-%d %H:%M:%S")
|
||||
diff = end - start
|
||||
seconds = int(diff.total_seconds())
|
||||
if seconds < 60:
|
||||
acc["duration"] = f"{seconds}s"
|
||||
else:
|
||||
acc["duration"] = f"{seconds // 60}m {seconds % 60}s"
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Try to resolve 'Unknown' name from any job in the group
|
||||
if acc["name"] == "Unknown":
|
||||
name = payload.get('Name') or payload.get('crm_name') or payload.get('FullName') or payload.get('ContactName')
|
||||
if not name and payload.get('Firstname'):
|
||||
name = f"{payload.get('Firstname')} {payload.get('Lastname', '')}".strip()
|
||||
if name:
|
||||
acc["name"] = name
|
||||
|
||||
# Update overall status based on most recent job
|
||||
# (Assuming jobs are sorted by updated_at DESC)
|
||||
if acc["job_count"] == 1:
|
||||
acc["status"] = job["status"]
|
||||
acc["updated_at"] = job["updated_at"]
|
||||
acc["error_msg"] = job["error_msg"]
|
||||
|
||||
# Determine Phase
|
||||
if job["status"] == "COMPLETED":
|
||||
acc["phases"] = {
|
||||
"received": "completed",
|
||||
"enriching": "completed",
|
||||
"syncing": "completed",
|
||||
"completed": "completed"
|
||||
}
|
||||
elif job["status"] == "FAILED":
|
||||
acc["phases"]["received"] = "completed"
|
||||
acc["phases"]["enriching"] = "failed"
|
||||
elif job["status"] == "PROCESSING":
|
||||
acc["phases"]["received"] = "completed"
|
||||
acc["phases"]["enriching"] = "processing"
|
||||
elif job["status"] == "PENDING":
|
||||
acc["phases"]["received"] = "completed"
|
||||
# If it has an error msg like 'processing', it's in enriching
|
||||
if job["error_msg"] and "processing" in job["error_msg"].lower():
|
||||
acc["phases"]["enriching"] = "processing"
|
||||
else:
|
||||
acc["phases"]["received"] = "processing"
|
||||
|
||||
# Final cleanup for names
|
||||
for acc in accounts.values():
|
||||
if acc["name"] == "Unknown":
|
||||
acc["name"] = f"Entity {acc['id']}"
|
||||
|
||||
return list(accounts.values())
|
||||
|
||||
Reference in New Issue
Block a user