From c898c0cf00a071fb2b7f7eb2fa59883727db8fc2 Mon Sep 17 00:00:00 2001 From: janvodopivec Date: Fri, 1 Feb 2019 11:34:13 +0100 Subject: [PATCH] solution --- SOLUTION.md | 55 +++++++++++++++++++++++++++-- src/Command/ReportYearlyCommand.php | 45 ++++++++++++++++++++++- 2 files changed, 97 insertions(+), 3 deletions(-) diff --git a/SOLUTION.md b/SOLUTION.md index defe675..27852f0 100755 --- a/SOLUTION.md +++ b/SOLUTION.md @@ -3,11 +3,62 @@ SOLUTION Estimation ---------- -Estimated: n hours +Estimated: 3 hours -Spent: x hours +Spent: 1 hour 20 min +Coment: Testing was minimal so im not sure all the edge cases are covered, i removed some data from DB to test the "n/a" requirement. Solution -------- Comments on your solution + +Solution solves all 3 requirments given. Im sure there is a better way of doing this but it woud requirte more time to think. The main problem is the 3rd point witch requires me to "make up" data in the DB this can also be done in code with some injections to the data given by DB. + +I tested the solution to folowing cases: +-clean database (as provided by default) +-inserted new user with no history +-inserted new history with no user +-inserted new user with duplicated name + +Test cases +-------- + + + Scenario: All data is avavible and properly connected (history is available) + When Business Analyst request a report + Then the script displays sum of all views on the screen in the format provided by the task + + Scenario: All data is avavible and properly connected (history is available) + When Business Analyst request a report + Then the script displays sum of all views on the screen gruped by month independant of the year + + Scenario: All data is avavible and properly connected (history is not available) + When Business Analyst request a report + Then the script displays sum of all views on the screen gruped by month independant of the year + And for the user with no history all the months will be summed as "n/a" + + Scenario: All data is avavible but there are some views in the table with no conection to the profiles + When Business Analyst request a report + Then the script displays sum of all views on the screen gruped by month independant of the year + And the the data with no connection to the profiles will not be used in the calculations + + Scenario: All data is avavible but there are some missing month for the user profile + When Business Analyst request a report + Then the script displays sum of all views on the screen gruped by month independant of the year + And the user with no history for specific month will have that month summed "n/a" + + Scenario: All data is avavible and properly connected (history is available) + When Business Analyst request a report + Then the script displays sum of all views on the screen gruped by month independant of the year, grouping must be done on profile_id but displayed as profile_name as names can duplicate + +Edge cases +-------- +Current solution treats 0 views as "n/a", possible modifications. +Duplicated usernames + +Other Notes +-------- +Localy I removed all the data in january using : DELETE FROM views WHERE MONTH(date) = 1, to test the "n/a" requirement +I added a new user caled "test" and "Karl Lagerfeld" +I added a new history item with non existing profile_id \ No newline at end of file diff --git a/src/Command/ReportYearlyCommand.php b/src/Command/ReportYearlyCommand.php index 97f026f..3161c4f 100755 --- a/src/Command/ReportYearlyCommand.php +++ b/src/Command/ReportYearlyCommand.php @@ -22,11 +22,54 @@ protected function execute(InputInterface $input, OutputInterface $output) /** @var $db Connection */ $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); + */ + + $profiles = $db->query(' + SELECT + `data`.`profile_name`, + IF ( SUM(Jan) != 0, SUM(Jan), "n/a") AS Jan, + IF ( SUM(Feb) != 0, SUM(Feb), "n/a") AS Feb, + IF ( SUM(Mar) != 0, SUM(Mar), "n/a") AS Mar, + IF ( SUM(Apr) != 0, SUM(Apr), "n/a") AS Apr, + IF ( SUM(Maj) != 0, SUM(Maj), "n/a") AS Maj, + IF ( SUM(Jun) != 0, SUM(Jun), "n/a") AS Jun, + IF ( SUM(Jul) != 0, SUM(Jul), "n/a") AS Jul, + IF ( SUM(Aug) != 0, SUM(Aug), "n/a") AS Aug, + IF ( SUM(Spe) != 0, SUM(Spe), "n/a") AS Spe, + IF ( SUM(Oct) != 0, SUM(Oct), "n/a") AS Oct, + IF ( SUM(Nov) != 0, SUM(Nov), "n/a") AS Nov, + IF ( SUM(`Dec`) != 0, SUM(`Dec`), "n/a") AS `Dec` + FROM( + SELECT + `profiles`.`profile_name`, + `profiles`.`profile_id`, + IF ( MONTH(date) = 1, SUM(views), 0) AS Jan, + IF ( MONTH(date) = 2, SUM(views), 0) AS Feb, + IF ( MONTH(date) = 3, SUM(views), 0) AS Mar, + IF ( MONTH(date) = 4, SUM(views), 0) AS Apr, + IF ( MONTH(date) = 5, SUM(views), 0) AS Maj, + IF ( MONTH(date) = 6, SUM(views), 0) AS Jun, + IF ( MONTH(date) = 7, SUM(views), 0) AS Jul, + IF ( MONTH(date) = 8, SUM(views), 0) AS Aug, + IF ( MONTH(date) = 8, SUM(views), 0) AS Spe, + IF ( MONTH(date) = 8, SUM(views), 0) AS oct, + IF ( MONTH(date) = 8, SUM(views), 0) AS Nov, + IF ( MONTH(date) = 8, SUM(views), 0) AS `Dec` + FROM `profiles` + LEFT JOIN `views` ON `profiles`.`profile_id` = `views`.`profile_id` + GROUP BY `profiles`.`profile_id`, MONTH(`views`.`date`) + ORDER BY `profiles`.`profile_name` + ) AS data + GROUP BY `data`.`profile_name`, `data`.`profile_id` + ')->fetchAll(); + + $io->table(['Profile',"Jan","Feb","Mar","Apr","Maj","Jun","Jul","Aug","Spe","Oct","Nov","Dec"], $profiles); } }