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
null: true,
description: 'Title of the alert'
field :description,
GraphQL::STRING_TYPE,
null: true,
description: 'Description of the alert'
field :severity,
AlertManagement::SeverityEnum,
null: true,
......@@ -38,6 +43,11 @@ module Types
null: true,
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,
Types::TimeType,
null: true,
......@@ -53,6 +63,21 @@ module Types
null: true,
description: 'Number of events of this alert',
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
......@@ -53,6 +53,12 @@ module AlertManagement
end
end
def details
details_payload = payload.except(*attributes.keys)
Gitlab::Utils::InlineHash.merge_keys(details_payload)
end
private
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 {
Describes an alert from the project's Alert Management
"""
type AlertManagementAlert {
"""
Timestamp the alert was created
"""
createdAt: Time
"""
Description of the alert
"""
description: String
"""
Alert details
"""
details: JSON
"""
Timestamp the alert ended
"""
......@@ -152,6 +167,11 @@ type AlertManagementAlert {
"""
eventCount: Int
"""
List of hosts the alert came from
"""
hosts: [String!]
"""
Internal ID of the alert
"""
......@@ -186,6 +206,11 @@ type AlertManagementAlert {
Title of the alert
"""
title: String
"""
Timestamp the alert was last updated
"""
updatedAt: Time
}
"""
......
......@@ -394,6 +394,48 @@
"name": "AlertManagementAlert",
"description": "Describes an alert from the project's Alert Management",
"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",
"description": "Timestamp the alert ended",
......@@ -422,6 +464,28 @@
"isDeprecated": false,
"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",
"description": "Internal ID of the alert",
......@@ -523,6 +587,20 @@
},
"isDeprecated": false,
"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,
......
......@@ -52,8 +52,12 @@ Describes an alert from the project's Alert Management
| 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 |
| `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 |
| `monitoringTool` | String | Monitoring tool the alert came from |
| `service` | String | Service the alert came from |
......@@ -61,6 +65,7 @@ Describes an alert from the project's Alert Management
| `startedAt` | Time | Timestamp the alert was raised |
| `status` | AlertManagementStatus | Status of the alert |
| `title` | String | Title of the alert |
| `updatedAt` | Time | Timestamp the alert was last updated |
## AwardEmoji
......
......@@ -11,13 +11,18 @@ describe GitlabSchema.types['AlertManagementAlert'] do
expected_fields = %i[
iid
title
description
severity
status
service
monitoring_tool
hosts
started_at
ended_at
event_count
details
created_at
updated_at
]
expect(described_class).to have_graphql_fields(*expected_fields)
......
......@@ -126,4 +126,30 @@ describe AlertManagement::Alert do
it { is_expected.to match_array(alert_1) }
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
......@@ -4,10 +4,11 @@ require 'spec_helper'
describe 'getting Alert Management Alerts' do
include GraphqlHelpers
let_it_be(:payload) { { 'custom' => { 'alert' => 'payload' } } }
let_it_be(:project) { create(:project, :repository) }
let_it_be(:current_user) { create(:user) }
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
<<~QUERY
......@@ -55,13 +56,18 @@ describe 'getting Alert Management Alerts' do
expect(first_alert).to include(
'iid' => alert_2.iid.to_s,
'title' => alert_2.title,
'description' => alert_2.description,
'severity' => alert_2.severity.upcase,
'status' => alert_2.status.upcase,
'monitoringTool' => alert_2.monitoring_tool,
'service' => alert_2.service,
'hosts' => alert_2.hosts,
'eventCount' => alert_2.events,
'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
......
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