Skip to content

Commit 5cc709f

Browse files
committed
Merge pull request #531 from magento-firedrakes/MAGETWO-36849
[Firedrakes] Visitors logging logic elimination
2 parents ae2a82d + d58fbff commit 5cc709f

File tree

94 files changed

+713
-4659
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+713
-4659
lines changed

app/code/Magento/Catalog/Model/Plugin/Log.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Plugin for \Magento\Log\Model\Resource\Log model
3+
* Plugin for \Magento\Customer\Model\Resource\Visitor model
44
*
55
* Copyright © 2015 Magento. All rights reserved.
66
* See COPYING.txt for license details.
@@ -26,13 +26,13 @@ public function __construct(\Magento\Catalog\Model\Product\Compare\Item $product
2626
* Catalog Product Compare Items Clean
2727
* after plugin for clean method
2828
*
29-
* @param \Magento\Log\Model\Resource\Log $subject
30-
* @param \Magento\Log\Model\Resource\Log $logResourceModel
29+
* @param \Magento\Customer\Model\Resource\Visitor $subject
30+
* @param \Magento\Customer\Model\Resource\Visitor $logResourceModel
3131
*
32-
* @return \Magento\Log\Model\Resource\Log
32+
* @return \Magento\Customer\Model\Resource\Visitor
3333
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3434
*/
35-
public function afterClean(\Magento\Log\Model\Resource\Log $subject, $logResourceModel)
35+
public function afterClean(\Magento\Customer\Model\Resource\Visitor $subject, $logResourceModel)
3636
{
3737
$this->_productCompareItem->clean();
3838
return $logResourceModel;

app/code/Magento/Catalog/Model/Resource/Product/Compare/Item.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function clean()
9292
['compare_table' => $this->getMainTable()],
9393
['catalog_compare_item_id']
9494
)->joinLeft(
95-
['visitor_table' => $this->getTable('log_visitor')],
95+
['visitor_table' => $this->getTable('customer_visitor')],
9696
'visitor_table.visitor_id=compare_table.visitor_id AND compare_table.customer_id IS NULL',
9797
[]
9898
)->where(

app/code/Magento/Catalog/Test/Unit/Model/Plugin/LogTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ class LogTest extends \PHPUnit_Framework_TestCase
1818
protected $compareItemMock;
1919

2020
/**
21-
* @var \PHPUnit_Framework_MockObject_MockObject
21+
* @var \Magento\Customer\Model\Resource\Visitor|\PHPUnit_Framework_MockObject_MockObject
2222
*/
2323
protected $logResourceMock;
2424

2525
/**
26-
* @var \PHPUnit_Framework_MockObject_MockObject
26+
* @var \Magento\Customer\Model\Resource\Visitor|\PHPUnit_Framework_MockObject_MockObject
2727
*/
2828
protected $subjectMock;
2929

3030
protected function setUp()
3131
{
32-
$this->logResourceMock = $this->getMock('Magento\Log\Model\Resource\Log', [], [], '', false);
32+
$this->logResourceMock = $this->getMock('Magento\Customer\Model\Resource\Visitor', [], [], '', false);
3333
$this->compareItemMock = $this->getMock(
3434
'Magento\Catalog\Model\Product\Compare\Item',
3535
[],
3636
[],
3737
'',
3838
false
3939
);
40-
$this->subjectMock = $this->getMock('Magento\Log\Model\Resource\Log', [], [], '', false);
40+
$this->subjectMock = $this->getMock('Magento\Customer\Model\Resource\Visitor', [], [], '', false);
4141
$this->model = new \Magento\Catalog\Model\Plugin\Log($this->compareItemMock);
4242
}
4343

app/code/Magento/Catalog/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
"magento/module-customer": "1.0.0-beta",
1111
"magento/module-theme": "1.0.0-beta",
1212
"magento/module-checkout": "1.0.0-beta",
13-
"magento/module-log": "1.0.0-beta",
1413
"magento/module-backend": "1.0.0-beta",
1514
"magento/module-widget": "1.0.0-beta",
1615
"magento/module-wishlist": "1.0.0-beta",

app/code/Magento/Catalog/etc/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<preference for="Magento\Catalog\Api\ProductAttributeManagementInterface" type="Magento\Catalog\Model\Product\Attribute\Management" />
4343
<preference for="Magento\Catalog\Api\AttributeSetManagementInterface" type="Magento\Catalog\Model\Product\Attribute\SetManagement" />
4444
<preference for="Magento\Catalog\Api\AttributeSetRepositoryInterface" type="Magento\Catalog\Model\Product\Attribute\SetRepository" />
45-
<type name="Magento\Log\Model\Resource\Log">
45+
<type name="Magento\Customer\Model\Resource\Visitor">
4646
<plugin name="catalogLog" type="Magento\Catalog\Model\Plugin\Log" />
4747
</type>
4848
<type name="Magento\Indexer\Model\Indexer\State">
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
*
4+
* Copyright © 2015 Magento. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
namespace Magento\Customer\Controller\Adminhtml\Online;
8+
9+
use Magento\Backend\App\Action\Context;
10+
use Magento\Framework\View\Result\PageFactory;
11+
12+
class Index extends \Magento\Backend\App\Action
13+
{
14+
/**
15+
* @var PageFactory
16+
*/
17+
protected $resultPageFactory;
18+
19+
/**
20+
* @param Context $context
21+
* @param PageFactory $resultPageFactory
22+
*/
23+
public function __construct(
24+
Context $context,
25+
PageFactory $resultPageFactory
26+
) {
27+
parent::__construct($context);
28+
$this->resultPageFactory = $resultPageFactory;
29+
}
30+
31+
/**
32+
* Check the permission to run it
33+
*
34+
* @return bool
35+
*/
36+
protected function _isAllowed()
37+
{
38+
return $this->_authorization->isAllowed('Magento_Customer::online');
39+
}
40+
41+
/**
42+
* Index action
43+
*
44+
* @return \Magento\Backend\Model\View\Result\Page
45+
*/
46+
public function execute()
47+
{
48+
/** @var \Magento\Backend\Model\View\Result\Page $resultPage */
49+
$resultPage = $this->resultPageFactory->create();
50+
$resultPage->setActiveMenu('Magento_Customer::customer_online');
51+
$resultPage->addBreadcrumb(__('Customers'), __('Customers'));
52+
$resultPage->addBreadcrumb(__('Online Customers'), __('Online Customers'));
53+
$resultPage->getConfig()->getTitle()->prepend(__('Customers Now Online'));
54+
55+
return $resultPage;
56+
}
57+
}

app/code/Magento/Customer/Model/Resource/Customer/CustomersTypeOptions.php

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Model\Resource\Online\Grid;
7+
8+
use Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult;
9+
use Magento\Customer\Model\Visitor;
10+
use Magento\Framework\Api;
11+
use Magento\Framework\Event\ManagerInterface as EventManager;
12+
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface as FetchStrategy;
13+
use Magento\Framework\Data\Collection\EntityFactoryInterface as EntityFactory;
14+
use Psr\Log\LoggerInterface as Logger;
15+
16+
/**
17+
* Flat customer online grid collection
18+
*
19+
* @author Magento Core Team <[email protected]>
20+
*/
21+
class Collection extends SearchResult
22+
{
23+
/**
24+
* Value of seconds in one minute
25+
*/
26+
const SECONDS_IN_MINUTE = 60;
27+
28+
/**
29+
* @var \Magento\Framework\Stdlib\DateTime\DateTime
30+
*/
31+
protected $date;
32+
33+
/**
34+
* @var Visitor
35+
*/
36+
protected $visitorModel;
37+
38+
/**
39+
* @param EntityFactory $entityFactory
40+
* @param Logger $logger
41+
* @param FetchStrategy $fetchStrategy
42+
* @param EventManager $eventManager
43+
* @param string $mainTable
44+
* @param string $resourceModel
45+
* @param Visitor $visitorModel
46+
* @param \Magento\Framework\Stdlib\DateTime\DateTime $date
47+
*/
48+
public function __construct(
49+
EntityFactory $entityFactory,
50+
Logger $logger,
51+
FetchStrategy $fetchStrategy,
52+
EventManager $eventManager,
53+
$mainTable,
54+
$resourceModel,
55+
Visitor $visitorModel,
56+
\Magento\Framework\Stdlib\DateTime\DateTime $date
57+
) {
58+
$this->date = $date;
59+
$this->visitorModel = $visitorModel;
60+
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $mainTable, $resourceModel);
61+
}
62+
63+
/**
64+
* Init collection select
65+
*
66+
* @return $this
67+
*/
68+
protected function _initSelect()
69+
{
70+
parent::_initSelect();
71+
$connection = $this->getConnection();
72+
$lastDate = $this->date->gmtTimestamp() - $this->visitorModel->getOnlineInterval() * self::SECONDS_IN_MINUTE;
73+
$this->getSelect()->joinLeft(
74+
['customer' => $this->getTable('customer_entity')],
75+
'customer.entity_id = main_table.customer_id',
76+
['email', 'firstname', 'lastname']
77+
)->where(
78+
'main_table.last_visit_at >= ?',
79+
$connection->formatDate($lastDate)
80+
);
81+
$expression = $connection->getCheckSql(
82+
'main_table.customer_id IS NOT NULL AND main_table.customer_id != 0',
83+
$connection->quote(Visitor::VISITOR_TYPE_CUSTOMER),
84+
$connection->quote(Visitor::VISITOR_TYPE_VISITOR)
85+
);
86+
$this->getSelect()->columns(['visitor_type' => $expression]);
87+
return $this;
88+
}
89+
90+
/**
91+
* Add field filter to collection
92+
*
93+
* @param string|array $field
94+
* @param string|int|array|null $condition
95+
* @return \Magento\Cms\Model\Resource\Block\Collection
96+
*/
97+
public function addFieldToFilter($field, $condition = null)
98+
{
99+
if ($field == 'visitor_type') {
100+
$field = 'customer_id';
101+
if (is_array($condition) && isset($condition['eq'])) {
102+
$condition = $condition['eq'] == Visitor::VISITOR_TYPE_CUSTOMER ? ['gt' => 0] : ['null' => true];
103+
}
104+
}
105+
return parent::addFieldToFilter($field, $condition);
106+
}
107+
}

app/code/Magento/Customer/Model/Visitor.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class Visitor extends \Magento\Framework\Model\AbstractModel
1616

1717
const VISITOR_TYPE_VISITOR = 'v';
1818

19+
const DEFAULT_ONLINE_MINUTES_INTERVAL = 15;
20+
21+
const XML_PATH_ONLINE_INTERVAL = 'customer/online_customers/online_minutes_interval';
22+
1923
/**
2024
* @var string[]
2125
*/
@@ -281,4 +285,20 @@ public function clean()
281285
$this->getResource()->clean($this);
282286
return $this;
283287
}
288+
289+
/**
290+
* Retrieve Online Interval (in minutes)
291+
*
292+
* @return int Minutes Interval
293+
*/
294+
public function getOnlineInterval()
295+
{
296+
$configValue = intval(
297+
$this->scopeConfig->getValue(
298+
static::XML_PATH_ONLINE_INTERVAL,
299+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
300+
)
301+
);
302+
return $configValue ?: static::DEFAULT_ONLINE_MINUTES_INTERVAL;
303+
}
284304
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Customer\Setup;
8+
9+
use Magento\Framework\Setup\UpgradeSchemaInterface;
10+
use Magento\Framework\Setup\ModuleContextInterface;
11+
use Magento\Framework\Setup\SchemaSetupInterface;
12+
13+
/**
14+
* @codeCoverageIgnore
15+
*/
16+
class UpgradeSchema implements UpgradeSchemaInterface
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
22+
{
23+
$setup->startSetup();
24+
25+
if (version_compare($context->getVersion(), '2.0.0.1') < 0) {
26+
$connection = $setup->getConnection();
27+
28+
$connection->addIndex(
29+
$setup->getTable('customer_visitor'),
30+
$setup->getIdxName('customer_visitor', ['last_visit_at']),
31+
['last_visit_at']
32+
);
33+
}
34+
35+
$setup->endSetup();
36+
}
37+
}

app/code/Magento/Customer/Test/Unit/Model/Layout/DepersonalizePluginTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class DepersonalizePluginTest extends \PHPUnit_Framework_TestCase
4343
protected $customerMock;
4444

4545
/**
46-
* @var \Magento\Log\Model\Visitor|\PHPUnit_Framework_MockObject_MockObject
46+
* @var \Magento\Customer\Model\Visitor|\PHPUnit_Framework_MockObject_MockObject
4747
*/
4848
protected $visitorMock;
4949

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Ui\Component\Listing\Column\Online;
7+
8+
use Magento\Ui\Component\Listing\Columns\Column;
9+
use Magento\Customer\Model\Visitor;
10+
11+
/**
12+
* Class Type
13+
*/
14+
class Type extends Column
15+
{
16+
/**
17+
* Prepare Data Source
18+
*
19+
* @param array $dataSource
20+
* @return void
21+
*/
22+
public function prepareDataSource(array & $dataSource)
23+
{
24+
if (isset($dataSource['data']['items'])) {
25+
foreach ($dataSource['data']['items'] as & $item) {
26+
$item[$this->getData('name')] = $item[$this->getData('name')] == Visitor::VISITOR_TYPE_VISITOR
27+
? __('Visitor')
28+
: __('Customer');
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)