| @@ -16,157 +16,152 @@ | ||
| 16 | 16 | * Class - Log_Stats |
| 17 | 17 | */ |
| 18 | 18 | class Log_Stats { |
| 19 | 19 | |
| 20 | - /** | |
| 21 | - * Class constructor. | |
| 22 | - */ | |
| 23 | - public function __construct() { | |
| 24 | - } | |
| 20 | + /** | |
| 21 | + * Class constructor. | |
| 22 | + */ | |
| 23 | + public function __construct() { | |
| 24 | + } | |
| 25 | 25 | |
| 26 | - /** | |
| 27 | - * Get stats data. | |
| 28 | - * | |
| 29 | - * @param array $items Logs items array. | |
| 30 | - */ | |
| 31 | - public static function get_stats_data( $items ) { | |
| 32 | - $data = array(); | |
| 33 | - if ( is_array( $items ) ) { | |
| 34 | - $total_count = 0; | |
| 35 | - $total_dura_time = 0; | |
| 36 | - foreach ( $items as $item ) { | |
| 37 | - if ( 'compact' !== $item->connector ) { | |
| 38 | - if ( ! isset( $data[ $item->connector ] ) ) { | |
| 39 | - $data[ $item->connector ] = array(); | |
| 40 | - } | |
| 41 | - if ( ! isset( $data[ $item->connector ][ $item->context ] ) ) { | |
| 42 | - $data[ $item->connector ][ $item->context ] = array(); | |
| 43 | - } | |
| 26 | + /** | |
| 27 | + * Get stats data. | |
| 28 | + * | |
| 29 | + * @param array $items Logs items array. | |
| 30 | + */ | |
| 31 | + public static function get_stats_data( $items ) { //phpcs:ignore -- NOSONAR - complex. | |
| 32 | + $data = array(); | |
| 33 | + if ( is_array( $items ) ) { | |
| 34 | + foreach ( $items as $item ) { | |
| 35 | + if ( 'compact' !== $item->connector ) { | |
| 36 | + if ( ! isset( $data[ $item->connector ] ) ) { | |
| 37 | + $data[ $item->connector ] = array(); | |
| 38 | + } | |
| 39 | + if ( ! isset( $data[ $item->connector ][ $item->context ] ) ) { | |
| 40 | + $data[ $item->connector ][ $item->context ] = array(); | |
| 41 | + } | |
| 44 | 42 | |
| 45 | - if ( ! isset( $data[ $item->connector ][ $item->context ]['total_events'] ) ) { | |
| 46 | - $data[ $item->connector ][ $item->context ]['total_events'] = array( | |
| 47 | - 'count' => 0, | |
| 48 | - 'dura_time' => 0, | |
| 49 | - ); | |
| 50 | - } | |
| 43 | + if ( ! isset( $data[ $item->connector ][ $item->context ]['total_events'] ) ) { | |
| 44 | + $data[ $item->connector ][ $item->context ]['total_events'] = array( | |
| 45 | + 'count' => 0, | |
| 46 | + 'dura_time' => 0, | |
| 47 | + ); | |
| 48 | + } | |
| 51 | 49 | |
| 52 | - if ( ! isset( $data[ $item->connector ][ $item->context ][ $item->action ] ) ) { | |
| 53 | - $data[ $item->connector ][ $item->context ][ $item->action ] = array( | |
| 54 | - 'count' => 1, | |
| 55 | - 'dura_time' => $item->duration, | |
| 56 | - ); | |
| 57 | - } else { | |
| 58 | - $data[ $item->connector ][ $item->context ][ $item->action ]['count'] += 1; | |
| 59 | - $data[ $item->connector ][ $item->context ][ $item->action ]['dura_time'] += $item->duration; | |
| 60 | - } | |
| 61 | - $data[ $item->connector ][ $item->context ]['total_events']['count'] += 1; | |
| 62 | - $data[ $item->connector ][ $item->context ]['total_events']['dura_time'] += $item->duration; | |
| 63 | - } | |
| 64 | - } | |
| 65 | - } | |
| 66 | - return $data; | |
| 67 | - } | |
| 50 | + if ( ! isset( $data[ $item->connector ][ $item->context ][ $item->action ] ) ) { | |
| 51 | + $data[ $item->connector ][ $item->context ][ $item->action ] = array( | |
| 52 | + 'count' => 1, | |
| 53 | + 'dura_time' => $item->duration, | |
| 54 | + ); | |
| 55 | + } else { | |
| 56 | + $data[ $item->connector ][ $item->context ][ $item->action ]['count'] += 1; | |
| 57 | + $data[ $item->connector ][ $item->context ][ $item->action ]['dura_time'] += $item->duration; | |
| 58 | + } | |
| 59 | + $data[ $item->connector ][ $item->context ]['total_events']['count'] += 1; | |
| 60 | + $data[ $item->connector ][ $item->context ]['total_events']['dura_time'] += $item->duration; | |
| 61 | + } | |
| 62 | + } | |
| 63 | + } | |
| 64 | + return $data; | |
| 65 | + } | |
| 68 | 66 | |
| 69 | - /** | |
| 70 | - * Render stats count. | |
| 71 | - * | |
| 72 | - * @param array $data Stats data array. | |
| 73 | - * @param string $action Action name. | |
| 74 | - */ | |
| 75 | - public static function get_stats_count( $data, $action ) { | |
| 76 | - return isset( $data[ $action ] ) && ! empty( $data[ $action ]['count'] ) ? $data[ $action ]['count'] : 0; | |
| 77 | - } | |
| 67 | + /** | |
| 68 | + * Render stats count. | |
| 69 | + * | |
| 70 | + * @param array $data Stats data array. | |
| 71 | + * @param string $action Action name. | |
| 72 | + */ | |
| 73 | + public static function get_stats_count( $data, $action ) { | |
| 74 | + return isset( $data[ $action ] ) && ! empty( $data[ $action ]['count'] ) ? $data[ $action ]['count'] : 0; | |
| 75 | + } | |
| 78 | 76 | |
| 79 | 77 | |
| 80 | - /** | |
| 81 | - * Render stats duration time. | |
| 82 | - * | |
| 83 | - * @param array $data Stats data array. | |
| 84 | - * @param string $action Action name. | |
| 85 | - */ | |
| 86 | - public static function render_stats_duration_time( $data, $action ) { | |
| 87 | - $dura_time = isset( $data[ $action ] ) && ! empty( $data[ $action ]['dura_time'] ) ? $data[ $action ]['dura_time'] : 0; | |
| 88 | - echo MainWP_Utility::format_duration_time( $dura_time ) ; //phpcs:ignore -- ok. | |
| 89 | - } | |
| 78 | + /** | |
| 79 | + * Render stats duration time. | |
| 80 | + * | |
| 81 | + * @param array $data Stats data array. | |
| 82 | + * @param string $action Action name. | |
| 83 | + */ | |
| 84 | + public static function render_stats_duration_time( $data, $action ) { | |
| 85 | + $dura_time = isset( $data[ $action ] ) && ! empty( $data[ $action ]['dura_time'] ) ? $data[ $action ]['dura_time'] : 0; | |
| 86 | + echo MainWP_Utility::format_duration_time( $dura_time ) ; //phpcs:ignore -- ok. | |
| 87 | + } | |
| 90 | 88 | |
| 91 | - /** | |
| 92 | - * Render stats column content. | |
| 93 | - * | |
| 94 | - * @param array $data Data stats array. | |
| 95 | - * @param string $action Action name. | |
| 96 | - * @param string $title Action title. | |
| 97 | - * @param array $data_prev Data Stats previous array. | |
| 98 | - */ | |
| 99 | - public static function render_stats_info( $data, $action, $title, $data_prev ) { | |
| 100 | - $count = self::get_stats_count( $data, $action ); | |
| 101 | - $prev_count = self::get_stats_count( $data_prev, $action ); | |
| 102 | - ?> | |
| 103 | - <div class="center aligned middle aligned column"> | |
| 104 | - <div class="ui mini vertical statistic"> | |
| 105 | - <div class="value"> | |
| 106 | - <?php self::render_stats_compare( $count, $prev_count ); ?> <?php echo intval( $count ); ?> | |
| 107 | - </div> | |
| 108 | - <div class="label"> | |
| 109 | - <?php echo esc_html( $title ); ?> | |
| 110 | - </div> | |
| 111 | - <span class="ui small text"> | |
| 112 | - <?php self::render_stats_duration_time( $data, $action ); ?> | |
| 113 | - </span> | |
| 114 | - </div> | |
| 115 | - </div> | |
| 116 | - <?php | |
| 117 | - } | |
| 89 | + /** | |
| 90 | + * Render stats column content. | |
| 91 | + * | |
| 92 | + * @param array $data Data stats array. | |
| 93 | + * @param string $action Action name. | |
| 94 | + * @param string $title Action title. | |
| 95 | + * @param array $data_prev Data Stats previous array. | |
| 96 | + */ | |
| 97 | + public static function render_stats_info( $data, $action, $title, $data_prev ) { | |
| 98 | + $count = static::get_stats_count( $data, $action ); | |
| 99 | + $prev_count = static::get_stats_count( $data_prev, $action ); | |
| 100 | + ?> | |
| 101 | + <div class="column"> | |
| 102 | + <div class="ui small header"> | |
| 103 | + <?php static::render_stats_compare( $count, $prev_count ); ?> <?php echo intval( $count ); ?> | |
| 104 | + <div class="sub header"> | |
| 105 | + <?php echo esc_html( $title ); ?> | |
| 106 | + <br /> | |
| 107 | + <span class="ui small text"><?php static::render_stats_duration_time( $data, $action ); ?></span> | |
| 108 | + </div> | |
| 109 | + </div> | |
| 110 | + </div> | |
| 111 | + <?php | |
| 112 | + } | |
| 118 | 113 | |
| 119 | - /** | |
| 120 | - * Render stats compare. | |
| 121 | - * | |
| 122 | - * @param int $count Count stats. | |
| 123 | - * @param int $prev_count Count previous stats. | |
| 124 | - */ | |
| 125 | - public static function render_stats_compare( $count, $prev_count ) { | |
| 126 | - echo '<span data-tooltip="' . intval( $count ) . ' actions this period, ' . intval( $prev_count ) . ' actions previous period, showing growth or decline." data-position="top left" data-inverted="">'; | |
| 127 | - if ( $count === $prev_count ) { | |
| 128 | - ?> | |
| 129 | - <i class="angle right icon"></i> | |
| 130 | - <?php | |
| 131 | - } elseif ( $count > $prev_count ) { | |
| 132 | - ?> | |
| 133 | - <i class="angle up green icon"></i> | |
| 134 | - <?php | |
| 135 | - } else { | |
| 136 | - ?> | |
| 137 | - <i class="angle down red icon"></i> | |
| 138 | - <?php | |
| 139 | - } | |
| 140 | - echo '</span>'; | |
| 141 | - } | |
| 114 | + /** | |
| 115 | + * Render stats compare. | |
| 116 | + * | |
| 117 | + * @param int $count Count stats. | |
| 118 | + * @param int $prev_count Count previous stats. | |
| 119 | + */ | |
| 120 | + public static function render_stats_compare( $count, $prev_count ) { | |
| 121 | + echo '<span data-tooltip="' . intval( $count ) . ' actions this period, ' . intval( $prev_count ) . ' actions previous period, showing growth or decline." data-position="top left" data-inverted="">'; | |
| 122 | + if ( $count === $prev_count ) { | |
| 123 | + ?> | |
| 124 | + <i class="angle right icon" style="float:none;"></i> | |
| 125 | + <?php | |
| 126 | + } elseif ( $count > $prev_count ) { | |
| 127 | + ?> | |
| 128 | + <i class="angle up green icon"></i> | |
| 129 | + <?php | |
| 130 | + } else { | |
| 131 | + ?> | |
| 132 | + <i class="angle down red icon"></i> | |
| 133 | + <?php | |
| 134 | + } | |
| 135 | + echo '</span>'; | |
| 136 | + } | |
| 142 | 137 | |
| 143 | 138 | |
| 144 | - /** | |
| 145 | - * Method render_chart_series(). | |
| 146 | - * | |
| 147 | - * @param array $data Stats data. | |
| 148 | - * @param string $action Action. | |
| 149 | - * @param string $title Title. | |
| 150 | - * @param array $data_prev Stats previous data. | |
| 151 | - */ | |
| 152 | - public static function render_chart_series( $data, $action, $title, $data_prev ) { | |
| 153 | - $count = self::get_stats_count( $data, $action ); | |
| 154 | - $prev_count = self::get_stats_count( $data_prev, $action ); | |
| 155 | - echo ' | |
| 156 | - { | |
| 157 | - x: "' . esc_html( $title ) . '", | |
| 158 | - y: "' . intval( $count ) . '", | |
| 159 | - goals: [ | |
| 160 | - { | |
| 161 | - name: "Previous", | |
| 162 | - value: "' . intval( $prev_count ) . '", | |
| 163 | - strokeHeight: 2, | |
| 164 | - strokeColor: "#7fb100", | |
| 165 | - } | |
| 166 | - ], | |
| 167 | - fillColor: "#18a4e0", | |
| 168 | - }, | |
| 169 | - | |
| 170 | - '; | |
| 171 | - } | |
| 139 | + /** | |
| 140 | + * Method render_chart_series(). | |
| 141 | + * | |
| 142 | + * @param array $data Stats data. | |
| 143 | + * @param string $action Action. | |
| 144 | + * @param string $title Title. | |
| 145 | + * @param array $data_prev Stats previous data. | |
| 146 | + */ | |
| 147 | + public static function render_chart_series( $data, $action, $title, $data_prev ) { | |
| 148 | + $count = static::get_stats_count( $data, $action ); | |
| 149 | + $prev_count = static::get_stats_count( $data_prev, $action ); | |
| 150 | + echo ' | |
| 151 | + { | |
| 152 | + x: "' . esc_html( $title ) . '", | |
| 153 | + y: "' . intval( $count ) . '", | |
| 154 | + goals: [ | |
| 155 | + { | |
| 156 | + name: "Previous", | |
| 157 | + value: "' . intval( $prev_count ) . '", | |
| 158 | + strokeHeight: 2, | |
| 159 | + strokeColor: "#7fb100", | |
| 160 | + } | |
| 161 | + ], | |
| 162 | + fillColor: "#18a4e0", | |
| 163 | + }, | |
| 164 | + | |
| 165 | + '; | |
| 166 | + } | |
| 172 | 167 | } |