Skip to content

Commit 8936ea5

Browse files
committed
adding project id to job table
1 parent 82c1d81 commit 8936ea5

5 files changed

Lines changed: 38 additions & 13 deletions

File tree

backend/app/api/routes/llm.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def llm_call(
7070

7171
# Fetch job details to return immediate response
7272
job_crud = JobCrud(session=session)
73-
job = job_crud.get(job_id=job_id)
73+
job = job_crud.get(job_id=job_id, project_id=project_id)
7474

7575
if not job:
7676
raise HTTPException(status_code=404, detail="Job not found")
@@ -106,16 +106,19 @@ def get_llm_call_status(
106106
Poll for LLM call job status and results.
107107
Returns job information with nested LLM response when complete.
108108
"""
109+
110+
project_id = _current_user.project_.id
111+
109112
job_crud = JobCrud(session=session)
110-
job = job_crud.get(job_id=job_id)
113+
job = job_crud.get(job_id=job_id, project_id=project_id)
111114

112115
if not job:
113116
raise HTTPException(status_code=404, detail="Job not found")
114117

115118
llm_call_response = None
116119
if job.status.value == JobStatus.SUCCESS:
117120
llm_calls = get_llm_calls_by_job_id(
118-
session=session, job_id=job_id, project_id=_current_user.project_.id
121+
session=session, job_id=job_id, project_id=project_id
119122
)
120123

121124
if llm_calls:

backend/app/crud/jobs.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ class JobCrud:
1212
def __init__(self, session: Session):
1313
self.session = session
1414

15-
def create(self, job_type: JobType, trace_id: str | None = None) -> Job:
16-
new_job = Job(
17-
job_type=job_type,
18-
trace_id=trace_id,
19-
)
15+
def create(
16+
self,
17+
job_type: JobType,
18+
trace_id: str | None = None,
19+
project_id: int | None = None,
20+
) -> Job:
21+
new_job = Job(job_type=job_type, trace_id=trace_id, project_id=project_id)
2022
self.session.add(new_job)
2123
self.session.commit()
2224
self.session.refresh(new_job)
@@ -38,5 +40,14 @@ def update(self, job_id: UUID, job_update: JobUpdate) -> Job:
3840

3941
return job
4042

41-
def get(self, job_id: UUID) -> Job | None:
42-
return self.session.get(Job, job_id)
43+
def get(self, job_id: UUID, project_id: int | None = None) -> Job | None:
44+
job = self.session.get(Job, job_id)
45+
if job is None:
46+
return None
47+
if (
48+
project_id is not None
49+
and job.project_id is not None
50+
and job.project_id != project_id
51+
):
52+
return None
53+
return job

backend/app/models/job.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Job(SQLModel, table=True):
4040
description="Tracing ID for correlating logs and traces.",
4141
sa_column_kwargs={"comment": "Tracing ID for correlating logs and traces"},
4242
)
43+
project_id: int | None = Field(
44+
default=None,
45+
description="Project ID of the project the job belongs to.",
46+
sa_column_kwargs={"comment": "Project ID of the job's project"},
47+
)
4348
error_message: str | None = Field(
4449
default=None,
4550
description="Error details if the job fails.",

backend/app/services/llm/jobs.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ def start_job(
4646
"""Create an LLM job and schedule Celery task."""
4747
trace_id = correlation_id.get() or "N/A"
4848
job_crud = JobCrud(session=db)
49-
job = job_crud.create(job_type=JobType.LLM_API, trace_id=trace_id)
49+
job = job_crud.create(
50+
job_type=JobType.LLM_API, trace_id=trace_id, project_id=project_id
51+
)
5052

5153
# Explicitly flush to ensure job is persisted before Celery task starts
5254
db.flush()
@@ -87,7 +89,9 @@ def start_chain_job(
8789
"""Create an LLM Chain job and schedule Celery task."""
8890
trace_id = correlation_id.get() or "N/A"
8991
job_crud = JobCrud(session=db)
90-
job = job_crud.create(job_type=JobType.LLM_CHAIN, trace_id=trace_id)
92+
job = job_crud.create(
93+
job_type=JobType.LLM_CHAIN, trace_id=trace_id, project_id=project_id
94+
)
9195

9296
# Explicitly flush to ensure job is persisted before Celery task starts
9397
db.flush()

backend/app/services/response/jobs.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ def start_job(
1919
"""Create a response job and schedule Celery task."""
2020
trace_id = correlation_id.get() or "N/A"
2121
job_crud = JobCrud(session=db)
22-
job = job_crud.create(job_type=JobType.RESPONSE, trace_id=trace_id)
22+
job = job_crud.create(
23+
job_type=JobType.RESPONSE, trace_id=trace_id, project_id=project_id
24+
)
2325

2426
try:
2527
task_id = start_response_job(

0 commit comments

Comments
 (0)