main
py 54 lines 2.26 KB
Raw
1 from datetime import datetime
2
3 from dotenv import load_dotenv
4 from loguru import logger
5 from sqlalchemy import select
6
7 from app.db.db_session import get_db_session
8 from app.db.db_session import get_sync_db_session
9 from app.integrations.models.customer_integration_settings import CustomerIntegrations
10 from app.integrations.modules.routes.huntress import collect_huntress_route
11 from app.integrations.modules.schema.huntress import InvokeHuntressRequest
12 from app.integrations.modules.schema.huntress import InvokeHuntressResponse
13 from app.schedulers.models.scheduler import JobMetadata
14 from app.schedulers.utils.universal import get_scheduled_job_metadata
15
16 load_dotenv()
17
18
19 async def invoke_huntress_integration_collect() -> InvokeHuntressResponse:
20 """
21 Invokes the Huntress integration collection.
22 """
23 logger.info("Invoking Huntress integration collection.")
24 customer_codes = []
25 async with get_db_session() as session:
26 stmt = select(CustomerIntegrations).where(
27 CustomerIntegrations.integration_service_name == "Huntress",
28 )
29 result = await session.execute(stmt)
30 customer_codes = [row.customer_code for row in result.scalars()]
31 logger.info(f"customer_codes: {customer_codes}")
32 for customer_code in customer_codes:
33 await collect_huntress_route(
34 InvokeHuntressRequest(
35 customer_code=customer_code,
36 integration_name="Huntress",
37 time_range=f"{(await get_scheduled_job_metadata('invoke_huntress_integration_collect')).time_interval}m",
38 ),
39 session,
40 )
41 # Close the session
42 await session.close()
43 with get_sync_db_session() as session:
44 # Synchronous ORM operations
45 job_metadata = session.query(JobMetadata).filter_by(job_id="invoke_huntress_integration_collect").one_or_none()
46 if job_metadata:
47 job_metadata.last_success = datetime.utcnow()
48 session.add(job_metadata)
49 session.commit()
50 else:
51 # Handle the case where job_metadata does not exist
52 print("JobMetadata for 'invoke_huntress_integration_collect' not found.")
53
54 return InvokeHuntressResponse(success=True, message="Huntress integration invoked.")