Skip to content

Commit

Permalink
changes to better support performance schema datasource
Browse files Browse the repository at this point in the history
+ added group by digest to default perf_schema report
+ cleaned up numerous warnings
+ created show_query page for perf_schema
  • Loading branch information
gtowey committed Dec 17, 2013
1 parent 5ec15d1 commit 45104af
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 23 deletions.
21 changes: 19 additions & 2 deletions conf/sample.config.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,16 @@
$conf['report_defaults']['performance_schema'] = array(
'fact-order' => 'SUM_TIMER_WAIT DESC',
'fact-limit' => '20',
'table_fields' => array( 'DIGEST', 'snippet', 'index_ratio', 'COUNT_STAR', 'SUM_LOCK_TIME','SUM_ROWS_AFFECTED','SUM_ROWS_SENT','SUM_ROWS_EXAMINED','SUM_CREATED_TMP_TABLES','SUM_SORT_SCAN','SUM_NO_INDEX_USED' )
'fact-group' => 'DIGEST',
'table_fields' => array( 'DIGEST', 'snippet', 'index_ratio', 'COUNT_STAR', 'SUM_TIMER_WAIT', 'SUM_LOCK_TIME','SUM_ROWS_AFFECTED','SUM_ROWS_SENT','SUM_ROWS_EXAMINED','SUM_CREATED_TMP_TABLES','SUM_SORT_SCAN','SUM_NO_INDEX_USED' )
);

// these are the default values for mysql 5.6 performance schema datasources
$conf['history_defaults']['performance_schema'] = array(
'fact-order' => 'SUM_TIMER_WAIT DESC',
'fact-limit' => '20',
'fact-group' => 'DIGEST',
'table_fields' => array( 'DIGEST', 'index_ratio', 'COUNT_STAR', 'SUM_LOCK_TIME','SUM_ROWS_AFFECTED','SUM_ROWS_SENT','SUM_ROWS_EXAMINED','SUM_CREATED_TMP_TABLES','SUM_SORT_SCAN','SUM_NO_INDEX_USED' )
);

// these are the default values for using performance schema to save your own
Expand Down Expand Up @@ -256,7 +265,7 @@
'explain' => function ($sample) {
$conn = array();

if (strlen($sample['hostname_max']) < 5)
if (!array_key_exists('hostname_max',$sample) or strlen($sample['hostname_max']) < 5)
{
return;
}
Expand Down Expand Up @@ -359,6 +368,7 @@
'where' => 'raw_where',
'DIGEST' => 'clear|where',
'DIGEST_TEXT' => 'clear|like|where',
'group' => 'group',
),
),
// custom fields
Expand All @@ -368,6 +378,13 @@
'rows_sent_avg' => 'ROUND(SUM_ROWS_SENT/COUNT_STAR,0)',

),

'special_field_names' => array(
'time' => 'FIRST_SEEN',
'checksum' => 'DIGEST',
'sample' => 'DIGEST_TEXT',
'fingerprint' => 'DIGEST_TEXT',
),
);

