private function getDurationsByUser(WakaUser $user) { $settings = new Settings(); $settings->username = $user->username; $settings->password = $user->password; $waka = new Client($settings); $now = $this->time->now(); $weekAgo = $now - 7 * 86400; $lastFetch = $user->lastFetch; if (!$lastFetch) { $lastFetch = $weekAgo; } $today = date('Y-m-d', $now); $lastDate = date('Y-m-d', $lastFetch); if ($today > $lastDate) { $begin = new \DateTime($lastDate); $end = new \DateTime($today); $interval = \DateInterval::createFromDateString('1 day'); $period = new \DatePeriod($begin, $interval, $end); /** @var \DateTime $dt */ foreach ($period as $dt) { $date = $dt->format("Y-m-d"); echo $date, PHP_EOL; $this->getDurationsByUserAndDate($user, $waka, $date); $user->lastFetch = strtotime($date); $user->save(); } } }
public function get($key) { $value = parent::get($key); if (null !== $value) { $expire = $this->expire->get($key); if ($expire && $expire < $this->time->now()) { $this->delete($key); return null; } return $value; } return $value; }
public function save() { if (empty($this->addedAt)) { $this->addedAt = TimeMachine::getInstance()->now(); } return parent::save(); }
public function performAction() { try { $identity = new Identity(); $identity->providerUserId = $this->login; $identity->providerId = Password::getProvider()->id; $identity->meta = Password::getPasswordHash($this->login, $this->password); if ($identity->findSaved()) { throw new \Exception('Login is already registered'); } $identity->save(); $user = new User(); $user->urlName = $this->login; $user->save(); $userIdentity = new UserIdentity(); $userIdentity->userId = $user->id; $userIdentity->identityId = $identity->id; $userIdentity->addedAt = TimeMachine::getInstance()->now(); $userIdentity->save(); AuthService::getInstance()->signIn($identity); Router::redirect($this->io->makeAnchor(Catalog::createState())); } catch (\Exception $exception) { $this->response->error($exception->getMessage()); $this->response->addContent(new Form(RegisterReceive::createState($this->io), $this->io)); } }
private function initDates(&$start, &$end) { if ($start !== null && $end !== null) { return; } $now = TimeMachine::getInstance()->now() - 86400; if (null === $start) { $start = date('Y-m-d', $now); } if (null === $end) { $end = $start; } }
public function set($key, $value, $ttl) { if (($this->dsn->compression || $this->dsn->binary) && !is_string($value)) { throw new \Yaoi\Storage\Exception('String data required for binary or compression', \Yaoi\Storage\Exception::SCALAR_REQUIRED); } if ($this->dsn->compression) { $v = gzcompress($value); } else { $v = $value; } if ($this->dsn->binary || $this->dsn->compression) { $v = new MongoBinData($v); } $this->collection->save(array('k' => $key, 'v' => $v, 't' => $ttl ? TimeMachine::getInstance()->now() + $ttl : null)); }
public function keyExists($key) { /** @noinspection PhpMethodParametersCountMismatchInspection */ $row = $this->db->select($this->table)->select('?', $this->expireField)->where('? = ?', $this->keyField, $key)->query()->fetchRow(); if (!$row) { return false; } else { $expireUt = $row[$this->expireField->name]; if ($expireUt && $this->time->now() > $expireUt) { $this->delete($key); return false; } return true; } }
public function testThe() { $ps = new Reader(); $time = TimeMachine::getInstance(); $time->mock(new Mock(new Storage('serialized-file:///' . __DIR__ . '/mocks/time.serialized'))); $ps->mock(new Mock(new Storage('serialized-file:///' . __DIR__ . '/mocks/ps-reader.serialized'))); $history = new History(); $now = $time->microNow(); do { $processStates = $ps->get(); $history->add($processStates); echo '.'; sleep(1); } while ($time->microNow() < $now + 5); print_r($history->processes); }
public function execute() { set_time_limit(0); $this->history = new History(); $this->history->minCpuPercent = $this->minCpuPercent; $this->history->minMemPercent = $this->minMemPercent; $reader = new Reader(); $time = TimeMachine::getInstance(); $start = $time->microNow(); $lastUpdate = $start; do { $processStates = $reader->get(); $this->history->add($processStates); echo '.'; $now = $time->microNow(); if ($lastUpdate < $now - $this->saveInterval) { $lastUpdate = $now; echo 's'; $this->renderReport(); } sleep($this->delay); } while ($this->timeLimit === null || $now > $start + $this->timeLimit); }
public function signIn(Identity $identity) { // TODO multi identity login $session = new Session(); $session->identityId = $identity->id; $users = $this->getUsersByIdentityId($identity->id); if (!$users) { // inactive identity $identity->delete(); throw new Exception('Identity without users found, deleted', Exception::IDENTITY_WITHOUT_USERS); } if (isset($_COOKIE[$this->settings->sessionName])) { Session::statement()->delete()->where('? = ?', Session::columns()->token, $_COOKIE[$this->settings->sessionName])->query(); } do { $token = $this->createSessionId(); } while (Session::findByToken($token)); setcookie($this->settings->sessionName, $token, time() + $this->settings->expireTime, '/', null, null, true); $session->identityId = $identity->id; $session->token = $token; $session->createdAt = TimeMachine::getInstance()->now(); $session->save(); }
/** * @param array|ProcessState[] $states */ public function add(array $states) { $now = $this->time->now(); $total = new State(); /** @var State[] $groups */ $groups = array(); foreach ($states as $processState) { $total->memPercent += $processState->memPercent; $total->cpuPercent += $processState->cpuPercent; if ($processState->command === 'ps aux') { continue; } $group =& $groups[$processState->getProgramName()]; if (null === $group) { $group = new State(); } $group->memPercent += $processState->memPercent; $group->cpuPercent += $processState->cpuPercent; $group->rss += $processState->rss; if ($this->minCpuPercent && $processState->cpuPercent < $this->minCpuPercent) { //echo 'l'; continue; } if ($this->minMemPercent && $processState->memPercent < $this->minMemPercent) { //echo 'l'; continue; } if (isset($this->processes[$processState->pid])) { $process = $this->processes[$processState->pid]; } else { $process = new Process(); $process->user = $processState->user; $process->command = $processState->command; $process->pid = $processState->pid; $process->started = $processState->started; $this->processes[$processState->pid] = $process; } //echo 'o'; $state = new State(); $state->cpuPercent = $processState->cpuPercent; $state->memPercent = $processState->memPercent; $state->rss = $processState->rss; $state->stat = $processState->stat; $state->time = $processState->time; $state->tt = $processState->tt; $state->vsz = $processState->vsz; $this->states[$process->pid][$now] = $state; } foreach ($groups as $name => $state) { if ($this->minCpuPercent && $state->cpuPercent < $this->minCpuPercent) { //echo 'l'; continue; } if ($this->minMemPercent && $state->memPercent < $this->minMemPercent) { //echo 'l'; continue; } $this->groupStates[$name][$now] = $state; } $this->totals[$now] = $total; }
} else { $m = $this->month; } if (strlen($m) < 2) { $m = '0' . $m; } } if (strlen($d) < 2) { $d = '0' . $d; } if ($m < $this->month) { return $this->year + 1 . '-' . $m . '-' . $d; } else { return $this->year . '-' . $m . '-' . $d; } } /** * @var Mock */ protected $mock; public function mock(Mock $dataSet = null) { if ($dataSet === null) { $dataSet = Mock::getNull(); } $this->mock = $dataSet; return $this; } } TimeMachine::register(new Service\Settings());
/** * @param $driver * @param $headers * @return mixed * @throws \Yaoi\Service\Exception */ private function prepareUpload($driver, $headers) { $driver->setMethod('POST'); $multipartBoundary = '--------------------------' . \Yaoi\Date\TimeMachine::getInstance()->microNow(); $content = ''; foreach ($this->post as $name => $value) { if ($value instanceof UploadFile) { $content .= "--" . $multipartBoundary . "\r\n" . "Content-Disposition: form-data; name=\"" . $name . "\"; filename=\"" . $value->getFileName() . "\"\r\n" . "Content-Type: " . $value->mimeType . "\r\n\r\n" . $value->getContents() . "\r\n"; } else { $content .= "--" . $multipartBoundary . "\r\n" . "Content-Disposition: form-data; name=\"{$name}\"\r\n\r\n" . "{$value}\r\n"; } } $content .= "--" . $multipartBoundary . "--\r\n"; $driver->setRequestContent($content); $headers['Content-Type'] = 'multipart/form-data; boundary=' . $multipartBoundary; unset($content); return $headers; }