Skip to content

Commit

Permalink
Remove the Sql::getHtmlForProfilingChart method
Browse files Browse the repository at this point in the history
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
  • Loading branch information
MauricioFauth committed Aug 25, 2020
1 parent 99abf3a commit e7e9bd7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 100 deletions.
116 changes: 36 additions & 80 deletions libraries/classes/Sql.php
Expand Up @@ -279,86 +279,38 @@ public function getHtmlForRelationalColumnDropdown($db, $table, $column, $curr_v
return $dropdown;
}

/**
* Get the HTML for the profiling table and accompanying chart if profiling is set.
* Otherwise returns null
*
* @param string|null $urlQuery url query
* @param string $database current database
* @param array $profilingResults array containing the profiling info
*
* @return string html for the profiling table and chart
*/
private function getHtmlForProfilingChart($urlQuery, $database, $profilingResults): string
/** @return array<string, int|array> */
private function getDetailedProfilingStats(array $profilingResults): array
{
if (! empty($profilingResults)) {
$urlQuery = $urlQuery ?? Url::getCommon(['db' => $database]);

[
$detailedTable,
$chartJson,
$profilingStats,
] = $this->analyzeAndGetTableHtmlForProfilingResults($profilingResults);

return $this->template->render('sql/profiling_chart', [
'url_query' => $urlQuery,
'detailed_table' => $detailedTable,
'states' => $profilingStats['states'],
'total_time' => $profilingStats['total_time'],
'chart_json' => $chartJson,
]);
}

return '';
}

/**
* Function to get HTML for detailed profiling results table, profiling stats, and
* $chart_json for displaying the chart.
*
* @param array $profiling_results profiling results
*
* @return array
*/
private function analyzeAndGetTableHtmlForProfilingResults(
$profiling_results
): array {
$profiling_stats = [
$profiling = [
'total_time' => 0,
'states' => [],
'chart' => [],
'profile' => [],
];
$chart_json = [];
$i = 1;
$table = '';
foreach ($profiling_results as $one_result) {
if (! isset($profiling_stats['states'][ucwords($one_result['Status'])])) {
$profiling_stats['states'][ucwords($one_result['Status'])] = [
'total_time' => $one_result['Duration'],
'calls' => 1,
];
}
$profiling_stats['total_time'] += $one_result['Duration'];

$table .= $this->template->render('sql/detailed_table', [
'index' => $i++,
'status' => $one_result['Status'],
'duration' => $one_result['Duration'],
]);
foreach ($profilingResults as $oneResult) {
$status = ucwords($oneResult['Status']);
$profiling['total_time'] += $oneResult['Duration'];
$profiling['profile'][] = [
'status' => $status,
'duration' => Util::formatNumber($oneResult['Duration'], 3, 1),
'duration_raw' => $oneResult['Duration'],
];

if (isset($chart_json[ucwords($one_result['Status'])])) {
$chart_json[ucwords($one_result['Status'])]
+= $one_result['Duration'];
if (! isset($profiling['states'][$status])) {
$profiling['states'][$status] = [
'total_time' => $oneResult['Duration'],
'calls' => 1,
];
$profiling['chart'][$status] = $oneResult['Duration'];
} else {
$chart_json[ucwords($one_result['Status'])]
= $one_result['Duration'];
$profiling['states'][$status]['calls']++;
$profiling['chart'][$status] += $oneResult['Duration'];
}
}

return [
$table,
$chart_json,
$profiling_stats,
];
return $profiling;
}

/**
Expand Down Expand Up @@ -1247,11 +1199,12 @@ private function getQueryResponseForNoResultsReturned(
$header = $response->getHeader();
$scripts = $header->getScripts();
$scripts->addFile('sql.js');
$profilingChart = $this->getHtmlForProfilingChart(
$url_query,
$db,
$profiling_results
);

$profiling = $this->getDetailedProfilingStats($profiling_results);
$profilingChart = $this->template->render('sql/profiling_chart', [
'url_query' => $url_query ?? Url::getCommon(['db' => $db]),
'profiling' => $profiling,
]);
}

$bookmark = '';
Expand Down Expand Up @@ -1748,11 +1701,14 @@ private function getQueryResponseForResultsReturned(
$disp_message ?? null
);

$profilingChartHtml = $this->getHtmlForProfilingChart(
$url_query,
$db,
$profiling_results ?? []
);
$profilingChartHtml = '';
if (! empty($profiling_results)) {
$profiling = $this->getDetailedProfilingStats($profiling_results);
$profilingChartHtml = $this->template->render('sql/profiling_chart', [
'url_query' => $url_query ?? Url::getCommon(['db' => $db]),
'profiling' => $profiling,
]);
}

$missingUniqueColumnMessage = $this->getMessageIfMissingColumnIndex(
$table,
Expand Down
8 changes: 0 additions & 8 deletions templates/sql/detailed_table.twig

This file was deleted.

33 changes: 21 additions & 12 deletions templates/sql/profiling_chart.twig
Expand Up @@ -20,7 +20,16 @@
</tr>
</thead>
<tbody>
{{ detailed_table|raw }}
{% for state in profiling.profile %}
<tr>
<td>{{ loop.index }}</td>
<td>{{ state.status }}</td>
<td class="text-right">
{{ state.duration }}s
<span class="rawvalue hide">{{ state.duration_raw }}</span>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
Expand Down Expand Up @@ -53,21 +62,21 @@
</tr>
</thead>
<tbody>
{% for name, stats in states %}
{% for name, stats in profiling.states %}
<tr>
<td>{{ name }}</td>
<td align="right">
{{ format_number(stats['total_time'], 3, 1) }}s
<span class="rawvalue hide">{{ stats['total_time'] }}</span>
<td class="text-right">
{{ format_number(stats.total_time, 3, 1) }}s
<span class="rawvalue hide">{{ stats.total_time }}</span>
</td>
<td align="right">
{{ format_number(100 * (stats['total_time'] / total_time), 0, 2) }}%
<td class="text-right">
{{ format_number(100 * (stats.total_time / profiling.total_time), 0, 2) }}%
</td>
<td align="right">{{ stats['calls'] }}</td>
<td align="right">
{{ format_number(stats['total_time'] / stats['calls'], 3, 1) }}s
<td class="text-right">{{ stats.calls }}</td>
<td class="text-right">
{{ format_number(stats.total_time / stats.calls, 3, 1) }}s
<span class="rawvalue hide">
{{ (stats['total_time'] / stats['calls'])|number_format(8, '.', '') }}
{{ (stats.total_time / stats.calls)|number_format(8, '.', '') }}
</span>
</td>
</tr>
Expand All @@ -82,7 +91,7 @@
<div class='clearfloat'></div>

<div id="profilingChartData" class="hide">
{{ chart_json|json_encode() }}
{{- profiling.chart|json_encode() -}}
</div>
<div id="profilingchart" class="hide"></div>

Expand Down

0 comments on commit e7e9bd7

Please sign in to comment.