Skip to content

Commit

Permalink
refactor: change incident_id to finding_id in table
Browse files Browse the repository at this point in the history
  • Loading branch information
DecFox committed Jul 25, 2024
1 parent af0efde commit 2289ced
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def upgrade() -> None:
op.create_table(
"oonifinding",
sa.Column(
"incident_id",
"finding_id",
sa.String(),
nullable=False,
primary_key=True,
Expand Down
2 changes: 1 addition & 1 deletion ooniapi/services/oonifindings/src/oonifindings/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class OONIFinding(Base):
__tablename__ = "oonifinding"

# TODO(decfox): add primary key finding id
incident_id: Mapped[str] = mapped_column(String, primary_key=True)
finding_id: Mapped[str] = mapped_column(String, primary_key=True)

create_time: Mapped[datetime] = mapped_column(UtcDateTime())
update_time: Mapped[datetime] = mapped_column(UtcDateTime())
Expand Down
36 changes: 18 additions & 18 deletions ooniapi/services/oonifindings/src/oonifindings/routers/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def list_oonifindings(
findings = []
for row in q.all():
oonifinding = OONIFinding(
id=row.incident_id,
id=row.finding_id,
update_time=row.update_time,
start_time=row.start_time,
end_time=row.end_time,
Expand Down Expand Up @@ -200,12 +200,12 @@ def list_oonifindings(


@router.get(
"/v1/incidents/show/{incident_id}",
"/v1/incidents/show/{finding_id}",
tags=["oonifindings"],
response_model=OONIFindingIncident
)
def get_oonifinding_by_id(
incident_id: str,
finding_id: str,
response: Response,
authorization: str = Header("authorization"),
db=Depends(get_postgresql_session),
Expand All @@ -216,7 +216,7 @@ def get_oonifinding_by_id(
"""
log.debug("showing incident")
q = db.query(models.OONIFinding).filter(
models.OONIFinding.incident_id == incident_id
models.OONIFinding.finding_id == finding_id
)
q = q.filter(models.OONIFinding.deleted != 1)

Expand All @@ -235,7 +235,7 @@ def get_oonifinding_by_id(
raise HTTPException(status_code=404, detail="OONI Finding not found")

oonifinding = OONIFindingWithText(
id=finding.incident_id,
id=finding.finding_id,
update_time=finding.update_time,
start_time=finding.start_time,
end_time=finding.end_time,
Expand Down Expand Up @@ -315,12 +315,12 @@ def create_oonifinding(
authorization, jwt_encryption_key=settings.jwt_encryption_key
)
now = utcnow_seconds()
incident_id = str(generate_random_intuid(collector_id=settings.collector_id))
finding_id = str(generate_random_intuid(collector_id=settings.collector_id))

log.info(f"Creating incident {incident_id}")
log.info(f"Creating incident {finding_id}")

db_oonifinding = models.OONIFinding(
incident_id=incident_id,
finding_id=finding_id,
create_time=now,
update_time=now,
start_time=create_request.start_time,
Expand All @@ -345,7 +345,7 @@ def create_oonifinding(
db.commit()

setnocacheresponse(response)
return OONIFindingsUpdateResponse(r=1, id=incident_id)
return OONIFindingsUpdateResponse(r=1, id=finding_id)


@router.post(
Expand All @@ -362,11 +362,11 @@ def update_oonifinding(
"""
Update an incident
"""
incident_id = update_request.incident_id
finding_id = update_request.incident_id
account_id = token["account_id"]

q = db.query(models.OONIFinding).filter(
models.OONIFinding.incident_id == incident_id
models.OONIFinding.finding_id == finding_id
)
if token["role"] == "user":
q = q.filter(models.OONIFinding.creator_account_id == account_id)
Expand All @@ -382,7 +382,7 @@ def update_oonifinding(
except sa.exc.NoResultFound:
raise HTTPException(status_code=404, detail="OONI Finding not found")

log.info(f"Updating incident {incident_id}")
log.info(f"Updating incident {finding_id}")

now = utcnow_seconds()
oonifinding.update_time = now
Expand All @@ -404,7 +404,7 @@ def update_oonifinding(
db.commit()

setnocacheresponse(response)
return OONIFindingsUpdateResponse(r=1, id=incident_id)
return OONIFindingsUpdateResponse(r=1, id=finding_id)


@router.post(
Expand All @@ -422,10 +422,10 @@ def delete_oonifinding(
"""
assert delete_request
account_id = token["account_id"]
incident_id = delete_request.incident_id
finding_id = delete_request.incident_id

q = db.query(models.OONIFinding).filter(
models.OONIFinding.incident_id == incident_id
models.OONIFinding.finding_id == finding_id
)
if token["role"] == "user":
q = q.filter(models.OONIFinding.creator_account_id == account_id)
Expand Down Expand Up @@ -465,10 +465,10 @@ def update_oonifinding_publish_status(
raise HTTPException(status_code=400, detail="Invalid query action")

assert publish_request
incident_id = publish_request.incident_id
finding_id = publish_request.incident_id

q = db.query(models.OONIFinding).filter(
models.OONIFinding.incident_id == publish_request.incident_id
models.OONIFinding.finding_id == finding_id
)

try:
Expand All @@ -480,4 +480,4 @@ def update_oonifinding_publish_status(
db.commit()

setnocacheresponse(response)
return OONIFindingsUpdateResponse(r=1, id=incident_id)
return OONIFindingsUpdateResponse(r=1, id=finding_id)
8 changes: 4 additions & 4 deletions ooniapi/services/oonifindings/tests/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_upgrade_to_head(postgresql):

db_finding = models.OONIFinding(
**finding,
incident_id="000000000",
finding_id="000000000",
create_time=utcnow_seconds(),
update_time=utcnow_seconds(),
creator_account_id="000000000",
Expand All @@ -99,7 +99,7 @@ def test_upgrade_to_head(postgresql):
with pytest.raises(sa.exc.StatementError):
db_finding = models.OONIFinding(
**finding,
incident_id="000000000",
finding_id="000000000",
create_time="NOT A DATE",
update_time=utcnow_seconds(),
creator_account_id="000000000",
Expand All @@ -112,7 +112,7 @@ def test_upgrade_to_head(postgresql):
naive_datetime = datetime.now()
db_finding = models.OONIFinding(
**finding,
incident_id="000000000",
finding_id="000000000",
create_time=naive_datetime,
update_time=utcnow_seconds(),
creator_account_id="000000000",
Expand All @@ -124,7 +124,7 @@ def test_upgrade_to_head(postgresql):
with pytest.raises(sa.exc.StatementError):
db_finding = models.OONIFinding(
**finding,
incident_id="000000000",
finding_id="000000000",
create_time=None,
update_time=utcnow_seconds(),
creator_account_id="000000000",
Expand Down

0 comments on commit 2289ced

Please sign in to comment.