| 1 | from typing import List |
| 2 | |
| 3 | from fastapi import HTTPException |
| 4 | from loguru import logger |
| 5 | |
| 6 | from app.connectors.graylog.schema.streams import GraylogStreamsResponse |
| 7 | from app.connectors.graylog.schema.streams import Stream |
| 8 | from app.connectors.graylog.utils.universal import send_delete_request |
| 9 | from app.connectors.graylog.utils.universal import send_get_request |
| 10 | from app.connectors.graylog.utils.universal import send_put_request |
| 11 | |
| 12 | |
| 13 | async def get_streams() -> GraylogStreamsResponse: |
| 14 | """Get streams from Graylog. |
| 15 | |
| 16 | Returns: |
| 17 | GraylogStreamsResponse: The response object containing the streams collected from Graylog. |
| 18 | |
| 19 | Raises: |
| 20 | HTTPException: If there is an error while collecting the streams. |
| 21 | """ |
| 22 | logger.info("Getting streams from Graylog") |
| 23 | streams_collected = await send_get_request(endpoint="/api/streams") |
| 24 | try: |
| 25 | if streams_collected["success"]: |
| 26 | streams_list = [Stream(**stream_data) for stream_data in streams_collected["data"]["streams"]] |
| 27 | return GraylogStreamsResponse( |
| 28 | streams=streams_list, |
| 29 | success=True, |
| 30 | message="Streams collected successfully", |
| 31 | total=streams_collected["data"]["total"], |
| 32 | ) |
| 33 | else: |
| 34 | return GraylogStreamsResponse( |
| 35 | streams=[], |
| 36 | success=False, |
| 37 | message="Failed to collect streams", |
| 38 | total=0, |
| 39 | ) |
| 40 | except KeyError as e: |
| 41 | logger.error(f"Failed to collect streams key: {e}") |
| 42 | raise HTTPException( |
| 43 | status_code=500, |
| 44 | detail=f"Failed to collect streams key: {e}", |
| 45 | ) |
| 46 | except Exception as e: |
| 47 | logger.error(f"Failed to collect streams: {e}") |
| 48 | raise HTTPException(status_code=500, detail=f"Failed to collect streams: {e}") |
| 49 | |
| 50 | |
| 51 | async def get_stream_ids() -> List[str]: |
| 52 | """Get stream IDs from Graylog. |
| 53 | |
| 54 | Returns: |
| 55 | List[str]: A list of stream IDs. |
| 56 | |
| 57 | Raises: |
| 58 | HTTPException: If there is an error collecting the stream IDs. |
| 59 | """ |
| 60 | logger.info("Getting stream IDs from Graylog") |
| 61 | streams_collected = await send_get_request(endpoint="/api/streams") |
| 62 | try: |
| 63 | if streams_collected["success"]: |
| 64 | return [stream_data["id"] for stream_data in streams_collected["data"]["streams"]] |
| 65 | else: |
| 66 | return [] |
| 67 | except KeyError as e: |
| 68 | logger.error(f"Failed to collect streams key: {e}") |
| 69 | raise HTTPException( |
| 70 | status_code=500, |
| 71 | detail=f"Failed to collect streams key: {e}", |
| 72 | ) |
| 73 | except Exception as e: |
| 74 | logger.error(f"Failed to collect streams: {e}") |
| 75 | raise HTTPException(status_code=500, detail=f"Failed to collect streams: {e}") |
| 76 | |
| 77 | |
| 78 | async def assign_stream_to_index(stream_id: str, index_id: str) -> bool: |
| 79 | """Assign a stream to an index. |
| 80 | |
| 81 | Args: |
| 82 | stream_id (str): The ID of the stream to assign. |
| 83 | index_id (str): The ID of the index to assign the stream to. |
| 84 | |
| 85 | Returns: |
| 86 | bool: True if the stream is successfully assigned to the index, False if it is not. |
| 87 | |
| 88 | Raises: |
| 89 | HTTPException: If there is an error assigning the stream to the index. |
| 90 | """ |
| 91 | logger.info(f"Assigning stream {stream_id} to index {index_id}") |
| 92 | response = await send_put_request(endpoint=f"/api/streams/{stream_id}", data={"index_set_id": index_id}) |
| 93 | if response["success"]: |
| 94 | return True |
| 95 | else: |
| 96 | raise HTTPException( |
| 97 | status_code=500, |
| 98 | detail=f"Failed to assign stream {stream_id} to index {index_id}", |
| 99 | ) |
| 100 | |
| 101 | |
| 102 | async def delete_stream(stream_id: str) -> bool: |
| 103 | """Delete a stream. |
| 104 | |
| 105 | Args: |
| 106 | stream_id (str): The ID of the stream to delete. |
| 107 | |
| 108 | Returns: |
| 109 | bool: True if the stream is successfully deleted, False if it is not. |
| 110 | |
| 111 | Raises: |
| 112 | HTTPException: If there is an error deleting the stream. |
| 113 | """ |
| 114 | logger.info(f"Deleting stream {stream_id}") |
| 115 | response = await send_delete_request(endpoint=f"/api/streams/{stream_id}") |
| 116 | if response["success"]: |
| 117 | return True |
| 118 | else: |
| 119 | raise HTTPException( |
| 120 | status_code=500, |
| 121 | detail=f"Failed to delete stream {stream_id}", |
| 122 | ) |