Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mail read schedule stop mechanism, filters and their related tests #1645

Closed

Conversation

hasinaxp
Copy link
Contributor

@hasinaxp hasinaxp commented Dec 12, 2024

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced a new endpoint to stop mail reading for a specified bot.
    • Added functionality to check for the existence of a mail channel.
    • Enhanced email processing with configurable criteria for fetching emails.
    • Expanded mail integration configuration options for improved email handling.
  • Bug Fixes

    • Improved error handling during mail channel operations.
  • Tests

    • Added comprehensive tests for new and modified functionalities in the MailProcessor and MailScheduler classes.

Copy link

coderabbitai bot commented Dec 12, 2024

Walkthrough

The changes introduced in this pull request include the addition of a new endpoint in the FastAPI application for stopping mail reading associated with a bot. This is implemented through a new method in the EventUtility class and is supported by updates to the MailProcessor and MailScheduler classes. Several new test cases have been added to ensure the functionality works as intended, particularly focusing on the new stopping mechanism for mail reading.

Changes

File Change Summary
kairon/events/server.py Added a new GET method stop_mail_reading to stop mail reading for a bot. Modified request_epoch method for proper formatting.
kairon/events/utility.py Introduced a static method stop_channel_mail_reading to stop mail reading for a bot. Removed unnecessary imports.
kairon/shared/channels/mail/processor.py Added a static method does_mail_channel_exist and a new method generate_criteria for constructing IMAP criteria. Updated get_mail_channel_state_data method signature. Refactored read_mails method to use generated criteria instead of hardcoded conditions. Added comma_sep_string_to_list method.
kairon/shared/channels/mail/scheduler.py Introduced a static method request_stop to stop email channel reading for a bot, incorporating checks for channel existence and error handling.
kairon/shared/chat/processor.py Modified delete_channel_config method to call MailScheduler.request_stop for non-Slack connectors, with added error handling.
metadata/integrations.yml Added optional fields to the mail integration section for enhanced configuration options.
tests/unit_test/channels/mail_channel_test.py Added tests for generate_criteria, comma_sep_string_to_list, and existence checks for mail channels. Enhanced coverage for MailProcessor functionalities.
tests/unit_test/channels/mail_scheduler_test.py Added tests for request_stop method, covering various scenarios including success and failure cases. Added a test for stopping mail reading when an event ID exists.

Possibly related PRs

  • mail channel fix #1637: The changes in this PR involve the removal of the MailProcessEvent and related functionality, which is directly connected to the stop_mail_reading method added in the main PR, as both are part of the mail processing logic.

Suggested reviewers

  • hiteshghuge

🐰 In the meadow where emails flow,
A new endpoint helps us know,
With a gentle stop, the bots will rest,
As we put the mail to the test.
Hopping through code, with joy we sing,
For every change, new wonders bring! 🌼📧


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (4)
kairon/shared/channels/mail/processor.py (1)

46-48: Simplify the return statement in does_mail_channel_exist method

You can return the condition directly to make the code more concise.

Apply this diff:

def does_mail_channel_exist(bot: str):
-    if Channels.objects(bot=bot, connector_type=ChannelTypes.MAIL.value).first():
-        return True
-    return False
+    return bool(Channels.objects(bot=bot, connector_type=ChannelTypes.MAIL.value).first())
🧰 Tools
🪛 Ruff (0.8.2)

46-48: Return the condition directly

Inline condition

(SIM103)

tests/unit_test/channels/mail_channel_test.py (3)

6-6: Remove unused import

The AND import from imap_tools is not used in the code.

-from imap_tools import MailMessage, AND
+from imap_tools import MailMessage
🧰 Tools
🪛 Ruff (0.8.2)

6-6: imap_tools.AND imported but unused

Remove unused import: imap_tools.AND

(F401)


282-323: Enhance test coverage and remove debug print

The test method covers basic criteria generation well, but could be improved:

  1. Remove the debug print statement on line 296
  2. Add test cases for edge cases:
    • Empty lists for subjects and from_addresses
    • Invalid read_status value
    • Special characters in subjects and from_addresses
-        print(criteria)
         assert criteria == '((SEEN) (UID 124:*))'

617-626: Consider adding error scenario tests

The mail channel existence tests cover the basic cases well. Consider adding tests for error scenarios:

  1. Database connection errors
  2. Invalid bot IDs
  3. Malformed channel configurations

