class_docstr: Dispatch alerts to the alert processor.
class_docstr_tok: ["Dispatch","alerts","to","the","alert","processor","."]
docstr: Find and dispatch all pending alerts to the alert processor.
docstr_tok: ["Find","and","dispatch","all","pending","alerts","to","the","alert","processor","."]
code: def dispatch(self):
      """Find and dispatch all pending alerts to the alert processor."""
      # To reduce the API calls to Dynamo, batch all additions and deletions until the end.
      merged_alerts = []  # List of newly created merge alerts
      alerts_to_delete = []  # List of alerts which can be deleted
  
      for rule_name in self.table.rule_names_generator():
          merge_enabled_alerts = []
          for alert in self._alert_generator(rule_name):
              if alert.remaining_outputs:
                  # If an alert still has pending outputs, it needs to be sent immediately.
                  # For example, all alerts are sent to the default firehose now even if they will
                  # later be merged when sending to other outputs.
                  self._dispatch_alert(alert)
              elif alert.merge_enabled:
                  # This alert has finished sending to non-merged outputs; it is now a candidate
                  # for alert merging.
                  merge_enabled_alerts.append(alert)
              else:
                  # This alert has sent successfully but doesn't need to be merged.
                  # It should have been deleted by the alert processor, but we can do it now.
                  alerts_to_delete.append(alert)
  
          for group in self._merge_groups(merge_enabled_alerts):
              # Create a new merged Alert.
              new_alert = Alert.merge(group.alerts)
              LOGGER.info(
                  "Merged %d alerts into a new alert with ID %s",
                  len(group.alerts),
                  new_alert.alert_id,
              )
              merged_alerts.append(new_alert)
  
              # Since we already guaranteed that the original alerts have sent to the unmerged
              # outputs (e.g. default firehose), they can be safely marked for deletion.
              alerts_to_delete.extend(group.alerts)
  
      if merged_alerts:
          # Add new merged alerts to the alerts table and send them to the alert processor.
          self.table.add_alerts(merged_alerts)
          for alert in merged_alerts:
              self._dispatch_alert(alert)
  
      if alerts_to_delete:
          self.table.delete_alerts(
              [
                  (alert.rule_name, alert.alert_id)
                  for alert in alerts_to_delete
              ]
          )
  
file_path: streamalert/streamalert/alert_merger/main.py
imports: from datetime import datetime
  import json
  import os
  import boto3
  from streamalert.shared.alert import Alert, AlertCreationError
  from streamalert.shared.alert_table import AlertTable
  from streamalert.shared.logger import get_logger
  from streamalert.shared.metrics import ALERT_MERGER_NAME, MetricLogger
label_desc: Use image node_id 6 for calling the AlertMerger obj's underlying member method code with example usage: obj.dispatch() without return types