* added caching to watchers

This commit is contained in:
Dávid Danyi 2018-09-13 11:33:06 +02:00
parent 3d42f16c38
commit bb937a664b
2 changed files with 50 additions and 36 deletions

View File

@ -31,6 +31,12 @@ class UpdatePageCachesCommand extends Command
->setDescription('Updates page-cache data for kanban pages'); ->setDescription('Updates page-cache data for kanban pages');
} }
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|null|void
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$teams = $this->teamService->listTeams(); $teams = $this->teamService->listTeams();
@ -39,6 +45,7 @@ class UpdatePageCachesCommand extends Command
if (null !== $team->getFilterId()) { if (null !== $team->getFilterId()) {
$this->jiraCollectorService->getKanbanBoard($team->getId(), true); $this->jiraCollectorService->getKanbanBoard($team->getId(), true);
} }
$this->jiraCollectorService->getTeamWatchedIssues($team->getId(), true);
} }
} }
} }

View File

@ -22,6 +22,7 @@ use Zend\Json\Json;
class JiraCollectorService class JiraCollectorService
{ {
const CACHE_KEY_KANBANBOARD = 'kanbanBoard'; const CACHE_KEY_KANBANBOARD = 'kanbanBoard';
const CACHE_KEY_WATCHED = 'watchedIssues';
const BACKLOG_FIELD_DELIMITER = ';'; const BACKLOG_FIELD_DELIMITER = ';';
const EPIC_TICKET_LINK = 'customfield_11711'; const EPIC_TICKET_LINK = 'customfield_11711';
@ -119,55 +120,61 @@ class JiraCollectorService
$kanbanBoard = $this->hydrateKanbanBoard($team, $parsedJsonData); $kanbanBoard = $this->hydrateKanbanBoard($team, $parsedJsonData);
$this->cache->setItem(sprintf("%s-%s", self::CACHE_KEY_KANBANBOARD, $teamName), serialize($kanbanBoard)); $this->cache->setItem(sprintf("%s-%s", self::CACHE_KEY_KANBANBOARD, $teamName), serialize($kanbanBoard));
} else { return $kanbanBoard;
$kanbanBoard = unserialize($kanbanBoard);
} }
return unserialize($kanbanBoard);
return $kanbanBoard;
} }
/** /**
* @param int $teamId * @param int $teamId
* @param bool $forceReload
* @return array * @return array
* @throws \Exception * @throws \Exception
*/ */
public function getTeamWatchedIssues(int $teamId) public function getTeamWatchedIssues(int $teamId, bool $forceReload = false)
{ {
$team = $this->teamService->getTeam($teamId); $team = $this->teamService->getTeam($teamId);
$members = array_map(function(array $member): string { $teamName = $team->getName();
return $member['signum']; $watchedIssues = $this->cache->getItem(sprintf("%s-%s", self::CACHE_KEY_WATCHED, $teamName));
}, $team->getMembers()); if ($forceReload || null === $watchedIssues) {
$preparedMembers = sprintf('"%s"', implode('","', $members)); $members = array_map(function (array $member): string {
$filter = sprintf( return $member['signum'];
self::WATCH_FILTER, }, $team->getMembers());
sprintf('"%s"', implode('","', self::IGNORED_STATUSES)), $preparedMembers = sprintf('"%s"', implode('","', $members));
$preparedMembers, $preparedMembers $filter = sprintf(
); self::WATCH_FILTER,
$user = $this->config->get('jira.user'); sprintf('"%s"', implode('","', self::IGNORED_STATUSES)),
$password = $this->config->get('jira.password'); $preparedMembers, $preparedMembers
/** @var Config $kanbanBoardUriParams */ );
$jiraWatchedIssues = $this->config->get('url.jiraWatchedIssues'); $user = $this->config->get('jira.user');
$kanbanBoardFilterFields = [ $password = $this->config->get('jira.password');
'assignee', /** @var Config $kanbanBoardUriParams */
'summary', $jiraWatchedIssues = $this->config->get('url.jiraWatchedIssues');
'comment', $kanbanBoardFilterFields = [
]; 'assignee',
$issueFields = implode(",", $kanbanBoardFilterFields); 'summary',
$jiraIssueUri = sprintf($jiraWatchedIssues, $filter, $issueFields); 'comment',
];
$issueFields = implode(",", $kanbanBoardFilterFields);
$jiraIssueUri = sprintf($jiraWatchedIssues, $filter, $issueFields);
$response = $this->httpClient $response = $this->httpClient
->setUri($jiraIssueUri) ->setUri($jiraIssueUri)
->setAuth($user, $password) ->setAuth($user, $password)
->send(); ->send();
if (!$response->isSuccess()) { if (!$response->isSuccess()) {
throw new \UnexpectedValueException(sprintf( throw new \UnexpectedValueException(sprintf(
"Bad JIRA result for URL:\n%s", "Bad JIRA result for URL:\n%s",
$jiraIssueUri $jiraIssueUri
), $response->getStatusCode()); ), $response->getStatusCode());
}
$watchedIssues = $this->hydrateWatchedIssues(Decoder::decode($response->getBody(), Json::TYPE_ARRAY), $members);
$this->cache->setItem(sprintf("%s-%s", self::CACHE_KEY_KANBANBOARD, $teamName), serialize($watchedIssues));
return $watchedIssues;
} }
return unserialize($watchedIssues);
return $this->hydrateWatchedIssues(Decoder::decode($response->getBody(), Json::TYPE_ARRAY), $members);
} }
/** /**