Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
d5bc3c70
Commit
d5bc3c70
authored
Dec 30, 2013
by
Mark
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improved database panel in debug module
parent
db11cfc5
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
221 additions
and
54 deletions
+221
-54
Exact.php
extensions/yii/debug/components/search/matches/Exact.php
+1
-1
Db.php
extensions/yii/debug/models/search/Db.php
+75
-0
DbPanel.php
extensions/yii/debug/panels/DbPanel.php
+82
-52
LogPanel.php
extensions/yii/debug/panels/LogPanel.php
+1
-1
detail.php
extensions/yii/debug/views/default/panels/db/detail.php
+54
-0
summary.php
extensions/yii/debug/views/default/panels/db/summary.php
+8
-0
No files found.
extensions/yii/debug/components/search/matches/Exact.php
View file @
d5bc3c70
...
...
@@ -29,7 +29,7 @@ class Exact extends Base
if
(
!
$this
->
partial
)
{
return
(
mb_strtolower
(
$this
->
value
,
'utf8'
)
==
mb_strtolower
(
$value
,
'utf8'
));
}
else
{
return
(
mb_strpos
(
$value
,
$this
->
value
)
!==
false
);
return
(
mb_strpos
(
mb_strtolower
(
$value
,
'utf8'
),
mb_strtolower
(
$this
->
value
,
'utf8'
)
)
!==
false
);
}
}
...
...
extensions/yii/debug/models/search/Db.php
0 → 100644
View file @
d5bc3c70
<?php
namespace
yii\debug\models\search
;
use
yii\data\ArrayDataProvider
;
use
yii\debug\components\search\Filter
;
/**
* Db represents the model behind the search form about current request database queries.
*/
class
Db
extends
Base
{
/**
* @var string type attribute input search value
*/
public
$type
;
/**
* @var integer query attribute input search value
*/
public
$query
;
public
function
rules
()
{
return
[
[[
'type'
,
'query'
],
'safe'
],
];
}
/**
* @inheritdoc
*/
public
function
attributeLabels
()
{
return
[
'type'
=>
'Type'
,
'query'
=>
'Query'
,
];
}
/**
* Returns data provider with filled models. Filter applied if needed.
* @param array $params
* @param array $models
* @return \yii\data\ArrayDataProvider
*/
public
function
search
(
$params
,
$models
)
{
$dataProvider
=
new
ArrayDataProvider
([
'allModels'
=>
$models
,
'pagination'
=>
[
'pageSize'
=>
10
,
],
'sort'
=>
[
'attributes'
=>
[
'duration'
,
'type'
,
'query'
],
'defaultOrder'
=>
[
'duration'
=>
SORT_DESC
,
],
],
]);
if
(
!
(
$this
->
load
(
$params
)
&&
$this
->
validate
()))
{
return
$dataProvider
;
}
$filter
=
new
Filter
();
$this
->
addCondition
(
$filter
,
'type'
,
true
);
$this
->
addCondition
(
$filter
,
'query'
,
true
);
$dataProvider
->
allModels
=
$filter
->
filter
(
$models
);
return
$dataProvider
;
}
}
extensions/yii/debug/panels/DbPanel.php
View file @
d5bc3c70
...
...
@@ -7,10 +7,10 @@
namespace
yii\debug\panels
;
use
Yii
;
use
yii\debug\Panel
;
use
yii\helpers\ArrayHelper
;
use
yii\log\Logger
;
use
yii\
helpers\Html
;
use
yii\
debug\models\search\Db
;
/**
* Debugger panel that collects and displays database queries performed.
...
...
@@ -20,6 +20,17 @@ use yii\helpers\Html;
*/
class
DbPanel
extends
Panel
{
/**
* @var array db queries info extracted to array as models, to use with data provider.
*/
private
$_models
;
/**
* @var array current database request timings
*/
private
$_timings
;
public
function
getName
()
{
return
'Database'
;
...
...
@@ -29,61 +40,27 @@ class DbPanel extends Panel
{
$timings
=
$this
->
calculateTimings
();
$queryCount
=
count
(
$timings
);
$queryTime
=
0
;
foreach
(
$timings
as
$timing
)
{
$queryTime
+=
$timing
[
3
];
}
$queryTime
=
number_format
(
$queryTime
*
1000
)
.
' ms'
;
$url
=
$this
->
getUrl
();
$output
=
<<<EOD
<div class="yii-debug-toolbar-block">
<a href="$url" title="Executed $queryCount database queries which took $queryTime.">
DB <span class="label">$queryCount</span> <span class="label">$queryTime</span>
</a>
</div>
EOD;
return
$queryCount
>
0
?
$output
:
''
;
$queryTime
=
number_format
(
$this
->
getTotalQueryTime
(
$timings
)
*
1000
)
.
' ms'
;
return
Yii
::
$app
->
view
->
render
(
'panels/db/summary'
,[
'timings'
=>
$this
->
calculateTimings
(),
'panel'
=>
$this
,
'queryCount'
=>
$queryCount
,
'queryTime'
=>
$queryTime
,
]);
}
public
function
getDetail
()
{
$timings
=
$this
->
calculateTimings
();
ArrayHelper
::
multisort
(
$timings
,
3
,
SORT_DESC
);
$rows
=
[];
foreach
(
$timings
as
$timing
)
{
$duration
=
sprintf
(
'%.1f ms'
,
$timing
[
3
]
*
1000
);
$procedure
=
Html
::
encode
(
$timing
[
1
]);
$traces
=
$timing
[
4
];
if
(
!
empty
(
$traces
))
{
$procedure
.=
Html
::
ul
(
$traces
,
[
'class'
=>
'trace'
,
'item'
=>
function
(
$trace
)
{
return
"<li>
{
$trace
[
'file'
]
}
(
{
$trace
[
'line'
]
}
)</li>"
;
},
]);
}
$rows
[]
=
"<tr><td style=
\"
width: 80px;
\"
>
$duration
</td><td>
$procedure
</td>"
;
}
$rows
=
implode
(
"
\n
"
,
$rows
);
return
<<<EOD
<h1>Database Queries</h1>
<table class="table table-condensed table-bordered table-striped table-hover" style="table-layout: fixed;">
<thead>
<tr>
<th style="width: 80px;">Time</th>
<th>Query</th>
</tr>
</thead>
<tbody>
$rows
</tbody>
</table>
EOD;
}
$searchModel
=
new
Db
();
$dataProvider
=
$searchModel
->
search
(
$_GET
,
$this
->
getModels
());
private
$_timings
;
return
Yii
::
$app
->
view
->
render
(
'panels/db/detail'
,[
'panel'
=>
$this
,
'dataProvider'
=>
$dataProvider
,
'searchModel'
=>
$searchModel
,
]);
}
protected
function
calculateTimings
()
{
...
...
@@ -120,4 +97,57 @@ EOD;
$messages
=
$target
->
filterMessages
(
$target
->
messages
,
Logger
::
LEVEL_PROFILE
,
[
'yii\db\Command::query'
,
'yii\db\Command::execute'
]);
return
[
'messages'
=>
$messages
];
}
/**
* Returns total queries time.
* @param array $timings
* @return integer total time
*/
protected
function
getTotalQueryTime
(
$timings
)
{
$queryTime
=
0
;
foreach
(
$timings
as
$timing
)
{
$queryTime
+=
$timing
[
3
];
}
return
$queryTime
;
}
/**
* Returns array of models that represents logs of the current request. Can be used with data providers,
* like yii\data\ArrayDataProvider.
* @return array models
*/
protected
function
getModels
()
{
if
(
$this
->
_models
===
null
||
$refresh
)
{
$this
->
_models
=
[];
$timings
=
$this
->
calculateTimings
();
foreach
(
$timings
as
$dbTiming
)
{
$this
->
_models
[]
=
[
'type'
=>
$this
->
detectQueryType
(
$dbTiming
[
1
]),
'query'
=>
$dbTiming
[
1
],
'duration'
=>
(
$dbTiming
[
3
]
*
1000
),
#in milliseconds
'trace'
=>
$dbTiming
[
4
],
];
}
}
return
$this
->
_models
;
}
/**
* Detects databse timing type. Detecting is produced through simple parsing to the first space|tab|new row.
* First word before space is timing type. If there is no such words, timing will have empty type.
* @param string $timing timing procedure string
* @return string query type select|insert|delete|etc
*/
protected
function
detectQueryType
(
$timing
)
{
$timing
=
ltrim
(
$timing
);
preg_match
(
'/^([a-zA-z]*)/'
,
$timing
,
$matches
);
return
count
(
$matches
)
?
$matches
[
0
]
:
''
;
}
}
extensions/yii/debug/panels/LogPanel.php
View file @
d5bc3c70
...
...
@@ -24,7 +24,7 @@ class LogPanel extends Panel
/**
* @var array log messages extracted to array as models, to use with data provider.
*/
private
$_models
;
private
$_models
;
public
function
getName
()
{
...
...
extensions/yii/debug/views/default/panels/db/detail.php
0 → 100644
View file @
d5bc3c70
<?php
use
yii\helpers\Html
;
use
yii\grid\GridView
;
?>
<h1>
Database Queries
</h1>
<?php
echo
GridView
::
widget
([
'dataProvider'
=>
$dataProvider
,
'id'
=>
'db-panel-detailed-grid'
,
'filterModel'
=>
$searchModel
,
'filterUrl'
=>
$panel
->
getUrl
(),
'columns'
=>
[
[
'class'
=>
'yii\grid\SerialColumn'
],
[
'attribute'
=>
'duration'
,
'value'
=>
function
(
$data
)
{
return
sprintf
(
'%.1f ms'
,
$data
[
'duration'
]);
},
],
[
'attribute'
=>
'type'
,
'value'
=>
function
(
$data
)
{
return
Html
::
encode
(
mb_strtoupper
(
$data
[
'type'
],
'utf8'
));
},
],
[
'attribute'
=>
'query'
,
'value'
=>
function
(
$data
)
{
$query
=
Html
::
encode
(
$data
[
'query'
]);
if
(
!
empty
(
$data
[
'trace'
]))
{
$query
.=
Html
::
ul
(
$data
[
'trace'
],
[
'class'
=>
'trace'
,
'item'
=>
function
(
$trace
)
{
return
"<li>
{
$trace
[
'file'
]
}
(
{
$trace
[
'line'
]
}
)</li>"
;
},
]);
}
return
$query
;
},
'format'
=>
'html'
,
'options'
=>
[
'width'
=>
'70%'
,
],
]
],
]);
?>
\ No newline at end of file
extensions/yii/debug/views/default/panels/db/summary.php
0 → 100644
View file @
d5bc3c70
<?php
if
(
$queryCount
)
:
?>
<div
class=
"yii-debug-toolbar-block"
>
<a
href=
"$url"
title=
"Executed
<?php
echo
$queryCount
;
?>
database queries which took
<?php
echo
$queryTime
;
?>
."
>
DB
<span
class=
"label"
>
<?php
echo
$queryCount
;
?>
</span>
<span
class=
"label"
>
<?php
echo
$queryTime
;
?>
</span>
</a>
</div>
<?php
endif
;
?>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment