ctl_item.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace refill;
  3. use Log;
  4. class ctl_item
  5. {
  6. const speed_secs = 60;
  7. private $mName;
  8. private $mCardType;
  9. private $mAmount;
  10. private $mQuality;
  11. private $mMaxSpeed;
  12. private $mPrefix;
  13. private $mLastReadTime;
  14. private $mRecords;
  15. private $mPriority;
  16. private $mStorge;
  17. private $mOpened;
  18. private $mPrice;
  19. private $mCommitSucc;
  20. private $mCommitFail;
  21. private $mNotifySucc;
  22. private $mNotifyFail;
  23. private $mLazySpeed;
  24. private $mSleepTime;
  25. private $mSleeping;
  26. public function __construct($chname, $card_type, $amount, $max_speed, $priority, $storge, $opened, $quality)
  27. {
  28. $this->mPrefix = "{$chname}-{$quality}-{$card_type}-{$amount}";
  29. $this->mName = $chname;
  30. $this->mCardType = $card_type;
  31. $this->mAmount = $amount;
  32. $this->mQuality = $quality;
  33. $this->mMaxSpeed = $max_speed;
  34. $this->mPriority = $priority;
  35. $this->mStorge = $storge;
  36. $this->mOpened = $opened;
  37. $this->mRecords = [];
  38. $this->mPrice = 0.0;
  39. $this->mCommitSucc = 0;
  40. $this->mCommitFail = 0;
  41. $this->mNotifySucc = 0;
  42. $this->mNotifyFail = 0;
  43. $this->mLazySpeed = 0;
  44. $this->mSleepTime = 0;
  45. $this->mSleeping = false;
  46. $this->load(time() - ctl_item::speed_secs + 1);
  47. }
  48. private function load($start_time)
  49. {
  50. $end = time();
  51. for ($time = $start_time; $time <= $end; $time++) {
  52. $val = util::hget_commit_pre_sec($this->mName, $this->mCardType, $this->mAmount, $this->mQuality, $time);
  53. $this->mRecords[$time] = $val;
  54. }
  55. $this->mLastReadTime = $end;
  56. $min = $end - ctl_item::speed_secs + 1;
  57. $rmkeys = [];
  58. foreach ($this->mRecords as $time => $val) {
  59. if ($time < $min) {
  60. $rmkeys[] = $time;
  61. }
  62. }
  63. foreach ($rmkeys as $key) {
  64. unset($this->mRecords[$key]);
  65. }
  66. }
  67. public function cur_speed(): int
  68. {
  69. $this->load($this->mLastReadTime);
  70. $speed = array_sum($this->mRecords);
  71. //Log::record("{$this->mPrefix} speed = {$speed} max_speed={$this->mMaxSpeed}", Log::DEBUG);
  72. return $speed;
  73. }
  74. public function speed_overload(): bool
  75. {
  76. if ($this->mMaxSpeed < 0) {
  77. return false;
  78. } elseif ($this->mMaxSpeed == 0) {
  79. return true;
  80. } else {
  81. return $this->lazy_speed() >= $this->mMaxSpeed;
  82. }
  83. }
  84. public function calc_speed()
  85. {
  86. $this->mLazySpeed = $this->cur_speed();
  87. }
  88. public function lazy_speed()
  89. {
  90. return $this->mLazySpeed;
  91. }
  92. public function name()
  93. {
  94. return $this->mName;
  95. }
  96. public function priority()
  97. {
  98. return $this->mPriority;
  99. }
  100. public function storge()
  101. {
  102. return $this->mStorge;
  103. }
  104. public function opened()
  105. {
  106. return $this->mOpened;
  107. }
  108. public function max_speed()
  109. {
  110. return $this->mMaxSpeed;
  111. }
  112. public function quality()
  113. {
  114. return $this->mQuality;
  115. }
  116. public function spec()
  117. {
  118. return $this->mAmount;
  119. }
  120. public function card_type()
  121. {
  122. return $this->mCardType;
  123. }
  124. public function set_price($price)
  125. {
  126. $this->mPrice = $price;
  127. }
  128. public function price()
  129. {
  130. return $this->mPrice;
  131. }
  132. public function set_ratio($ratio)
  133. {
  134. $this->mCommitSucc = $ratio[0];
  135. $this->mCommitFail = $ratio[1];
  136. $this->mNotifySucc = $ratio[2];
  137. $this->mNotifyFail = $ratio[3];
  138. }
  139. public function commit_statics()
  140. {
  141. $count = $this->mCommitSucc + $this->mCommitFail;
  142. return [$count, $this->mCommitSucc];
  143. }
  144. public function notify_statics()
  145. {
  146. $count = $this->mNotifySucc + $this->mNotifyFail;
  147. return [$count, $this->mNotifySucc];
  148. }
  149. public function notify_ratio()
  150. {
  151. $count = $this->mNotifySucc + $this->mNotifyFail;
  152. if ($count > 0) {
  153. return [$count, $this->mNotifySucc / $count];
  154. } else {
  155. return [$count, 0];
  156. }
  157. }
  158. public function sleeping()
  159. {
  160. return [$this->mSleeping, $this->mSleepTime];
  161. }
  162. public function sleep()
  163. {
  164. $this->mSleeping = true;
  165. $this->mSleepTime = time();
  166. }
  167. public function wakeup()
  168. {
  169. $this->mSleeping = false;
  170. $this->mSleepTime = 0;
  171. }
  172. }