Skip to content

Commit 0aa1209

Browse files
authored
improve wait functions (#116)
* refactor silence msg handling and add tests Signed-off-by: Chris Collins <collins.christopher@gmail.com> * Improve handling of incident wait msg Improves the waitForIncidentThenDo function, removing the unnecessary sleep that may have been causing slow responsiveness in the TUI, and consolidating specific "wait" functions into the generic waitForSelectedIncidentThenDo function. Signed-off-by: Chris Collins <collins.christopher@gmail.com> --------- Signed-off-by: Chris Collins <collins.christopher@gmail.com>
1 parent d1ccd54 commit 0aa1209

File tree

4 files changed

+65
-42
lines changed

4 files changed

+65
-42
lines changed

pkg/tui/commands.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,6 @@ type unAcknowledgedIncidentsMsg struct {
545545
err error
546546
}
547547

548-
type waitForSelectedIncidentsThenAcknowledgeMsg string
549-
type waitForSelectedIncidentsThenUnAcknowledgeMsg string
550-
551548
func acknowledgeIncidents(p *pd.Config, incidents []pagerduty.Incident, reEscalate bool) tea.Cmd {
552549
return func() tea.Msg {
553550
var err error
@@ -682,6 +679,16 @@ func getDetailFieldFromAlert(f string, a pagerduty.IncidentAlert) string {
682679
return ""
683680
}
684681

682+
// getEscalationPolicyKey is a helper function to determine the escalation policy key
683+
func getEscalationPolicyKey(serviceID string, policies map[string]*pagerduty.EscalationPolicy) string {
684+
if policy, ok := policies[serviceID]; ok {
685+
log.Debug("Update", "getEscalationPolicyKey", "escalation policy override found for service", "service", serviceID, "policy", policy.Name)
686+
return serviceID
687+
}
688+
log.Debug("Update", "getEscalationPolicyKey", "no escalation policy override for service; using default", "service", serviceID, "policy", silentDefaultPolicyKey)
689+
return silentDefaultPolicyKey
690+
}
691+
685692
// stateShorthand returns the state of the incident as a single character
686693
// A = acknowledged by user
687694
// a = acknowledged by someone else

pkg/tui/commands_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,36 @@ func TestNewListIncidentOptsFromConfig(t *testing.T) {
324324
})
325325
}
326326
}
327+
func TestGetEscalationPolicyKey(t *testing.T) {
328+
mockPolicies := map[string]*pagerduty.EscalationPolicy{
329+
"service1": {Name: "Policy1"},
330+
"service2": {Name: "Policy2"},
331+
}
332+
333+
tests := []struct {
334+
name string
335+
serviceID string
336+
policies map[string]*pagerduty.EscalationPolicy
337+
expectedPolicy string
338+
}{
339+
{
340+
name: "return serviceID if policy exists for the service",
341+
serviceID: "service1",
342+
policies: mockPolicies,
343+
expectedPolicy: "service1",
344+
},
345+
{
346+
name: "return silentDefaultPolicyKey if no policy exists for the service",
347+
serviceID: "unknownService",
348+
policies: mockPolicies,
349+
expectedPolicy: silentDefaultPolicyKey,
350+
},
351+
}
352+
353+
for _, test := range tests {
354+
t.Run(test.name, func(t *testing.T) {
355+
actual := getEscalationPolicyKey(test.serviceID, test.policies)
356+
assert.Equal(t, test.expectedPolicy, actual)
357+
})
358+
}
359+
}

pkg/tui/msgHandlers.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,27 @@ func switchTableFocusMode(m model, msg tea.Msg) (tea.Model, tea.Cmd) {
218218
case key.Matches(msg, defaultKeyMap.Ack):
219219
return m, doIfIncidentSelected(&m, tea.Sequence(
220220
func() tea.Msg { return getIncidentMsg(incidentID) },
221-
func() tea.Msg { return waitForSelectedIncidentsThenAcknowledgeMsg("Ack") },
221+
func() tea.Msg {
222+
return waitForSelectedIncidentThenDoMsg{
223+
msg: "acknowledge",
224+
action: func() tea.Msg {
225+
return acknowledgeIncidentsMsg{incidents: []pagerduty.Incident{*m.selectedIncident}}
226+
},
227+
}
228+
},
222229
))
223230

224231
case key.Matches(msg, defaultKeyMap.UnAck):
225232
return m, doIfIncidentSelected(&m, tea.Sequence(
226233
func() tea.Msg { return getIncidentMsg(incidentID) },
227-
func() tea.Msg { return waitForSelectedIncidentsThenUnAcknowledgeMsg("UnAck") },
234+
func() tea.Msg {
235+
return waitForSelectedIncidentThenDoMsg{
236+
msg: "un-acknowledge",
237+
action: func() tea.Msg {
238+
return unAcknowledgeIncidentsMsg{incidents: []pagerduty.Incident{*m.selectedIncident}}
239+
},
240+
}
241+
},
228242
))
229243

230244
case key.Matches(msg, defaultKeyMap.Note):

pkg/tui/tui.go

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
377377
}
378378

