/** * @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 array $results * @throws TypeNotValidException * @throws \QCharts\CoreBundle\Exception\WriteReadException */ public function writeSnapshot(array $results) { $this->prepareDirectory("{$this->getSnapshotsPath()}"); $fileName = SnapshotManager::WRITING_FILE; $filePath = "{$this->getSnapshotsPath()}/{$fileName}"; try { // save it! $this->writeFile($filePath, $results); /** @var SaverInterface $saver */ $saver = SaverFactory::createSaver($this->queryRequest->getConfig()->getIsCached()); $fileSiblings = $this->getSnapshotPaths(); $options = ["siblings" => $fileSiblings]; $saver->save($this->fileSystem, $this->getSnapshotsPath(), SnapshotManager::WRITING_FILE, $options); } catch (TypeNotValidException $e) { throw new TypeNotValidException("Type when creating the snapshot was not valid", 500, $e); } catch (IOException $e) { //creating symlink was not possible or mostly not probable the filename already exists in the same context throw new WriteReadException("Couldn't write or read", 500, $e); } }
/** * @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 $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); }
/** * @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); }