Hey,
The trap here is treating serviceLevel as a metric you can sum. It’s just a boolean flag per interval, so summing it tells you nothing about the actual percentage. You need to calculate the weighted average based on the volume of conversations in each interval.
Here’s how I handle this in Python. You pull the intervals, loop through them, and track the total answered calls and the total answered calls that met the SLA. The API gives you serviceLevel (true/false) alongside answeredCount. If serviceLevel is true, those answeredCount calls contributed to the SLA.
total_answered = 0
total_sla_met = 0
for interval in response['items']:
answered = interval['metrics']['answered']['count']
# Check if the majority of calls in this interval met the threshold
# Note: The API returns true if the interval's SLA % >= target
# But for precise calc, we often rely on the boolean flag provided
if interval['metrics']['serviceLevel']['value']:
# This is a simplification. The boolean indicates if the interval
# as a whole met the SLA threshold defined in the query.
# For a precise "percentage of calls answered in time",
# you actually need the raw 'answerSpeed' histogram or
# use the 'serviceLevel' metric's 'count' if available in newer endpoints.
# However, using the boolean flag on answered count is the common quick hack.
total_sla_met += answered
total_answered += answered
if total_answered > 0:
overall_sl = (total_sla_met / total_answered) * 100
print(f"Calculated SL: {overall_sl}%")
Wait, that logic above is slightly flawed if the boolean represents the interval average, not every single call. The boolean serviceLevel.value tells you if the interval’s performance met the target, not that 100% of calls were answered in time.
The correct way is to use the serviceLevel metric’s count if you query with metric: serviceLevel. But if you only have answeredCount and the boolean, you’re stuck estimating.
Better approach: Query specifically for metric: serviceLevel and metric: answered. The serviceLevel metric in the response will have a count field representing the number of conversations that met the SLA threshold.
# In your query params:
params = {
"interval": "PT15M",
"from": start_time,
"to": end_time,
"metrics": "serviceLevel,answered" # Crucial
}
# Then in processing:
total_sla_met = sum(i['metrics']['serviceLevel']['count'] for i in response['items'])
total_answered = sum(i['metrics']['answered']['count'] for i in response['items'])
overall_sl = (total_sla_met / total_answered) * 100
This gives you the exact percentage. Don’t rely on the boolean flag for aggregation.