histogram_snapshot.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "histogram_snapshot.h"
  2. #include <util/stream/output.h>
  3. #include <iostream>
  4. namespace NMonitoring {
  5. IHistogramSnapshotPtr ExplicitHistogramSnapshot(TConstArrayRef<TBucketBound> bounds, TConstArrayRef<TBucketValue> values) {
  6. Y_ENSURE(bounds.size() == values.size(),
  7. "mismatched sizes: bounds(" << bounds.size() <<
  8. ") != buckets(" << values.size() << ')');
  9. auto snapshot = TExplicitHistogramSnapshot::New(bounds.size());
  10. for (size_t i = 0; i != bounds.size(); ++i) {
  11. (*snapshot)[i].first = bounds[i];
  12. (*snapshot)[i].second = values[i];
  13. }
  14. return snapshot;
  15. }
  16. } // namespace NMonitoring
  17. namespace {
  18. template <typename TStream>
  19. auto& Output(TStream& os, const NMonitoring::IHistogramSnapshot& hist) {
  20. os << TStringBuf("{");
  21. ui32 i = 0;
  22. ui32 count = hist.Count();
  23. if (count > 0) {
  24. for (; i < count - 1; ++i) {
  25. os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i);
  26. os << TStringBuf(", ");
  27. }
  28. if (hist.UpperBound(i) == Max<NMonitoring::TBucketBound>()) {
  29. os << TStringBuf("inf: ") << hist.Value(i);
  30. } else {
  31. os << hist.UpperBound(i) << TStringBuf(": ") << hist.Value(i);
  32. }
  33. }
  34. os << TStringBuf("}");
  35. return os;
  36. }
  37. } // namespace
  38. std::ostream& operator<<(std::ostream& os, const NMonitoring::IHistogramSnapshot& hist) {
  39. return Output(os, hist);
  40. }
  41. template <>
  42. void Out<NMonitoring::IHistogramSnapshot>(IOutputStream& os, const NMonitoring::IHistogramSnapshot& hist) {
  43. Output(os, hist);
  44. }