123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace refill;
- use Log;
- class ctl_item
- {
- const speed_secs = 60;
- private $mName;
- private $mCardType;
- private $mAmount;
- private $mQuality;
- private $mMaxSpeed;
- private $mPrefix;
- private $mLastReadTime;
- private $mRecords;
- private $mPriority;
- private $mStorge;
- private $mOpened;
- private $mPrice;
- private $mCommitSucc;
- private $mCommitFail;
- private $mNotifySucc;
- private $mNotifyFail;
- private $mLazySpeed;
- private $mSleepTime;
- private $mSleeping;
- public function __construct($chname, $card_type, $amount, $max_speed, $priority, $storge, $opened, $quality)
- {
- $this->mPrefix = "{$chname}-{$quality}-{$card_type}-{$amount}";
- $this->mName = $chname;
- $this->mCardType = $card_type;
- $this->mAmount = $amount;
- $this->mQuality = $quality;
- $this->mMaxSpeed = $max_speed;
- $this->mPriority = $priority;
- $this->mStorge = $storge;
- $this->mOpened = $opened;
- $this->mRecords = [];
- $this->mPrice = 0.0;
- $this->mCommitSucc = 0;
- $this->mCommitFail = 0;
- $this->mNotifySucc = 0;
- $this->mNotifyFail = 0;
- $this->mLazySpeed = 0;
- $this->mSleepTime = 0;
- $this->mSleeping = false;
- $this->load(time() - ctl_item::speed_secs + 1);
- }
- private function load($start_time)
- {
- $end = time();
- for ($time = $start_time; $time <= $end; $time++) {
- $val = util::hget_commit_pre_sec($this->mName, $this->mCardType, $this->mAmount, $this->mQuality, $time);
- $this->mRecords[$time] = $val;
- }
- $this->mLastReadTime = $end;
- $min = $end - ctl_item::speed_secs + 1;
- $rmkeys = [];
- foreach ($this->mRecords as $time => $val) {
- if ($time < $min) {
- $rmkeys[] = $time;
- }
- }
- foreach ($rmkeys as $key) {
- unset($this->mRecords[$key]);
- }
- }
- public function cur_speed(): int
- {
- $this->load($this->mLastReadTime);
- $speed = array_sum($this->mRecords);
- //Log::record("{$this->mPrefix} speed = {$speed} max_speed={$this->mMaxSpeed}", Log::DEBUG);
- return $speed;
- }
- public function speed_overload(): bool
- {
- if ($this->mMaxSpeed < 0) {
- return false;
- } elseif ($this->mMaxSpeed == 0) {
- return true;
- } else {
- return $this->lazy_speed() >= $this->mMaxSpeed;
- }
- }
- public function calc_speed()
- {
- $this->mLazySpeed = $this->cur_speed();
- }
- public function lazy_speed()
- {
- return $this->mLazySpeed;
- }
- public function name()
- {
- return $this->mName;
- }
- public function priority()
- {
- return $this->mPriority;
- }
- public function storge()
- {
- return $this->mStorge;
- }
- public function opened()
- {
- return $this->mOpened;
- }
- public function max_speed()
- {
- return $this->mMaxSpeed;
- }
- public function quality()
- {
- return $this->mQuality;
- }
- public function spec()
- {
- return $this->mAmount;
- }
- public function card_type()
- {
- return $this->mCardType;
- }
- public function set_price($price)
- {
- $this->mPrice = $price;
- }
- public function price()
- {
- return $this->mPrice;
- }
- public function set_ratio($ratio)
- {
- $this->mCommitSucc = $ratio[0];
- $this->mCommitFail = $ratio[1];
- $this->mNotifySucc = $ratio[2];
- $this->mNotifyFail = $ratio[3];
- }
- public function commit_statics()
- {
- $count = $this->mCommitSucc + $this->mCommitFail;
- return [$count, $this->mCommitSucc];
- }
- public function notify_statics()
- {
- $count = $this->mNotifySucc + $this->mNotifyFail;
- return [$count, $this->mNotifySucc];
- }
- public function notify_ratio()
- {
- $count = $this->mNotifySucc + $this->mNotifyFail;
- if ($count > 0) {
- return [$count, $this->mNotifySucc / $count];
- } else {
- return [$count, 0];
- }
- }
- public function sleeping()
- {
- return [$this->mSleeping, $this->mSleepTime];
- }
- public function sleep()
- {
- $this->mSleeping = true;
- $this->mSleepTime = time();
- }
- public function wakeup()
- {
- $this->mSleeping = false;
- $this->mSleepTime = 0;
- }
- }
|