Skip to main content

Query Defects and Root Causes

This query example demonstrates how to retrieve the first 20 high-severity, active defects and root causes from the Causely platform using the GraphQL utilities defined above.

note

The START_TIME is set to the current time in UTC. You may need to adjust the timestamp to your needs.

Prerequisite

This example reuses the helper utilities defined in the Authentication and GraphQL Clients sections—fetching an access token, creating the GraphQL client, and sending a request with the post_query wrapper (CLI, Python, or Go). Keep those helpers in scope before running this query.

import os
from datetime import datetime, timezone

if __name__ == "__main__":
# Get credentials from environment
cid, secret = os.getenv("CAUSELY_CLIENT_ID"), os.getenv("CAUSELY_CLIENT_SECRET")
if not cid or not secret:
raise EnvironmentError("Missing CAUSELY_CLIENT_ID or CAUSELY_CLIENT_SECRET")

# Get access token and create GraphQL client
token = get_causely_access_token(cid, secret)
client = create_graphql_client(token)

# Prepare query variables
start_time = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S") + ".000+00:00"
variables = {
"defectFilter": {
"state": "ACTIVE",
"severities": ["Critical", "High"],
"startTime": start_time,
"scopesFilter": {"scopes": []},
"entityTypes": [],
"defectNames": [],
"entityName": ""
},
"groupRecurring": True,
"first": 20
}

# Execute the query
query = """
fragment LabelFragment on Label { key value __typename }
fragment BasicEntityWithLabelsFragment on Entity { id name typeName labels { ...LabelFragment __typename } __typename }
fragment DefectFragment on Defect { id name fromTime toTime remediated serviceCount entityId entityType entity { ...BasicEntityWithLabelsFragment __typename } activeCount missingCount severity __typename }
fragment DefectRelatedOccurrencesFragment on Defect { relatedOccurrrences { id name fromTime toTime entity { id name typeName __typename } severity __typename } __typename }
fragment BasicEntityFragment on Entity { id name typeName __typename }
fragment SymptomFragmentWithoutLabels on Symptom { id name active state entityId entityType fromTime toTime isPropagated entity { ...BasicEntityFragment __typename } __typename }
fragment EventFragmentWithoutLabels on Event { id name active time entity { ...BasicEntityFragment __typename } __typename }
query defectConnection($defectFilter: DefectFilter, $groupRecurring: Boolean, $first: Int, $after: String, $last: Int, $before: String) {
defectConnection(defectFilter: $defectFilter groupRecurring: $groupRecurring first: $first after: $after last: $last before: $before) {
totalCount edges { node { ...DefectFragment ...DefectRelatedOccurrencesFragment symptoms { ...SymptomFragmentWithoutLabels __typename } events { ...EventFragmentWithoutLabels __typename } __typename } cursor __typename }
pageInfo { hasNextPage hasPreviousPage startCursor endCursor totalCount __typename } __typename
}
}
"""

result = post_query(client, query, variables)
print(result)