| #ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_SUBSCRIPTION_CONFIG_H_ |
| #define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_SUBSCRIPTION_CONFIG_H_ |
| |
| #include <algorithm> |
| #include <cmath> |
| #include <cstdint> |
| #include <tuple> |
| |
| #include "absl/status/status.h" |
| #include "absl/status/statusor.h" |
| #include "absl/strings/substitute.h" |
| |
| namespace milotic_tlbmc { |
| |
| struct SubscriptionParams { |
| uint64_t sample_interval_ns = 0; |
| uint64_t export_interval_ms = 0; |
| uint32_t max_batch_size = 0; |
| |
| // Returns the batch size for the subscription based on the sample interval |
| // and export interval and the max batch size if applicable. |
| absl::StatusOr<uint32_t> GetBatchSizeForSubscription() const { |
| if (sample_interval_ns == 0 || export_interval_ms == 0) { |
| return absl::InvalidArgumentError( |
| "Sample interval and export interval must be positive."); |
| } |
| |
| uint64_t export_interval_ns = export_interval_ms * 1000000; |
| if (export_interval_ns < sample_interval_ns) { |
| return absl::InvalidArgumentError( |
| absl::Substitute("Export interval $0 must be greater than or equal " |
| "to sample interval $1.", |
| export_interval_ns, sample_interval_ns)); |
| } |
| |
| uint32_t batch_size_based_on_export_interval = static_cast<uint32_t>( |
| std::floor(export_interval_ns / sample_interval_ns)); |
| return std::min(max_batch_size > 0 ? max_batch_size |
| : batch_size_based_on_export_interval, |
| batch_size_based_on_export_interval); |
| } |
| |
| struct Compare { |
| bool operator()(const SubscriptionParams* a, |
| const SubscriptionParams* b) const { |
| return std::tie(a->sample_interval_ns, a->export_interval_ms, |
| a->max_batch_size) < std::tie(b->sample_interval_ns, |
| b->export_interval_ms, |
| b->max_batch_size); |
| } |
| }; |
| }; |
| |
| } // namespace milotic_tlbmc |
| |
| #endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_CONFIGS_SUBSCRIPTION_CONFIG_H_ |