Zend_Db_Profiler not logging db connection time? - zend-framework

Following the sample code on http://framework.zend.com/manual/en/zend.db.profiler.html I have set up db profiling of my Zend Framework app.
application.ini:
db.profiler.enabled = true
View Helper:
$totalTime = $profiler->getTotalElapsedSecs();
$queryCount = $profiler->getTotalNumQueries();
$longestTime = 0;
$longestQuery = null;
foreach ($profiler->getQueryProfiles() as $query) {
if ($query->getElapsedSecs() > $longestTime) {
$longestTime = $query->getElapsedSecs();
$longestQuery = $query->getQuery();
}
}
echo 'Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "\n";
echo 'Average query length: ' . $totalTime / $queryCount . ' seconds' . "\n";
echo 'Queries per second: ' . $queryCount / $totalTime . "\n";
echo 'Longest query length: ' . $longestTime . "\n";
echo "Longest query: \n" . $longestQuery . "\n";
It works fine for select/insert/update/delete queries.
But I cannot find anyway to get the profiler to show the time taken to initiate the actual db connection, despite the documenation implying that it does log this.
I suspect that Zend_Db simply does not log the connection to the db with the profiler.
Does anyone know what is going on here?
I am using the Oracle database adapter, and ZF 1.10.1
UPDATE:
I understand it is possible to filter the profiler output, such that it will only show certain query types, e.g. select/insert/update. There also appears to be an option to filter just the connection records:
$profiler->setFilterQueryType(Zend_Db_Profiler::CONNECT);
However, my problem is that the profiler is not logging the connections to begin with, so this filter does nothing.
I know this for a fact, because if I print the profiler object, it contains data for many different queries - but no data for the connection queries:
print_r($profiler);
//output
Zend_Db_Profiler Object
(
[_queryProfiles:protected] => Array
(
[0] => Zend_Db_Profiler_Query Object
(
[_query:protected] => select * from table1
[_queryType:protected] => 32
[_startedMicrotime:protected] => 1268104035.3465
[_endedMicrotime:protected] => 1268104035.3855
[_boundParams:protected] => Array
(
)
)
[1] => Zend_Db_Profiler_Query Object
(
[_query:protected] => select * from table2
[_queryType:protected] => 32
[_startedMicrotime:protected] => 1268104035.3882
[_endedMicrotime:protected] => 1268104035.419
[_boundParams:protected] => Array
(
)
)
)
[_enabled:protected] => 1
[_filterElapsedSecs:protected] =>
[_filterTypes:protected] =>
)
Am I doing something wrong - or has logging of connections just not been added to Zend Framework yet?

The profiler 'bundles' connection and other operations in with the general queries.
There's three ways you might examine the connection specifically:
Set a filter on the profiler during setup:
$profiler->setFilterQueryType(**Zend_Db_Profiler::CONNECT**);
Then the resultant profiles will only include 'connect' operations.
Specify a query type when you retrieve the Query Profiles:
$profiles = $profiler->getQueryProfiles(**Zend_Db_Profiler::CONNECT**);
Examine the query objects directly during the iteration:
foreach($profiler->getQueryProfiles() as $query) {
if ($query->getQueryType() == Zend_Db_Profiler::CONNECT &&
$query->getElapsedSecs() > $longestConnectionTime) {
$longestConnectionTime = $query->getElapsedSecs();
}
}
You'll not find great detail in there, it's logged just as a 'connect' operation along with the time taken.

Related

How to get all table names from db and paginate them

Trying to paginate DB::select('SHOW TABLES')->paginate(1); but got error Call to a member function paginate() on array. How can I get all the tables and paginate them?
Since you can use \DB:select for your query, it will response the array instead of the collection. So, you can't make the pagination from it.
You can use LengthAwarePaginator for this case, please see the following code:
$sql .= 'SHOW TABLES LIMIT ' . $offset . ', ' . $limit;
$result = \DB::select(\DB::raw($sql));
$myPaginator = new \Illuminate\Pagination\LengthAwarePaginator($result, $data['count'], $limit, $page, ['path' => action('MyController#index')]);
$data['result'] = $result;

Instagram Real-time API duplicate requests

