Merge pull request #210 from grafana/matiasb-limit-slack-resolution-note-list

Limit number of slack messages in resolution notes popup
This commit is contained in:
Matias Bordese 2022-07-08 10:21:53 -03:00 committed by GitHub
commit a1a67e0c8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 100 additions and 2 deletions

View file

@ -237,6 +237,7 @@ class ResolutionNoteModalStep(CheckAlertIsUnarchivedMixin, scenario_step.Scenari
]
RESOLUTION_NOTE_TEXT_BLOCK_ID = "resolution_note_text"
RESOLUTION_NOTE_MESSAGES_MAX_COUNT = 25
def process_scenario(self, slack_user_identity, slack_team_identity, payload, action=None, data=None):
AlertGroup = apps.get_model("alerts", "AlertGroup")
@ -299,7 +300,27 @@ class ResolutionNoteModalStep(CheckAlertIsUnarchivedMixin, scenario_step.Scenari
blocks = []
other_resolution_notes = alert_group.resolution_notes.filter(~Q(source=ResolutionNote.Source.SLACK))
resolution_note_slack_messages = alert_group.resolution_note_slack_messages.filter(posted_by_bot=False)
resolution_note_slack_messages = alert_group.resolution_note_slack_messages.filter(
posted_by_bot=False
).order_by("-pk")
if resolution_note_slack_messages.count() > self.RESOLUTION_NOTE_MESSAGES_MAX_COUNT:
blocks.extend(
[
{
"type": "divider",
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": (
":warning: Listing up to last {} thread messages, "
"you can still add any other message using contextual menu actions."
).format(self.RESOLUTION_NOTE_MESSAGES_MAX_COUNT),
},
},
]
)
if action_resolve:
blocks.extend(
[
@ -333,7 +354,7 @@ class ResolutionNoteModalStep(CheckAlertIsUnarchivedMixin, scenario_step.Scenari
]
)
for message in resolution_note_slack_messages:
for message in resolution_note_slack_messages[: self.RESOLUTION_NOTE_MESSAGES_MAX_COUNT]:
user_verbal = message.user.get_user_verbal_for_team_for_slack(mention=True)
blocks.append(
{

View file

@ -101,3 +101,80 @@ def test_get_resolution_notes_blocks_non_empty(
]
assert blocks == expected_blocks
@pytest.mark.django_db
def test_get_resolution_notes_blocks_latest_limit(
make_organization_and_user_with_slack_identities,
make_alert_receive_channel,
make_alert_group,
make_resolution_note_slack_message,
):
SlackResolutionNoteModalStep = ScenarioStep.get_step("resolution_note", "ResolutionNoteModalStep")
organization, user, slack_team_identity, _ = make_organization_and_user_with_slack_identities()
step = SlackResolutionNoteModalStep(slack_team_identity)
alert_receive_channel = make_alert_receive_channel(organization)
alert_group = make_alert_group(alert_receive_channel)
max_count = SlackResolutionNoteModalStep.RESOLUTION_NOTE_MESSAGES_MAX_COUNT
messages = [
make_resolution_note_slack_message(alert_group=alert_group, user=user, added_by_user=user, ts=i, text=i)
for i in range(max_count * 2)
]
blocks = step.get_resolution_notes_blocks(alert_group, "", False)
expected_blocks = [
{
"type": "divider",
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": (
":warning: Listing up to last {} thread messages, "
"you can still add any other message using contextual menu actions."
).format(max_count),
},
},
]
for m in list(reversed(messages))[:max_count]:
expected_blocks += [
{
"type": "divider",
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "{} <!date^{:.0f}^{{date_num}} {{time_secs}}|message_created_at>\n{}".format(
m.user.get_user_verbal_for_team_for_slack(mention=True),
float(m.ts),
m.text,
),
},
"accessory": {
"type": "button",
"style": "primary",
"text": {
"type": "plain_text",
"text": "Add",
"emoji": True,
},
"action_id": "AddRemoveThreadMessageStep",
"value": json.dumps(
{
"resolution_note_window_action": "edit",
"msg_value": "add",
"message_pk": m.pk,
"resolution_note_pk": None,
"alert_group_pk": alert_group.pk,
}
),
},
},
]
assert blocks == expected_blocks