Added timeline iterator for automatic splitting of nested events.

This commit is contained in:
Andrey Savchenko committed 2018-02-09 18:47:13 +02:00
1 parent bee765071b
commit a9484ed4ee
4 files changed
+49 -19

No files matched your search

+1 -1
View File
@@ -2,7 +2,7 @@
namespace Rarst\Laps\Events;
class Recursive_Event_Iterator extends \RecursiveArrayIterator {
class Recursive_Event_Iterator extends \ArrayIterator implements \RecursiveIterator {
protected $children = [];
+8 -16
View File
@@ -143,22 +143,10 @@ class Laps extends Container {
$start = $timestart * 1000;
$end = microtime( true ) * 1000;
$total = $end - $start;
$event_data = [];
foreach ( $events as $event ) {
$event['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 );
$event['width'] = round( $event['duration'] / $total * 100, 2 );
$event_data[] = $event;
}
$iterator = new Recursive_Event_Iterator( $event_data );
$timelines = [ [ 'events' => $iterator ] ];
while ( $iterator->hasChildren() ) {
$children = $iterator->getChildren();
$timelines[] = [ 'events' => $children ];
$iterator = $children;
foreach ( $events as $key => $event ) {
$events[ $key ]['offset'] = round( ( $event['origin'] - $start ) / $total * 100, 2 );
$events[ $key ]['width'] = round( $event['duration'] / $total * 100, 2 );
}
$wp_admin_bar->add_node( [
@@ -169,7 +157,11 @@ class Laps extends Container {
$wp_admin_bar->add_node( [
'id' => 'laps_output',
'parent' => 'laps',
'meta' => [ 'html' => $this['mustache']->render( 'laps', [ 'timelines' => $timelines ] ) ],
'meta' => [
'html' => $this['mustache']->render( 'laps', [
'timelines' => new Timeline_Iterator( new Recursive_Event_Iterator( $events ) ),
] ),
],
] );
}
}
+38
View File
@@ -0,0 +1,38 @@
<?php
namespace Rarst\Laps;
use Rarst\Laps\Events\Recursive_Event_Iterator;
class Timeline_Iterator implements \Iterator {
/** @var Recursive_Event_Iterator */
protected $iterator;
/** @var Recursive_Event_Iterator */
protected $current;
public function __construct( Recursive_Event_Iterator $iterator ) {
$this->iterator = $iterator;
}
public function current() {
return $this->current;
}
public function next() {
$this->current = $this->current->getChildren();
}
public function key() {
}
public function valid() {
return (bool) count( $this->current );
}
public function rewind() {
$this->current = $this->iterator;
}
}