Example test case:

@patch('kairon.shared.channels.mail.processor.Channels')
def test_mail_channel_exist_error(self, mock_channels):
    mock_channels.objects.side_effect = Exception("Database error")
    with pytest.raises(AppException):
        MailProcessor.does_mail_channel_exist("test_bot")
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 521990a and 4116ea2.

📒 Files selected for processing (8)
  • kairon/events/server.py (1 hunks)
  • kairon/events/utility.py (2 hunks)
  • kairon/shared/channels/mail/processor.py (4 hunks)
  • kairon/shared/channels/mail/scheduler.py (1 hunks)
  • kairon/shared/chat/processor.py (1 hunks)
  • metadata/integrations.yml (1 hunks)
  • tests/unit_test/channels/mail_channel_test.py (3 hunks)
  • tests/unit_test/channels/mail_scheduler_test.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
kairon/events/server.py

156-156: Do not perform function call Path in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable

(B008)

kairon/events/utility.py

74-74: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

tests/unit_test/channels/mail_channel_test.py

6-6: imap_tools.AND imported but unused

Remove unused import: imap_tools.AND

(F401)

tests/unit_test/channels/mail_scheduler_test.py

145-145: Redefinition of unused test_request_stop_success from line 132

(F811)

kairon/shared/channels/mail/processor.py

46-48: Return the condition directly

Inline condition

(SIM103)

🔇 Additional comments (8)
kairon/shared/channels/mail/processor.py (2)

327-336: Handle missing or malformed configuration values gracefully

When retrieving configuration values like subjects, from_emails, etc., ensure that the code handles cases where these configurations might be missing or not in the expected format. This will prevent potential runtime errors.

Run the following script to verify the usage of configuration values:


281-283: Ensure correct handling of last_read_timestamp

The variable last_read_timestamp is calculated but not adjusted for time zones or potential daylight saving time changes. Consider using timezone-aware datetime objects to avoid potential issues with date and time calculations.

Run the following script to check for timezone-aware datetime usage:

metadata/integrations.yml (1)

88-92: Fields added to enhance mail integration configurability

The addition of subjects, ignore_subjects, from_emails, ignore_from_emails, and seen_status fields allows for more granular control over email processing.

kairon/events/server.py (1)

155-158: LGTM! The endpoint implementation looks good.

The implementation follows the established patterns in the codebase and properly handles the response.

🧰 Tools
🪛 Ruff (0.8.2)

156-156: Do not perform function call Path in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable

(B008)

tests/unit_test/channels/mail_scheduler_test.py (2)

111-127: LGTM! Test coverage for stop_channel_mail_reading is comprehensive

The test properly verifies the deletion of the job when stopping mail reading.


129-176: LGTM! Test coverage for request_stop scenarios is thorough

The tests cover all important scenarios:

  • Success case
  • Response not success case
  • No channel exists case
🧰 Tools
🪛 Ruff (0.8.2)

145-145: Redefinition of unused test_request_stop_success from line 132

(F811)

kairon/shared/chat/processor.py (1)

73-78: LGTM! Mail scheduler stopping is properly handled

The implementation:

  • Correctly checks for non-Slack connector types
  • Properly handles exceptions with logging
  • Maintains the existing functionality
tests/unit_test/channels/mail_channel_test.py (1)

591-615: LGTM! Well-structured test cases

The test method is comprehensive and covers various edge cases including:

  • Empty elements and strings
  • None values
  • Special characters
  • Numeric values
  • Spaces and formatting

Comment on lines 7 to +8
from pydantic.validators import datetime
from imap_tools import MailBox, AND
from imap_tools import MailBox, AND, OR, NOT
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix incorrect imports of datetime and timedelta

The datetime and timedelta classes are being imported from pydantic, which is incorrect and will lead to errors. They should be imported from the built-in datetime module.

Apply this diff to correct the imports:

-from pydantic.schema import timedelta
-from pydantic.validators import datetime
+from datetime import datetime, timedelta

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +33 to +40
resp = Utility.execute_http_request(
"GET",
urljoin(
event_server_url,
f"/api/mail/stop/{bot}",
),
err_msg="Failed to request epoch",
)
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Correct the error message in execute_http_request

