I am trying to filter Alerts using API integration using my query and and list the alerts which appeared only in the last hour.
I am using the below code but it is not filtering based on the "createdAtSince" criteria:
import opsgenie_sdk
import requests
from datetime import datetime, timedelta, timezone
# Replace with your actual API key
API_KEY = "API_KEY"
headers = {
"Authorization": f"GenieKey {API_KEY}",
"Content-Type": "application/json"
}
# Create time window for alerts created approx. 1 minute ago (30 sec window)
end_time = datetime.now(timezone.utc)
start_time = end_time - timedelta(seconds=3600)
# Convert to milliseconds since epoch
start_epoch_ms = int(start_time.timestamp() * 1000)
end_epoch_ms = int(end_time.timestamp() * 1000)
params = {
"limit": 100,
"offset": 0,
"createdAtSince": start_epoch_ms,
"createdAtUntil": end_epoch_ms
}
params["query"] = """priority:(P1 or P2) AND NOT application:("Cloud Sync Module") AND detailsPair(severity:CRITICAL) AND NOT issueKey AND NOT incident-alert-type:Associated AND detailsPair(noc_bypass:false) AND (detailsPair(noc_desk:"Cluster 1") OR detailsPair(noc_desk:"Cluster 2") OR detailsPair(noc_desk:"Cluster 3")) AND (responders:"I_EXT_SERVICESUPPORT_TIERIANDII" OR responders:"Automation_Alert_Delay_TIERIANDII_DG") AND (teams:"Automation_Alert_Delay_TIERIANDII_DG" OR teams:"I_EXT_SERVICESUPPORT_TIERIANDII")"""
response = requests.get(BASE_URL, headers=headers, params=params)
if response.status_code == 200:
alerts = response.json().get('data', [])
for alert in alerts:
print(f"ID: {alert['id']} | Message: {alert['message']} | Status: {alert['status']}")
else:
print(f"Failed to fetch alerts: {response.status_code} - {response.text}")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.