Python / App Engine app that compares changes efficiently

Status
Not open for further replies.

fyleow

Ars Scholae Palatinae
641
I've got an app engine app that compares values which are scrapped hourly and I'm running into high CPU usage. I'm doing a lot of calculation on the page request so I think I'm not approaching it the right way and I'm looking for suggestions.

For example, suppose I scrape Amazon's prices of electronic products on an hourly basis. I want to have a page where a user can enter an hour and it will compare the change between the current price and the price of the product X hours ago.

On the page load the following happens:

User enters 5 hours
1) curr_prices = 'select * from products where time = now'
2) prev_prices = 'select * from products where time = 5 hours ago'
3)
Code:
for curr in curr_prices
       for prev in prev_prices
           if curr.product_name = prev.product_name
              curr.price_diff = curr.price - prev.price
Is there a more efficient way to do this quicker than O(n^2)?
I can set a cron task to do this once an hour and store the values so that a client request just pulls precalculated values out the db, but since I allow the user to enter an arbitrary hour I would need to store multiple datasets (possibly of the most used values)
 

bk0

Ars Tribunus Militum
1,801
Why not let the RDBMS calc the diff from previous time each update, then you can do a simple aggregate calc in your SELECT upon page load, rather than doing it in Python.

Something like this, off of the top of my head (in sqlite SQL, but should be close enough I guess):

Code:
SELECT sum(diff) FROM products WHERE dt <= datetime("now", "-5 hours")

As long as your diff calculation stores positive/negative integers accurately it should all work.
 

fyleow

Ars Scholae Palatinae
641
bk0":1qrvzb45 said:
Why not let the RDBMS calc the diff from previous time each update, then you can do a simple aggregate calc in your SELECT upon page load, rather than doing it in Python.

Something like this, off of the top of my head (in sqlite SQL, but should be close enough I guess):

Code:
SELECT sum(diff) FROM products WHERE dt <= datetime("now", "-5 hours")

As long as your diff calculation stores positive/negative integers accurately it should all work.

Unfortunately I'm using Google App Engine and the datastore does not allow such calculations. It's pretty limited in what it will do.

After experimenting more I think I'm going to switch to something like Django + a regular database. Some of the limitations I can live with but some are really getting in the way. For example only one property per query can have inequality filters.

So you can't do this:

Code:
select * from products where
    order_date > :1 and
    price > :2

I suppose you could just perform the query with one inequality filter and do the rest of the filtering in python on the larger dataset.
 

levine

Ars Tribunus Militum
1,986
The idea in the GAE DataStore is to do all your calculations -- even ones you may not need -- once, when the values are first loaded. In your case, this might mean every time you scrape a price, you perform as part of the insert the hourly diffs back 24h, then 4-hour diffs back 48h, and so on. Then on page requests you simply poll for the stuff that's been pre-calculated. There's a little philosophical tutorial about this shift in thinking somewhere early on in the GAE documentation.
 
Status
Not open for further replies.