Spaces:
Running
Running
File size: 8,377 Bytes
89ae94f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
"""
Service for alert operations.
"""
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.future import select
from sqlalchemy import func, or_, and_
from datetime import datetime
from typing import List, Optional, Dict, Any, Union
from src.models.alert import Alert, AlertStatus, AlertCategory
from src.models.threat import ThreatSeverity
from src.api.schemas import PaginationParams
async def create_alert(
db: AsyncSession,
title: str,
description: str,
severity: ThreatSeverity,
category: AlertCategory,
source_url: Optional[str] = None,
threat_id: Optional[int] = None,
mention_id: Optional[int] = None,
) -> Alert:
"""
Create a new alert.
Args:
db: Database session
title: Alert title
description: Alert description
severity: Alert severity
category: Alert category
source_url: Source URL for the alert
threat_id: ID of related threat
mention_id: ID of related dark web mention
Returns:
Alert: Created alert
"""
db_alert = Alert(
title=title,
description=description,
severity=severity,
status=AlertStatus.NEW,
category=category,
generated_at=datetime.utcnow(),
source_url=source_url,
is_read=False,
threat_id=threat_id,
mention_id=mention_id,
)
db.add(db_alert)
await db.commit()
await db.refresh(db_alert)
return db_alert
async def get_alert_by_id(db: AsyncSession, alert_id: int) -> Optional[Alert]:
"""
Get alert by ID.
Args:
db: Database session
alert_id: Alert ID
Returns:
Optional[Alert]: Alert or None if not found
"""
result = await db.execute(select(Alert).filter(Alert.id == alert_id))
return result.scalars().first()
async def get_alerts(
db: AsyncSession,
pagination: PaginationParams,
severity: Optional[List[ThreatSeverity]] = None,
status: Optional[List[AlertStatus]] = None,
category: Optional[List[AlertCategory]] = None,
is_read: Optional[bool] = None,
search_query: Optional[str] = None,
from_date: Optional[datetime] = None,
to_date: Optional[datetime] = None,
) -> List[Alert]:
"""
Get alerts with filtering and pagination.
Args:
db: Database session
pagination: Pagination parameters
severity: Filter by severity
status: Filter by status
category: Filter by category
is_read: Filter by read status
search_query: Search in title and description
from_date: Filter by generated_at >= from_date
to_date: Filter by generated_at <= to_date
Returns:
List[Alert]: List of alerts
"""
query = select(Alert)
# Apply filters
if severity:
query = query.filter(Alert.severity.in_(severity))
if status:
query = query.filter(Alert.status.in_(status))
if category:
query = query.filter(Alert.category.in_(category))
if is_read is not None:
query = query.filter(Alert.is_read == is_read)
if search_query:
search_filter = or_(
Alert.title.ilike(f"%{search_query}%"),
Alert.description.ilike(f"%{search_query}%")
)
query = query.filter(search_filter)
if from_date:
query = query.filter(Alert.generated_at >= from_date)
if to_date:
query = query.filter(Alert.generated_at <= to_date)
# Apply pagination
query = query.order_by(Alert.generated_at.desc())
query = query.offset((pagination.page - 1) * pagination.size).limit(pagination.size)
result = await db.execute(query)
return result.scalars().all()
async def count_alerts(
db: AsyncSession,
severity: Optional[List[ThreatSeverity]] = None,
status: Optional[List[AlertStatus]] = None,
category: Optional[List[AlertCategory]] = None,
is_read: Optional[bool] = None,
search_query: Optional[str] = None,
from_date: Optional[datetime] = None,
to_date: Optional[datetime] = None,
) -> int:
"""
Count alerts with filtering.
Args:
db: Database session
severity: Filter by severity
status: Filter by status
category: Filter by category
is_read: Filter by read status
search_query: Search in title and description
from_date: Filter by generated_at >= from_date
to_date: Filter by generated_at <= to_date
Returns:
int: Count of alerts
"""
query = select(func.count(Alert.id))
# Apply filters (same as in get_alerts)
if severity:
query = query.filter(Alert.severity.in_(severity))
if status:
query = query.filter(Alert.status.in_(status))
if category:
query = query.filter(Alert.category.in_(category))
if is_read is not None:
query = query.filter(Alert.is_read == is_read)
if search_query:
search_filter = or_(
Alert.title.ilike(f"%{search_query}%"),
Alert.description.ilike(f"%{search_query}%")
)
query = query.filter(search_filter)
if from_date:
query = query.filter(Alert.generated_at >= from_date)
if to_date:
query = query.filter(Alert.generated_at <= to_date)
result = await db.execute(query)
return result.scalar()
async def update_alert_status(
db: AsyncSession,
alert_id: int,
status: AlertStatus,
action_taken: Optional[str] = None,
) -> Optional[Alert]:
"""
Update alert status.
Args:
db: Database session
alert_id: Alert ID
status: New status
action_taken: Description of action taken
Returns:
Optional[Alert]: Updated alert or None if not found
"""
alert = await get_alert_by_id(db, alert_id)
if not alert:
return None
alert.status = status
if action_taken:
alert.action_taken = action_taken
if status == AlertStatus.RESOLVED:
alert.resolved_at = datetime.utcnow()
alert.updated_at = datetime.utcnow()
await db.commit()
await db.refresh(alert)
return alert
async def mark_alert_as_read(
db: AsyncSession,
alert_id: int,
) -> Optional[Alert]:
"""
Mark alert as read.
Args:
db: Database session
alert_id: Alert ID
Returns:
Optional[Alert]: Updated alert or None if not found
"""
alert = await get_alert_by_id(db, alert_id)
if not alert:
return None
alert.is_read = True
alert.updated_at = datetime.utcnow()
await db.commit()
await db.refresh(alert)
return alert
async def assign_alert(
db: AsyncSession,
alert_id: int,
user_id: int,
) -> Optional[Alert]:
"""
Assign alert to a user.
Args:
db: Database session
alert_id: Alert ID
user_id: User ID to assign to
Returns:
Optional[Alert]: Updated alert or None if not found
"""
alert = await get_alert_by_id(db, alert_id)
if not alert:
return None
alert.assigned_to_id = user_id
alert.status = AlertStatus.ASSIGNED
alert.updated_at = datetime.utcnow()
await db.commit()
await db.refresh(alert)
return alert
async def get_alert_counts_by_severity(
db: AsyncSession,
from_date: Optional[datetime] = None,
to_date: Optional[datetime] = None,
) -> Dict[str, int]:
"""
Get count of alerts by severity.
Args:
db: Database session
from_date: Filter by generated_at >= from_date
to_date: Filter by generated_at <= to_date
Returns:
Dict[str, int]: Mapping of severity to count
"""
result = {}
for severity in ThreatSeverity:
query = select(func.count(Alert.id)).filter(Alert.severity == severity)
if from_date:
query = query.filter(Alert.generated_at >= from_date)
if to_date:
query = query.filter(Alert.generated_at <= to_date)
count_result = await db.execute(query)
count = count_result.scalar() or 0
result[severity.value] = count
return result |