Skip to content

Commit 49210e0

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Enable pre onboarding and commerce plugin logging (#752)
* Adding new cron job to log enabled modules, removing that logging from catalog cron job, falling back to persist logs with client access token if access token is null * Logging in LogCatalogSetup even if access token is null (will use client access token to persist log) * Having EnabledModuleLogging cron job use system access token to persist log if possible * Update cron job schedule to run once per week on Tuesday * Fixing some issues * Using ModuleList explicitly since it'll only return enabled modules * Removing external_business_id from logging as it depends on storeId * Updating array declaration per static test failure --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent 0a2c477 commit 49210e0

File tree

4 files changed

+106
-23
lines changed

4 files changed

+106
-23
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\BusinessExtension\Cron;
22+
23+
use Magento\Framework\Module\ModuleList;
24+
use Magento\Framework\App\ProductMetadataInterface;
25+
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
26+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
27+
28+
class EnabledModuleLogging
29+
{
30+
/**
31+
* @var GraphAPIAdapter
32+
*/
33+
private $graphAPIAdapter;
34+
35+
/**
36+
* @var ModuleList
37+
*/
38+
private $moduleList;
39+
40+
/**
41+
* @var SystemConfig
42+
*/
43+
private $systemConfig;
44+
45+
/**
46+
* @var ProductMetadataInterface
47+
*/
48+
private $productMetadata;
49+
50+
/**
51+
* Construct
52+
*
53+
* @param GraphAPIAdapter $graphAPIAdapter
54+
* @param ModuleList $moduleList
55+
* @param SystemConfig $systemConfig
56+
* @param ProductMetadataInterface $productMetadata
57+
*/
58+
public function __construct(
59+
GraphAPIAdapter $graphAPIAdapter,
60+
ModuleList $moduleList,
61+
SystemConfig $systemConfig,
62+
ProductMetadataInterface $productMetadata
63+
) {
64+
$this->graphAPIAdapter = $graphAPIAdapter;
65+
$this->moduleList = $moduleList;
66+
$this->systemConfig = $systemConfig;
67+
$this->productMetadata = $productMetadata;
68+
}
69+
70+
/**
71+
* Execute cron
72+
*/
73+
public function execute()
74+
{
75+
$cmsIds = [];
76+
$accessToken = null;
77+
foreach ($this->systemConfig->getAllFBEInstalledStores() as $store) {
78+
$storeId = $store->getId();
79+
$cmsId = $this->systemConfig->getCommerceAccountId($storeId);
80+
$cmsIds[] = $cmsId;
81+
$storeAccessToken = $this->systemConfig->getAccessToken($storeId);
82+
if ($storeAccessToken) {
83+
$accessToken = $storeAccessToken;
84+
}
85+
}
86+
$this->graphAPIAdapter->persistLogToMeta(
87+
[
88+
'event' => 'commerce_plugin_and_extension_logging',
89+
'event_type' => 'enabled_modules',
90+
'seller_platform_app_version' => $this->productMetadata->getVersion(),
91+
'extra_data' => [
92+
'enabled_modules' => json_encode($this->moduleList->getNames()),
93+
'extension_version' => $this->systemConfig->getModuleVersion(),
94+
'cms_ids' => json_encode($cmsIds)
95+
]
96+
],
97+
$accessToken
98+
);
99+
}
100+
}

app/code/Meta/BusinessExtension/Helper/GraphAPIAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ public function getFBEInstalls($accessToken, $externalBusinessId)
10241024
public function persistLogToMeta($context, $accessToken = null)
10251025
{
10261026
$request = [
1027-
'access_token' => $accessToken ?? $this->accessToken,
1027+
'access_token' => $accessToken ?? $this->clientAccessToken,
10281028
'event' => $this->getContextData($context, 'event'),
10291029
'event_type' => $this->getContextData($context, 'event_type'),
10301030
'commerce_merchant_settings_id' => $this->getContextData($context, 'commerce_merchant_settings_id'),

app/code/Meta/BusinessExtension/etc/crontab.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
<job name="facebook_automation_update_mbe_settings" instance="Meta\BusinessExtension\Cron\UpdateMBESettings" method="execute">
55
<schedule>0 * * * *</schedule>
66
</job>
7+
<job name="enabled_module_logging" instance="Meta\BusinessExtension\Cron\EnabledModuleLogging" method="execute">
8+
<schedule>0 0 * * 2</schedule>
9+
</job>
710
</group>
811
</config>

app/code/Meta/Catalog/Cron/LogCatalogSetup.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
namespace Meta\Catalog\Cron;
2222

2323
use Magento\Framework\App\ResourceConnection;
24-
use Magento\Framework\Module\FullModuleList;
2524
use Meta\BusinessExtension\Helper\FBEHelper;
2625
use Meta\BusinessExtension\Helper\GraphAPIAdapter;
2726
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
@@ -37,44 +36,32 @@ class LogCatalogSetup
3736
* @var SystemConfig
3837
*/
3938
private $systemConfig;
39+
4040
/**
4141
* @var FBEHelper
4242
*/
4343
private $fbeHelper;
4444

45-
/**
46-
* @var FullModuleList
47-
*/
48-
private $fullModuleList;
49-
5045
/**
5146
* @var ResourceConnection
5247
*/
5348
private $resourceConnection;
5449

55-
/**
56-
* @var bool
57-
*/
58-
private static $logInstalledModules = false;
59-
6050
/**
6151
* @param GraphAPIAdapter $graphAPIAdapter
6252
* @param SystemConfig $systemConfig
6353
* @param FBEHelper $fbeHelper
64-
* @param FullModuleList $fullModuleList
6554
* @param ResourceConnection $resourceConnection
6655
*/
6756
public function __construct(
6857
GraphAPIAdapter $graphAPIAdapter,
6958
SystemConfig $systemConfig,
7059
FBEHelper $fbeHelper,
71-
FullModuleList $fullModuleList,
7260
ResourceConnection $resourceConnection
7361
) {
7462
$this->graphAPIAdapter = $graphAPIAdapter;
7563
$this->systemConfig = $systemConfig;
7664
$this->fbeHelper = $fbeHelper;
77-
$this->fullModuleList = $fullModuleList;
7865
$this->resourceConnection = $resourceConnection;
7966
}
8067

@@ -89,10 +76,6 @@ public function execute()
8976
$storeId = $store->getId();
9077
try {
9178
$accessToken = $this->systemConfig->getAccessToken($storeId);
92-
if (!$accessToken) {
93-
continue;
94-
}
95-
9679
$this->graphAPIAdapter->persistLogToMeta(
9780
[
9881
'event' => 'log_catalog_setup_data',
@@ -106,10 +89,7 @@ public function execute()
10689
'group_count' => $this->queryGroupCount(),
10790
'breakdown' => $this->queryBreakdown(),
10891
]
109-
),
110-
'extensions' => self::$logInstalledModules
111-
? json_encode($this->fullModuleList->getAll())
112-
: 'Logging installed modules not enabled'
92+
)
11393
]
11494
],
11595
$accessToken

0 commit comments

Comments
 (0)