Class: Perus::Server::Metric
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Perus::Server::Metric
- Defined in:
- lib/perus/server/models/metric.rb
Class Method Summary collapse
- .add(name, system_id, type, file_data = nil) ⇒ Object
- .add_file(name, system_id, file_data) ⇒ Object
- .add_numeric(name, system_id) ⇒ Object
- .add_string(name, system_id) ⇒ Object
Instance Method Summary collapse
- #after_destroy ⇒ Object
- #file? ⇒ Boolean
- #num_values_over_period(period) ⇒ Object
- #numeric? ⇒ Boolean
- #path ⇒ Object
- #string? ⇒ Boolean
- #url ⇒ Object
- #values_dataset ⇒ Object
- #values_over_period(period) ⇒ Object
Class Method Details
.add(name, system_id, type, file_data = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/perus/server/models/metric.rb', line 57 def self.add(name, system_id, type, file_data = nil) existing = Metric.where(system_id: system_id, name: name, type: type).first if existing return if type != 'file' record = existing else record = Metric.new(system_id: system_id, name: name, type: type) end record.file = file_data record.save end |
.add_file(name, system_id, file_data) ⇒ Object
79 80 81 |
# File 'lib/perus/server/models/metric.rb', line 79 def self.add_file(name, system_id, file_data) self.add(name, system_id, 'file', file_data) end |
.add_numeric(name, system_id) ⇒ Object
71 72 73 |
# File 'lib/perus/server/models/metric.rb', line 71 def self.add_numeric(name, system_id) self.add(name, system_id, 'num') end |
.add_string(name, system_id) ⇒ Object
75 76 77 |
# File 'lib/perus/server/models/metric.rb', line 75 def self.add_string(name, system_id) self.add(name, system_id, 'str') end |
Instance Method Details
#after_destroy ⇒ Object
52 53 54 55 |
# File 'lib/perus/server/models/metric.rb', line 52 def after_destroy super File.unlink(path) if file && File.exists?(path) end |
#file? ⇒ Boolean
17 18 19 |
# File 'lib/perus/server/models/metric.rb', line 17 def file? type == 'file' end |
#num_values_over_period(period) ⇒ Object
47 48 49 50 |
# File 'lib/perus/server/models/metric.rb', line 47 def num_values_over_period(period) values = values_over_period(period) values.map(&:num_value) end |
#numeric? ⇒ Boolean
9 10 11 |
# File 'lib/perus/server/models/metric.rb', line 9 def numeric? type == 'num' end |
#path ⇒ Object
27 28 29 |
# File 'lib/perus/server/models/metric.rb', line 27 def path File.join(system.uploads_dir, file['filename']) end |
#string? ⇒ Boolean
13 14 15 |
# File 'lib/perus/server/models/metric.rb', line 13 def string? type == 'str' end |
#url ⇒ Object
21 22 23 24 25 |
# File 'lib/perus/server/models/metric.rb', line 21 def url prefix = URI(Server..uploads_url) path = File.join(system_id.to_s, file['filename']) (prefix + path).to_s end |
#values_dataset ⇒ Object
31 32 33 |
# File 'lib/perus/server/models/metric.rb', line 31 def values_dataset system.values_dataset.where(metric: name) end |
#values_over_period(period) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/perus/server/models/metric.rb', line 35 def values_over_period(period) if period.keys.include?(:hours) min_timeout = Time.now.to_i - (period[:hours] * 60 * 60) elsif period.keys.include?(:mins) min_timeout = Time.now.to_i - (period[:mins] * 60) else raise 'invalid period - must be :hours or :mins' end values_dataset.where("timestamp >= #{min_timeout}") end |