Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add alert if site has a low cache hit ratio #22

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
79 changes: 79 additions & 0 deletions inc/pantheon-metrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* If this is a Pantheon site, show a notice about low cache hit ratio
*
* @package pantheon
*/

// If on Pantheon...
if ( isset( $_ENV['PANTHEON_ENVIRONMENT'] ) ) {

Check failure on line 9 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L9

Direct use of $_ENV Superglobal detected.

Check notice on line 9 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L9

First condition of a multi-line IF statement must directly follow the opening parenthesis
add_action( 'admin_notices', '_pantheon_metrics_notice' );

Check notice on line 10 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L10

Line indented incorrectly; expected at least 4 spaces, found 1
add_action( 'network_admin_notices', '_pantheon_metrics_notice' );

Check notice on line 11 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L11

Line indented incorrectly; expected at least 4 spaces, found 1
}

/**
* Fetch traffic metrics from the Pantheon API
*
* @return array
*/
function _pantheon_get_metrics() {
$metrics = get_transient( '_pantheon_metrics' );
if ( $metrics != false ) {

Check notice on line 21 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L21

Expected 0 spaces after opening bracket; 1 found
return $metrics;
}

Check notice on line 23 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L23

No blank line found after control structure
if ( true == get_transient( '_pantheon_metrics_no_retry' ) ) {

Check notice on line 24 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L24

Expected 0 spaces after opening bracket; 1 found

Check notice on line 24 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L24

Operator == prohibited; use === instead

Check notice on line 24 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L24

Space after opening parenthesis of function call prohibited
return false;
}
$url = 'https:/api.live.getpantheon.com/sites/self/environments/live/traffic?duration=28d';

Check notice on line 27 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L27

Line indented incorrectly; expected at least 4 spaces, found 1
$req = pantheon_curl( $url, NULL, 8443 );

Check notice on line 28 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L28

Space after opening parenthesis of function call prohibited
if( 200 != $req['status-code'] || ! $req['body'] ) {

Check notice on line 29 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L29

Expected "if (...) {\n"; found "if(...) {\n"

Check notice on line 29 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L29

First condition of a multi-line IF statement must directly follow the opening parenthesis

Check notice on line 29 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L29

Line indented incorrectly; expected 4 spaces, found 1
// Don't retry for two minutes

Check notice on line 30 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L30

Line indented incorrectly; expected at least 8 spaces, found 2
set_transient( '_pantheon_metrics_no_retry', true, 120 );

Check notice on line 31 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L31

Line indented incorrectly; expected at least 8 spaces, found 2
return false;
}
$metrics = json_decode( $req['body'], TRUE );

Check notice on line 34 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L34

Line indented incorrectly; expected at least 4 spaces, found 1
set_transient( '_pantheon_metrics', $metrics['timeseries'], 86400);

Check notice on line 35 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L35

Space after opening parenthesis of function call prohibited
return $metrics['timeseries'];

Check notice on line 36 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L36

Line indented incorrectly; expected at least 4 spaces, found 1
}

Check notice on line 37 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L37

Expected //end _pantheon_get_metrics()

/**
* Get the most recent day's cache hit ratio
*
* @return int Cache hit ratio, or false on error
*/
function _pantheon_get_cache_hit_ratio() {
$metrics = _pantheon_get_metrics();
if ( ! $metrics ) {
return false;
}
$last_day = array_pop($metrics);
if ( ! $last_day['pages_served'] ) {
return false;
}
return number_format( $last_day['cache_hits'] / $last_day['pages_served'] * 100, 0);
}

/**
* Add a notice of low cache hit ratio on the dashboard and site health pages
*
* @return void
*/
function _pantheon_metrics_notice() {

Check notice on line 61 in inc/pantheon-metrics.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/pantheon-metrics.php#L61

_pantheon_metrics_notice accesses the super-global variable $_ENV.
$cache_hit_ratio = _pantheon_get_cache_hit_ratio();
if($cache_hit_ratio > 50 || $cache_hit_ratio === false) {
return false;
}
$screen = get_current_screen();
if ( 'dashboard' === $screen->id || 'site-health' === $screen->id ) {
?>
<div class="update-nag notice notice-warning is-dismissible" style="display: table;">
<p style="font-size: 14px; margin: 0;">
<?php
// Translators: %s is a URL to the user's Pantheon Dashboard.
echo wp_kses_post( sprintf( __( 'The live site is currently only serving %d%% of traffic from the GCDN cache. See more details in the <a href="%s">Metrics tab</a> in your Pantheon dashboard.', 'pantheon-systems' ), $cache_hit_ratio, 'https://dashboard.pantheon.io/sites/' . $_ENV['PANTHEON_SITE'] . '#live/metrics' ) );
?>
</p>
</div>
<?php
}
}
31 changes: 17 additions & 14 deletions pantheon.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@
if ( 'dev' === $_ENV['PANTHEON_ENVIRONMENT'] && function_exists( 'wp_is_writable' ) ) {
require_once 'inc/pantheon-plugin-install-notice.php';
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once 'inc/cli.php';
}
if ( ! defined('SHOW_PANTHEON_METRICS_NOTICE') || SHOW_PANTHEON_METRICS_NOTICE ) {

Check notice on line 25 in pantheon.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pantheon.php#L25

Expected 0 spaces after opening bracket; 1 found

Check notice on line 25 in pantheon.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pantheon.php#L25

Implicit true comparisons prohibited; use === TRUE instead

Check notice on line 25 in pantheon.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pantheon.php#L25

Line indented incorrectly; expected 4 spaces, found 1
require_once 'inc/pantheon-metrics.php';

Check notice on line 26 in pantheon.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

pantheon.php#L26

Line indented incorrectly; expected at least 8 spaces, found 2
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once 'inc/cli.php';
}
if ( ! defined( 'FS_METHOD' ) ) {
/**
* When this constant is not set, WordPress writes and then deletes a
Expand All @@ -37,15 +40,15 @@
*/
define( 'FS_METHOD', 'direct' );
}
// When developing a WordPress Multisite locally, ensure that this constant is set.
// This will set the Multisite variable in all Pantheon environments.
if ( getenv( 'FRAMEWORK' ) === 'wordpress_network' && ! defined( 'WP_ALLOW_MULTISITE' ) ) {
define( 'WP_ALLOW_MULTISITE', true );
}
if ( defined( 'MULTISITE' ) && defined( 'WP_ALLOW_MULTISITE' ) && WP_ALLOW_MULTISITE ) {
require_once 'inc/pantheon-network-setup.php';
}
if ( defined( 'WP_ALLOW_MULTISITE' ) && ( ! defined( 'MULTISITE' ) || empty( MULTISITE ) ) ) {
require_once 'inc/pantheon-multisite-finalize.php';
}
// When developing a WordPress Multisite locally, ensure that this constant is set.
// This will set the Multisite variable in all Pantheon environments.
if ( getenv( 'FRAMEWORK' ) === 'wordpress_network' && ! defined( 'WP_ALLOW_MULTISITE' ) ) {
define( 'WP_ALLOW_MULTISITE', true );
}
if ( defined( 'MULTISITE' ) && defined( 'WP_ALLOW_MULTISITE' ) && WP_ALLOW_MULTISITE ) {
require_once 'inc/pantheon-network-setup.php';
}
if ( defined( 'WP_ALLOW_MULTISITE' ) && ( ! defined( 'MULTISITE' ) || empty( MULTISITE ) ) ) {
require_once 'inc/pantheon-multisite-finalize.php';
}
} // Ensuring that this is on Pantheon.