Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions SOLUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
28 changes: 24 additions & 4 deletions src/Command/ReportYearlyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
}
}
28 changes: 19 additions & 9 deletions src/Command/TestDataResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,41 @@ 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'];
$currentDate = $startDate;

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);

}

}