calculateThroughput function

double calculateThroughput(
  1. List<PacketMetrics> packets
)

Calculates the throughput (in kbps) of a list of packets. Assumes all packets are from the same device.

Implementation

double calculateThroughput(List<PacketMetrics> packets) {
  if (packets.isEmpty) return 0.0;

  // Filter received packets (not discarded)
  final receivedPackets =
  packets.where((p) => p.packetSent && !p.packetDiscarded).toList();

  if (receivedPackets.length < 2) return 0.0;

  // Sort by time
  receivedPackets.sort((a, b) => a.dateTime.compareTo(b.dateTime));

  final firstTime = receivedPackets.first.dateTime;
  final lastTime = receivedPackets.last.dateTime;
  final totalTimeSec = lastTime.difference(firstTime).inMilliseconds / 1000.0;

  // Sum total packet size in bits
  final totalBits =
      receivedPackets.fold(0, (sum, p) => sum + p.packetSize) * 8;

  return totalTimeSec > 0 ? totalBits / totalTimeSec / 1000.0 : 0.0; // kbps
}