The err_msg passed to Utility.execute_http_request should accurately reflect the operation being performed. Currently, it incorrectly states "Failed to request epoch" when the operation is to stop the mail channel.

Apply this diff to correct the error message:

 resp = Utility.execute_http_request(
     "GET",
     urljoin(
         event_server_url,
         f"/api/mail/stop/{bot}",
     ),
-    err_msg="Failed to request epoch",
+    err_msg="Failed to stop mail channel",
 )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
resp = Utility.execute_http_request(
"GET",
urljoin(
event_server_url,
f"/api/mail/stop/{bot}",
),
err_msg="Failed to request epoch",
)
resp = Utility.execute_http_request(
"GET",
urljoin(
event_server_url,
f"/api/mail/stop/{bot}",
),
err_msg="Failed to stop mail channel",
)

Comment on lines 73 to 74
except Exception as e:
raise logger.error(f"Failed to stop mail reading for bot {bot}. Error: {str(e)}")
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix incorrect exception handling in stop_channel_mail_reading

Raising logger.error() is not appropriate. You should log the error and then raise the exception to propagate it correctly.

Apply this diff to fix the exception handling:

            except Exception as e:
-                raise logger.error(f"Failed to stop mail reading for bot {bot}. Error: {str(e)}")
+                logger.error(f"Failed to stop mail reading for bot {bot}. Error: {str(e)}")
+                raise e

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Ruff (0.8.2)

74-74: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

Comment on lines +155 to +156
def stop_mail_reading(bot: Text = Path(description="Bot id")):
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid function call in parameter defaults

The Path function call in the parameter default could lead to runtime issues. Instead, declare the parameter type annotation separately.

-def stop_mail_reading(bot: Text = Path(description="Bot id")):
+def stop_mail_reading(
+    bot: Text = ...,  # type: ignore
+) -> JSONResponse:
+    """Stop mail reading for a bot.
+    
+    Args:
+        bot (Text): Bot id
+    """
+    bot = Path(description="Bot id")(bot)

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 Ruff (0.8.2)

156-156: Do not perform function call Path in argument defaults; instead, perform the call within the function, or read the default from a module-level singleton variable

(B008)

Comment on lines 142 to 154
@patch('kairon.shared.channels.mail.processor.MailProcessor.does_mail_channel_exist')
@patch('kairon.shared.channels.mail.scheduler.Utility.get_event_server_url')
@patch('kairon.shared.channels.mail.scheduler.Utility.execute_http_request')
def test_request_stop_success(mock_execute_http_request, mock_get_event_server_url, mock_imp):
mock_get_event_server_url.return_value = "http://localhost"
mock_execute_http_request.return_value = {'success': True}
mock_imp.return_value = True
bot = "test_bot"
try:
MailScheduler.request_stop(bot)
except AppException:
pytest.fail("request_epoch() raised AppException unexpectedly!")

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Remove duplicate test function

The test function test_request_stop_success is defined twice. Remove the duplicate implementation.

-@patch('kairon.shared.channels.mail.processor.MailProcessor.does_mail_channel_exist')
-@patch('kairon.shared.channels.mail.scheduler.Utility.get_event_server_url')
-@patch('kairon.shared.channels.mail.scheduler.Utility.execute_http_request')
-def test_request_stop_success(mock_execute_http_request, mock_get_event_server_url, mock_imp):
-    mock_get_event_server_url.return_value = "http://localhost"
-    mock_execute_http_request.return_value = {'success': True}
-    mock_imp.return_value = True
-    bot = "test_bot"
-    try:
-        MailScheduler.request_stop(bot)
-    except AppException:
-        pytest.fail("request_epoch() raised AppException unexpectedly!")
🧰 Tools
🪛 Ruff (0.8.2)

145-145: Redefinition of unused test_request_stop_success from line 132

(F811)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (6)
kairon/events/utility.py (1)

69-72: Consider handling the no-op case explicitly.

The method silently succeeds when no event_id exists. Consider either:

  1. Logging this case for better observability
  2. Throwing an exception if there's no active mail reading to stop
  3. Returning a status indicating whether anything was actually stopped

Here's one possible implementation:

             mail_processor = MailProcessor(bot)
             event_id = mail_processor.state.event_id
             if event_id:
                 mail_processor.update_event_id(None)
                 KScheduler().delete_job(event_id)
+                logger.info(f"Successfully stopped mail reading for bot {bot}")
+            else:
+                logger.info(f"No active mail reading found for bot {bot}")
tests/unit_test/channels/mail_scheduler_test.py (5)

124-124: Fix comment formatting.

Remove the extra '#' and fix the indentation.

-#     # Test case when event_id is None
+    # Test case when event_id is None

111-127: Add test case for non-existent event_id.

The test only covers the case where event_id exists. Consider adding a test case where event_id is None to ensure complete coverage of the stop functionality.


140-140: Fix incorrect error message in pytest.fail.

The error message refers to "request_epoch" but should refer to "request_stop".

-        pytest.fail("request_epoch() raised AppException unexpectedly!")
+        pytest.fail("request_stop() raised AppException unexpectedly!")

146-146: Fix inconsistent test function naming.

The function name uses double underscore which is inconsistent with other test names in the file.

-def test_request_stop__response_not_success
+def test_request_stop_response_not_success

111-164: Add error handling test for stop_channel_mail_reading.

Consider adding a test case that verifies error handling in stop_channel_mail_reading, similar to test_schedule_channel_mail_reading_exception. This would ensure consistent error handling coverage across the codebase.

Example test case:

@patch('kairon.events.utility.KScheduler.delete_job')
@patch('kairon.events.utility.KScheduler.__init__', return_value=None)
@patch('kairon.shared.channels.mail.processor.MailProcessor')
@patch('pymongo.MongoClient', autospec=True)
def test_stop_channel_mail_reading_exception(mock_mongo, mock_mail_processor, mock_kscheduler, mock_delete_job):
    from kairon.events.utility import EventUtility

    bot = "test_bot"
    mock_mail_processor.side_effect = Exception("Test Exception")

    with pytest.raises(AppException) as excinfo:
        EventUtility.stop_channel_mail_reading(bot)
    assert str(excinfo.value) == f"Failed to stop mail reading for bot {bot}. Error: Test Exception"
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4116ea2 and 1c6fa33.

📒 Files selected for processing (2)
  • kairon/events/utility.py (2 hunks)
  • tests/unit_test/channels/mail_scheduler_test.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
kairon/events/utility.py

74-74: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

🔇 Additional comments (4)
kairon/events/utility.py (2)

8-8: LGTM! Good cleanup of imports.

Removing unused imports helps maintain code cleanliness.


63-74: 🛠️ Refactor suggestion

Improve exception handling and add validation.

The implementation needs the following improvements:

  1. Use raise ... from err pattern for proper exception chaining
  2. Add input validation for the bot parameter
  3. Add success logging for better observability

Apply this diff to implement the suggested improvements:

     @staticmethod
     def stop_channel_mail_reading(bot: str):
         from kairon.shared.channels.mail.processor import MailProcessor

+        if not bot:
+            raise AppException("Bot parameter cannot be empty")
+
         try:
             mail_processor = MailProcessor(bot)
             event_id = mail_processor.state.event_id
             if event_id:
                 mail_processor.update_event_id(None)
                 KScheduler().delete_job(event_id)
+                logger.info(f"Successfully stopped mail reading for bot {bot}")
         except Exception as e:
-            raise AppException(f"Failed to stop mail reading for bot {bot}. Error: {str(e)}")
+            logger.error(f"Failed to stop mail reading for bot {bot}. Error: {str(e)}")
+            raise AppException(f"Failed to stop mail reading for bot {bot}. Error: {str(e)}") from e

Likely invalid or redundant comment.

🧰 Tools
🪛 Ruff (0.8.2)

74-74: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)

tests/unit_test/channels/mail_scheduler_test.py (2)

132-141: Remove duplicate test function.

The test function test_request_stop_success is defined twice.


154-164: LGTM!

The test properly validates the edge case where the mail channel doesn't exist.

@@ -70,6 +70,12 @@ def delete_channel_config(bot: Text, **kwargs):
:return: None
"""
kwargs.update({"bot": bot})
if kwargs.get("connector_type") != ChannelTypes.SLACK.value:
Copy link
Collaborator

Choose a reason for hiding this comment

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

check not needed

@staticmethod
def does_mail_channel_exist(bot:str):
"""
Check if mail channel exists
Copy link
Collaborator

Choose a reason for hiding this comment

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

use existing function Utility.is_exist()

@hasinaxp hasinaxp closed this Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants