ddsnowboard / FantasyStocks

A website where you play a game of fantasy stocks.
http://fantasystocks.herokuapp.com
Apache License 2.0
0 stars 1 forks source link

Ponder optimizing the stock price update logic #19

Closed ddsnowboard closed 8 years ago

ddsnowboard commented 8 years ago

They say that premature optimization is the root of all evil, but I thought it would be cool if I could use some async stuff or maybe concurrency to make the updating of the stocks faster, such that I can start on the next request while the last one is out. But maybe it's fine, and maybe it's more trouble than it's worth. But I should check on that.

ddsnowboard commented 8 years ago

I had an epiphany. [Epiphany joke]. So, I thought it would be cool to offload some of the stock loading to the clients by just making the price look to the user as some sort of spinning wheel until their computer sends and gets back an XHR and then it would be replaced with the price, as well as sending the price back to my server so that I have it. Thinking back on that, that would have been a security nightmare, but whatever. Then I realized that the same-origin policy forbids that, so I would have to call my server for the price. The thing is, I can do that. All I have to do is have a url that goes to Yahoo to get the price, just like now, and sends it back to you, while also tacitly saving it in the database for next time. What I really like about this system is that, since Django automatically pushes each separate request out to a separate process (or so it seems), this would accomplish the concurrency that I have been pondering without me actually having to write any concurrent code. So that could be a good idea.

I think what I would do specifically is make the template check if the stock's price is new enough to show. If it's not, instead of asking the server to go check, it would just put in place of the price some sort of waiting gif, along with giving the <div> or whatever that holds the price a special class, and put the symbol in the id. Then, in JavaScript, I would go looking for <div>s with that class, and for each of them, I would send an XHR to the server asking for the price of the stock, getting the symbol from the id. The server would use that to update itself too, then send it back to the client, who would then have an onResponse function, or whatever it's called, that would replace the spinning wheel with the price.

This might be pretty slick.

ddsnowboard commented 8 years ago

Got it in 840d9a88cd3f1e4114023f9d6e86253f2a86194c.

It's really, really slick. I need to do something to make the images not look so ridiculous, but once that's done, it's pretty much bulletproof, I think. It's really fast, looks cool, and removes a huge issue that existed before, the extreme slowness of the dashboard.

I need to do some visual stuff, and then I'm good.

EDIT: Also, I need to make it so that it remembers the prices such that it doesn't have to go get them again every time that you click on a tab that you've been to before. [DONE]

ddsnowboard commented 8 years ago

This is working pretty well, but it doesn't work if you are looking at the player page and a stock isn't loaded yet. I need to factor out the loading code into a script that follows around all the stock board instead of just sitting on the dashboard.

EDIT: Fixed in fb82be9d8879c7287bb92a1b2164e44ccce268cb.