ארכיטקטורת המערכת

סקירה מקיפה של הארכיטקטורה, מבנה השכבות, רכיבים מרכזיים ו-Design Patterns של מערכת Communication Hub.

סקירה כללית

מערכת Communication Hub בנויה בארכיטקטורת Layered Architecture (ארכיטקטורה שכבתית) עם הפרדה ברורה בין שכבות. המערכת מיישמת מספר Design Patterns מוכרים לשמירה על קוד נקי, ניתן לתחזוקה והרחבה.

דיאגרמת ארכיטקטורה כללית

Presentation Layer

HTML Templates, JavaScript Manager, Bootstrap 5 RTL

API Layer

Flask Blueprints, RESTful Endpoints, WebSocket

Service Layer

CommunicationHubService, QueueManager, TemplateEngine

Adapter Layer

EmailAdapter, WhatsAppAdapter, SMSAdapter, PushAdapter

Data Layer

SQLAlchemy Models, PostgreSQL, Redis Cache

שכבות המערכת

1. Presentation Layer (שכבת הצגה)

שכבת הממשק האחראית על הצגת הנתונים למשתמש ואיסוף קלט.

רכיב תיאור טכנולוגיה
HTML Templates 6 עמודי Jinja2 templates Jinja2, Bootstrap 5 RTL
JavaScript Manager CommunicationHubManager class ES6+, Chart.js
CSS Styles עיצוב מותאם RTL Custom CSS, Bootstrap 5
// דוגמת אתחול JavaScript Manager class CommunicationHubManager { constructor() { this.pageType = this.detectPageType(); this.initPage(); this.setupWebSocket(); } detectPageType() { const path = window.location.pathname; if (path.includes('dashboard')) return 'dashboard'; if (path.includes('messages')) return 'messages'; // ... } }

2. API Layer (שכבת API)

שכבת ה-API מספקת ממשק RESTful לתקשורת בין ה-Frontend ל-Backend.

Blueprint: api_communication_hub

מרכז את כל ה-endpoints של המערכת

  • Messages API (CRUD)
  • Templates API
  • Channels API
  • Campaigns API
  • Analytics API
WebSocket Support

תמיכה בעדכונים בזמן אמת

  • Flask-SocketIO
  • Real-time notifications
  • Status updates
  • Polling fallback

3. Service Layer (שכבת שירותים)

הלב של המערכת - מכיל את כל הלוגיקה העסקית והתזמור בין הרכיבים.

CommunicationHubService MessageQueueManager TemplateEngine CampaignManager AnalyticsService
# CommunicationHubService - שירות מרכזי class CommunicationHubService: def __init__(self): self.adapters = { 'email': EmailAdapter(), 'whatsapp': WhatsAppAdapter(), 'sms': SMSAdapter(), 'push': PushAdapter() } self.queue_manager = MessageQueueManager() self.template_engine = TemplateEngine() def send_message(self, channel, recipient, content, **kwargs): """שליחת הודעה דרך ערוץ ספציפי""" adapter = self.adapters.get(channel) if not adapter: raise ValueError(f"Unknown channel: {channel}") # עיבוד תבנית אם יש if kwargs.get('template_id'): content = self.template_engine.render( kwargs['template_id'], kwargs.get('variables', {}) ) # הוספה לתור return self.queue_manager.enqueue( adapter=adapter, recipient=recipient, content=content, priority=kwargs.get('priority', 'normal') )

4. Adapter Layer (שכבת מתאמים)

שכבת ה-Adapters מיישמת את דפוס ה-Adapter Pattern ליצירת ממשק אחיד לכל ערוצי התקשורת.

EmailAdapter
SMTP / SendGrid
WhatsAppAdapter
Green-API / Twilio
SMSAdapter
Twilio / Custom
PushAdapter
Firebase / OneSignal
# Base Adapter Interface class BaseChannelAdapter(ABC): @abstractmethod def send(self, recipient: str, content: str, **kwargs) -> dict: """שליחת הודעה""" pass @abstractmethod def get_status(self, message_id: str) -> str: """בדיקת סטטוס הודעה""" pass @abstractmethod def validate_recipient(self, recipient: str) -> bool: """אימות כתובת נמען""" pass # Email Adapter Implementation class EmailAdapter(BaseChannelAdapter): def __init__(self, config=None): self.config = config or self._load_config() self.smtp_client = None def send(self, recipient, content, **kwargs): subject = kwargs.get('subject', 'הודעה חדשה') html_content = kwargs.get('html_content', content) msg = MIMEMultipart('alternative') msg['Subject'] = subject msg['From'] = self.config['from_email'] msg['To'] = recipient # ... SMTP sending logic return {'message_id': msg_id, 'status': 'sent'}

