Everything is quite simple, but since there is interest, I supplement the article.
At first it was necessary to catch how data comes from Yandex. It turned out two XML-ki:
graph_1.xml and
graph_1006.xml . This is all the data that will be used.
Since I myself am engaged in development on Ruby, I took the lightweight framework
Sinatra as a basis, connected the
Slim templating system for a single view. Two actions - one renders a template (tied to the site root), and the second
returns json with the data for the chart . To generate json, I took the
oj gem, and for parsing xml, I took
ox .
The original data, of course, turned out to be different time stamps (the ruble / dollar and dollar / barrel rates are not updated synchronously), so the nearest update of the ruble exchange rate to the barrel update in question is taken.
It turned out that the json-ki request takes a noticeable amount of time - primarily due to two requests to Yandex. Therefore, the result wanted to cache. Moreover, the data from Yandex will be updated twice a day. Having studied the possibilities of Heroku on a free account, I was delighted with a bunch of cloud options Redis and Memcache from 5 to 25 MB, which is much more than the required amount. But it turned out that they are provided only after entering data on a credit card, which is somewhat alarming - there is a suspicion that the money will be written off if you exceed the free volume. And I wanted to pour in - and not think. Therefore, a class has been filed, with the proud name
StupidCache . The point is that the application on Sinatra is resident in memory, and therefore the static class attributes are stored between requests. All controller code turned out like this:
get '/chart/rub_bar.json' do result_json = StupidCache.fetch :rub_bar do
At first there were thoughts to request data for certain time intervals, but considering the size of the resulting json <100KB, I decided that you can always give everything entirely - besides, you can change ranges without reloading the page.
Left the interface itself. For the assembly, I took
bower , pulled out bootstrap, jquery,
fontawesome and
chartjs (then switched, though jquery and fontawesome to CDN, and fontawesome itself was useful only for the github icon in the basement).
Special magic had to deal only with chartjs. Chartjs is quite handy:
var data = { labels: labels,
What is in the application is almost a configuration out of the box, except for one trouble: if you run the above code again (and this is necessary to switch between periods), then the graphs overlap each other. The problem was successfully crutched with this line before redrawing:
$("#oil_chart").replaceWith('<canvas id="oil_chart" height="400"></canvas>');
Another trifle: the data for the month fits well on the screen, i.e. 30 points. A few thousand people slow down and look bad. Therefore, we skip the counts so that the points are from 30 to 59:
var skip = Math.floor(values_all.length/30); skip = skip < 1 ? 1 : skip; var values = []; for (var i = 0; i < values_all.length; i = i + skip) { values.push(values_all[i]); }
And the last - on Heroku, about which there is a decent amount of articles on Habré.
For production, it is better to get rid of the standard WEBrick and use the smart multi-threaded Puma. Although Heroku and recommends Unicorn, but on a free account, so even with requests for third-party services Puma should feel much more comfortable. To do this, simply add the gem 'puma' to the gemfile. A Procfile has been added for the order, although without it Heroku runs everything.
For the deployment itself, we register the account, set up their console utility, log in through it, commit the project to git, git push heroku.
A curtain.
GitHab Code