job_counters.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "job_counters.h"
  2. namespace NYT {
  3. ////////////////////////////////////////////////////////////////////
  4. namespace {
  5. ui64 CountTotal(const TNode& data)
  6. {
  7. if (data.IsMap()) {
  8. if (auto totalPtr = data.AsMap().FindPtr("total")) {
  9. return data["total"].IntCast<ui64>();
  10. } else {
  11. ui64 total = 0;
  12. for (const auto& keyVal: data.AsMap()) {
  13. total += CountTotal(keyVal.second);
  14. }
  15. return total;
  16. }
  17. } else {
  18. return data.IntCast<ui64>();
  19. }
  20. }
  21. TNode GetNode(const TNode& data, const TStringBuf& key)
  22. {
  23. if (auto resPtr = data.AsMap().FindPtr(key)) {
  24. return *resPtr;
  25. }
  26. return TNode();
  27. }
  28. } // namespace
  29. ////////////////////////////////////////////////////////////////////
  30. TJobCounter::TJobCounter(TNode data)
  31. : Data_(std::move(data))
  32. {
  33. if (Data_.HasValue()) {
  34. Total_ = CountTotal(Data_);
  35. }
  36. }
  37. TJobCounter::TJobCounter(ui64 total)
  38. : Total_(total)
  39. { }
  40. ui64 TJobCounter::GetTotal() const
  41. {
  42. return Total_;
  43. }
  44. ui64 TJobCounter::GetValue(const TStringBuf key) const
  45. {
  46. if (Data_.HasValue()) {
  47. return CountTotal(Data_[key]);
  48. }
  49. return 0;
  50. }
  51. ////////////////////////////////////////////////////////////////////
  52. TJobCounters::TJobCounters(const NYT::TNode& counters)
  53. : Total_(0)
  54. {
  55. if (!counters.IsMap()) {
  56. ythrow yexception() << "TJobCounters must be initialized with Map type TNode";
  57. }
  58. auto abortedNode = GetNode(counters, "aborted");
  59. if (abortedNode.HasValue()) {
  60. Aborted_ = TJobCounter(GetNode(abortedNode, "total"));
  61. AbortedScheduled_ = TJobCounter(GetNode(abortedNode, "scheduled"));
  62. AbortedNonScheduled_ = TJobCounter(GetNode(abortedNode, "non_scheduled"));
  63. }
  64. auto completedNode = GetNode(counters, "completed");
  65. if (completedNode.HasValue()) {
  66. Completed_ = TJobCounter(GetNode(completedNode, "total"));
  67. CompletedNonInterrupted_ = TJobCounter(GetNode(completedNode, "non-interrupted"));
  68. CompletedInterrupted_ = TJobCounter(GetNode(completedNode, "interrupted"));
  69. }
  70. Lost_ = TJobCounter(GetNode(counters, "lost"));
  71. Invalidated_ = TJobCounter(GetNode(counters, "invalidated"));
  72. Failed_ = TJobCounter(GetNode(counters, "failed"));
  73. Running_ = TJobCounter(GetNode(counters, "running"));
  74. Suspended_ = TJobCounter(GetNode(counters, "suspended"));
  75. Pending_ = TJobCounter(GetNode(counters, "pending"));
  76. Blocked_ = TJobCounter(GetNode(counters, "blocked"));
  77. Total_ = CountTotal(counters);
  78. }
  79. const TJobCounter& TJobCounters::GetAborted() const
  80. {
  81. return Aborted_;
  82. }
  83. const TJobCounter& TJobCounters::GetAbortedScheduled() const
  84. {
  85. return AbortedScheduled_;
  86. }
  87. const TJobCounter& TJobCounters::GetAbortedNonScheduled() const
  88. {
  89. return AbortedNonScheduled_;
  90. }
  91. const TJobCounter& TJobCounters::GetCompleted() const
  92. {
  93. return Completed_;
  94. }
  95. const TJobCounter& TJobCounters::GetCompletedNonInterrupted() const
  96. {
  97. return CompletedNonInterrupted_;
  98. }
  99. const TJobCounter& TJobCounters::GetCompletedInterrupted() const
  100. {
  101. return CompletedInterrupted_;
  102. }
  103. const TJobCounter& TJobCounters::GetLost() const
  104. {
  105. return Lost_;
  106. }
  107. const TJobCounter& TJobCounters::GetInvalidated() const
  108. {
  109. return Invalidated_;
  110. }
  111. const TJobCounter& TJobCounters::GetFailed() const
  112. {
  113. return Failed_;
  114. }
  115. const TJobCounter& TJobCounters::GetRunning() const
  116. {
  117. return Running_;
  118. }
  119. const TJobCounter& TJobCounters::GetSuspended() const
  120. {
  121. return Suspended_;
  122. }
  123. const TJobCounter& TJobCounters::GetPending() const
  124. {
  125. return Pending_;
  126. }
  127. const TJobCounter& TJobCounters::GetBlocked() const
  128. {
  129. return Blocked_;
  130. }
  131. ui64 TJobCounters::GetTotal() const
  132. {
  133. return Total_;
  134. }
  135. ////////////////////////////////////////////////////////////////////
  136. } // namespace NYT