|
package smoke |
|
|
|
import ( |
|
"encoding/json" |
|
"fmt" |
|
"testing" |
|
"time" |
|
|
|
"github.com/stretchr/testify/require" |
|
"github.com/target/goalert/test/smoke/harness" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestGraphQLServiceAlertStats(t *testing.T) { |
|
t.Parallel() |
|
|
|
const sql = ` |
|
insert into escalation_policies (id, name) |
|
values |
|
({{uuid "eid"}}, 'esc policy'); |
|
insert into services (id, escalation_policy_id, name) |
|
values |
|
({{uuid "sid"}}, {{uuid "eid"}}, 'service'); |
|
insert into alerts (id, service_id, status, summary) |
|
values |
|
(1, {{uuid "sid"}}, 'closed', 'alert 1'), |
|
(2, {{uuid "sid"}}, 'closed', 'alert 2'), |
|
(3, {{uuid "sid"}}, 'closed', 'alert 3'), |
|
(4, {{uuid "sid"}}, 'closed', 'alert 4'), |
|
(5, {{uuid "sid"}}, 'closed', 'alert 5'); |
|
insert into alert_metrics (alert_id, service_id, time_to_ack, time_to_close, escalated, closed_at) |
|
values |
|
-- First 4 alerts: consistent timing, all in 2022-01-01 |
|
(1, {{uuid "sid"}}, '1 hour'::interval, '2 hours'::interval, false, '2022-01-01 00:01:00'), |
|
(2, {{uuid "sid"}}, '1 hour'::interval, '2 hours'::interval, false, '2022-01-01 00:02:00'), |
|
(3, {{uuid "sid"}}, '1 hour'::interval, '2 hours'::interval, false, '2022-01-01 00:03:00'), |
|
(4, {{uuid "sid"}}, '1 hour'::interval, '2 hours'::interval, false, '2022-01-01 00:04:00'), |
|
-- Alert 5: different timing, escalated, closed on 2022-01-02 (outside first window) |
|
(5, {{uuid "sid"}}, '30 minutes'::interval, '3 hours'::interval, true, '2022-01-02 00:05:00'); |
|
` |
|
|
|
h := harness.NewHarness(t, sql, "om-history-index") |
|
defer h.Close() |
|
|
|
|
|
resp1 := h.GraphQLQueryT(t, fmt.Sprintf(`{service(id: "%s") {alertStats(input: {start: "2022-01-01T00:00:00Z", end: "2022-01-02T00:00:00Z"}) {avgAckSec{value}, avgCloseSec{value}, alertCount{value}, escalatedCount{value}}}}`, h.UUID("sid"))) |
|
|
|
type bucket struct { |
|
Start, End time.Time |
|
Value float64 |
|
} |
|
type statsResponse struct { |
|
Service struct { |
|
AlertStats struct { |
|
AvgAckSec []bucket |
|
AvgCloseSec []bucket |
|
AlertCount []bucket |
|
EscalatedCount []bucket |
|
} |
|
} |
|
} |
|
var result1 statsResponse |
|
err := json.Unmarshal(resp1.Data, &result1) |
|
require.NoError(t, err, "should return valid JSON") |
|
|
|
|
|
require.Len(t, result1.Service.AlertStats.AvgAckSec, 1, "should have one time bucket") |
|
require.Len(t, result1.Service.AlertStats.AvgCloseSec, 1, "should have one time bucket") |
|
require.Len(t, result1.Service.AlertStats.AlertCount, 1, "should have one time bucket") |
|
require.Len(t, result1.Service.AlertStats.EscalatedCount, 1, "should have one time bucket") |
|
|
|
|
|
require.Equal(t, 3600.0, result1.Service.AlertStats.AvgAckSec[0].Value, "average ack time should be 1 hour (3600 sec)") |
|
require.Equal(t, 7200.0, result1.Service.AlertStats.AvgCloseSec[0].Value, "average close time should be 2 hours (7200 sec)") |
|
require.Equal(t, 4.0, result1.Service.AlertStats.AlertCount[0].Value, "should have 4 alerts") |
|
require.Equal(t, 0.0, result1.Service.AlertStats.EscalatedCount[0].Value, "should have 0 escalated alerts") |
|
|
|
|
|
resp2 := h.GraphQLQueryT(t, fmt.Sprintf(`{service(id: "%s") {alertStats(input: {start: "2022-01-01T00:00:00Z", end: "2022-01-03T00:00:00Z"}) {avgAckSec{value}, avgCloseSec{value}, alertCount{value}, escalatedCount{value}}}}`, h.UUID("sid"))) |
|
|
|
var result2 statsResponse |
|
err = json.Unmarshal(resp2.Data, &result2) |
|
require.NoError(t, err, "should return valid JSON") |
|
|
|
|
|
|
|
require.Len(t, result2.Service.AlertStats.AvgAckSec, 2, "should have two time buckets") |
|
require.Len(t, result2.Service.AlertStats.AvgCloseSec, 2, "should have two time buckets") |
|
require.Len(t, result2.Service.AlertStats.AlertCount, 2, "should have two time buckets") |
|
require.Len(t, result2.Service.AlertStats.EscalatedCount, 2, "should have two time buckets") |
|
|
|
|
|
require.Equal(t, 3600.0, result2.Service.AlertStats.AvgAckSec[0].Value, "Jan 1 average ack time should be 1 hour") |
|
require.Equal(t, 7200.0, result2.Service.AlertStats.AvgCloseSec[0].Value, "Jan 1 average close time should be 2 hours") |
|
require.Equal(t, 4.0, result2.Service.AlertStats.AlertCount[0].Value, "Jan 1 should have 4 alerts") |
|
require.Equal(t, 0.0, result2.Service.AlertStats.EscalatedCount[0].Value, "Jan 1 should have 0 escalated alerts") |
|
|
|
|
|
require.Equal(t, 1800.0, result2.Service.AlertStats.AvgAckSec[1].Value, "Jan 2 average ack time should be 30 minutes (1800 sec)") |
|
require.Equal(t, 10800.0, result2.Service.AlertStats.AvgCloseSec[1].Value, "Jan 2 average close time should be 3 hours (10800 sec)") |
|
require.Equal(t, 1.0, result2.Service.AlertStats.AlertCount[1].Value, "Jan 2 should have 1 alert") |
|
require.Equal(t, 1.0, result2.Service.AlertStats.EscalatedCount[1].Value, "Jan 2 should have 1 escalated alert") |
|
|
|
|
|
resp3 := h.GraphQLQueryT(t, fmt.Sprintf(`{service(id: "%s") {alertStats(input: {start: "2022-01-02T00:00:00Z", end: "2022-01-03T00:00:00Z"}) {avgAckSec{value}, avgCloseSec{value}, alertCount{value}, escalatedCount{value}}}}`, h.UUID("sid"))) |
|
|
|
var result3 statsResponse |
|
err = json.Unmarshal(resp3.Data, &result3) |
|
require.NoError(t, err, "should return valid JSON") |
|
|
|
|
|
require.Len(t, result3.Service.AlertStats.AvgAckSec, 1, "should have one time bucket") |
|
require.Equal(t, 1800.0, result3.Service.AlertStats.AvgAckSec[0].Value, "should match alert 5's ack time (30 min)") |
|
require.Equal(t, 10800.0, result3.Service.AlertStats.AvgCloseSec[0].Value, "should match alert 5's close time (3 hours)") |
|
require.Equal(t, 1.0, result3.Service.AlertStats.AlertCount[0].Value, "should have 1 alert") |
|
require.Equal(t, 1.0, result3.Service.AlertStats.EscalatedCount[0].Value, "should have 1 escalated alert") |
|
} |
|
|