Skip to main content

Simple Example

Simple example for v2/workloads/batch API - Batch download with polling.

Use this when you need to download large volumes of workloads that exceed the scroll API limits.

The batch API processes your request asynchronously and provides presigned URLs to download results.


Code Example

import requests
import logging
import time
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: Submit batch request
end_time = datetime.utcnow()
start_time = end_time - timedelta(hours=6)

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")
}

headers = {
"Authorization": f"Bearer {access_token}",
"Content-Type": "application/json",
"client_id": CLIENT_ID
}

logger.info("Submitting batch request...")
response = requests.post(
f"{BASE_URL}/v2/workloads/batch",
json=payload,
headers=headers
)

if response.status_code == 200:
data = response.json()
token = data.get("data", {}).get("token")
logger.info(f"Batch request submitted. Token: {token}")

# Step 3: Poll for results
logger.info("Polling for results...")
max_attempts = 60 # Poll for up to 5 minutes
attempt = 0

while attempt < max_attempts:
time.sleep(5) # Wait 5 seconds between polls
attempt += 1

# Query batch status
status_response = requests.post(
f"{BASE_URL}/v2/workloads/batch/status",
json={"token": token},
headers=headers
)

if status_response.status_code == 200:
status_data = status_response.json()
partitions = status_data.get("presigned_urls", [])
completed = sum(1 for p in partitions if p.get("completed"))
total = len(partitions)

logger.info(f"Progress: {completed}/{total} partitions completed")

# Check if all partitions are complete
if completed == total:
logger.info("All partitions completed!")

# Display results
for i, partition in enumerate(partitions, 1):
logger.info(f"Partition {i}:")
logger.info(f" - Time range: {partition.get('start_time')} to {partition.get('end_time')}")
logger.info(f" - Workload count: {partition.get('workload_count')}")
logger.info(f" - Download URL: {partition.get('presigned_url')[:80]}...")
break
elif status_response.status_code == 404:
logger.error("Token not found or expired")
break
else:
logger.warning("Polling timeout - request may still be processing")
else:
logger.error(f"Batch request failed: {response.status_code}")
logger.error(f" {response.text}")

Using Presigned URLs

Once you have the presigned URLs from the batch response, you can download the workloads using a simple HTTP GET request:

import requests
import json

# Example: Download workloads from a presigned URL
presigned_url = "https://s3.amazonaws.com/bucket/workloads?AWSAccessKeyId=...&Signature=...&Expires=..."

# Make GET request to download workloads
response = requests.get(presigned_url)

if response.status_code == 200:
# Parse JSON response
workloads = response.json()

logger.info(f"Downloaded {len(workloads)} workload entries")

# Process each workload entry
for workload_entry in workloads:
workload_data = workload_entry.get('_source', {})
# Process workload data as needed
print(f"Timestamp: {workload_data.get('@timestamp')}")
print(f"Workflow: {workload_data.get('workflow', {}).get('name', 'N/A')}")
print("---")
else:
logger.error(f"Failed to download workloads: {response.status_code}")

You can also download the workloads directly in a browser by opening the presigned URL, which will download a JSON file containing the workloads.