Simple Example
Simple example for v2/workloads/scroll API - Single request without pagination.
Use this when you want to retrieve a small number of workloads without handling pagination.
Code Example
import requests
import logging
from datetime import datetime, timedelta
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Configuration
BASE_URL = "https://central-monitoring-data-api.mywizard-aiops.com"
TOKEN_URL = "https://your-auth-endpoint.com/oauth2/token"
CLIENT_ID = "your-client-id"
CLIENT_SECRET = "your-client-secret"
# Step 1: Get access token
logger.info("Getting access token...")
token_response = requests.post(
TOKEN_URL,
data={
"grant_type": "client_credentials",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET
},
headers={"Content-Type": "application/x-www-form-urlencoded"}
)
access_token = token_response.json()["access_token"]
logger.info("Authenticated")
# Step 2: Prepare request
end_time = datetime.utcnow()
start_time = end_time - timedelta(hours=1)
payload = {
"application": "atr",
"app_type": "kubernetes",
"domain": ["*"],
"start_time": start_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
"end_time": end_time.strftime("%Y-%m-%dT%H:%M:%SZ"),
"size": 50
}
headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
"client_id": CLIENT_ID
}
# Step 3: Make request
logger.info("Fetching workloads...")
response = requests.post(
f"{BASE_URL}/v2/workloads/scroll",
json=payload,
headers=headers
)
# Step 4: Process response
if response.status_code == 200:
data = response.json()
workloads = data.get("data", {}).get("workloads", [])
workload_count = data.get("meta", {}).get("workloads_available", 0)
scroll_id = data.get("meta", {}).get("scroll_id")
logger.info(f"Retrieved {len(workloads)} workloads (total available: {workload_count})")
if scroll_id:
logger.info(f"More workloads available. Use scroll_id to fetch next page:")
logger.info(f"scroll_id: {scroll_id[:50]}...")
# Display first workload
if workloads:
logger.info("First workload:")
source = workloads[0].get('_source', {})
logger.info(f" Timestamp: {source.get('@timestamp')}")
logger.info(f" Workflow: {source.get('workflow', {}).get('name', 'N/A')}")
logger.info(f" Domain: {source.get('atr', {}).get('domain_name', 'N/A')}")
else:
logger.error(f"Request failed: {response.status_code}")
logger.error(f" {response.text}")