49 lines
include/batch_exporter.h
Declares BatchExporter, Record, and the addRecord/flush contract.
// BatchExporter: accumulates telemetry records and exports them in fixed batches.
#pragma once
#include <cstddef>
#include <functional>
#include <string>
#include <vector>
 
// One telemetry event from a device.
struct Record {
  std::string eventType; // event identifier, e.g. "temp_reading"
  int64_t     timestampMs; // event wall-clock time in milliseconds since epoch
};
 
// Callback type invoked by flush() to ship a batch to the upstream collector.
// Parameters:
//   batch - records to deliver; non-empty
// Returns: true if all records were accepted by the collector; false on any error
using SendFn = std::function<bool(const std::vector<Record>&)>;
 
// Accumulates telemetry records and exports them in one network round-trip.
// Not thread-safe; callers must serialise access.
class BatchExporter {
public:
  // Parameters:
  //   sender   - callback to deliver a completed batch to the upstream collector
  //   maxBatch - maximum number of records the exporter may hold before flush
  // Returns: new BatchExporter with an empty pending queue
  BatchExporter(SendFn sender, std::size_t maxBatch);
 
  // Adds one record to the pending batch.
  // Parameters:
  //   record - event to enqueue
  // Returns: true if the record was accepted; false if the batch is already at maxBatch
  bool addRecord(Record record);
 
  // Exports all pending records to the upstream collector and clears the batch.
  // If the send fails, the pending records must be preserved for a subsequent retry.
  // Parameters: none
  // Returns: true if all pending records were delivered; false on collector error
  bool flush();
 
  // Returns the number of records currently in the pending batch.
  std::size_t pendingCount() const { return pending_.size(); }
 
private:
  SendFn               sender_;   // upstream delivery callback
  std::size_t          maxBatch_; // maximum pending-queue capacity
  std::vector<Record>  pending_;  // accumulated records awaiting export
};