Skip to content

Commit

Permalink
Merge branch 'main' into feature/2845-add-possibility-to-disable-frig…
Browse files Browse the repository at this point in the history
…ade-integration
  • Loading branch information
shahargl authored Dec 17, 2024
2 parents 6138d6d + 42cc6df commit 92dc6c9
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2189,23 +2189,30 @@ def update_key_last_used(


def get_linked_providers(tenant_id: str) -> List[Tuple[str, str, datetime]]:
# Alert table may be too huge, so cutting the query without mercy
LIMIT_BY_ALERTS = 10000

with Session(engine) as session:
providers = (
session.query(
Alert.provider_type,
Alert.provider_id,
func.max(Alert.timestamp).label("last_alert_timestamp"),
alerts_subquery = select(Alert).filter(
Alert.tenant_id == tenant_id,
Alert.provider_type != "group"
).limit(LIMIT_BY_ALERTS).subquery()

providers = session.exec(
select(
alerts_subquery.c.provider_type,
alerts_subquery.c.provider_id,
func.max(alerts_subquery.c.timestamp).label("last_alert_timestamp")
)
.outerjoin(Provider, Alert.provider_id == Provider.id)
.select_from(alerts_subquery)
.filter(
Alert.tenant_id == tenant_id,
Alert.provider_type != "group",
Provider.id
== None, # Filters for alerts with a provider_id not in Provider table
~exists().where(Provider.id == alerts_subquery.c.provider_id)
)
.group_by(Alert.provider_type, Alert.provider_id)
.all()
)
.group_by(
alerts_subquery.c.provider_type,
alerts_subquery.c.provider_id
)
).all()

return providers

Expand Down

0 comments on commit 92dc6c9

Please sign in to comment.