From 43df16c06268de1fc82e66911e2a057517881547 Mon Sep 17 00:00:00 2001 From: vagrant Date: Thu, 6 Jun 2019 11:34:51 +0000 Subject: [PATCH] konec --- SOLUTION.md | 9 ++- src/Command/ReportYearlyCommand.php | 22 ++++++-- src/Profile/Profile.php | 54 ++++++++++++++++++ src/Profile/Report.php | 88 +++++++++++++++++++++++++++++ src/Tests/YearlyReport.feature | 14 +++++ 5 files changed, 180 insertions(+), 7 deletions(-) create mode 100644 src/Profile/Profile.php create mode 100644 src/Profile/Report.php diff --git a/SOLUTION.md b/SOLUTION.md index defe675..4525eb8 100755 --- a/SOLUTION.md +++ b/SOLUTION.md @@ -3,11 +3,14 @@ SOLUTION Estimation ---------- -Estimated: n hours +Estimated: 4 hours -Spent: x hours +Spent: 4.5 hours Solution -------- -Comments on your solution +Najbrž bi bilo smiselno predelati leto v wildcard, ki bi dopuščal izpis vseh profilov po letih. - 2 uri +Možnost opcijskega filtra(sortiranje po določenem polju(leto, največ ogledov) asc/dec) - 4 ure + + diff --git a/src/Command/ReportYearlyCommand.php b/src/Command/ReportYearlyCommand.php index 97f026f..81d6053 100755 --- a/src/Command/ReportYearlyCommand.php +++ b/src/Command/ReportYearlyCommand.php @@ -6,6 +6,9 @@ use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use BOF\Profile\Report; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputDefinition; class ReportYearlyCommand extends ContainerAwareCommand { @@ -14,6 +17,11 @@ protected function configure() $this ->setName('report:profiles:yearly') ->setDescription('Page views report') + ->setDefinition( + new InputDefinition([ + new InputOption('year', 'y', InputOption::VALUE_OPTIONAL), + ]) + ); ; } @@ -23,10 +31,16 @@ protected function execute(InputInterface $input, OutputInterface $output) $io = new SymfonyStyle($input,$output); $db = $this->getContainer()->get('database_connection'); - $profiles = $db->query('SELECT profile_name FROM profiles')->fetchAll(); - - // Show data in a table - headers, data - $io->table(['Profile'], $profiles); + $report = new Report($db); + if (!$report->setYear($input->getOption("year"))) { + $io->writeln("Year must be a valid integer."); + return false; + } + + if (!$report->setProfileViews()) + $io->table("Profile", []); + + $io->table($report->getHeaders(), $report->getProfiles()); } } diff --git a/src/Profile/Profile.php b/src/Profile/Profile.php new file mode 100644 index 0000000..0ec702d --- /dev/null +++ b/src/Profile/Profile.php @@ -0,0 +1,54 @@ +id = $id; + } + + public function getId() + { + return $this->id; + } + + public function getName() + { + return $this->name; + } + + public function setName($name) + { + $this->name = $name; + } + + public function addViewsToMonth($month, $views) + { + if (isset($this->months[$month])) { + $this->months[$month] += $views; + } else { + $this->months[$month] = $views; + } + } + + public function sortMonths() + { + for ($i = 1; $i <= 12; $i++) { + if (!isset($this->months[$i])) + $this->months[$i] = "N/A"; + } + + ksort($this->months); + } + + public function getMonths() + { + return $this->months; + } +} \ No newline at end of file diff --git a/src/Profile/Report.php b/src/Profile/Report.php new file mode 100644 index 0000000..2a323e9 --- /dev/null +++ b/src/Profile/Report.php @@ -0,0 +1,88 @@ +db = $db; + } + + public function setProfileViews() + { + if (!isset($this->year)) + $this->setYear(date('Y')); + + return $this->getProfileViewsByYear(); + } + + public function getProfileViewsByYear() + { + $profiles = $this->db->query("SELECT p.profile_name, p.profile_id, date_format(v.date, '%m') dateMonth, v.views from profiles p left join views v on p.profile_id = v.profile_id AND YEAR(v.date) = $this->year")->fetchAll(); + + if (!$profiles) + return false; + + $this->sortProfileViews($profiles); + + return true; + } + + public function getProfiles() + { + $return = []; + + foreach ($this->profiles as $profile) { + $profile->sortMonths(); + $values = array_values($profile->getMonths()); + array_unshift($values, $profile->getName()); + $return[$profile->getName()] = $values; + } + + ksort($return); + return array_values($return); + } + + public function getHeaders() + { + return ['Profile ' . $this->year, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; + } + + private function sortProfileViews($profiles) + { + $sortedProfiles = []; + + foreach ($profiles as $view) { + if (isset($sortedProfiles[$view["profile_id"]])) { + $user = $sortedProfiles[$view["profile_id"]]; + } else { + $user = new Profile($view["profile_id"]); + $user->setName($view["profile_name"]); + $sortedProfiles[$user->getId()] = $user; + } + + if (isset($view["dateMonth"])) + $user->addViewsToMonth((int) $view["dateMonth"], (int) $view["views"]); + } + + $this->profiles = $sortedProfiles; + } + + public function setYear($year) + { + if ((int) $year == 0) + return false; + + $this->year = (int) $year; + + return true; + } +} \ No newline at end of file diff --git a/src/Tests/YearlyReport.feature b/src/Tests/YearlyReport.feature index e69de29..8d4a6c7 100755 --- a/src/Tests/YearlyReport.feature +++ b/src/Tests/YearlyReport.feature @@ -0,0 +1,14 @@ +GIVEN that there is 0 profile data available +WHEN I execute the Yearly Views report +THEN I expect to see a blank table with Profile header + +GIVEN that there isn't input option "year" set +WHEN I execute the Yearly Views report +THEN I expect to see a monthly breakdown of the total views per profiles for active year + +GIVEN that there is option "year" set +AND year is invalid integer +WHEN I execute the Yearly Views report +THEN I expect to see error text: "Year must be a valid integer" + +