Rise
The Vieneo Province
Loading...
Searching...
No Matches
TelemetryUploadCadence.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <cmath>
5
6namespace Telemetry
7{
9
10 PeriodicGate::PeriodicGate(double intervalSeconds) :
11 intervalSeconds_(std::isfinite(intervalSeconds) && intervalSeconds > 0.0
12 ? intervalSeconds
14 {
15 }
16
17 bool PeriodicGate::Advance(double elapsedSeconds)
18 {
19 if (!std::isfinite(elapsedSeconds) || elapsedSeconds <= 0.0)
20 return false;
21
22 accumulatedSeconds_ += elapsedSeconds;
24 return false;
25
26 // Preserve the fractional remainder for stable average cadence while
27 // coalescing a long stall into one event. Subtracting whole intervals is
28 // more stable than fmod for values such as 2.0 % 0.05.
29 const double elapsedIntervals = std::floor(accumulatedSeconds_ / intervalSeconds_ + 1.0e-9);
30 accumulatedSeconds_ -= elapsedIntervals * intervalSeconds_;
33 return true;
34 }
35
37 {
39 }
40
42 {
43 return intervalSeconds_;
44 }
45
47 {
49 }
50
58
59 void UploadDiagnostics::RecordPacket(double nowSeconds)
60 {
61 if (!std::isfinite(nowSeconds) || nowSeconds < 0.0)
62 return;
63
64 lastPacketSeconds_ = nowSeconds;
65 metrics_.hasPacket = true;
67
68 if (packetWindowCount_ == 0)
69 {
70 packetWindowStartSeconds_ = nowSeconds;
72 return;
73 }
74
76 const double elapsed = nowSeconds - packetWindowStartSeconds_;
77 if (elapsed >= 0.5)
78 {
79 metrics_.packetsPerSecond = static_cast<double>(packetWindowCount_ - 1) / elapsed;
80 packetWindowStartSeconds_ = nowSeconds;
82 }
83 }
84
86 {
87 UploadMetrics result = metrics_;
88 if (result.hasPacket && std::isfinite(nowSeconds))
89 result.ageOfLastPacketSeconds = std::max(0.0, nowSeconds - lastPacketSeconds_);
90 return result;
91 }
92}
PeriodicGate(double intervalSeconds)
bool Advance(double elapsedSeconds)
UploadMetrics GetMetrics(double nowSeconds) const
UploadDiagnostics gUploadDiagnostics
constexpr double ClientUploadIntervalSeconds