Feature/add logger to rin (https://github.com/woocommerce/woocommerce-admin/pull/7194)
* Add evaluation logger * Update readme * Use simplified log format * Add a new line at the end * Move constant checking to consumer level * Rename constant name * Check constant before checking the value * Fix test * Add changelog * Move initialization of the logger to RuleEvaluator and use more generic constant name * Remove the default source value * Update readme.txt Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Update src/RemoteInboxNotifications/README.md Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Update src/RemoteInboxNotifications/RuleEvaluator.php Co-authored-by: Joshua T Flowers <joshuatf@gmail.com> * Add soruce -- remote-inbox-notifications * Move WC_ADMIN_DEBUG_RULE_EVALUATOR check to the logger Co-authored-by: Joshua T Flowers <joshuatf@gmail.com>
This commit is contained in:
parent
0a1a07cc54
commit
aaf7832ddb
|
@ -84,6 +84,9 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt
|
|||
- Fix: Currency display on Orders activity card on homescreen #7181
|
||||
- Fix: Fix obsolete key property in gateway defaults #7229
|
||||
- Fix: Fixing button state logic for remote payment gateways #7200
|
||||
- Add: Add a logger to Remote Inbox Notifications #7194
|
||||
- Add: Add a logger to the rule evaluator #7194
|
||||
- Fix: WCPay not working in local payments task #7151
|
||||
- Fix: Include onboarding settings on the analytic pages #7109
|
||||
- Fix: Load Analytics API only when feature is turned on #7193
|
||||
- Fix: Localize string for description #7219
|
||||
|
|
|
@ -16,9 +16,9 @@ class EvaluateAndGetStatus {
|
|||
/**
|
||||
* Evaluates the spec and returns a status.
|
||||
*
|
||||
* @param array $spec The spec to evaluate.
|
||||
* @param array $spec The spec to evaluate.
|
||||
* @param string $current_status The note's current status.
|
||||
* @param object $stored_state Stored state.
|
||||
* @param object $stored_state Stored state.
|
||||
* @param object $rule_evaluator Evaluates rules into true/false.
|
||||
*
|
||||
* @return string The evaluated status.
|
||||
|
@ -29,7 +29,14 @@ class EvaluateAndGetStatus {
|
|||
return $current_status;
|
||||
}
|
||||
|
||||
$evaluated_result = $rule_evaluator->evaluate( $spec->rules, $stored_state );
|
||||
$evaluated_result = $rule_evaluator->evaluate(
|
||||
$spec->rules,
|
||||
$stored_state,
|
||||
array(
|
||||
'slug' => $spec->slug,
|
||||
'source' => 'remote-inbox-notifications',
|
||||
)
|
||||
);
|
||||
|
||||
// Pending notes should be the spec status if the spec passes,
|
||||
// left alone otherwise.
|
||||
|
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace Automattic\WooCommerce\Admin\RemoteInboxNotifications;
|
||||
|
||||
/**
|
||||
* Class EvaluationLogger
|
||||
*
|
||||
* @package Automattic\WooCommerce\Admin\RemoteInboxNotifications
|
||||
*/
|
||||
class EvaluationLogger {
|
||||
/**
|
||||
* Slug of the spec.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $slug;
|
||||
|
||||
/**
|
||||
* Results of rules in the given spec.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $results = array();
|
||||
|
||||
/**
|
||||
* Logger class to use.
|
||||
*
|
||||
* @var WC_Logger_Interface|null
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* Logger source.
|
||||
*
|
||||
* @var string logger source.
|
||||
*/
|
||||
private $source = '';
|
||||
|
||||
/**
|
||||
* EvaluationLogger constructor.
|
||||
*
|
||||
* @param string $slug Slug of a spec that is being evaluated.
|
||||
* @param null $source Logger source.
|
||||
* @param \WC_Logger_Interface $logger Logger class to use.
|
||||
*/
|
||||
public function __construct( $slug, $source = null, \WC_Logger_Interface $logger = null ) {
|
||||
$this->slug = $slug;
|
||||
if ( null === $logger ) {
|
||||
$logger = wc_get_logger();
|
||||
}
|
||||
|
||||
if ( $source ) {
|
||||
$this->source = $source;
|
||||
}
|
||||
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add evaluation result of a rule.
|
||||
*
|
||||
* @param string $rule_type name of the rule being tested.
|
||||
* @param boolean $result result of a given rule.
|
||||
*/
|
||||
public function add_result( $rule_type, $result ) {
|
||||
array_push(
|
||||
$this->results,
|
||||
array(
|
||||
'rule' => $rule_type,
|
||||
'result' => $result ? 'passed' : 'failed',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the results.
|
||||
*/
|
||||
public function log() {
|
||||
if ( false === defined( 'WC_ADMIN_DEBUG_RULE_EVALUATOR' ) || true !== constant( 'WC_ADMIN_DEBUG_RULE_EVALUATOR' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $this->results as $result ) {
|
||||
$this->logger->debug(
|
||||
"[{$this->slug}] {$result['rule']}: {$result['result']}",
|
||||
array( 'source' => $this->source )
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -497,3 +497,17 @@ WooCommerce Admin plugin.
|
|||
```
|
||||
|
||||
No other values are needed.
|
||||
|
||||
## Debugging Specification
|
||||
|
||||
You can see the evaluation of each specification by turning on an optional evaluation logger.
|
||||
|
||||
1. Define `WC_ADMIN_DEBUG_RULE_EVALUATOR` constant in `wp-config.php`. `define('WC_ADMIN_DEBUG_RULE_EVALUATOR', true);`
|
||||
2. Run `wc_admin_daily`
|
||||
3. cd to `wp-content/uploads/wc-logs/` and check a log file in `remote-inbox-notifications-:date-hash.log` format.
|
||||
|
||||
You can tail the log file with a slug name to see the evaluation of a rule that you are testing.
|
||||
|
||||
Example:
|
||||
|
||||
`tail -f remote-inbox-notifications-2021-06-15-128.log | grep 'wcpay-promo-2021-6-incentive-2'`
|
|
@ -28,12 +28,15 @@ class RuleEvaluator {
|
|||
* Evaluate the given rules as an AND operation - return false early if a
|
||||
* rule evaluates to false.
|
||||
*
|
||||
* @param array|object $rules The rule or rules being processed.
|
||||
* @param array|object $rules The rule or rules being processed.
|
||||
* @param object|null $stored_state Stored state.
|
||||
* @param array $logger_args Arguments for the event logger. `slug` is required.
|
||||
*
|
||||
* @throws \InvalidArgumentException Thrown when $logger_args is missing slug.
|
||||
*
|
||||
* @return bool The result of the operation.
|
||||
*/
|
||||
public function evaluate( $rules, $stored_state = null ) {
|
||||
public function evaluate( $rules, $stored_state = null, $logger_args = array() ) {
|
||||
if ( ! is_array( $rules ) ) {
|
||||
$rules = array( $rules );
|
||||
}
|
||||
|
@ -42,15 +45,31 @@ class RuleEvaluator {
|
|||
return false;
|
||||
}
|
||||
|
||||
$evaluation_logger = null;
|
||||
|
||||
if ( count( $logger_args ) ) {
|
||||
if ( ! array_key_exists( 'slug', $logger_args ) ) {
|
||||
throw new \InvalidArgumentException( 'Missing required field: slug in $logger_args.' );
|
||||
}
|
||||
|
||||
array_key_exists( 'source', $logger_args ) ? $source = $logger_args['source'] : $source = null;
|
||||
|
||||
$evaluation_logger = new EvaluationLogger( $logger_args['slug'], $source );
|
||||
}
|
||||
|
||||
foreach ( $rules as $rule ) {
|
||||
$processor = $this->get_rule_processor->get_processor( $rule->type );
|
||||
$processor_result = $processor->process( $rule, $stored_state );
|
||||
$evaluation_logger && $evaluation_logger->add_result( $rule->type, $processor_result );
|
||||
|
||||
if ( ! $processor_result ) {
|
||||
$evaluation_logger && $evaluation_logger->log();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$evaluation_logger && $evaluation_logger->log();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ class WC_Tests_RemoteInboxNotifications_EvaluateAndGetStatus extends WC_Unit_Tes
|
|||
private function get_spec( $allow_redisplay ) {
|
||||
return json_decode(
|
||||
'{
|
||||
"slug": "test",
|
||||
"status": "unactioned",
|
||||
"rules": [],
|
||||
"allow_redisplay": ' . ( $allow_redisplay ? 'true' : 'false' ) . '
|
||||
|
|
Loading…
Reference in New Issue