5. Data Layer (שכבת נתונים)

שכבת הנתונים אחראית על שמירה ואחזור מידע מבסיס הנתונים.

CommunicationMessage CommunicationTemplate CommunicationChannel CommunicationCampaign MessageQueue

Design Patterns

המערכת מיישמת מספר Design Patterns מוכרים:

Adapter Pattern

מאפשר ממשק אחיד לכל ערוצי התקשורת. כל Adapter מיישם את אותו interface בעוד הוא מתממשק לשירות חיצוני שונה.

adapter = self.adapters[channel] adapter.send(recipient, content)
Factory Pattern

יצירת Adapters לפי סוג הערוץ. המערכת יודעת ליצור את ה-Adapter המתאים בהתאם לקונפיגורציה.

def get_adapter(channel_type): return ADAPTER_FACTORY[channel_type]()
Queue Pattern

ניהול תור הודעות עם תמיכה בעדיפויות, ניסיונות חוזרים ועיבוד אסינכרוני.

queue_manager.enqueue(message, priority='high') queue_manager.process_batch()
Template Method

עיבוד תבניות עם משתנים דינמיים. מאפשר החלפת משתנים בתוכן ההודעה.

template.render({ 'customer_name': 'ישראל', 'order_id': '12345' })
Observer Pattern

עדכונים בזמן אמת דרך WebSocket. כל שינוי בסטטוס הודעה משודר לכל הצופים המעוניינים.

socketio.emit('message_status_changed', { 'message_id': msg_id, 'status': 'delivered' })
Singleton Pattern

CommunicationHubService מיושם כ-Singleton לוודא מופע יחיד במערכת.

hub = get_communication_hub() # Always same instance

זרימת נתונים

שליחת הודעה - Flow Diagram

User
Frontend
API
Service
Queue
Adapter
Provider

תהליך שליחת הודעה - שלב אחר שלב

  1. קלט משתמש: המשתמש ממלא טופס שליחת הודעה בממשק
  2. בקשת API: JavaScript Manager שולח POST request ל-API
  3. ולידציה: ה-API מבצע ולידציה על הקלט
  4. עיבוד תבנית: אם יש תבנית, מוחלפים המשתנים
  5. הוספה לתור: ההודעה נוספת לתור עם עדיפות
  6. עיבוד תור: ה-Queue Manager מעבד את ההודעה
  7. שליחה דרך Adapter: ה-Adapter המתאים שולח לספק
  8. עדכון סטטוס: הסטטוס מתעדכן ב-DB ומשודר ב-WebSocket

מבנה קבצים

app/ ├── services/ │ └── communication_hub_service.py # שירות מרכזי │ ├── adapters/ │ ├── base_adapter.py # Base class │ ├── email_adapter.py # Email implementation │ ├── whatsapp_adapter.py # WhatsApp implementation │ ├── sms_adapter.py # SMS implementation │ └── push_adapter.py # Push implementation │ ├── models/ │ └── models_communication_hub.py # Database models │ ├── routes/ │ └── api_communication_hub.py # API endpoints │ ├── templates/ │ ├── communication-hub-dashboard.html │ ├── communication-hub-messages.html │ ├── communication-hub-templates.html │ ├── communication-hub-channels.html │ ├── communication-hub-campaigns.html │ └── communication-hub-analytics.html │ └── static/ ├── js/ │ └── modules/ │ └── communication-hub-manager.js │ └── docs/ └── communication-hub/ # תיעוד זה ├── index.html ├── architecture.html ├── database.html ├── api-reference.html ├── channels.html ├── templates.html └── ui-guide.html

טכנולוגיות

Backend
  • Python 3.11+
  • Flask 3.x
  • SQLAlchemy 2.x
  • Flask-SocketIO
  • Celery (Queue)
Frontend
  • Bootstrap 5 RTL
  • JavaScript ES6+
  • Chart.js
  • SweetAlert2
  • WebSocket API
Infrastructure
  • PostgreSQL 15
  • Redis (Cache/Queue)
  • Docker
  • Gunicorn
  • Traefik