Mailing List Archive

Why my ajax request is too slow?
Hi,

I am using Trac-1.0.15 and I am using an ajax request to get a small
piece of data from server. I use ajax 'GET' method.
var all_data = {'opp_details':true, 'opp_id':$(this).val()};
$.ajax({
type:'GET',
//url:prefix+'/SalesProposalReport/',
url:'SalesProposalReport/',
data:all_data,
success:function(){
console.log("Ajax_over!");
},
});



Server side code in web_ui.py file,

# Special case for ajax
if ((path == "/SalesProposalReport/") and (req.method == 'GET')):
details =
self.get_opp_details(req.args.get('opp_id'))
data = {'message':'success', 'details':details}
self.respond(req, data)



the get_opp_details method is
def get_opp_details(self, opp_id):
with self.env.db_transaction as db:
cursor = db.cursor()
dtls_query = "select oppname from opportunity where
oppid='%s'"%(opp_id)
cursor.execute(dtls_query)
dtls = cursor.fetchall()
desc = cursor.description
if not desc:
return []
details = {}
if dtls:
for item in dtls:
for field, col in zip(item, desc):
details[col[0]] = str(field)

return details


And the respond method is

def respond(self, req, data, code=200):
json_data = json.dumps(data)
req.send_response(code)
req.send_header('Content-Type','application/json')
req.send_header('Content-Length',len(json_data))
req.write(json_data)
raise RequestDone


Any guess why my ajax is very slow? it takes 11 sec everytime to load a
single piece of data from server. How can I reduce this? Please help me.

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/trac-users/d55222b9-ab65-43b0-bf5a-4ce7b16d1deeo%40googlegroups.com.
Re: Why my ajax request is too slow? [ In reply to ]
On Thursday, July 9, 2020 at 12:35:34 AM UTC-7, Velu Narasimman wrote:
>
> Hi,
>
> I am using Trac-1.0.15 and I am using an ajax request to get a small
> piece of data from server. I use ajax 'GET' method.
> var all_data = {'opp_details':true, 'opp_id':$(this).val()};
> $.ajax({
> type:'GET',
> //url:prefix+'/SalesProposalReport/',
> url:'SalesProposalReport/',
> data:all_data,
> success:function(){
> console.log("Ajax_over!");
> },
> });
>
>
>
> Server side code in web_ui.py file,
>
> # Special case for ajax
> if ((path == "/SalesProposalReport/") and (req.method == 'GET')):
> details =
> self.get_opp_details(req.args.get('opp_id'))
> data = {'message':'success',
> 'details':details}
> self.respond(req, data)
>
>
>
> the get_opp_details method is
> def get_opp_details(self, opp_id):
> with self.env.db_transaction as db:
> cursor = db.cursor()
> dtls_query = "select oppname from opportunity where
> oppid='%s'"%(opp_id)
> cursor.execute(dtls_query)
> dtls = cursor.fetchall()
> desc = cursor.description
> if not desc:
> return []
> details = {}
> if dtls:
> for item in dtls:
> for field, col in zip(item, desc):
> details[col[0]] = str(field)
>
> return details
>
>
> And the respond method is
>
> def respond(self, req, data, code=200):
> json_data = json.dumps(data)
> req.send_response(code)
> req.send_header('Content-Type','application/json')
> req.send_header('Content-Length',len(json_data))
> req.write(json_data)
> raise RequestDone
>
>
> Any guess why my ajax is very slow? it takes 11 sec everytime to load a
> single piece of data from server. How can I reduce this? Please help me.
>


Not sure off hand. I'd log the time spent in get_opp_details to rule that
out.

We advise against using string interpolation for SQL queries.
https://trac.edgewall.org/wiki/TracDev/DatabaseApi#Parameterpassing

What type of data is "oppname"?

from trac.db.api import get_column_names

def get_opp_details(self, opp_id):
with self.env.db_query as db:
cursor = db.cursor()
dtls_query = "select oppname from opportunity where oppid=%s"
cursor.execute(dtls_query, (opp_id,))
dtls = cursor.fetchall()
desc = get_column_names(cursor)
if not desc:
return []
details = {}
if dtls:
for item in dtls:
for field, col in zip(item, desc):
details[col[0]] = str(field)

return details

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/trac-users/8cc55bac-a4c2-4e83-b836-b481707c3ef3o%40googlegroups.com.
Re: Re: Why my ajax request is too slow? [ In reply to ]
On 7/9/2020 12:19 PM, RjOllos wrote:

> dtls_query = "select oppname from opportunity where oppid=%s"
> cursor.execute(dtls_query, (opp_id,))
> dtls = cursor.fetchall()

The obvious question is how many rows fetchall() returns, and how many
columns.

Dima

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/trac-users/abc1b64b-8184-a03a-d862-19304146d05f%40bmrb.wisc.edu.
Re: Re: Why my ajax request is too slow? [ In reply to ]
Hi,

