From 5532015d1d2403a0939f211394e0e2268512d16d Mon Sep 17 00:00:00 2001 From: Dule84 Date: Mon, 4 Feb 2019 00:51:58 +0100 Subject: [PATCH 1/2] first commit --- src/Command/ReportYearlyCommand.php | 28 ++++++++++++++++++++++++---- src/Command/TestDataResetCommand.php | 28 +++++++++++++++++++--------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/Command/ReportYearlyCommand.php b/src/Command/ReportYearlyCommand.php index 97f026f..2e3756c 100755 --- a/src/Command/ReportYearlyCommand.php +++ b/src/Command/ReportYearlyCommand.php @@ -23,10 +23,30 @@ 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); + $profiles = $db->query("SELECT profiles.profile_name, DATE_FORMAT(date, '%Y') as Year, + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '1' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '2' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '3' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '4' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '5' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '6' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '7' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '8' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '9' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '10' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '11' THEN (views.views) END), 'N'), 'n/a'), + IFNULL(FORMAT(SUM(CASE WHEN MONTH(views.date) = '12' THEN (views.views) END), 'N'), 'n/a') + FROM profiles + LEFT JOIN views ON profiles.profile_id = views.profile_id + GROUP BY views.profile_id, year(views.date) ORDER BY profiles.profile_name ASC")->fetchAll(); + try { + // Setting up the headers and print data. + $io->table(['Profile ' , 'Year', + 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Avg', 'Sep', 'Oct', 'Nov', 'Dec'], $profiles); + } catch (\Exception $ex) { + $output->write("Failed to output: " . $ex->getMessage()); + }; } } diff --git a/src/Command/TestDataResetCommand.php b/src/Command/TestDataResetCommand.php index bd0d52a..7a53516 100755 --- a/src/Command/TestDataResetCommand.php +++ b/src/Command/TestDataResetCommand.php @@ -31,6 +31,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $profiles = $db->query('SELECT * FROM profiles')->fetchAll(); $progress = $io->createProgressBar(count($profiles)); + foreach ($profiles as $profile) { $profileId = $profile['profile_id']; @@ -38,24 +39,33 @@ protected function execute(InputInterface $input, OutputInterface $output) while ($currentDate <= $endDate) { - for ($i = 0; $i <= $dataPerDay; $i++) { + for ($i = 0; $i <= $dataPerDay; $i++) { $views = rand(100, 9999); $date = date('Y-m-d', $currentDate); - $sql = sprintf( - "INSERT INTO views (`profile_id`, `date`, `views`) VALUES (%s, '%s', %s)", - $profileId, - $date, - $views - ); - $db->query($sql); - } + $insert[] = [ + 'profile_id' => $profileId, + 'date' => $date, + 'views' => $views + ]; + } $currentDate = mktime(0,0,0, date('m', $currentDate), date('d', $currentDate) + 1, date('Y', $currentDate)); } $progress->advance(); } + foreach($insert as $i){ + $inserted[] = '('.$i['profile_id'].', '.$i['date'].', '.$i['views'].')'; + } + + $sql = " INSERT INTO views (`profile_id`, `date`, `views`) VALUES " . implode(',', $inserted); + + print_r($sql); + + //$db->query($sql); + } + } From e5e4d7dc662171caee8c05ab40a25a4c2c4bd866 Mon Sep 17 00:00:00 2001 From: Dule84 Date: Mon, 4 Feb 2019 14:15:04 +0100 Subject: [PATCH 2/2] Solution --- SOLUTION.md | 15 +++++++++++++-- src/Command/TestDataResetCommand.php | 6 +++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/SOLUTION.md b/SOLUTION.md index defe675..0e1731e 100755 --- a/SOLUTION.md +++ b/SOLUTION.md @@ -3,11 +3,22 @@ SOLUTION Estimation ---------- -Estimated: n hours +Estimated: 3 hours -Spent: x hours +Spent: 4 hours 30 minutes Solution -------- Comments on your solution + +First of all, I just want to say that for main task I needed around 2 hours with cloning and project setup. + +My solution is to print in terminal all views by month and year and if there are no values(NULL columns) print "n/a". I tested also with deleting first 120 rows with "DELETE FROM views LIMIT 120" so all results regarding, in this case Carl Lagerfeld are deleted for first month. The results are ordered by +name alphabetically. + +My biggest problem was with inserting data in views table. First time when I insert data in table, it took almost 7 minutes and 30 seconds to import +17900 results. I know that this wasn't main idea of the task but I wanted to create faster insertation. At first sight, everything was working fine and I managed to insert this amount of data in 2 seconds. But dates in table was inserted in '0000-00-00' format. When I print query everything looked just fine +but in database all dates were same. So I didn't realize that date in query was observed like integer and finally after some concatenation and adding double and single quotes, everything worked. + +I would also add foreign key between these two tables. \ No newline at end of file diff --git a/src/Command/TestDataResetCommand.php b/src/Command/TestDataResetCommand.php index 7a53516..9649963 100755 --- a/src/Command/TestDataResetCommand.php +++ b/src/Command/TestDataResetCommand.php @@ -57,14 +57,14 @@ protected function execute(InputInterface $input, OutputInterface $output) } foreach($insert as $i){ - $inserted[] = '('.$i['profile_id'].', '.$i['date'].', '.$i['views'].')'; + $inserted[] = '('.$i['profile_id'].', '."'".$i['date']."'".', '.$i['views'].')'; } $sql = " INSERT INTO views (`profile_id`, `date`, `views`) VALUES " . implode(',', $inserted); - print_r($sql); + //print_r($sql); - //$db->query($sql); + $db->query($sql); }