$conf['reports']['performance_schema_history'] = array(
Expand Down
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
$action = isset($_GET['action']) ? $_GET['action'] : 'index';

$conf = array();
include "conf/config.inc.php";
@include "conf/config.inc.php";
if (empty($conf))
{
$action = 'noconfig';
Expand Down
42 changes: 30 additions & 12 deletions lib/Anemometer.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ public function samples() {

private function translate_checksum($checksum)
{
if (!in_array($this->data_model->get_source_type(), array('slow_query_log','default')))
{
return $checksum;
}

if (preg_match('/^[0-9]+$/', $checksum))
{
return $checksum;
Expand Down Expand Up @@ -352,10 +357,14 @@ public function show_query() {

$data['datasource'] = get_var('datasource');
$sample_field_name = $this->data_model->get_field_name('sample');
$checksum_field_name = $this->data_model->get_field_name('checksum');
$fingerprint_field_name = $this->data_model->get_field_name('fingerprint');

// query and most recent sample
$row = $this->data_model->get_query_by_checksum($checksum);
$source_type = $this->data_model->get_source_type();
$data['source_type'] = $source_type;

$callbacks = $this->data_model->get_callbacks($source_type, $output);
if (isset($callbacks))
{
Expand All @@ -366,21 +375,20 @@ public function show_query() {
}
}
}
$data['checksum'] = $row['checksum'];
$_GET['checksum'] = $row['checksum'];

$data['checksum'] = $row[$checksum_field_name];
$_GET['checksum'] = $row[$checksum_field_name];

$data['row'] = $row;

$data['sample'] = $this->data_model->get_query_samples($checksum, 1)->fetch_assoc();
$sample = $data['sample'][$sample_field_name];

// review info
$data['review_types'] = $this->data_model->get_review_types();
$data['reviewers'] = $this->data_model->get_reviewers();
$data['current_auth_user'] = $this->get_auth_user();

$sample = $data['sample'][$sample_field_name];
// get explain plan and extra info
// TODO convert to ajax calls, just get the url
$source_type = $this->data_model->get_source_type();

$data['show_samples'] = true;
if ($source_type == 'performance_schema_history')
Expand Down Expand Up @@ -415,11 +423,14 @@ public function show_query() {
$data['timezone_offset'] = timezone_offset_get( new DateTimeZone( ini_get('date.timezone' )), new DateTime());

$data['tables'] = $this->report_obj->get_tables();
$dimension_table = $this->report_obj->get_table_by_alias('dimension');
$data['hosts'] = $this->report_obj->get_distinct_values($dimension_table, $data['hostname_field_name']);
// check
$data[$data['hostname_field_name']] = get_var($data['hostname_field_name']);
$this->report_obj->set_pivot_values('dimension-pivot-'.$data['hostname_field_name'], $data['hosts']);
if ($source_type == 'slow_query_log')
{
$dimension_table = $this->report_obj->get_table_by_alias('dimension');
$data['hosts'] = $this->report_obj->get_distinct_values($dimension_table, $data['hostname_field_name']);
// check
$data[$data['hostname_field_name']] = get_var($data['hostname_field_name']);
$this->report_obj->set_pivot_values('dimension-pivot-'.$data['hostname_field_name'], $data['hosts']);
}

// get custom fields for search form
foreach ($data['tables'] as $t) {
Expand All @@ -438,13 +449,20 @@ public function show_query() {

$_GET['table_fields'][] = get_var('plot_field');
$_GET['fact-checksum'] = get_var('checksum');
$_GET['fact-DIGEST'] = get_var('checksum');
$data['ajax_request_url'] = site_url() . '?action=api&output=json2&noheader=1&datasource=' . $data['datasource'] . '&' . $this->report_obj->get_search_uri();

$data['sample_field_name'] = $this->data_model->get_field_name('sample');
$data['hostname_field_name'] =$this->data_model->get_field_name('hostname');
$data['time_field_name'] =$this->data_model->get_field_name('time');
$data['fingerprint_field_name'] = $fingerprint_field_name;

$this->load->view("show_query", $data);
$view = "show_query";
if ($source_type == 'performance_schema')
{
$view = "show_query_perf_schema";
}
$this->load->view($view, $data);

// Show the history for this query
// just set some form fields and call report
Expand Down
19 changes: 11 additions & 8 deletions lib/AnemometerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,12 @@ public function get_query_by_checksum($checksum) {
public function get_query_samples($checksum, $limit = 1, $offset = 0) {
$checksum_field_name = $this->get_field_name('checksum');
$time_field_name = $this->get_field_name('time');
$sql = "SELECT * FROM `{$this->dimension_table}` WHERE `{$checksum_field_name}`='{$checksum}' ORDER BY `{$time_field_name}` DESC LIMIT {$limit} OFFSET {$offset}";
$table = $this->dimension_table;
if ($this->get_source_type() == 'performance_schema')
{
$table = $this->fact_table;
}
$sql = "SELECT * FROM `{$table}` WHERE `{$checksum_field_name}`='{$checksum}' ORDER BY `{$time_field_name}` DESC LIMIT {$limit} OFFSET {$offset}";
return $this->mysqli->query($sql);
}

Expand Down Expand Up @@ -414,13 +419,9 @@ public function get_table_status($query) {

public function get_field_name($type)
{
if (!isset($this->datasource_name))
{
throw new Exception("Cannot get report special field names without a datasource defined");
}
$source_type = $this->conf['datasources'][$this->datasource_name]['source_type'];

if (array_key_exists('special_field_names', $this->conf['reports'][$source_type]))
$source_type = $this->get_source_type();
if (array_key_exists('special_field_names', $this->conf['reports'][$source_type])
and array_key_exists($type, $this->conf['reports'][$source_type]['special_field_names']))
{
return $this->conf['reports'][$source_type]['special_field_names'][$type];
}
Expand All @@ -432,6 +433,8 @@ public function get_field_name($type)
return 'ts_min';
case 'hostname':
return 'hostname_max';
case 'fingerprint':
return 'fingerprint';
default:
return $type;
}
Expand Down
80 changes: 80 additions & 0 deletions views/show_query_perf_schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<p><center><h2><a href="<?php echo site_url()."?action=show_query&datasource={$datasource}&checksum={$checksum}"; ?>">Query <?php echo $checksum; ?></a></h2></center></p>

<div class="row">
<div class="span4 offset3"><strong>First Seen</strong>: <?php echo ( isset($row['FIRST_SEEN']) ? $row['FIRST_SEEN'] : $row['first_seen']); ?></div>
<div class="span4"><strong>Last Seen</strong>: <?php echo (isset($row['LAST_SEEN']) ? $row['LAST_SEEN'] : $row['last_seen']); ?></div>
</div>

<hr>

<div class="row">
<div class="span12">
<strong>Fingerprint</strong><br>
<pre class="prettyprint lang-sql"><?php echo $row[$fingerprint_field_name]; ?></pre>
</div>
</div>
<hr>


<?php if (isset($create_table) and $create_table != '') { ?>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThree">
Create Table Statements
</a>
</div>
<div id="collapseThree" class="accordion-body collapse">
<div class="accordion-inner">
<pre class="prettyprint lang-sql"><?php echo $create_table; ?></pre>
</div>
</div>
</div>
<?php } ?>

<?php if (isset($table_status) and $table_status != '') { ?>
<div class="accordion-group">
<div class="accordion-heading">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseFour">
Table Status
</a>
</div>
<div id="collapseFour" class="accordion-body collapse">
<div class="accordion-inner">
<pre><?php echo $table_status; ?></pre>
</div>
</div>
</div>

<?php } ?>
</div>
<!-- </div> -->
</div>
</div>

<p><center><h2>Current Statistics</h2></center>

<script>
$(function() {
// $("#dimension-ts_min_start").datetimepicker({ dateFormat: 'yy-mm-dd', timeFormat: 'hh:mm:ss' });
// $("#dimension-ts_min_end").datetimepicker({ dateFormat: 'yy-mm-dd', timeFormat: 'hh:mm:ss' });
$('.combobox').combobox();
prettyPrint();

<?php if ($source_type != 'performance_schema') { ?>

$('#graph').on('hidden', function () {
el = document.getElementById('graph-btn');
el.innerHTML = '<i class="icon-plus"></i> Show Graph Options'
});
$('#graph').on('show', function () {
el = document.getElementById('graph-btn');
el.innerHTML = '<i class="icon-minus"></i> Hide Graph Options'
});

show_graph = <?php echo get_var('show_form') ? 'true' : 'false' ?>;
if (show_graph) {
$("#graph-btn").trigger("click");
}
<?php } ?>
});
</script>

0 comments on commit 45104af

Please sign in to comment.