Class: Chawk::Models::Range

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/range.rb

Instance Method Summary collapse

Instance Method Details

#build_datasetObject



43
44
45
# File 'lib/range.rb', line 43

def build_dataset
  populate!
end

#build_subnodeObject



29
30
31
32
33
34
35
# File 'lib/range.rb', line 29

def build_subnode
  if subkey.to_s == ''
    self.subkey = parent_node.key + "/" + SecureRandom.hex.to_s
  end
  self.data_node = Chawk::Models::Node.create(key:subkey)
  grant_node_access
end

#cluster(now, benow) ⇒ Object



47
48
49
50
# File 'lib/range.rb', line 47

def cluster(now, benow)
  sum = parent_node.points.where("observed_at > ? AND observed_at <= ?",benow,now).sum(:value)
  data_node.points.create(observed_at:now, recorded_at:Time.now, value:sum)
end

#grant_node_accessObject



23
24
25
26
27
# File 'lib/range.rb', line 23

def grant_node_access
  # TODO: vet this very carefully.
  # The only way to get here should be through an authorized source.
  self.data_node.access=:full
end

#order_is_correctObject



37
38
39
40
41
# File 'lib/range.rb', line 37

def order_is_correct
  if self.start_ts >= self.stop_ts
    errors.add(:stop_ts, "must be after start_ts.")
  end
end

#populate!Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/range.rb', line 69

def populate!
  # TODO: Accounting hook
  # TODO: perform in callback (celluloid?)
  self.data_node.points.destroy_all
  step = 0.25 * self.beats
  now = (self.start_ts*4).round/4.to_f
  benow = now - step
  beval = 0
  while now < self.stop_ts
    #binding.pry
    case strategy
    when "cluster"
      point = cluster now, benow
    when "tally"
      point = tally now, benow, beval
    when "recent_point","",nil
      point = recent_point now, benow
    end
    beval = point.value
    benow = now
    now += step
  end
end

#recent_point(now, benow) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/range.rb', line 59

def recent_point(now, benow)
  point = parent_node.points.where("observed_at <= :dt_to",{dt_to:now}).order(observed_at: :desc, id: :desc).first
  if point
    value = point.value
  else
    value = default || 0
  end
  data_node.points.create(observed_at:now, recorded_at:Time.now, value:value)
end

#reloadObject



18
19
20
21
# File 'lib/range.rb', line 18

def reload
  super
  grant_node_access
end

#tally(now, benow, beval) ⇒ Object



52
53
54
55
56
57
# File 'lib/range.rb', line 52

def tally(now, benow, beval)
  sum = parent_node.points.where("observed_at > ? AND observed_at <= ?",benow,now).sum(:value)
  #puts "\n\n VAL #{beval} SUM #{sum}"
  value = beval + sum
  data_node.points.create(observed_at:now, recorded_at:Time.now, value:value)
end