/** * @param QChartsSubjectInterface $user * @param QueryRequest $queryRequest * @throws DatabaseException */ public function removeFavourite(QChartsSubjectInterface $user, QueryRequest $queryRequest) { try { $queryRequest->removeFavoritedBy($user); $this->queryRepository->update($queryRequest); } catch (\Exception $e) { throw new DatabaseException("Error while attempting to remove the favorite, {$e->getMessage()}", $e->getCode(), $e); } }
/** * @param QueryRequest $qr * @return array * @throws TypeNotValidException * @throws ValidationFailedException */ public function getResults(QueryRequest $qr) { // if the query request is cached then call the cached version! $configurations = ["time" => $qr->getConfig()->getExecutionLimit(), "rows" => $qr->getConfig()->getQueryLimit(), "chartType" => $qr->getConfig()->getTypeOfChart(), "offset" => $qr->getConfig()->getOffset(), "connection" => $qr->getConfig()->getDatabaseConnection(), "cronExpression" => $qr->getCronExpression()]; if ($this->hasDependency(LiveStrategy::DEPENDENCY_NAME)) { try { $dependencies = $this->getDependencies(); /** @var QueryValidatorService $queryValidator */ $queryValidator = $dependencies[LiveStrategy::DEPENDENCY_NAME]; $query = $qr->getQuery()->getQuery(); $queryValidator->isValidQuery($query, $configurations["connection"]); $results = $queryValidator->validateQueryExecution($query, $configurations); return $results; } catch (OffLimitsException $e) { $exception = new ValidationFailedException("Query validation has failed. {$e->getMessage()}", $e->getCode(), $e); throw $exception; } } throw new TypeNotValidException(ExceptionMessage::DEPENDENCY_NOT_AVAILABLE(", QueryValidator was absent"), 500); }
/** * @param QueryRequest $query * @param SymfonyStyle $io * @param DateTime $date * @throws \QCharts\CoreBundle\Exception\WriteReadException */ protected function updateQuery(QueryRequest $query, SymfonyStyle $io, DateTime $date) { $modes = QueryUpdateCommand::getModes(); $dateFormat = SnapshotService::FILE_DATE_FORMAT; /** @var SnapshotService $snapshotService */ $snapshotService = $this->getContainer()->get("qcharts.core.snapshot_service"); /** @var QueryRepository $qrRepo */ $qrRepo = $this->getContainer()->get("qcharts.query_repo"); $cron = CronExpression::factory($query->getCronExpression()); $queryDate = $query->getConfig()->getFetchedOn()->format($dateFormat); $io->newLine(); $io->section("QCharts checking: '{$query->getTitle()}' with date: {$queryDate}"); if ($cron->isDue($date)) { //update it! $duration = $snapshotService->updateSnapshot($query); $io->newLine(1); $io->note("QCharts updating:"); $io->table(['Title', 'CronExpression', 'Last fetch', 'Mode', 'Query execution time'], [[$query->getTitle(), $query->getCronExpression(), $query->getConfig()->getFetchedOn()->format($dateFormat), $modes[$query->getConfig()->getIsCached()], "{$duration} secs."]]); $qrRepo->setUpdatedOn($query, $date); } $io->success("QCharts '{$query->getTitle()}' is up to date, next run: {$cron->getNextRunDate()->format($dateFormat)}"); $io->progressAdvance(1); $io->newLine(2); }
/** * @return string */ protected function getSnapshotsPath() { return "{$this->getFullPath()}/{$this->queryRequest->getId()}"; }
/** * @param QueryRequest $queryRequest * @param \DateTime $dateTime * @param bool $autoFlush */ public function setUpdatedOn(QueryRequest $queryRequest, \DateTime $dateTime, $autoFlush = true) { $queryRequest->getConfig()->setFetchedOn($dateTime); $this->getEntityManager()->persist($queryRequest); $autoFlush ? $this->getEntityManager()->flush() : null; }
/** * @param QueryRequest $queryRequest * @param $snapshot * @return string * @throws TypeNotValidException * @throws SnapshotException */ public function formatSnapshotName(QueryRequest $queryRequest, $snapshot = null) { if ($queryRequest->getConfig()->getIsCached() == 0) { throw new SnapshotException("Results are being call live", 500); } if (is_null($snapshot) || !is_numeric($snapshot)) { $snapshots = $this->getSnapshotsFormatted($queryRequest); ksort($snapshots); $fresh = array_pop($snapshots); return $fresh; } $date = new \DateTime(); $date = $date->setTimestamp($snapshot); return $date->format(SnapshotService::FRIENDLY_FILE_NAME); }