Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -1015,6 +1015,7 @@ class CeleryConfig: # pylint: disable=too-few-public-methods
"superset.tasks.scheduler",
"superset.tasks.thumbnails",
"superset.tasks.cache",
"superset.tasks.slack",
)
result_backend = "db+sqlite:///celery_results.sqlite"
worker_prefetch_multiplier = 1
Expand Down Expand Up @@ -1046,6 +1047,11 @@ class CeleryConfig: # pylint: disable=too-few-public-methods
# "schedule": crontab(minute="*", hour="*"),
# "kwargs": {"retention_period_days": 180},
# },
# Uncomment to enable Slack channel cache warm-up
# "slack.cache_channels": {
# "task": "slack.cache_channels",
# "schedule": crontab(minute="0", hour="*"),
# },
Comment on lines +1050 to +1054
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

}


Expand Down Expand Up @@ -1488,6 +1494,7 @@ def EMAIL_HEADER_MUTATOR( # pylint: disable=invalid-name,unused-argument # noq
# Slack API token for the superset reports, either string or callable
SLACK_API_TOKEN: Callable[[], str] | str | None = None
SLACK_PROXY = None
SLACK_CACHE_TIMEOUT = int(timedelta(days=1).total_seconds())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!


# The webdriver to use for generating reports. Use one of the following
# firefox
Expand Down
35 changes: 35 additions & 0 deletions superset/tasks/slack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import logging

Check warning on line 17 in superset/tasks/slack.py

View check run for this annotation

Codecov / codecov/patch

superset/tasks/slack.py#L17

Added line #L17 was not covered by tests

from flask import current_app

Check warning on line 19 in superset/tasks/slack.py

View check run for this annotation

Codecov / codecov/patch

superset/tasks/slack.py#L19

Added line #L19 was not covered by tests

from superset.extensions import celery_app
from superset.utils.slack import get_channels

Check warning on line 22 in superset/tasks/slack.py

View check run for this annotation

Codecov / codecov/patch

superset/tasks/slack.py#L21-L22

Added lines #L21 - L22 were not covered by tests

logger = logging.getLogger(__name__)

Check warning on line 24 in superset/tasks/slack.py

View check run for this annotation

Codecov / codecov/patch

superset/tasks/slack.py#L24

Added line #L24 was not covered by tests


@celery_app.task(name="slack.cache_channels")
def cache_channels() -> None:
try:
get_channels(

Check warning on line 30 in superset/tasks/slack.py

View check run for this annotation

Codecov / codecov/patch

superset/tasks/slack.py#L27-L30

Added lines #L27 - L30 were not covered by tests
force=True, cache_timeout=current_app.config["SLACK_CACHE_TIMEOUT"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dpgaspar can you confirm if this task would work properly for fetching the slack token (when it is a callable) for multi-tenant applications? Do we need to pass in some user context to this function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should just work

)
except Exception as ex:
logger.exception("An error occurred while caching Slack channels: %s", ex)
raise

Check warning on line 35 in superset/tasks/slack.py

View check run for this annotation

Codecov / codecov/patch

superset/tasks/slack.py#L33-L35

Added lines #L33 - L35 were not covered by tests
25 changes: 16 additions & 9 deletions superset/utils/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


import logging
from typing import Any, Optional
from typing import Callable, Optional

from flask import current_app
from slack_sdk import WebClient
Expand Down Expand Up @@ -60,7 +60,7 @@ def get_slack_client() -> WebClient:
key="slack_conversations_list",
cache=cache_manager.cache,
)
def get_channels(limit: int, extra_params: dict[str, Any]) -> list[SlackChannelSchema]:
def get_channels() -> list[SlackChannelSchema]:
"""
Retrieves a list of all conversations accessible by the bot
from the Slack API, and caches results (to avoid rate limits).
Expand All @@ -71,11 +71,12 @@ def get_channels(limit: int, extra_params: dict[str, Any]) -> list[SlackChannelS
client = get_slack_client()
channel_schema = SlackChannelSchema()
channels: list[SlackChannelSchema] = []
extra_params = {"types": ",".join(SlackChannelTypes)}
cursor = None

while True:
response = client.conversations_list(
limit=limit, cursor=cursor, exclude_archived=True, **extra_params
limit=999, cursor=cursor, exclude_archived=True, **extra_params
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we hardcoding this now?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I inlined the limit because it never governed the amount of channels returned by the function. The function always returns all channels. The limit parameter here only governs the amount of channels returned by each Slack API response.

Going for the maximum (999) allowed by Slack is most efficient, imo. Lower values are more likely to result in rate-limiting (which is based on requests, not rows returned or similar).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we see a need we could add paging to our /slack_channels endpoint (and get_channels_with_search(...)). I don't know about that though, our only usage is to get all channels to give the user a choice for the report notification setting.

)
channels.extend(
channel_schema.load(channel) for channel in response.data["channels"]
Expand All @@ -89,7 +90,6 @@ def get_channels(limit: int, extra_params: dict[str, Any]) -> list[SlackChannelS

def get_channels_with_search(
search_string: str = "",
limit: int = 999,
types: Optional[list[SlackChannelTypes]] = None,
exact_match: bool = False,
force: bool = False,
Expand All @@ -99,18 +99,25 @@ def get_channels_with_search(
all channels and filter them ourselves
This will search by slack name or id
"""
extra_params = {}
extra_params["types"] = ",".join(types) if types else None
try:
channels = get_channels(
limit=limit,
extra_params=extra_params,
force=force,
cache_timeout=86400,
cache_timeout=current_app.config["SLACK_CACHE_TIMEOUT"],
)
except (SlackClientError, SlackApiError) as ex:
raise SupersetException(f"Failed to list channels: {ex}") from ex

if types and not len(types) == len(SlackChannelTypes):
conditions: list[Callable[[SlackChannelSchema], bool]] = []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention here was to make this more easily pluggable in the future if we add more types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

if SlackChannelTypes.PUBLIC in types:
conditions.append(lambda channel: not channel["is_private"])
if SlackChannelTypes.PRIVATE in types:
conditions.append(lambda channel: channel["is_private"])

channels = [
channel for channel in channels if any(cond(channel) for cond in conditions)
]

# The search string can be multiple channels separated by commas
if search_string:
search_array = recipients_string_to_list(search_string)
Expand Down
39 changes: 26 additions & 13 deletions tests/unit_tests/utils/slack_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import pytest

from superset.utils.slack import get_channels_with_search
from superset.utils.slack import get_channels_with_search, SlackChannelTypes


class MockResponse:
Expand Down Expand Up @@ -150,15 +150,35 @@ def test_handle_slack_client_error_listing_channels(self, mocker):
The server responded with: missing scope: channels:read"""
)

def test_filter_channels_by_specified_types(self, mocker):
@pytest.mark.parametrize(
"types, expected_channel_ids",
[
([SlackChannelTypes.PUBLIC], {"public_channel_id"}),
([SlackChannelTypes.PRIVATE], {"private_channel_id"}),
(
[SlackChannelTypes.PUBLIC, SlackChannelTypes.PRIVATE],
{"public_channel_id", "private_channel_id"},
),
([], {"public_channel_id", "private_channel_id"}),
],
)
def test_filter_channels_by_specified_types(
self, types: list[SlackChannelTypes], expected_channel_ids: set[str], mocker
):
mock_data = {
"channels": [
{
"id": "C12345",
"name": "general",
"id": "public_channel_id",
"name": "open",
"is_member": False,
"is_private": False,
},
{
"id": "private_channel_id",
"name": "secret",
"is_member": False,
"is_private": True,
},
],
"response_metadata": {"next_cursor": None},
}
Expand All @@ -168,15 +188,8 @@ def test_filter_channels_by_specified_types(self, mocker):
mock_client.conversations_list.return_value = mock_response_instance
mocker.patch("superset.utils.slack.get_slack_client", return_value=mock_client)

result = get_channels_with_search(types=["public"])
assert result == [
{
"id": "C12345",
"name": "general",
"is_member": False,
"is_private": False,
}
]
result = get_channels_with_search(types=types)
assert {channel["id"] for channel in result} == expected_channel_ids

def test_handle_pagination_multiple_pages(self, mocker):
mock_data_page1 = {
Expand Down
Loading