-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.rb
178 lines (136 loc) · 4.46 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require 'rubygems'
require 'sinatra'
require 'lib/power_hungry/config'
require 'lib/power_hungry/database'
require 'active_support'
PowerHungry::Config.init
PowerHungry::Database.connect
DEBUG = true
NUM_INTERVALS = 500
class Cache
@@cache = nil
def self.empty?
@@cache.nil?
end
def self.expired?
@@expires_at < Time.now
end
def self.fetch(options = {})
if empty? || expired?
@@expires_at = Time.now + 20 # Prevent race condition
@@cache = yield
@@expires_at = options[:expires_at]
end
@@cache
end
def self.get(name)
@@cache[name]
end
def self.set(name, value)
@@empty = false
@@cache[name] = value
end
end
helpers do
def amps(sensor)
_data_to_faketime_pairs(_to_timestamp(sensor.current_reading.updated_at), sensor.current_reading.amperage_data)
end
def amps_bounds(sensor)
bound = 0
if sensor.current_reading.amperage_data.min.abs > sensor.current_reading.amperage_data.max
bound = sensor.current_reading.amperage_data.min.abs
else
bound = sensor.current_reading.amperage_data.max
end
bound + (bound * 0.1)
end
def interval_points(intervals, method)
intervals.map do |interval|
[interval[:updated_at], interval[method]]
end
end
def voltages(sensor)
_data_to_faketime_pairs(_to_timestamp(sensor.current_reading.updated_at), sensor.current_reading.voltage_data)
end
def downsample(intervals, num_samples)
start = Time.now
# factor = count / num_samples
factor = intervals.size / num_samples
factor = 1 if factor == 0
downsampled = []
intervals.in_groups_of(factor) do |group|
next if group.last.nil? # only consider full sets
updated_at = (_to_timestamp(group.first.updated_at) + _to_timestamp(group.last.updated_at)) / 2
interval_length = _average(group, :interval_length)
watts = _average(group, :watts)
downsampled << {:interval_length => interval_length, :updated_at => updated_at, :watts => watts}
end
downsampled
end
def interval(data)
{:interval_length => data[:interval_length], :updated_at => _to_timestamp(data[:updated_at]), :watts => data[:watts]}
end
def watt_hours(sensor_intervals)
start = Time.now
watt_hours = []
cummulative_watt_hours = 0
timestamp_intervals = sensor_intervals.inject({}) do |grouped_intervals, (sensor, intervals)|
intervals.each do |interval|
grouped_intervals[interval[:updated_at]] ||= []
grouped_intervals[interval[:updated_at]] << interval
end
grouped_intervals
end
all_timestamps = timestamp_intervals.keys.sort
all_timestamps.each do |timestamp|
timestamp_watt_hours = 0
timestamp_watt_hours = timestamp_intervals[timestamp].inject(0) do |watt_hours_sum, interval|
interval_watt_hours = interval[:watts] * interval[:interval_length] / (60.0 * 60)
watt_hours_sum + interval_watt_hours
end
cummulative_watt_hours += timestamp_watt_hours
watt_hours << [timestamp, cummulative_watt_hours]
end
watt_hours
end
def watts(sensor)
_data_to_faketime_pairs(_to_timestamp(sensor.current_reading.updated_at), sensor.current_reading.wattage_data)
end
def _average(set, attribute)
set.inject(0.0) { |sum, val| sum + val.send(attribute) } / set.size
end
def _data_to_faketime_pairs(start, data)
pairs = []
data.each_with_index do |value, i|
pairs << [start + i, value]
end
pairs
end
def _to_timestamp(datetime)
usec = (datetime.sec_fraction * 60 * 60 * 24 * (10**6)).to_i
time = Time.gm(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.min,
datetime.sec, usec)
time.to_i * 1000
end
end
get '/' do
@sensors,
@past_day,
@past_day_watt_hours,
@past_week,
@past_week_watt_hours = Cache.fetch(:expires_at => Time.now + 120) do
past_day = {}
past_week = {}
sensors = Sensor.all
sensors.each do |sensor|
day_intervals = Interval.all(:sensor => sensor, :created_at.gt => Time.now - (60*60*24))
past_day[sensor] = downsample(day_intervals, NUM_INTERVALS)
week_intervals = Interval.all(:sensor => sensor, :created_at.gt => Time.now - (60*60*24 * 7))
past_week[sensor] = downsample(week_intervals, NUM_INTERVALS)
end
past_day_watt_hours = watt_hours(past_day)
past_week_watt_hours = watt_hours(past_week)
[sensors, past_day, watt_hours(past_day), past_week, watt_hours(past_week)]
end
erb :graph
end