I have an issue where when I create a real-time subscription I get duplicate notifications from different Instagram IP addresses. I have it set up so that when I get a notification, I send a request for latest updates using the min_tag_id setting. I store that in my db to use it for the next request. I don't always get duplicates, but when I do, everything about the notification is the same (time, object,changed_aspect), except I can tell they are different from my debugging output which lists two almost identical requests... the only differing info being a different IP address and the REQUEST_TIME_FLOAT is different by about 1/10th of a second. They even have the same HTTP_X_HUB_SIGNATURE value.
My general algorithm is:
process_subscription_update($data){
# get old min_id
$min_tag_id = mysqli_fetch_object(mysqli_query($dbconnecti,sprintf("SELECT instagram_min_id+0 as instaid FROM xxxx WHERE xxxx=%d",$_GET['xxxx'])));
$min_id = $min_tag_id->instaid;
# make api call
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://api.instagram.com/v1/tags/'.$_GET['tag'].'/media/recent?client_id=xxxx&min_tag_id='.$min_id.($min_id==0?'&count=1':''));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$i = json_decode($result);
if ($min_id == $i->pagination->min_tag_id) { exit; }
# write new min_id to db
record_min_id($i->pagination->min_tag_id);
$data2 = $i->data;
foreach($data2 as $d) {
process_instagram($d);
}
// debugging output: ****************
$file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'instagram/updates.txt');
$foo = "\n";
foreach($_SERVER as $key_name => $key_value) {
$foo .= $key_name . " = " . $key_value . "\n";
}
$fulldata = $file . "\n\n\n" . $result . "\n min_id = " . $min_id . $foo;
$fulldata .= "\nTIME:".$data[0]->time;
$fulldata .= "\nOBJECT:".$data[0]->object;
$fulldata .= "\nCHANGED_ASPECT:".$data[0]->changed_aspect;
file_put_contents($_SERVER['DOCUMENT_ROOT'].'instagram/updates.txt', $fulldata);
// end debugging output *************
}
I'd like to avoid checking if the instagram message id already exists in my db within the process_instagram function, and with the duplicates only coming 1/10th of a second apart, I don't know if that would work anyway.
Anybody else experience this and/or have a solution?
I solved this. I don't think there is anything I can do about receiving the duplicate notifications. So, when writing the Instagram to my db, I have a field for the Instagram id and put a unique constraint on the field. After doing the mysqli INSERT, I check to see if the errno = 1062, and if it does, I exit.
mysqli_query($dbconnecti,"INSERT INTO xxx (foo, etc, instagram_id ...")
if ($dbconnecti->errno==1062) { exit; }
...
// more script runs here if we don't have a duplicate.

PostgreSQL Query Cache - closing connection unexpectedly

<?php
$c = pg_connect( $connectionString, PGSQL_CONNECT_FORCE_NEW );
$queries = array (
"SELECT COUNT(*) AS data FROM articles",
"SELECT * FROM posts WHERE visible = TRUE",
"SELECT * FROM countries WHERE visible = FALSE",
"SELECT * FROM types WHERE visible = TRUE"
);
foreach ( $queries as $query ) {
$res = #pg_query( $c, $query );
if ( empty( $res ) ) {
echo "[ERR] " . pg_last_error( $c ) . "\n";
} else {
echo "[OK]\n";
}
}
The snippet of code above is generating for the first time this:
[OK]
[OK]
[OK]
[OK]
but for the second time this:
[OK]
[OK]
[ERR] server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
[ERR] server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
This means that some cached queries cause a problem. We tried to change the order of quires, but it didn't help. Only simple queries like SELECT 1+8, which are probably not cached always run well.
Similar problem can be simulated using psql and probably any other language driver (not only PHP).
All troubles came up when we installed PostgreSQL Query Cache.
Should the query cache be configured somehow not behave this way?
Our config files are here:
http://pastebin.com/g2dBjba0 – pcqd_hba.conf
http://pastebin.com/X9Y3zrjx – pcqd.conf
Chances are you have a bug in the PostgreSQL Query Cache which is causing segfaults in the backend. Your best solution is to uninstall this add-on.

SeleniumRC/Perl dynamic XPath selector not working

This is more a question for XPath syntax than anything else.
I have multiple product pages on a site that have multiple products on each product pages. Each product has a unique ID for the add-to-cart button. I'm trying to return all of the unique ID's so that I can add a couple of the products to the bag. Searching with XPath seems to be the correct solution for this. I have the following code for querying the HTML with XPath and returning the unique ID's:
$XPATH_COUNT = $sel->get_xpath_count("//div[\#class='quick-info-link']/a");
#my_array;
$my_array[0] = $sel->get_attribute("//div[\#class='quick-info-link']/a/\#id");
print $my_array[0];
$count = 0;
while( $count < $XPATH_COUNT )
{
$arrayCount=0;
$a = "//";
foreach( #my_array )
{
$tmp = "a[\#id!='" . $my_array[$arrayCount] . "' and ";
$b .= $tmp;
$d .= "]";
$arrayCount++;
}
$c = "img[\#alt='Quick Shop']";
$e = $c . $d . "/\#id";
$xpath_query = $a . $b . $e;
$my_array[$count] = $sel->get_attribute($xpath_query);
$count++;
}
The output of the first run of this is an XPath query that looks like this:
//a[#id!='quickview-link-PROD7029' and img[#alt='Quick Shop']]/#id
Which correctly returns quickview-link-PROD6945. The second run produces this:
//a[#id!='quickview-link-PROD7029' and a[#id!='quickview-link-PROD6945' and img[#alt='Quick Shop']]]/#id
Which throws an error in my SeleniumRC terminal window of ERROR: Element [..xpath query..] not found on session.
I am aware of the possible use of indexes (i.e. adding an [i] to the end of the XPath query) to access elements on the page, however this isn't something that has worked for me in Selenium.
Any help would be great. Thanks for your time,
Steve
//a[#id!='quickview-link-PROD7029'
and a[#id!='quickview-link-PROD6945' and
img[#alt='Quick Shop']
]
]/#id
Which throws an error in my SeleniumRC
terminal window of ERROR: Element
[..xpath query..] not found on session
It would greatly help if you provide the XML document on which the XPath expression is applied and explain which node(s) you want to select.
Without this necessary information:
The most obvious reason for this problem is that the above expression is looking for a elements that have an a child with some property.
Usually an a element doesn't have any a children.
What you really want is something like:
//a[#id != 'quickview-link-PROD7029'
and
#id != 'quickview-link-PROD6945' and img[#alt='Quick Shop']
]/#id
This can be simplified a bit:
//a[img[#alt='Quick Shop']/#id
[not(. = 'quickview-link-PROD7029'
or
. = 'quickview-link-PROD6945'
)
]

