Add file rotation navigation

This commit is contained in:
Corey McKrill 2023-10-31 17:03:41 -07:00
parent 22343ba78d
commit b3489a7af7
No known key found for this signature in database
GPG Key ID: 84BBFE669C4D97B8
3 changed files with 114 additions and 3 deletions

View File

@ -1308,6 +1308,16 @@ table.wc_status_table--tools {
* FileV2 logs
*/
.wc-logs-header {
margin: 1em 0;
display: flex;
flex-flow: row wrap;
align-items: center;
gap: 1em;
h2 {
margin: 0;
}
.file-id {
padding: 3px 5px;
background: rgba( 0, 0, 0, 0.07 );
@ -1315,6 +1325,29 @@ table.wc_status_table--tools {
}
}
.wc-logs-single-file-rotations {
display: flex;
align-items: center;
gap: 0.5em;
h3 {
font-size: inherit;
margin: 0;
}
.wc-logs-rotation-links {
list-style: none;
margin: 0;
display: flex;
gap: 0.5em;
li {
display: block;
margin: 0;
}
}
}
.wc-logs-entries {
background: #f6f7f7;
border: 1px solid #c3c4c7;

View File

@ -158,7 +158,9 @@ class FileController {
foreach ( $file_ids as $file_id ) {
$glob = glob( $this->log_directory . $file_id . '*.log' );
$paths = array_merge( $paths, $glob );
if ( is_array( $glob ) ) {
$paths = array_merge( $paths, $glob );
}
}
$files = $this->convert_paths_to_objects( $paths );
@ -186,6 +188,51 @@ class FileController {
return reset( $result );
}
/**
* Get File instances for a given file ID and all of its related rotations.
*
* @param string $file_id A file ID (file basename without the hash).
*
* @return File[]|WP_Error An associative array where the rotation integer of the file is the key, and a "current"
* key for the iteration of the file that hasn't been rotated (if it exists).
*/
public function get_file_rotations( string $file_id ) {
$file = $this->get_file_by_id( $file_id );
if ( is_wp_error( $file ) ) {
return $file;
}
$current = array();
$rotations = array();
$source = $file->get_source();
$created = gmdate( 'Y-m-d', $file->get_created_timestamp() );
if ( is_null( $file->get_rotation() ) ) {
$current['current'] = $file;
} else {
$current_file_id = $source . '-' . $created;
$result = $this->get_file_by_id( $current_file_id );
if ( ! is_wp_error( $result ) ) {
$current['current'] = $result;
}
}
$rotation_pattern = $this->log_directory . $source . '.[0123456789]-' . $created . '*.log';
$rotation_paths = glob( $rotation_pattern );
$rotation_files = $this->convert_paths_to_objects( $rotation_paths );
foreach ( $rotation_files as $rotation_file ) {
if ( $rotation_file->is_readable() ) {
$rotations[ $rotation_file->get_rotation() ] = $rotation_file;
}
}
ksort( $rotations );
return array_merge( $current, $rotations );
}
/**
* Helper method to get an array of File instances.
*

View File

@ -186,6 +186,9 @@ class PageController {
return;
}
$rotations = $this->file_controller->get_file_rotations( $file->get_file_id() );
$rotation_url_base = add_query_arg( 'view', 'single_file', $this->get_logs_tab_url() );
$stream = $file->get_stream();
$line_number = 1;
@ -203,8 +206,36 @@ class PageController {
);
?>
</h2>
<?php if ( count( $rotations ) > 1 ) : ?>
<nav class="wc-logs-single-file-rotations">
<h3><?php esc_html_e( 'File rotations:', 'woocommerce' ); ?></h3>
<ul class="wc-logs-rotation-links">
<?php if ( isset( $rotations['current'] ) ) : ?>
<?php
printf(
'<li><a href="%1$s" class="button button-small button-%2$s">%3$s</a></li>',
esc_url( add_query_arg( 'file_id', $rotations['current']->get_file_id(), $rotation_url_base ) ),
$file->get_file_id() === $rotations['current']->get_file_id() ? 'primary' : 'secondary',
esc_html__( 'Current', 'woocommerce' )
);
unset( $rotations['current'] );
?>
<?php endif; ?>
<?php foreach ( $rotations as $rotation ) : ?>
<?php
printf(
'<li><a href="%1$s" class="button button-small button-%2$s">%3$s</a></li>',
esc_url( add_query_arg( 'file_id', $rotation->get_file_id(), $rotation_url_base ) ),
$file->get_file_id() === $rotation->get_file_id() ? 'primary' : 'secondary',
absint( $rotation->get_rotation() )
);
?>
<?php endforeach; ?>
</ul>
</nav>
<?php endif; ?>
</header>
<div id="logs-entries" class="wc-logs-entries">
<section id="logs-entries" class="wc-logs-entries">
<?php while ( ! feof( $stream ) ) : ?>
<?php
$line = fgets( $stream );
@ -215,7 +246,7 @@ class PageController {
}
?>
<?php endwhile; ?>
</div>
</section>
<?php
}