Skip to content

Commit

Permalink
Update SugarModules/modules/Asterisk/include/callListener.php
Browse files Browse the repository at this point in the history
  • Loading branch information
superles committed May 13, 2012
1 parent 0151225 commit 6e24e2d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions SugarModules/modules/Asterisk/include/callListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,11 @@
//$sqlReplace= "REGEXP '%s$' = 1";

// TODO fix the join so that account is optional... I think just add INNER
$selectPortion = "SELECT c.id as contact_id, first_name, last_name,phone_work, phone_home, phone_mobile, phone_other, a.name as account_name, account_id "
$selectPortion = "SELECT c.id as contact_id, first_name, last_name,phone_work, phone_home, phone_mobile, phone_other, a.name as account_name, account_id "
. "FROM contacts c left join accounts_contacts ac on (c.id=ac.contact_id) left join accounts a on (ac.account_id=a.id) ";

if( $row['contact_id'] ) {
$wherePortion = " WHERE c.id='{$row['contact_id']}' and c.deleted='0' and ac.deleted='0' and a.deleted='0'";
$wherePortion = " WHERE c.id='{$row['contact_id']}' and c.deleted='0' and (ac.deleted='0' or ac.deleted is null) and (a.deleted='0' or a.deleted is null)";
// log_entry("Quick WHERE $selectPortion $wherePortion\n", "c:\callListenerLog.txt");

}
Expand All @@ -198,7 +198,7 @@
$wherePortion .= sprintf($sqlReplace, "phone_home", $phoneToFind) . " OR ";
$wherePortion .= sprintf($sqlReplace, "phone_other", $phoneToFind) . " OR ";
$wherePortion .= sprintf($sqlReplace, "assistant_phone", $phoneToFind) . " OR ";
$wherePortion .= sprintf($sqlReplace, "phone_mobile", $phoneToFind) . ") and c.deleted='0' and ac.deleted='0' and a.deleted='0'";
$wherePortion .= sprintf($sqlReplace, "phone_mobile", $phoneToFind) . ") and c.deleted='0' and (ac.deleted='0' or ac.deleted is null) and (a.deleted='0' or a.deleted is null)";
}

$queryContact = $selectPortion . $wherePortion;
Expand Down

4 comments on commit 6e24e2d

@Guard1an
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to add the joined table conditions to the WHERE clause to make the LEFT JOIN work.
You have just to add the (ac.deleted='0') condition to ON clause.
replace
FROM contacts c left join accounts_contacts ac on (c.id=ac.contact_id) left join accounts a on (ac.account_id=a.id)
by
FROM contacts c
LEFT JOIN accounts_contacts ac
ON (c.id=ac.contact_id) AND ac.deleted='0'
LEFT JOIN accounts a
ON (ac.account_id=a.id) AND a.deleted='0'

and replace
$wherePortion = " WHERE c.id='{$row['contact_id']}' and c.deleted='0' and (ac.deleted='0' or ac.deleted is null) and (a.deleted='0' or a.deleted is null)";
by
$wherePortion = " WHERE c.id='{$row['contact_id']}' and c.deleted='0'";

@blak3r
Copy link
Owner

@blak3r blak3r commented on 6e24e2d Jun 6, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Guard1an Thanks so much!!!

That makes total sense that you could do that but I'm not very experienced with SQL so I didn't know! I'm traveling for the next 2 weeks so I will incorporate your fix then.

Since you seem to be great with databases... any chance you could take a look at optimizing a query for me? I explain the issue in detail here:
https://github.com/blak3r/yaai/wiki/Project-TODO-List#wiki-Issue_1_Optimize_callListenerphp

The answer is probably as simple as index such and such column. I just haven't gotten around to learning about the MYSQL EXPLAIN command.

Thanks again for your contribution.

@Guard1an
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right,
Adding indexes for the fields being searched should increase the performance.

I would rather recoment to optimize the table structure: use ENUM instead of VARCHAR for *state fields and add appropriate constants in Php.

You can check the result of SELECT * FROM asterisk_log PROCEDURE ANALYSE(); for further details.

Regarding the callListener.php performance in general, I would recoment to implement SQL query result caching in PHP.
We can remove the 'channel' condition from the query or replace it by LIKE 'SIP/%' to fetch the result for all extensions, cache the serialized array in PHP where keys equal to extension.

//Pseudocode
$cachableLogByExtensions = array();
while ($row = mysql_fetch_assoc($logQuery)){
    $matches = array();
    if (preg_match('/SIP\/(\d+)-.*/', $row[''], $matches)){
        $extension = $matches[1];
        if (!isset($cachableLogByExtensions[$extension])){
            $cachableLogByExtensions[$extension] = array();
        }
        $cachableLogByExtensions[$extension] []= $row;
}
$cache->save('asterisk_log', $cachableLogByExtensions);

@blak3r
Copy link
Owner

@blak3r blak3r commented on 6e24e2d Jun 18, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather recoment to optimize the table structure: use ENUM instead of VARCHAR for *state fields and add appropriate constants in Php.

I believe this would be fairly difficult. I think the state names vary slightly between versions.

I like your query cache idea. Any chance you could implement it and provide a pull request?

Please sign in to comment.