Skip to content

Commit

Permalink
Merge branch '177_add_slack_report_anomaly_detection_step' into 'master'
Browse files Browse the repository at this point in the history
RFC: Add slack anomaly detection step

Closes #177

See merge request mutt_data/soam!129
  • Loading branch information
guido-mutt committed Sep 17, 2021
2 parents ec82766 + c8850d4 commit 65612a0
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .bumpversion.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.9.6
current_version = 0.10.0
tag = False

[bumpversion:file:soam/__init__.py]
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.10.0- 2021-09-10]

### Added
- Slack Anomaly Report Task

## [0.9.6- 2021-09-10]

### Fixed
- Update SlackReport to use current API.

### Added
- Add client_token to SlackReport init.
>>>>>>> ec8276626b283e1773b31457e4c84dd2c28b4306
## [0.9.4- 2021-08-17]

Expand Down
2 changes: 1 addition & 1 deletion soam/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Version."""

__version__ = '0.9.6'
__version__ = '0.10.0'
48 changes: 48 additions & 0 deletions soam/reporting/slack_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,54 @@ def run( # type: ignore
)


class SlackAnomalyReportTask(Step):
"""
Builds up the task of the anomaly report designed for Slack.
"""

def __init__(self, **kwargs: Any):
"""
Parameters
----------
channel_id: str
Slack channel id where the report will be sent.
metric_name: str
Performance metric being measured.
setting_path: str
Setting path.
kwargs:
Extra args to pass.
"""
Step.__init__(self, **kwargs) # type: ignore

def run( # type: ignore
self,
slack_client: slack.WebClient,
channel_id: str,
plot: Union[Path, IO],
metric_name: str,
anomaly_df: pd.DataFrame,
date_col: str,
):
"""
Parameters
----------
channel_id: str
Slack channel id where the report will be sent.
plot: str, pathlib.Path or buffer
Anomaly plot
metric_name: str
Metric to report.
anomaly_df: pd.DataFrame
DataFrame with anomalous values. Must have the following columns: ['y','yhat','yhat_lower','yhat_upper']
date_col: str
Name of the date column
"""
return send_anomaly_report(
slack_client, channel_id, plot, metric_name, anomaly_df, date_col
)


class SlackMessage:
def __init__(
self,
Expand Down
23 changes: 22 additions & 1 deletion tests/reporting/test_slack_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
from datetime import date
from io import BytesIO
from pathlib import Path, PosixPath
from unittest.mock import MagicMock
from unittest.mock import MagicMock, patch

from jinja2 import Template
import pandas as pd
import pytest

from soam.reporting.slack_report import (
SlackAnomalyReportTask,
SlackMessage,
send_anomaly_report,
send_multiple_slack_messages,
Expand Down Expand Up @@ -219,3 +220,23 @@ def test_send_no_anomaly_report():
initial_comment=expected_message,
thread_ts=None,
)


def test_slack_anomaly_report_task():
with patch("soam.reporting.slack_report.send_anomaly_report") as send_report_mock:
task = SlackAnomalyReportTask()
client_mock = MagicMock()
plot_file = BytesIO(b"abcdef")
anomaly_df = pd.DataFrame(
[
[date(2021, 1, 1), 1.5, 1.01, 2.2, 0.5],
[date(2021, 1, 2), 4.01, 4.02, 4.7, 3.9],
],
columns=["date", "y", "yhat", "yhat_upper", "yhat_lower"],
)
metric_name = "test"
test_channel = "test"
task.run(client_mock, test_channel, plot_file, metric_name, anomaly_df, "date")
send_report_mock.assert_called_once_with(
client_mock, test_channel, plot_file, metric_name, anomaly_df, "date"
)

0 comments on commit 65612a0

Please sign in to comment.