Skip to content

Commit

Permalink
fix: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
talboren committed Nov 19, 2024
1 parent 0fe790d commit 353e42c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
34 changes: 20 additions & 14 deletions keep/api/core/demo_mode_runner.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio
import datetime
import logging
import os
import random
import threading
import time
from datetime import timezone

Expand Down Expand Up @@ -299,7 +299,7 @@ def remove_old_incidents(keep_api_key, keep_api_url):
response.raise_for_status()


async def simulate_alerts(
def simulate_alerts(
keep_api_url=None,
keep_api_key=None,
sleep_interval=5,
Expand Down Expand Up @@ -378,12 +378,15 @@ async def simulate_alerts(
time.sleep(sleep_interval)
continue

if response.status_code != 202:
if not response.ok:
logger.error("Failed to send alert: {}".format(response.text))
else:
logger.info("Alert sent successfully")

await asyncio.sleep(sleep_interval)
logger.info(
"Sleeping for {} seconds before next iteration".format(sleep_interval)
)
time.sleep(sleep_interval)


def launch_demo_mode():
Expand All @@ -392,7 +395,9 @@ def launch_demo_mode():
"""
logger.info("Demo mode launched.")

keep_api_url = "http://localhost:" + str(os.environ.get("PORT", 8080))
keep_api_url = os.environ.get(
"KEEP_API_URL", "http://localhost:" + str(os.environ.get("PORT", 8080))
)
keep_api_key = os.environ.get("KEEP_READ_ONLY_BYPASS_KEY")
if keep_api_key is None:
with get_session_sync() as session:
Expand All @@ -404,14 +409,15 @@ def launch_demo_mode():
system_description="Simulate Alerts API key",
)

loop = asyncio.get_event_loop()
loop.create_task(
simulate_alerts(
keep_api_url,
keep_api_key,
sleep_interval=5,
demo_correlation_rules=True,
demo_topology=True,
)
thread = threading.Thread(
target=simulate_alerts,
kwargs={
"keep_api_key": keep_api_key,
"keep_api_url": keep_api_url,
"sleep_interval": 0.2,
"demo_correlation_rules": True,
},
)
thread.daemon = True
thread.start()
logger.info("Demo mode initialized.")
13 changes: 7 additions & 6 deletions scripts/simulate_alerts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import logging
import asyncio
import os

from keep.api.core.demo_mode_runner import simulate_alerts

Expand All @@ -13,11 +12,13 @@
logger = logging.getLogger(__name__)


async def main():
SLEEP_INTERVAL = float(os.environ.get("SLEEP_INTERVAL", 0.2)) # Configurable sleep interval from env variable
def main():
SLEEP_INTERVAL = float(
os.environ.get("SLEEP_INTERVAL", 0.2)
) # Configurable sleep interval from env variable
keep_api_key = os.environ.get("KEEP_API_KEY")
keep_api_url = os.environ.get("KEEP_API_URL") or "http://localhost:8080"
await simulate_alerts(
simulate_alerts(
keep_api_key=keep_api_key,
keep_api_url=keep_api_url,
sleep_interval=SLEEP_INTERVAL,
Expand All @@ -26,4 +27,4 @@ async def main():


if __name__ == "__main__":
asyncio.run(main())
main()

0 comments on commit 353e42c

Please sign in to comment.