Zend DB Framework examine query for an update

So you can use something like this:
$query = $db->select();
$query->from('pages', array('url'));
echo $query->__toString();
to examine the sql that the Zend Db Framework is going to use for that SELECT query. Is there an equivilent way to view the SQL for an update?
$data = array(
'content' => stripslashes(htmlspecialchars_decode($content))
);
$n = $db->update('pages', $data, "url = '".$content."'");
??
Use Zend_Db_Profiler to capture and report SQL statements:
$db->getProfiler()->setEnabled(true);
$db->update( ... );
print $db->getProfiler()->getLastQueryProfile()->getQuery();
print_r($db->getProfiler()->getLastQueryProfile()->getQueryParams());
$db->getProfiler()->setEnabled(false);
Remember to turn the profiler off if you don't need it! I talked to one fellow who thought he had a memory leak, but it was the profiler instantiating a few PHP objects for each of the millions of SQL queries he was running.
PS: You should use quoteInto() in that query:
$n = $db->update('pages', $data, $db->quoteInto("url = ?", $content));
No, not directly, since Zend Framework builds and executes the SQL inside the adapter method Zend_Db_Adapter_Abstract::update:
/**
* Updates table rows with specified data based on a WHERE clause.
*
* #param mixed $table The table to update.
* #param array $bind Column-value pairs.
* #param mixed $where UPDATE WHERE clause(s).
* #return int The number of affected rows.
*/
public function update($table, array $bind, $where = '')
{
/**
* Build "col = ?" pairs for the statement,
* except for Zend_Db_Expr which is treated literally.
*/
$set = array();
foreach ($bind as $col => $val) {
if ($val instanceof Zend_Db_Expr) {
$val = $val->__toString();
unset($bind[$col]);
} else {
$val = '?';
}
$set[] = $this->quoteIdentifier($col, true) . ' = ' . $val;
}
$where = $this->_whereExpr($where);
/**
* Build the UPDATE statement
*/
$sql = "UPDATE "
. $this->quoteIdentifier($table, true)
. ' SET ' . implode(', ', $set)
. (($where) ? " WHERE $where" : '');
/**
* Execute the statement and return the number of affected rows
*/
$stmt = $this->query($sql, array_values($bind));
$result = $stmt->rowCount();
return $result;
}
You can, temporarily, insert a var_dump and exit inside this method to inspect the sql to ensure that it is correct:
/**
* Build the UPDATE statement
*/
$sql = "UPDATE "
. $this->quoteIdentifier($table, true)
. ' SET ' . implode(', ', $set)
. (($where) ? " WHERE $where" : '');
var_dump($sql); exit;
I quess another way is to log the actual SQL query, rather than changing the ZF library code, by combining the profiler data.
$db->getProfiler()->setEnabled(true);
$db->update( ... );
$query = $db->getProfiler()->getLastQueryProfile()->getQuery();
$queryParams = $db->getProfiler()->getLastQueryProfile()->getQueryParams();
$logger->log('SQL: ' . $db->quoteInto($query, $queryParams), Zend_Log::DEBUG);
$db->getProfiler()->setEnabled(false);
Recently came across this looking for a way to debug a zend_db_statement. If anyone else comes across this with the same search, you can use the following function.
Just replace "self::getDefaultAdapter()" with your method of getting a DB connection or adapter.
/**
* replace any named parameters with placeholders
* #param string $sql sql string with placeholders, e.g. :theKey
* #param array $bind array keyed on placeholders, e.g. array('theKey', 'THEVALUE')
*
* #return String sql statement with the placeholders replaced
*/
public static function debugNamedParamsSql($sql, array $bind) {
$sqlDebug = $sql;
foreach($bind as $needle => $replace) {
$sqlDebug = str_replace(
':' . $needle,
self::getDefaultAdapter()->quote($replace),
$sqlDebug
);
}
return $sqlDebug;
}