-
Notifications
You must be signed in to change notification settings - Fork 818
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add alerts_count, affected_services and sources fields to the In…
…cident (#1473) Co-authored-by: Tal <[email protected]>
- Loading branch information
1 parent
d07c7c0
commit 1ea44bf
Showing
7 changed files
with
356 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
keep/api/models/db/migrations/versions/2024-07-25-17-13_67f1efb93c99.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"""Add fields for prepopulated data from alerts | ||
Revision ID: 67f1efb93c99 | ||
Revises: dcbd2873dcfd | ||
Create Date: 2024-07-25 17:13:04.428633 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
from sqlalchemy.orm import Session, joinedload | ||
|
||
from keep.api.models.alert import AlertDto | ||
from keep.api.models.db.alert import Incident | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "67f1efb93c99" | ||
down_revision = "dcbd2873dcfd" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def populate_db(session): | ||
|
||
incidents = session.query(Incident).options(joinedload(Incident.alerts)).all() | ||
|
||
for incident in incidents: | ||
alerts_dto = [AlertDto(**alert.event) for alert in incident.alerts] | ||
|
||
incident.sources = list( | ||
set([source for alert_dto in alerts_dto for source in alert_dto.source]) | ||
) | ||
incident.affected_services = list( | ||
set([alert.service for alert in alerts_dto if alert.service is not None]) | ||
) | ||
incident.alerts_count = len(incident.alerts) | ||
session.add(incident) | ||
session.commit() | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column("incident", sa.Column("affected_services", sa.JSON(), nullable=True)) | ||
op.add_column("incident", sa.Column("sources", sa.JSON(), nullable=True)) | ||
op.add_column("incident", sa.Column("alerts_count", sa.Integer(), nullable=False, server_default="0")) | ||
|
||
session = Session(op.get_bind()) | ||
populate_db(session) | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column("incident", "alerts_count") | ||
op.drop_column("incident", "sources") | ||
op.drop_column("incident", "affected_services") | ||
# ### end Alembic commands ### |
Oops, something went wrong.