379379
// This is a catch all for any action that requires a selected incident
380-
//
381380
case waitForSelectedIncidentThenDoMsg:
382381
if msg.action == nil {
383382
m.setStatus("failed to perform action: no action included in msg")
@@ -388,14 +387,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
388387
return m, nil
389388
}
390389

390+
// Re-queue the message if the selected incident is not yet available
391391
if m.selectedIncident == nil {
392-
time.Sleep(waitTime)
393392
m.setStatus("waiting for incident info...")
394-
return m, func() tea.Msg { return waitForSelectedIncidentThenDoMsg{action: msg.action, msg: msg.msg} }
393+
return m, func() tea.Msg { return msg }
395394
}
396395

396+
// Perform the action once the selected incident is available
397397
log.Debug("Update", "waitForSelectedIncidentThenDoMsg", "performing action", "action", msg.action, "incident", m.selectedIncident.ID)
398-
cmds = append(cmds, msg.action)
398+
return m, msg.action
399399

400400
case renderIncidentMsg:
401401
if m.selectedIncident == nil {
@@ -461,26 +461,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
461461

462462
return m, func() tea.Msg { return updateIncidentListMsg("sender: unAcknowledgedIncidentsMsg") }
463463

464-
case waitForSelectedIncidentsThenAcknowledgeMsg:
465-
if m.selectedIncident == nil {
466-
time.Sleep(waitTime)
467-
m.setStatus("waiting for incident info...")
468-
return m, func() tea.Msg { return waitForSelectedIncidentsThenAcknowledgeMsg(msg) }
469-
}
470-
return m, func() tea.Msg {
471-
return acknowledgeIncidentsMsg{incidents: []pagerduty.Incident{*m.selectedIncident}}
472-
}
473-
474-
case waitForSelectedIncidentsThenUnAcknowledgeMsg:
475-
if m.selectedIncident == nil {
476-
time.Sleep(waitTime)
477-
m.setStatus("waiting for incident info...")
478-
return m, func() tea.Msg { return waitForSelectedIncidentsThenUnAcknowledgeMsg(msg) }
479-
}
480-
return m, func() tea.Msg {
481-
return unAcknowledgeIncidentsMsg{incidents: []pagerduty.Incident{*m.selectedIncident}}
482-
}
483-
484464
case reassignIncidentsMsg:
485465
if msg.incidents == nil {
486466
m.setStatus("failed reassigning incidents - no incidents provided")
@@ -521,17 +501,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
521501
}
522502
}
523503

524-
var policyKey string
525-
526-
_, ok := m.config.EscalationPolicies[m.selectedIncident.Service.ID]
527-
528-
if !ok {
529-
log.Debug("Update", "silenceSelectedIncidentMsg", "no escalation policy override for service; using default", "service", m.selectedIncident.Service.ID, "policy", m.config.EscalationPolicies[silentDefaultPolicyKey].Name)
530-
policyKey = silentDefaultPolicyKey
531-
} else {
532-
log.Debug("Update", "silenceSelectedIncidentMsg", "escalation policy override found for service", "service", m.selectedIncident.Service.ID, "policy", m.config.EscalationPolicies[m.selectedIncident.Service.ID].Name)
533-
policyKey = m.selectedIncident.Service.ID
534-
}
504+
policyKey := getEscalationPolicyKey(m.selectedIncident.Service.ID, m.config.EscalationPolicies)
535505

536506
return m, tea.Sequence(
537507
silenceIncidents([]pagerduty.Incident{*m.selectedIncident}, m.config.EscalationPolicies[policyKey], silentDefaultPolicyLevel),
@@ -544,8 +514,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
544514
return m, nil
545515
}
546516

547-
var incidents = msg.incidents
548-
incidents = append(incidents, *m.selectedIncident)
517+
incidents := append(msg.incidents, *m.selectedIncident)
549518
return m, tea.Sequence(
550519
silenceIncidents(incidents, m.config.EscalationPolicies["silent_default"], silentDefaultPolicyLevel),
551520
func() tea.Msg { return clearSelectedIncidentsMsg("sender: silenceIncidentsMsg") },

0 commit comments

Comments
 (0)