/** * Broker Types * * Type definitions for message broker operations */ import { MessageBrokerTypes } from '../../types'; export { IProductMessageBroker, IProductMessageBrokerTopic, IMessageBrokerEnvs, IPublishRequest, ISubscribeRequest, IMessageBrokerPublishInput, IMessageBrokerSubscribeInput, } from '../../types'; /** * Configuration for broker service initialization */ export interface IBrokerServiceConfig { workspace_id: string; public_key: string; user_id: string; token: string; env_type?: string; /** Optional Redis client for caching */ redis_client?: any; /** Optional access key for API authentication */ access_key?: string; /** Optional pre-initialized product builder (e.g. from feature executor) to reuse prefetched broker/topic data */ preInitializedProductBuilder?: any; /** Workspace private key for encrypting log data. When set, LogsService encrypts log payloads. */ workspace_private_key?: string; } /** * Producer registration options */ export interface IProducerOptions { /** Producer tag (auto-generated if not provided) */ tag?: string; /** Producer name */ name?: string; /** Producer description */ description?: string; } /** * Consumer registration options */ export interface IConsumerOptions { /** Consumer tag (auto-generated if not provided) */ tag?: string; /** Consumer name */ name?: string; /** Consumer description */ description?: string; } /** * Options for publishing a message to a broker topic */ export interface IPublishOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Event in format broker_tag:topic_tag */ event: string; /** Message payload to publish */ message: Record; /** Optional cache key */ cache?: string; /** Optional session info */ session?: { tag: string; token: string; }; /** Optional producer registration options */ producer?: IProducerOptions; } /** * Options for subscribing to a broker topic */ export interface ISubscribeOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Event in format broker_tag:topic_tag */ event: string; /** Callback function invoked when a message is received */ callback: (message: object) => Promise; /** Optional consumer registration options */ consumer?: IConsumerOptions; } /** * Result of a publish operation */ export interface IPublishResult { success: boolean; process_id?: string; error?: string; } /** * Result of a subscribe operation */ export interface ISubscribeResult { success: boolean; process_id?: string; error?: string; } export { RedisConfig, GooglePubSubConfig, RabbitMQConfig, KafkaConfig, AWSSQSConfig, NatsConfig, } from '../../types'; /** * Union type for all broker configurations * Uses the existing config types from productsBuilder.types */ export type BrokerConfig = import('../../types').RedisConfig | import('../../types').GooglePubSubConfig | import('../../types').RabbitMQConfig | import('../../types').KafkaConfig | import('../../types').AWSSQSConfig | import('../../types').NatsConfig; /** * Interface for message broker service implementations */ export interface IMessageBrokerService { connect(): Promise; publish(topic: string, message: object, options?: IBrokerMessageRoutingOptions): Promise; subscribe(topic: string, callback: (message: object) => Promise, options?: IBrokerMessageRoutingOptions): Promise; disconnect(): Promise; } /** Broker-independent routing metadata used when logical topics share a physical resource. */ export interface IBrokerMessageRoutingOptions { logicalTopic?: string; } /** * Bootstrap data structure for broker operations */ export interface IBrokerBootstrapData { broker: { tag: string; name: string; envs: Array<{ slug: string; type: MessageBrokerTypes; config: BrokerConfig; }>; }; topic: { name: string; tag: string; queueUrls?: Array<{ env_slug: string; url: string; }>; }; env: { slug: string; active: boolean; }; } /** * Options for dispatching broker operations via bootstrap */ export interface IBrokerDispatchOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Event in format broker_tag:topic_tag */ event: string; /** Message payload (for publish) */ message?: Record; /** Callback function (for subscribe) */ callback?: (message: object) => Promise; /** Optional cache key */ cache?: string; /** Optional session info */ session?: { tag: string; token: string; }; } /** * Broker event categories */ export type BrokerEventCategory = 'consumer' | 'producer' | 'dead-letter' | 'message' | 'error'; /** * Broker event status */ export type BrokerEventStatus = 'success' | 'failed' | 'pending' | 'duplicate' | 'retrying'; /** * Broker event metadata */ export interface IBrokerEventMetadata { consumer_id?: string; producer_id?: string; error_message?: string; retry_count?: number; message_id?: string; idempotency_key?: string; partition?: number; offset?: number; } /** * Broker event record */ export interface IBrokerEvent { id: string; event_type: string; category: BrokerEventCategory; topic: string; broker_tag: string; message: string; timestamp: Date; status: BrokerEventStatus; idempotent: boolean; request_data?: Record; response_data?: Record; metadata?: IBrokerEventMetadata; product_tag: string; env: string; process_id?: string; } /** * Options for fetching broker events */ export interface IGetBrokerEventsOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Broker tag */ brokerTag: string; /** Filter by topic */ topic?: string; /** Filter by category */ category?: BrokerEventCategory; /** Filter by status */ status?: BrokerEventStatus; /** Filter by idempotent flag */ idempotent?: boolean; /** Start date for filtering */ startDate?: Date; /** End date for filtering */ endDate?: Date; /** Page number */ page?: number; /** Items per page */ limit?: number; } /** * Result of fetching broker events */ export interface IGetBrokerEventsResult { events: IBrokerEvent[]; total: number; page: number; limit: number; hasMore: boolean; } /** * Options for getting a single broker event */ export interface IGetBrokerEventOptions { /** Product tag */ product: string; /** Event ID */ eventId: string; } /** * Options for replaying/reprocessing a broker event */ export interface IReplayEventOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Event ID to replay */ eventId: string; /** Force replay even if already successful */ force?: boolean; } /** * Result of replaying a broker event */ export interface IReplayEventResult { success: boolean; new_event_id?: string; process_id?: string; error?: string; } /** * Options for publishing with idempotency */ export interface IIdempotentPublishOptions extends IPublishOptions { /** Unique idempotency key to prevent duplicate processing */ idempotencyKey: string; /** TTL for idempotency check in seconds (default: 86400 = 24 hours) */ idempotencyTtl?: number; } /** * Options for reprocessing dead letter queue messages */ export interface IReprocessDLQOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Broker tag */ brokerTag: string; /** Topic tag */ topicTag?: string; /** Specific message IDs to reprocess (if empty, reprocess all) */ messageIds?: string[]; /** Maximum number of messages to reprocess */ limit?: number; } /** * Result of reprocessing dead letter queue */ export interface IReprocessDLQResult { success: boolean; reprocessed_count: number; failed_count: number; event_ids: string[]; errors?: string[]; } /** * Broker event statistics */ export interface IBrokerEventStats { total_events: number; success_count: number; failed_count: number; pending_count: number; duplicate_count: number; dead_letter_count: number; idempotent_count: number; events_by_topic: Record; events_by_category: Record; } /** * Broker message status */ export type BrokerMessageStatus = 'pending' | 'success' | 'failed' | 'partial'; /** * Consumer delivery status for a message */ export interface IBrokerMessageConsumerDelivery { consumer_tag: string; status: BrokerMessageStatus; consumed_at?: Date; error?: string; retry_count?: number; response_data?: string; } /** * Broker message entity */ export interface IBrokerMessage { _id?: string; message_id: string; idempotency_key?: string; workspace_id: string; product_id: string; product_tag: string; env: string; broker_tag: string; topic_tag: string; event: string; producer_tag: string; message_encrypted: string; message_decrypted?: Record; status: BrokerMessageStatus; produced_at: Date; consumer_deliveries: IBrokerMessageConsumerDelivery[]; process_id?: string; session_tag?: string; metadata?: Record; created_at?: Date; updated_at?: Date; } /** * Options for querying broker messages */ export interface IGetBrokerMessagesOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Broker tag */ brokerTag: string; /** Filter by topic */ topicTag?: string; /** Filter by producer */ producerTag?: string; /** Filter by consumer */ consumerTag?: string; /** Filter by status */ status?: BrokerMessageStatus; /** Start date for filtering */ startDate?: Date; /** End date for filtering */ endDate?: Date; /** Page number */ page?: number; /** Items per page */ limit?: number; } /** * Result of fetching broker messages */ export interface IGetBrokerMessagesResult { messages: IBrokerMessage[]; total: number; page: number; limit: number; hasMore: boolean; } /** * Producer instance with aggregated stats */ export interface IBrokerProducer { tag: string; name?: string; description?: string; topic: string; broker_tag: string; message_count: number; success_count: number; failed_count: number; pending_count: number; last_activity?: Date; status: 'active' | 'inactive' | 'error'; created_at?: Date; } /** * Consumer instance with aggregated stats */ export interface IBrokerConsumer { tag: string; name?: string; description?: string; topic: string; broker_tag: string; message_count: number; success_count: number; failed_count: number; pending_count: number; avg_processing_time?: number; last_activity?: Date; status: 'active' | 'inactive' | 'error'; created_at?: Date; } /** * Dead letter message */ export interface IBrokerDeadLetter { message_id: string; original_message: IBrokerMessage; error: string; failed_at: Date; retry_count: number; consumer_tag: string; can_retry: boolean; } /** * Options for fetching producers */ export interface IGetBrokerProducersOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Broker tag */ brokerTag: string; /** Filter by topic */ topicTag?: string; /** Page number */ page?: number; /** Items per page */ limit?: number; } /** * Result of fetching producers */ export interface IGetBrokerProducersResult { producers: IBrokerProducer[]; total: number; page: number; limit: number; hasMore: boolean; } /** * Options for fetching consumers */ export interface IGetBrokerConsumersOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Broker tag */ brokerTag: string; /** Filter by topic */ topicTag?: string; /** Page number */ page?: number; /** Items per page */ limit?: number; } /** * Result of fetching consumers */ export interface IGetBrokerConsumersResult { consumers: IBrokerConsumer[]; total: number; page: number; limit: number; hasMore: boolean; } /** * Options for fetching dead letters */ export interface IGetBrokerDeadLettersOptions { /** Product tag */ product: string; /** Environment slug */ env: string; /** Broker tag */ brokerTag: string; /** Filter by topic */ topicTag?: string; /** Filter by consumer */ consumerTag?: string; /** Start date for filtering */ startDate?: Date; /** End date for filtering */ endDate?: Date; /** Page number */ page?: number; /** Items per page */ limit?: number; } /** * Result of fetching dead letters */ export interface IGetBrokerDeadLettersResult { deadLetters: IBrokerDeadLetter[]; total: number; page: number; limit: number; hasMore: boolean; } /** * Broker message statistics */ export interface IBrokerMessageStats { total: number; pending: number; success: number; failed: number; partial: number; producer_count: number; consumer_count: number; dead_letter_count: number; messages_by_topic: Record; messages_by_producer: Record; avg_processing_time?: number; } /** * Broker overview dashboard data */ export interface IBrokerOverviewDashboard { stats: IBrokerMessageStats; recent_messages: IBrokerMessage[]; top_producers: IBrokerProducer[]; top_consumers: IBrokerConsumer[]; daily_activity: Array<{ date: string; published: number; consumed: number; failed: number; }>; hourly_distribution: Array<{ hour: number; count: number; }>; }