Guest User

Untitled

a guest
Jun 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. ## models/Interpolate.rb
  2.  
  3. #Class to store interpolated data derived fro readings
  4. class Interpolation < ActiveRecord::Base
  5.  
  6. # holds readings that should be interpolated
  7. attr_reader :readings
  8. attr_reader :rdates
  9. attr_reader :rdiff_counter_days
  10. attr_reader :rdiff_co2_days
  11.  
  12. rec = nil
  13.  
  14. # Class to calculate interpolation for each day
  15. def self.calc(unit_id, type)
  16. #get all readings
  17. @readings = Reading.find_all_by_unit_id_and_type(unit_id,type, :select => "date, diff_counter_days, diff_co2_days", :order => "date asc")
  18. #init arrays
  19. @rdates = []
  20. @rdiff_counter_days = []
  21. @rdiff_co2_days = []
  22. #send readings to arrays
  23. @readings.each do |r|
  24. @rdates << r.date
  25. @rdiff_counter_days << r.diff_counter_days
  26. @rdiff_co2_days << r.diff_co2_days
  27. end
  28.  
  29. #iterate down through each day in the period
  30. day = @rdates.last
  31. diff_counter_days = @rdiff_counter_days.last
  32. diff_co2_days = @rdiff_co2_days
  33.  
  34. while day>=@rdates.first do
  35. #check if day has reading
  36. if i=@rdates.rindex(day)
  37. print "match! : "
  38. # set interpolated fields with matched data
  39. diff_counter_days = @rdiff_counter_days[i]
  40. diff_co2_days = @rdiff_co2_days[i]
  41. end
  42. # debug output
  43. print day
  44. print ": "
  45. print diff_counter_days
  46. print ", "
  47. print diff_co2_days
  48. print "\n"
  49.  
  50. # check update if exist or create new if not
  51. rec = Interpolation.find(:first, :conditions => ["unit_id = ? and date = ? and type = ?", unit_id, day, type] )
  52. if rec
  53. rec.update_attributes(:diff_counter_days => diff_counter_days, :diff_co2_days => diff_co2_days )
  54. puts "update"
  55. else
  56. puts "create"
  57. rec = Interpolation.new(:type => type, :date => day, :diff_counter_days => diff_counter_days, :diff_co2_days => diff_co2_days )
  58. rec.save
  59. end
  60.  
  61. # decrease day by one
  62. day=day.yesterday
  63. end
  64. end
  65. end
  66.  
  67. ## somehow updates use the wrong class. They update Reading and not Interpolate
  68.  
  69. Loading development environment (Rails 2.3.2)
  70. >> rec = Interpolation.find(:first, :conditions => ["unit_id = ? and date = ? and type = ?", 1, "2009-05-07", "ElectricityReading"] )
  71. => #<ElectricityReading id: 1, unit_id: 1, type: "ElectricityReading", date: "2009-05-07", created_at: nil, updated_at: nil, diff_counter_days: #<BigDecimal:18a6adc,'0.123E3',4(8)>, diff_co2_days: #<BigDecimal:18a69d8,'0.234E3',4(8)>>
  72. >> rec.diff_counter_days =0
  73. => 0
  74. >> rec.save!
  75. => true
  76. >> rec = Interpolation.find(:first, :conditions => ["unit_id = ? and date = ? and type = ?", 1, "2009-05-07", "ElectricityReading"] )
  77. => #<ElectricityReading id: 1, unit_id: 1, type: "ElectricityReading", date: "2009-05-07", created_at: nil, updated_at: nil, diff_counter_days: #<BigDecimal:183ebbc,'0.123E3',4(8)>, diff_co2_days: #<BigDecimal:183e9f0,'0.234E3',4(8)>>
Add Comment
Please, Sign In to add comment