PropertyReport.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Tencent is pleased to support the open source community by making Tars available.
  3. *
  4. * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
  5. *
  6. * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  7. * in compliance with the License. You may obtain a copy of the License at
  8. *
  9. * https://opensource.org/licenses/BSD-3-Clause
  10. *
  11. * Unless required by applicable law or agreed to in writing, software distributed
  12. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  13. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  14. * specific language governing permissions and limitations under the License.
  15. */
  16. #include "servant/PropertyReport.h"
  17. #include "util/tc_common.h"
  18. namespace tars
  19. {
  20. const string PropertyReport::Sum = "Sum";
  21. const string PropertyReport::Distr = "Distr";
  22. const string PropertyReport::Avg = "Avg";
  23. const string PropertyReport::Min = "Min";
  24. const string PropertyReport::Max = "Max";
  25. const string PropertyReport::Count = "Count";
  26. string PropertyReport::sumProperty::get()
  27. {
  28. string s = TC_Common::tostr(_d);
  29. clear();
  30. return s;
  31. }
  32. string PropertyReport::avgProperty::get()
  33. {
  34. if(_count == 0)
  35. {
  36. return "0";
  37. }
  38. string s = TC_Common::tostr(static_cast<double>(_sum)/_count);
  39. clear();
  40. return s;
  41. }
  42. PropertyReport::distrProperty::distrProperty(const vector<int>& range)
  43. {
  44. _range = range;
  45. std::sort(_range.begin(), _range.end());
  46. _range.erase(unique(_range.begin(), _range.end()),_range.end());
  47. _result.resize(_range.size());
  48. }
  49. void PropertyReport::distrProperty::set(int o)
  50. {
  51. vector<int>::iterator it = std::upper_bound(_range.begin(), _range.end(), o);
  52. if (it != _range.end())
  53. {
  54. size_t n = it - _range.begin();
  55. ++_result[n];
  56. }
  57. }
  58. string PropertyReport::distrProperty::get()
  59. {
  60. string s = "";
  61. for(unsigned i = 0; i < _range.size(); ++i)
  62. {
  63. if (i != 0)
  64. {
  65. s += ",";
  66. }
  67. s = s + TC_Common::tostr(_range[i]) + "|" + TC_Common::tostr(_result[i]);
  68. }
  69. for(unsigned i = 0; i < _result.size(); ++i)
  70. {
  71. _result[i] = 0;
  72. }
  73. return s;
  74. }
  75. string PropertyReport::maxProperty::get()
  76. {
  77. string s = TC_Common::tostr(_d);
  78. clear();
  79. return s;
  80. }
  81. string PropertyReport::minProperty::get()
  82. {
  83. string s = TC_Common::tostr(_d);
  84. clear();
  85. return s;
  86. }
  87. void PropertyReport::minProperty::set(int o)
  88. {
  89. //非0最小值
  90. if(_d == 0 ||(_d > o && o != 0))
  91. {
  92. _d = o;
  93. }
  94. }
  95. string PropertyReport::countProperty::get()
  96. {
  97. string s = TC_Common::tostr(_d);
  98. clear();
  99. return s;
  100. }
  101. ///////////////////////////////////////////////
  102. }