Skip to content

Commit

Permalink
ahora se puede usar todo lo standard dentro de rails para postgresql
Browse files Browse the repository at this point in the history
la consulta quedo en el framework con comentarios de uso
refactorizado el metodo sqm
  • Loading branch information
gagoar committed May 3, 2012
1 parent 66528a9 commit be1be58
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
29 changes: 8 additions & 21 deletions app/controllers/plots_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,13 @@ class PlotsController < ApplicationController
def index
end
def sqm
puts params[:sqm][:from].to_s,params[:sqm][:to].to_s
#binding.pry
#@data = Instrument.first.surveys.order(:field_id).group_by(&:field_id).values.map{ |x,y| {:x=> x.value.to_i, :y => y.value}}.to_json
#@data = Instrument.first.surveys.where('field_id IN (SELECT field_id FROM surveys WHERE type=\'ValueTime\' AND DATE(value_time)>"'+params[:sqm][:from].to_s+' AND DATE(value_time)<'+params[:sqm][:to].to_s+'")').order(:field_id).group_by(&:field_id).values.map{ |x,y| {:x=> x.value.to_i, :y => y.value}}.to_json
timeWindows=['year','month' ,'week','day','hour','minute']
count=0
begin
vals=[]
timeWindow = timeWindows[count]
count=count+1
@conn ||= PG.connect( user: 'gonzalo' , dbname: 'drod_development' )
sql_sentence = 'SELECT date_trunc(\''+timeWindow+'\', t),avg(v) FROM (SELECT max(value_flt) as v,max(value_time) as t FROM "surveys" INNER JOIN "fields" ON "surveys"."field_id" = "fields"."id" WHERE "fields"."instrument_id" = 2 GROUP BY field_id) AS xy WHERE DATE(t)>\''+params[:sqm][:from].to_s+'\' AND DATE(t)<\''+params[:sqm][:to].to_s+'\' GROUP BY date_trunc(\''+timeWindow+'\',t) ORDER BY date_trunc(\''+timeWindow+'\',t);'
@conn.exec(sql_sentence) do |result|
result.each do |row|
vals << row.values
end
end
puts count.to_s+' '+vals.count.to_s
end while vals.count < 50 && count<6

@data = vals.map{|x,y| {:x=>Time.parse(DateTime.parse(x).to_s).to_i, :y=>y.to_f }}.to_json
from, to = params[:sqm][:from].to_s, params[:sqm][:to].to_s
# habria que ordenar esto de una manera mas logica, como una busqueda binaria(del medio hacia los costados o tener un contador en el modelo para saber cual es el mas problable, y ordenar esto mediante la base asi se ordenaria por la probabilidad mas cercana)
['year','month','week','day','hour','minute'].each do |value|
sample = Instrument.find_by_sql("SELECT date_trunc('" + value + "', t) as date,avg(v) as value FROM (SELECT max(value_flt) as v,max(value_time) as t FROM surveys INNER JOIN fields ON surveys.field_id = fields.id WHERE fields.instrument_id = 2 GROUP BY field_id) AS xy WHERE DATE(t)>'2011-02-08' AND DATE(t)<'" + to + "' GROUP BY date_trunc('" + value + "',t) ORDER BY date_trunc('" + value +"',t);")
#if you use alias in the resultant data, you can use element.alias to invoke the result value.(ej sample.first.date and sample.first.value ) dont care if the attribute its in the model.
@data = sample.map{|x| {:x=>Time.parse(DateTime.parse(x.date).to_s).to_i, :y=>x.value.to_f }}.to_json
return unless sample.size < 50
end
end
end
2 changes: 1 addition & 1 deletion app/views/plots/index.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
:javascript
:javascript
$(function() {
var dates = $( ".from, .to" ).datepicker({
defaultDate: "-3m",
Expand Down
12 changes: 6 additions & 6 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
# gem 'sqlite3'
development:
adapter: postgresql
database: drod_development
username: gonzalo
database: drod
username: gagoar
pool: 5
timeout: 5000

Expand All @@ -15,13 +15,13 @@ development:
# Do not set this db to the same as development or production.
test:
adapter: postgresql
database: drod_test
username: gonzalo
database: drod
username: gagoar
pool: 5
timeout: 5000
production:
adapter: postgresql
database: drod_production
username: gonzalo
database: drod
username: gagoar
pool: 5
timeout: 5000

0 comments on commit be1be58

Please sign in to comment.