There are 399 rows in that 'opportunity' table and query returns just one
column (399 rows and one column). The data type of the oppname field is
character varying(300). Does my ajax code complies coding standards? or is
there a different and convenient way to make ajax in Trac? If yes please
share me an example. It would help me a lot. I welcome your views on db
query and further optimisations too.

Thank you!

On Friday, July 10, 2020 at 3:10:58 AM UTC+5:30, dmaziuk wrote:
>
> On 7/9/2020 12:19 PM, RjOllos wrote:
>
> > dtls_query = "select oppname from opportunity where
> oppid=%s"
> > cursor.execute(dtls_query, (opp_id,))
> > dtls = cursor.fetchall()
>
> The obvious question is how many rows fetchall() returns, and how many
> columns.
>
> Dima
>

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/trac-users/aa333724-4d58-455a-8f46-12cec8d069beo%40googlegroups.com.
Re: Why my ajax request is too slow? [ In reply to ]
Hi,
Data type of the field oppname is varchar. Thanks for get_column_names.
And how do you measure time taken? any practical example please? I welcome
more performance optimisation techniques from you.

Thank you!

On Thursday, July 9, 2020 at 10:49:42 PM UTC+5:30, RjOllos wrote:
>
>
>
> On Thursday, July 9, 2020 at 12:35:34 AM UTC-7, Velu Narasimman wrote:
>>
>> Hi,
>>
>> I am using Trac-1.0.15 and I am using an ajax request to get a small
>> piece of data from server. I use ajax 'GET' method.
>> var all_data = {'opp_details':true, 'opp_id':$(this).val()};
>> $.ajax({
>> type:'GET',
>> //url:prefix+'/SalesProposalReport/',
>> url:'SalesProposalReport/',
>> data:all_data,
>> success:function(){
>> console.log("Ajax_over!");
>> },
>> });
>>
>>
>>
>> Server side code in web_ui.py file,
>>
>> # Special case for ajax
>> if ((path == "/SalesProposalReport/") and (req.method == 'GET')):
>> details =
>> self.get_opp_details(req.args.get('opp_id'))
>> data = {'message':'success',
>> 'details':details}
>> self.respond(req, data)
>>
>>
>>
>> the get_opp_details method is
>> def get_opp_details(self, opp_id):
>> with self.env.db_transaction as db:
>> cursor = db.cursor()
>> dtls_query = "select oppname from opportunity where
>> oppid='%s'"%(opp_id)
>> cursor.execute(dtls_query)
>> dtls = cursor.fetchall()
>> desc = cursor.description
>> if not desc:
>> return []
>> details = {}
>> if dtls:
>> for item in dtls:
>> for field, col in zip(item, desc):
>> details[col[0]] = str(field)
>>
>> return details
>>
>>
>> And the respond method is
>>
>> def respond(self, req, data, code=200):
>> json_data = json.dumps(data)
>> req.send_response(code)
>> req.send_header('Content-Type','application/json')
>> req.send_header('Content-Length',len(json_data))
>> req.write(json_data)
>> raise RequestDone
>>
>>
>> Any guess why my ajax is very slow? it takes 11 sec everytime to load a
>> single piece of data from server. How can I reduce this? Please help me.
>>
>
>
> Not sure off hand. I'd log the time spent in get_opp_details to rule that
> out.
>
> We advise against using string interpolation for SQL queries.
> https://trac.edgewall.org/wiki/TracDev/DatabaseApi#Parameterpassing
>
> What type of data is "oppname"?
>
> from trac.db.api import get_column_names
>
> def get_opp_details(self, opp_id):
> with self.env.db_query as db:
> cursor = db.cursor()
> dtls_query = "select oppname from opportunity where oppid=%s"
> cursor.execute(dtls_query, (opp_id,))
> dtls = cursor.fetchall()
> desc = get_column_names(cursor)
> if not desc:
> return []
> details = {}
> if dtls:
> for item in dtls:
> for field, col in zip(item, desc):
> details[col[0]] = str(field)
>
> return details
>

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/trac-users/d6b445bc-bc27-4292-8c47-67d06e3d8106o%40googlegroups.com.
Re: Why my ajax request is too slow? [ In reply to ]
On Sunday, July 12, 2020 at 9:14:09 PM UTC-7, Velu Narasimman wrote:
>
> Hi,
> Data type of the field oppname is varchar. Thanks for get_column_names.
> And how do you measure time taken? any practical example please? I welcome
> more performance optimisation techniques from you.
>
> Thank you!
>

Something like this:

import time
start = time.time()

<< body of get_opp_details method >>

end = time.time()
self.log.debug("Elapsed: %.3f seconds" % (end - start))

--
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/trac-users/682109bd-2e59-4056-abb4-69891de41e89o%40googlegroups.com.