Nuanda / smogmapper

Smog Mapper
http://smogmapper.smogathon.pl/
1 stars 0 forks source link

Caching heatmap data for faster responses #17

Closed Nuanda closed 8 years ago

Nuanda commented 8 years ago

@mkasztelnik @kammerer Guys, please have a look and tell me if this will work as intended ;).

What I mean, I want to cache 15-minutes-intervals of measurement readings, for approx. a day, and return these instead of hitting the DB and preparing new JSON documents.

Just read that and tell me if there is a semantic error somewhere or not. Thanks.

mkasztelnik commented 8 years ago

There is a problem when ref_time is e.g. Time.new(2015, 1, 2, 0, 15) and you are asking for iteration 2. As a conclusion interval_number will be 95 but day will point into 2015.01.02. It can be simply corrected:

iteration_time = ref_time - params[:iteration].to_i * interval
cache_key = { id: params[:id],
              day: iteration_time.yday,
              interval_number: interval_number(iteration_time) }

Or it can be changed to use iteration timestamp:

iteration_time = ref_time - params[:iteration].to_i * interval
cache_key = { id: params[:id],
              timestamp: Time.new(iteration_time.year, iteration_time.month, iteration_time.day) + 
                         interval_number(iteration_time) * interval }

The second solution has this + that it is resistant to interval change.

Nuanda commented 8 years ago

Thanks for noticing that. Fixed. Also added time index - cut 1/3 of the query time.