Skip to content

Commit 5607f6b

Browse files
author
Deepanshu Deepanshu
committed
made changes in code
1 parent 89b906c commit 5607f6b

File tree

4 files changed

+84
-24
lines changed

4 files changed

+84
-24
lines changed

pkg/ui/constants.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
// Page Titles
2323
AlertsPageTitle = "Alerts"
2424
AlertDataPageTitle = "Metadata"
25+
AlertMetadata = "AlertData"
2526
TrigerredAlertsPageTitle = "Trigerred"
2627
HighAlertsPageTitle = "High Alerts"
2728
LowAlertsPageTitle = "Low Alerts"
@@ -32,10 +33,10 @@ const (
3233
AllTeamsOncallPageTitle = "All Teams Oncall"
3334
ServiceLogsPageTitle = "Service Logs"
3435

35-
// Footer
36+
//Footer
3637
FooterText = "[Esc] Go Back"
37-
FooterTextAlerts = "[R] Refresh Alerts | [1] Trigerred Alerts | [2] Acknowledged Incidents | [3] Trigerred Incidents\n" + FooterText
38-
FooterTextTrigerredAlerts = "[1] Trigerred Alerts | [2] Acknowledged Incidents | [3] Trigerred Incidents\n" + FooterText
38+
FooterTextAlerts = "[R] Refresh Alerts | [1] Acknowledged Incidents | [2] Trigerred Incidents\n" + FooterText
39+
FooterTextTrigerredAlerts = "[1] Acknowledged Incidents | [2] Trigerred Incidents\n" + FooterText
3940
FooterTextIncidents = "[ENTER] Select Incident | [CTRL+A] Acknowledge Incidents\n" + FooterText
4041
FooterTextOncall = "[N] Your Next Oncall Schedule | [A] All Teams Oncall | [<-] Previous Layer Oncall | [->] Next Layer Oncall \n" + FooterText
4142
TerminalFooterText = "[CTRL + N] Next Slide | [CTRL + P] Previous Slide | [CTRL + A] Add Slide | [CTRL + E] Exit Slide | [CTRL + B] + [Num] Change to Slide with [Num] | [CTRL + Q] Quit "

pkg/ui/events.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ package ui
33
import (
44
"fmt"
55

6-
"github.com/gdamore/tcell/v2"
6+
pdApi "github.com/PagerDuty/go-pagerduty"
7+
"github.com/openshift/pagerduty-short-circuiter/pkg/client"
78
"github.com/openshift/pagerduty-short-circuiter/pkg/ocm"
89
pdcli "github.com/openshift/pagerduty-short-circuiter/pkg/pdcli/alerts"
910
"github.com/openshift/pagerduty-short-circuiter/pkg/utils"
@@ -45,17 +46,39 @@ func (tui *TUI) SetIncidentsTableEvents() {
4546
tui.SelectedIncidents = make(map[string]string)
4647
tui.IncidentsTable.SetSelectedFunc(func(row, column int) {
4748

49+
var incident pdApi.Incident
50+
client, _ := client.NewClient().Connect()
4851
incidentID := tui.IncidentsTable.GetCell(row, 0).Text
52+
incident.Id = incidentID
53+
var clusterName string
54+
var alertData string
55+
56+
alerts, _ := pdcli.GetIncidentAlerts(client, incident)
57+
Alert := alerts[0]
58+
59+
for _, alert := range alerts {
60+
if incidentID == alert.IncidentID {
61+
alertData = pdcli.ParseAlertMetaData(alert)
62+
clusterName = alert.ClusterName
63+
tui.ClusterID = alert.ClusterID
64+
break
65+
}
66+
}
67+
if len(alerts) == 1 {
68+
alertData = pdcli.ParseAlertMetaData(Alert)
69+
tui.AlertMetadata.SetText(alertData)
70+
tui.Pages.AddAndSwitchToPage(AlertMetadata, tui.AlertMetadata, true)
4971

50-
if _, ok := tui.SelectedIncidents[incidentID]; !ok || tui.SelectedIncidents[incidentID] == "" {
51-
tui.IncidentsTable.GetCell(row, 0).SetTextColor(tcell.ColorLimeGreen)
52-
tui.SelectedIncidents[incidentID] = incidentID
53-
utils.InfoLogger.Printf("Selected incident: %s", incidentID)
5472
} else {
55-
tui.IncidentsTable.GetCell(row, 0).SetTextColor(tcell.ColorWhite)
56-
tui.SelectedIncidents[incidentID] = ""
57-
utils.InfoLogger.Printf("Deselected incident: %s", incidentID)
73+
tui.SetAlertsTableEvents(alerts)
74+
tui.InitAlertsUI(alerts, AlertMetadata, AlertMetadata)
75+
5876
}
77+
// Do not prompt for cluster login if there's no cluster ID associated with the alert (v3 clusters)
78+
if tui.ClusterID != "N/A" && tui.ClusterID != "" && alertData != "" {
79+
tui.SecondaryWindow.SetText(fmt.Sprintf("Press 'Y' to log into the cluster: %s", clusterName)).SetTextColor(PromptTextColor)
80+
}
81+
5982
})
6083
}
6184

@@ -90,6 +113,7 @@ func (tui *TUI) ackowledgeSelectedIncidents() {
90113
tui.AckIncidents = []string{}
91114

92115
// Refresh Page
116+
93117
tui.SetIncidentsTableEvents()
94118
tui.Pages.SwitchToPage(IncidentsPageTitle)
95119
}

pkg/ui/input.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/gdamore/tcell/v2"
1212

1313
"github.com/openshift/pagerduty-short-circuiter/pkg/constants"
14-
pdcli "github.com/openshift/pagerduty-short-circuiter/pkg/pdcli/alerts"
1514
"github.com/openshift/pagerduty-short-circuiter/pkg/utils"
1615
)
1716

@@ -43,19 +42,20 @@ func (tui *TUI) initKeyboard() {
4342
case ServiceLogsPageTitle:
4443
tui.Pages.SwitchToPage(AlertDataPageTitle)
4544
tui.InitAlertDataSecondaryView()
45+
46+
case AlertMetadata:
47+
tui.Pages.SwitchToPage(IncidentsPageTitle)
4648
default:
4749
tui.InitAlertsUI(tui.Alerts, AlertsTableTitle, AlertsPageTitle)
4850
tui.Pages.SwitchToPage(AlertsPageTitle)
4951
tui.Footer.SetText(FooterTextAlerts)
5052
}
5153
}
52-
5354
// Check if oncall command is executed
5455
if title, _ := tui.Pages.GetFrontPage(); strings.Contains(title, "Oncall") {
5556
tui.Pages.SwitchToPage(fmt.Sprintf("%s%d", OncallPageTitle, 2))
5657
tui.Footer.SetText(FooterTextOncall)
5758
}
58-
5959
return nil
6060
}
6161
if event.Key() == tcell.KeyLeft && CursorPos > 0 {
@@ -164,11 +164,6 @@ func (tui *TUI) setupAlertsPageInput() {
164164
tui.Pages.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
165165

166166
if event.Rune() == '1' {
167-
utils.InfoLogger.Print("Switching to trigerred alerts view")
168-
tui.InitAlertsUI(pdcli.TrigerredAlerts, TrigerredAlertsTableTitle, TrigerredAlertsPageTitle)
169-
}
170-
171-
if event.Rune() == '2' {
172167
utils.InfoLogger.Print("Switching to acknowledged incidents view")
173168
tui.SeedAckIncidentsUI()
174169

@@ -179,7 +174,7 @@ func (tui *TUI) setupAlertsPageInput() {
179174
tui.Pages.SwitchToPage(AckIncidentsPageTitle)
180175
}
181176

182-
if event.Rune() == '3' {
177+
if event.Rune() == '2' {
183178
utils.InfoLogger.Print("Switching to incidents view")
184179
tui.SeedIncidentsUI()
185180

@@ -195,7 +190,6 @@ func (tui *TUI) setupAlertsPageInput() {
195190
utils.InfoLogger.Print("Refreshing alerts...")
196191
tui.SeedAlertsUI()
197192
}
198-
199193
return event
200194
})
201195
}
@@ -217,7 +211,6 @@ func (tui *TUI) setupIncidentsPageInput() {
217211
tui.ackowledgeSelectedIncidents()
218212
}
219213
}
220-
221214
return event
222215
})
223216
}
@@ -262,7 +255,6 @@ func (tui *TUI) setupOncallPageInput() {
262255
if event.Rune() == 'N' || event.Rune() == 'n' {
263256
utils.InfoLogger.Print("Viewing user next on-call schedule")
264257
tui.Pages.SwitchToPage(NextOncallPageTitle)
265-
tui.Footer.SetText(FooterText)
266258

267259
if len(tui.AckIncidents) == 0 {
268260
utils.InfoLogger.Print("You are not scheduled for any oncall duties for the next 3 months. Cheer up!")
@@ -274,7 +266,6 @@ func (tui *TUI) setupOncallPageInput() {
274266
if event.Rune() == 'A' || event.Rune() == 'a' {
275267
utils.InfoLogger.Print("Switching to all team on-call view")
276268
tui.Pages.SwitchToPage(AllTeamsOncallPageTitle)
277-
tui.Footer.SetText(FooterText)
278269
}
279270
}
280271
if event.Key() == tcell.KeyLeft {

tests/alerts_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,50 @@ var _ = Describe("view alerts", func() {
222222
})
223223
})
224224

225+
When("the triggered incidents are fetched", func() {
226+
It("gives the alert metadata page for triggered incidents", func() {
227+
228+
var alertData pdcli.Alert
229+
230+
alertResponse := &pdApi.ListAlertsResponse{
231+
Alerts: []pdApi.IncidentAlert{
232+
alert(
233+
"incident-id-1",
234+
"my-service-id",
235+
"alert-name",
236+
"cluster-id",
237+
"triggered",
238+
),
239+
},
240+
}
241+
242+
// Set the mock cluster name
243+
serviceResponse := &pdApi.Service{
244+
Description: "my-cluster-name",
245+
}
246+
247+
expectedAlertData := pdcli.Alert{
248+
IncidentID: "incident-id-1",
249+
Name: "alert-name",
250+
ClusterID: "cluster-id",
251+
ClusterName: "my-cluster-name",
252+
Status: "triggered",
253+
Console: "<nil>",
254+
Labels: "<nil>",
255+
Sop: "<nil>",
256+
}
257+
258+
mockClient.EXPECT().GetService("my-service-id", gomock.Any()).Return(serviceResponse, nil).Times(1)
259+
260+
err := alertData.ParseAlertData(mockClient, &alertResponse.Alerts[0])
261+
262+
Expect(err).ShouldNot(HaveOccurred())
263+
264+
Expect(alertData).To(Equal(expectedAlertData))
265+
266+
})
267+
})
268+
225269
When("a user acknowledges an incident(s)", func() {
226270
It("it changes the incident status to acknowledged and returns the incident(s)", func() {
227271

0 commit comments

Comments
 (0)