Skip to content

Commit

Permalink
ref: Remove log and use on_conflict_do_nothing
Browse files Browse the repository at this point in the history
  • Loading branch information
jangjodi committed Sep 20, 2024
1 parent 331fc5d commit 66d436b
Showing 1 changed file with 11 additions and 40 deletions.
51 changes: 11 additions & 40 deletions src/seer/grouping/grouping.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pydantic import BaseModel, ValidationInfo, field_validator
from sentence_transformers import SentenceTransformer
from sqlalchemy.dialects.postgresql import insert
from sqlalchemy.exc import IntegrityError
from torch.cuda import OutOfMemoryError

from seer.db import DbGroupingRecord, Session
Expand Down Expand Up @@ -481,48 +480,20 @@ def insert_new_grouping_record(self, issue: GroupingRequest, embedding: np.ndarr
:param embedding: The embedding of the stacktrace.
"""
with Session() as session:
existing_record = (
session.query(DbGroupingRecord)
.filter_by(hash=issue.hash, project_id=issue.project_id)
.first()
insert_stmt = insert(DbGroupingRecord).values(
project_id=issue.project_id,
message=issue.message,
stacktrace_embedding=embedding,
hash=issue.hash,
error_type=issue.exception_type,
)

extra = {
"project_id": issue.project_id,
"stacktrace_length": len(issue.stacktrace),
"input_hash": issue.hash,
}

if existing_record is None:
new_record = GroupingRecord(
project_id=issue.project_id,
message=issue.message,
stacktrace_embedding=embedding,
hash=issue.hash,
error_type=issue.exception_type,
).to_db_model()
session.add(new_record)

try:
session.commit()
except IntegrityError:
session.rollback()
existing_record = (
session.query(DbGroupingRecord)
.filter_by(hash=issue.hash, project_id=issue.project_id)
.first()
)
extra["existing_hash"] = existing_record.hash
logger.info(
"group_already_exists_in_seer_db",
extra=extra,
)
else:
extra["existing_hash"] = existing_record.hash
logger.info(
"group_already_exists_in_seer_db",
extra=extra,
session.execute(
insert_stmt.on_conflict_do_nothing(
index_elements=(DbGroupingRecord.project_id, DbGroupingRecord.hash)
)
)
session.commit()

@sentry_sdk.tracing.trace
def delete_grouping_records_for_project(self, project_id: int) -> bool:
Expand Down

0 comments on commit 66d436b

Please sign in to comment.