Commit 56f7bd67 authored by Vitali Tatarintev's avatar Vitali Tatarintev Committed by Peter Leitzen

Add AlertManagement::Alert#details method

Renders the alert payload as inline hash, and skips alert attributes
parent d1618331
...@@ -18,6 +18,11 @@ module Types ...@@ -18,6 +18,11 @@ module Types
null: true, null: true,
description: 'Title of the alert' description: 'Title of the alert'
field :description,
GraphQL::STRING_TYPE,
null: true,
description: 'Description of the alert'
field :severity, field :severity,
AlertManagement::SeverityEnum, AlertManagement::SeverityEnum,
null: true, null: true,
...@@ -38,6 +43,11 @@ module Types ...@@ -38,6 +43,11 @@ module Types
null: true, null: true,
description: 'Monitoring tool the alert came from' description: 'Monitoring tool the alert came from'
field :hosts,
[GraphQL::STRING_TYPE],
null: true,
description: 'List of hosts the alert came from'
field :started_at, field :started_at,
Types::TimeType, Types::TimeType,
null: true, null: true,
...@@ -53,6 +63,21 @@ module Types ...@@ -53,6 +63,21 @@ module Types
null: true, null: true,
description: 'Number of events of this alert', description: 'Number of events of this alert',
method: :events method: :events
field :details,
GraphQL::Types::JSON,
null: true,
description: 'Alert details'
field :created_at,
Types::TimeType,
null: true,
description: 'Timestamp the alert was created'
field :updated_at,
Types::TimeType,
null: true,
description: 'Timestamp the alert was last updated'
end end
end end
end end
...@@ -53,6 +53,12 @@ module AlertManagement ...@@ -53,6 +53,12 @@ module AlertManagement
end end
end end
def details
details_payload = payload.except(*attributes.keys)
Gitlab::Utils::InlineHash.merge_keys(details_payload)
end
private private
def hosts_length def hosts_length
......
---
title: Exposes description, hosts, details, and timestamps for Alert Management Alert GraphQL
merge_request: 31091
author:
type: changed
...@@ -142,6 +142,21 @@ type AdminSidekiqQueuesDeleteJobsPayload { ...@@ -142,6 +142,21 @@ type AdminSidekiqQueuesDeleteJobsPayload {
Describes an alert from the project's Alert Management Describes an alert from the project's Alert Management
""" """
type AlertManagementAlert { type AlertManagementAlert {
"""
Timestamp the alert was created
"""
createdAt: Time
"""
Description of the alert
"""
description: String
"""
Alert details
"""
details: JSON
""" """
Timestamp the alert ended Timestamp the alert ended
""" """
...@@ -152,6 +167,11 @@ type AlertManagementAlert { ...@@ -152,6 +167,11 @@ type AlertManagementAlert {
""" """
eventCount: Int eventCount: Int
"""
List of hosts the alert came from
"""
hosts: [String!]
""" """
Internal ID of the alert Internal ID of the alert
""" """
...@@ -186,6 +206,11 @@ type AlertManagementAlert { ...@@ -186,6 +206,11 @@ type AlertManagementAlert {
Title of the alert Title of the alert
""" """
title: String title: String
"""
Timestamp the alert was last updated
"""
updatedAt: Time
} }
""" """
......
...@@ -394,6 +394,48 @@ ...@@ -394,6 +394,48 @@
"name": "AlertManagementAlert", "name": "AlertManagementAlert",
"description": "Describes an alert from the project's Alert Management", "description": "Describes an alert from the project's Alert Management",
"fields": [ "fields": [
{
"name": "createdAt",
"description": "Timestamp the alert was created",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "Time",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "description",
"description": "Description of the alert",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "details",
"description": "Alert details",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "JSON",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{ {
"name": "endedAt", "name": "endedAt",
"description": "Timestamp the alert ended", "description": "Timestamp the alert ended",
...@@ -422,6 +464,28 @@ ...@@ -422,6 +464,28 @@
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
}, },
{
"name": "hosts",
"description": "List of hosts the alert came from",
"args": [
],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
},
"isDeprecated": false,
"deprecationReason": null
},
{ {
"name": "iid", "name": "iid",
"description": "Internal ID of the alert", "description": "Internal ID of the alert",
...@@ -523,6 +587,20 @@ ...@@ -523,6 +587,20 @@
}, },
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
},
{
"name": "updatedAt",
"description": "Timestamp the alert was last updated",
"args": [
],
"type": {
"kind": "SCALAR",
"name": "Time",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
} }
], ],
"inputFields": null, "inputFields": null,
......
...@@ -52,8 +52,12 @@ Describes an alert from the project's Alert Management ...@@ -52,8 +52,12 @@ Describes an alert from the project's Alert Management
| Name | Type | Description | | Name | Type | Description |
| --- | ---- | ---------- | | --- | ---- | ---------- |
| `createdAt` | Time | Timestamp the alert was created |
| `description` | String | Description of the alert |
| `details` | JSON | Alert details |
| `endedAt` | Time | Timestamp the alert ended | | `endedAt` | Time | Timestamp the alert ended |
| `eventCount` | Int | Number of events of this alert | | `eventCount` | Int | Number of events of this alert |
| `hosts` | String! => Array | List of hosts the alert came from |
| `iid` | ID! | Internal ID of the alert | | `iid` | ID! | Internal ID of the alert |
| `monitoringTool` | String | Monitoring tool the alert came from | | `monitoringTool` | String | Monitoring tool the alert came from |
| `service` | String | Service the alert came from | | `service` | String | Service the alert came from |
...@@ -61,6 +65,7 @@ Describes an alert from the project's Alert Management ...@@ -61,6 +65,7 @@ Describes an alert from the project's Alert Management
| `startedAt` | Time | Timestamp the alert was raised | | `startedAt` | Time | Timestamp the alert was raised |
| `status` | AlertManagementStatus | Status of the alert | | `status` | AlertManagementStatus | Status of the alert |
| `title` | String | Title of the alert | | `title` | String | Title of the alert |
| `updatedAt` | Time | Timestamp the alert was last updated |
## AwardEmoji ## AwardEmoji
......
...@@ -11,13 +11,18 @@ describe GitlabSchema.types['AlertManagementAlert'] do ...@@ -11,13 +11,18 @@ describe GitlabSchema.types['AlertManagementAlert'] do
expected_fields = %i[ expected_fields = %i[
iid iid
title title
description
severity severity
status status
service service
monitoring_tool monitoring_tool
hosts
started_at started_at
ended_at ended_at
event_count event_count
details
created_at
updated_at
] ]
expect(described_class).to have_graphql_fields(*expected_fields) expect(described_class).to have_graphql_fields(*expected_fields)
......
...@@ -126,4 +126,30 @@ describe AlertManagement::Alert do ...@@ -126,4 +126,30 @@ describe AlertManagement::Alert do
it { is_expected.to match_array(alert_1) } it { is_expected.to match_array(alert_1) }
end end
describe '.details' do
let(:payload) do
{
'title' => 'Details title',
'custom' => {
'alert' => {
'fields' => %w[one two]
}
},
'yet' => {
'another' => 'field'
}
}
end
let(:alert) { build(:alert_management_alert, title: 'Details title', payload: payload) }
subject { alert.details }
it 'renders the payload as inline hash' do
is_expected.to eq(
'custom.alert.fields' => %w[one two],
'yet.another' => 'field'
)
end
end
end end
...@@ -4,10 +4,11 @@ require 'spec_helper' ...@@ -4,10 +4,11 @@ require 'spec_helper'
describe 'getting Alert Management Alerts' do describe 'getting Alert Management Alerts' do
include GraphqlHelpers include GraphqlHelpers
let_it_be(:payload) { { 'custom' => { 'alert' => 'payload' } } }
let_it_be(:project) { create(:project, :repository) } let_it_be(:project) { create(:project, :repository) }
let_it_be(:current_user) { create(:user) } let_it_be(:current_user) { create(:user) }
let_it_be(:alert_1) { create(:alert_management_alert, :all_fields, project: project) } let_it_be(:alert_1) { create(:alert_management_alert, :all_fields, project: project) }
let_it_be(:alert_2) { create(:alert_management_alert, :all_fields, project: project) } let_it_be(:alert_2) { create(:alert_management_alert, :all_fields, project: project, payload: payload) }
let(:fields) do let(:fields) do
<<~QUERY <<~QUERY
...@@ -55,13 +56,18 @@ describe 'getting Alert Management Alerts' do ...@@ -55,13 +56,18 @@ describe 'getting Alert Management Alerts' do
expect(first_alert).to include( expect(first_alert).to include(
'iid' => alert_2.iid.to_s, 'iid' => alert_2.iid.to_s,
'title' => alert_2.title, 'title' => alert_2.title,
'description' => alert_2.description,
'severity' => alert_2.severity.upcase, 'severity' => alert_2.severity.upcase,
'status' => alert_2.status.upcase, 'status' => alert_2.status.upcase,
'monitoringTool' => alert_2.monitoring_tool, 'monitoringTool' => alert_2.monitoring_tool,
'service' => alert_2.service, 'service' => alert_2.service,
'hosts' => alert_2.hosts,
'eventCount' => alert_2.events, 'eventCount' => alert_2.events,
'startedAt' => alert_2.started_at.strftime('%Y-%m-%dT%H:%M:%SZ'), 'startedAt' => alert_2.started_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
'endedAt' => alert_2.ended_at.strftime('%Y-%m-%dT%H:%M:%SZ') 'endedAt' => alert_2.ended_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
'details' => { 'custom.alert' => 'payload' },
'createdAt' => alert_2.created_at.strftime('%Y-%m-%dT%H:%M:%SZ'),
'updatedAt' => alert_2.updated_at.strftime('%Y-%m-%dT%H:%M:%SZ')
) )
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment