Skip to content

refactor(notifications): deduplicate AlertMarkerStore + add pluggable channel Protocols - #121

Merged
Pigbibi merged 2 commits into
mainfrom
refactor/notification-channels
Jun 28, 2026
Merged

refactor(notifications): deduplicate AlertMarkerStore + add pluggable channel Protocols#121
Pigbibi merged 2 commits into
mainfrom
refactor/notification-channels

Conversation

@Pigbibi

@Pigbibi Pigbibi commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Summary

通知模块两项重构:消除重复代码 + 添加可插拔渠道 Protocol。

1. 消除 4x 重复的 AlertMarkerStore(-164 行)

新增 alert_marker.py,抽取共享的 CloudAlertMarkerStore 基类:

  • has_alert() / record_alert() / _local_path() / _cloud_uri() 统一实现
  • _clean_relative_key_parse_cloud_uri 统一到基类(同时支持 gs://, s3://, az://)
  • 4 个渠道各自的 StrategyPlugin*AlertMarkerStore 从 ~55 行缩减为 3 行子类

改动前(4 个文件各 55 行重复):

class StrategyPluginTelegramAlertMarkerStore:
    def has_alert(...): ...
    def record_alert(...): ...
    def _local_path(...): ...
    def _cloud_uri(...): ...
    def _object_store(...): ...

改动后

class StrategyPluginTelegramAlertMarkerStore(CloudAlertMarkerStore):
    namespace: str = "strategy_plugin_telegram_alerts"
    schema_version: str = "strategy_plugin_telegram_alert_marker.v1"

2. 可插拔通知渠道 Protocol

新增 channel.py,定义 4 个 Protocol + 5 个默认实现:

Protocol 默认实现 可替换为
SmsChannel TwilioSmsChannel 阿里云短信、AWS SNS
PushChannel PushoverChannel + NtfyChannel 企业微信、Slack
EmailChannel SmtpEmailChannel SendGrid、AWS SES
ChatChannel TelegramChatChannel Discord、WhatsApp

替换示例:

from quant_platform_kit.notifications import SmsChannel

class AliyunSmsChannel:
    def send_sms(self, recipient, body, *, sender=None):
        ...  # call Aliyun API
        return True

publish_strategy_plugin_sms_alerts(
    signals,
    sms_settings=settings,
    send_notification=AliyunSmsChannel().send_sms,
)

Testing

  • 373 passed, 0 failed, 1 skipped (pre-existing email.py/stdlib collision)

Breaking Changes

None. All existing public APIs preserved.

Pigbibi and others added 2 commits June 29, 2026 06:50
…x duplicated code

- New alert_marker.py: shared CloudAlertMarkerStore base class with
  has_alert/record_alert/_local_path/_cloud_uri + _clean_relative_key helper
- strategy_plugin_{email,sms,push,telegram}.py: replace ~55-line duplicate
  marker store classes with thin 3-line subclasses (~200 lines removed)
- All 4 classes preserved (backward compat), just inherit from base now

Also removes 4 duplicate _clean_relative_key + _parse_cloud_uri helpers
(now in alert_marker.py which correctly supports gs://, s3://, and az://).

373 passed, 0 failed.

Co-Authored-By: Claude <noreply@anthropic.com>
- channel.py: define SmsChannel, PushChannel, EmailChannel, ChatChannel Protocols
- 5 default implementations: TwilioSmsChannel, PushoverChannel, NtfyChannel,
  SmtpEmailChannel, TelegramChatChannel — thin wrappers around existing functions
- Users can now swap providers by implementing a Protocol and passing it
  as send_notification to publish_strategy_plugin_*_alerts()
- Export all channels from notifications/__init__.py

This completes the notification decoupling: previously Twilio/Pushover/SMTP/Telegram
were hardcoded; now any provider implementing the Protocol can be dropped in.

373 passed, 0 failed.

Co-Authored-By: Claude <noreply@anthropic.com>
@Pigbibi
Pigbibi merged commit 5bc321b into main Jun 28, 2026
1 of 2 checks passed
@Pigbibi
Pigbibi deleted the refactor/notification-channels branch June 28, 2026 23:01
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.

1 participant