Skip to content
This repository has been archived by the owner on Apr 30, 2023. It is now read-only.

Commit

Permalink
added new info page for debugging
Browse files Browse the repository at this point in the history
Summary: Created a new info page to assist with the debugging process.

Reviewed By: zlik

Differential Revision: D30734422

fbshipit-source-id: 60097568
  • Loading branch information
Anand Krishnan authored and facebook-github-bot committed Sep 23, 2021
1 parent e1d4306 commit 0f62824
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 9 deletions.
115 changes: 115 additions & 0 deletions Block/Adminhtml/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/

namespace Facebook\BusinessExtension\Block\Adminhtml;

use Facebook\BusinessExtension\Helper\FBEHelper;
use Magento\Framework\Module\ModuleListInterface;
use Magento\Framework\App\ProductMetadataInterface;
use Magento\Framework\Escaper;
use Facebook\BusinessExtension\Helper\LogOrganization;


class Info extends \Magento\Backend\Block\Template
{
/**
* @var FBEHelper
*/
protected $fbeHelper;

/**
* @var ModuleListInterface
*/
protected $moduleList;

/**
* @var ProductMetadataInterface
*/
protected $productMetadataInterface;

/**
* @var Escaper
*/
private $escaper;



/**
* @param \Magento\Backend\Block\Template\Context $context
* @param FBEHelper $fbeHelper
* @param ModuleListInterface $moduleList
* @param ProductMetadataInterface $productMetadataInterface
* @param Escaper $escaper
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
FBEHelper $fbeHelper,
ModuleListInterface $moduleList,
ProductMetadataInterface $productMetadataInterface,
Escaper $escaper,
array $data = []
) {
$this->fbeHelper = $fbeHelper;
$this->moduleList = $moduleList;
$this->productMetadataInterface = $productMetadataInterface;
$this->escaper = $escaper;
parent::__construct($context, $data);
}

/**
* @return string
*/
public function getFBEVersion() {
return $this->moduleList->getOne("Facebook_BusinessExtension")["setup_version"];
}

/**
* @return string
*/
public function getMagentoVersion() {
return $this->productMetadataInterface->getVersion();
}

/**
* @return string|null
*/
public function fetchPixelId()
{
return $this->fbeHelper->getConfigValue('fbpixel/id');
}

/**
* @return string|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function getExternalBusinessId()
{
return $this->fbeHelper->getFBEExternalBusinessId();
}

public function allLogs() {
$sortedString = implode("\n", LogOrganization::organizeLogs());
return nl2br($sortedString);
}

public function publicIssueLink() {
$magento_version = $this->getMagentoVersion();
$plugin_version = $this->getFBEVersion();
return "https://github.com/facebookincubator/facebook-for-magento2/issues/new?&template=bug-report.yml&magento_version="
. $magento_version . "&plugin_version=" . $plugin_version;
}

public function privateIssueLink() {
$magento_version = $this->getMagentoVersion();
$plugin_version = $this->getFBEVersion();
$extern_bus_id = $this->getExternalBusinessId();
$pixel_id = $this->fetchPixelId();

return "https://www.facebook.com/help/contact/224834796277182?Field1264912827263491="
. $magento_version . "&Field1465992913778514=" . $plugin_version
. "&Field1500601380300162=" . $extern_bus_id . "&Field2972445263018062=" . $pixel_id;
}
}
48 changes: 48 additions & 0 deletions Controller/Adminhtml/Info/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Facebook\BusinessExtension\Controller\Adminhtml\Info;

use Magento\Backend\App\Action;
use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;

/**
* Class Index
*/

class Index extends \Magento\Backend\App\Action
{
/**
* @var PageFactory
*/
protected $resultPageFactory;

/**
* Index constructor.
*
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
parent::__construct($context);

$this->resultPageFactory = $resultPageFactory;
}

/**
* Load the page defined in view/adminhtml/layout/exampleadminnewpage_helloworld_index.xml
*
* @return Page
*/
public function execute()
{
$resultPage = $this->resultPageFactory->create();
$resultPage->getConfig()->getTitle()->prepend(__('Info'));

return $resultPage;
}
}
7 changes: 2 additions & 5 deletions Cron/AAMSettingsCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ public function __construct(
public function execute()
{
$pixelId = $this->fbeHelper->getPixelID();
$this->fbeHelper->log('In CronJob for fetching AAM Settings for Pixel: ' . $pixelId);
$settingsAsString = null;
if ($pixelId) {
$settingsAsString = $this->fbeHelper->fetchAndSaveAAMSettings($pixelId);
if ($settingsAsString) {
$this->fbeHelper->log('Saving settings '.$settingsAsString);
} else {
$this->fbeHelper->log('Error saving settings');
if (!$settingsAsString) {
$this->fbeHelper->log('Error saving settings. Currently:', $settingsAsString);
}
}
return $settingsAsString;
Expand Down
1 change: 0 additions & 1 deletion Cron/CategorySyncCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function __construct(
public function execute()
{
if ($this->systemConfig->isActiveCollectionsSync() == true) {
$this->fbeHelper->log('start category sync cron job ');
$this->categoryCollection->pushAllCategoriesToFbCollections();
return true;
}
Expand Down
1 change: 0 additions & 1 deletion Helper/FBEHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ public function getFBEExternalBusinessId()
return $stored_external_id;
}
$storeId = $this->getStore()->getId();
$this->log("Store id---" . $storeId);
return uniqid('fbe_magento_' . $storeId . '_');
}

Expand Down
119 changes: 119 additions & 0 deletions Helper/LogOrganization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?php
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/

/**
* Helper class for generating and organizing log files.
*/
namespace Facebook\BusinessExtension\Helper;

class LogOrganization
{
// 4096 -- Good balance for tracking backwards
const BUFFER = 4096;

static $criticalLines = array();

public static function organizeLogs() {
$arrayOfFiles = array("var/log/debug.log", "var/log/cron.log", "var/log/magento.cron.log", "var/log/system.log");
$countCrit = 0;

foreach($arrayOfFiles as $value) {
if (!file_exists($value)) {
continue;
}

$fp = fopen($value, 'r');
$pos = -2; // Skip final new line character (Set to -1 if not present)
$currentLine = '';

while (-1 !== fseek($fp, $pos, SEEK_END)) {
$char = fgetc($fp);
if ("\n" == $char) {
if (substr($currentLine, 0, 3) == "[20") {
$time = strtotime(substr($currentLine, 1, 20));
if ($time > strtotime('-7 day')) {
break;
}

// Saving only critical log entries with "Facebook" phrase
if (strpos($currentLine, "CRITICAL") !== false &&
strpos($currentLine, "Facebook") !== false) {
self::$criticalLines[] = $currentLine;
$countCrit++;
}
$currentLine = '';

if ($countCrit == 500) {
break;
}
}
} else {
$currentLine = $char . $currentLine;
}
$pos--;
}

if (substr($currentLine, 0, 3) == "[20") {
$time = strtotime(substr($currentLine, 1, 20));
if ($time > strtotime('-7 day')) {
break;
}

if (strpos($currentLine, "CRITICAL") !== false &&
strpos($currentLine, "Facebook") !== false) {
self::$criticalLines[] = $currentLine;
$countCrit++;
}
$currentLine = '';
}

$countCrit = 0;
}
fclose($fp);

$amuLogs = self::tailCustom("var/log/facebook-business-extension.log", 100);
$amuLogsArr = explode("\n", $amuLogs);
self::$criticalLines = array_merge(self::$criticalLines, $amuLogsArr);

usort(self::$criticalLines, function ($x, $y) {
$t1 = strtotime(substr($x, 1, 19));
$t2 = strtotime(substr($y, 1, 19));

return ($t1 - $t2);
});

return self::$criticalLines;
}

public static function tailCustom($filepath, $lines) {
$f = fopen($filepath, "rb");
if ($f === false) {
return false;
}

fseek($f, -1, SEEK_END);
if (fread($f, 1) != "\n") {
$lines -= 1;
}

$output = '';
$chunk = '';

while (ftell($f) > 0 && $lines >= 0) {
$seek = min(ftell($f), self::BUFFER);
fseek($f, -$seek, SEEK_CUR);
$output = ($chunk = fread($f, $seek)) . $output;
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
$lines -= substr_count($chunk, "\n");
}

while ($lines++ < 0) {
$output = substr($output, strpos($output, "\n") + 1);
}

fclose($f);
return trim($output);
}
}
2 changes: 1 addition & 1 deletion Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function upgrade(
// verify if already installed before
if (!$eavSetup->getAttributeId(Product::ENTITY, $attrCode)) {
//Create the attribute
$this->helper->log($attrCode . " not exist before, process it");
// $this->helper->log($attrCode . " not exist before, process it");
// attribute does not exist
// add a new attribute
// and assign it to the "FacebookAttributeSet" attribute set
Expand Down
12 changes: 11 additions & 1 deletion etc/adminhtml/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
parent="Facebook_BusinessExtension::facebook"
sortOrder="10"
dependsOnModule="Facebook_BusinessExtension"
action="fbeadmin/setup"
action="fbeadmin/setup/index"
resource="Facebook_BusinessExtension::facebook"/>
<add
id="Facebook_BusinessExtension::facebook_business_extension_info"
title="Info"
translate="title"
module="Facebook_BusinessExtension"
parent="Facebook_BusinessExtension::facebook"
sortOrder="10"
dependsOnModule="Facebook_BusinessExtension"
action="fbeadmin/info/index"
resource="Facebook_BusinessExtension::facebook"/>
</menu>
</config>
13 changes: 13 additions & 0 deletions view/adminhtml/layout/fbeadmin_info_index.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<referenceBlock name="page.title">
<action method="setPageTitle">
<argument name="title" xsi:type="string">Information</argument>
</action>
</referenceBlock>
<body>
<referenceContainer name="content">
<block class="Facebook\BusinessExtension\Block\Adminhtml\Info" template="Facebook_BusinessExtension::info.phtml"/>
</referenceContainer>
</body>
</page>
Loading

0 comments on commit 0f62824

Please sign in to comment.