Class: Chawk::Models::Node

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

Overview

The Node, where most Chawk:Addr information is persisted..

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



23
24
25
# File 'lib/models.rb', line 23

def agent
  @agent
end

Instance Method Details

#_insert_point(val, ts, options = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/models.rb', line 35

def _insert_point(val,ts,options={})
	values = {value:val,observed_at:ts.to_f}
	if options[:meta]
		if options[:meta].is_a?(Hash)
			values[:meta] = options[:meta].to_json
		else
			raise ArgumentError, "Meta must be a JSON-representable Hash. #{options[:meta].inspect}"
		end
	end
	self.points.create(values)
end

#_insert_point_array(item, options) ⇒ Object



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

def _insert_point_array(item,options)
	if item.length == 2 && item[0].is_a?(Integer)
		_insert_point item[0],item[1], options
	else
		raise ArgumentError, "Array Items must be in [value,timestamp] format. #{item.inspect}"
	end
end

#_insert_point_hash(item, ts, options) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/models.rb', line 47

def _insert_point_hash(item,ts,options)
	if item['v'] && item['v'].is_a?(Integer)
		if item['t']
			_insert_point item['v'],item['t'], options
		else
			_insert_point item['v'],ts, options
		end
	else
		raise ArgumentError, "Hash must have 'v' key set to proper type.. #{item.inspect}"
	end
end

#_insert_point_string(item, ts, options) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/models.rb', line 67

def _insert_point_string(item,ts,options)
	if item.length > 0 && item =~ /\A[-+]?[0-9]+/
		_insert_point item.to_i,ts, options
	else
		raise ArgumentError, "String Items must represent Integer. #{item.inspect}"
	end
end

#add_points(args, options = {}) ⇒ Object

Add an item or an array of items (one at a time) to the datastore.

Parameters:

  • args (Object, Array of Objects)
  • options (Hash) (defaults to: {})

    You can also pass in :meta and :timestamp



93
94
95
96
97
98
99
100
101
102
# File 'lib/models.rb', line 93

def add_points(args,options={})
	options[:observed_at] ? dt = options[:observed_at] : dt = Time.now
	if args.is_a?(Array)
		args.each do |arg|
			point_recognizer(arg, dt, options)
		end
	else
		point_recognizer(args, dt, options)
	end
end

#decrement(value = 1, options = {}) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/models.rb', line 113

def decrement(value=1, options={})
	if value.is_a?(Integer)
		increment (-1) * value, options
	else 
		raise ArgumentError, "Value must be an Integer"
	end
end

#increment(value = 1, options = {}) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/models.rb', line 104

def increment(value=1, options={})
	if value.is_a?(Integer)
		last = self.points.last
		add_points last.value + value,options 
	else 
		raise ArgumentError, "Value must be an Integer"
	end
end

#initObject



31
32
33
# File 'lib/models.rb', line 31

def init
	@agent = nil
end

#maxObject



121
122
123
# File 'lib/models.rb', line 121

def max()
	points.maximum('value') || 0
end

#minObject



125
126
127
# File 'lib/models.rb', line 125

def min()
	points.minimum('value') || 0
end

#point_recognizer(item, dt, options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/models.rb', line 75

def point_recognizer(item, dt, options={})
	case 
	when item.is_a?(Integer)
		_insert_point item,dt, options
	when item.is_a?(Array)
		_insert_point_array(item, options)
	when item.is_a?(Hash)
		_insert_point_hash(item,dt,options)
	when item.is_a?(String)
		_insert_point_string(item,dt,options)
	else
		raise ArgumentError, "Can't recognize format of data item. #{item.inspect}"
	end
end

#points_range(dt_from, dt_to, options = {}) ⇒ Array of Objects

Returns items whose observed_at times fit within from a range.

Parameters:

  • dt_from (Time::Time)

    The start time.

  • dt_to (Time::Time)

    The end time.

Returns:

  • (Array of Objects)


133
134
135
136
# File 'lib/models.rb', line 133

def points_range(dt_from, dt_to,options={})
	vals = points.where("observed_at >= :dt_from AND  observed_at <= :dt_to",{dt_from:dt_from.to_f,dt_to:dt_to.to_f}, limit:1000,order:"observed_at asc, id asc")
	return vals
end

#points_since(dt_from) ⇒ Array of Objects

Returns items whose observed_at times fit within from a range ending now.

Parameters:

  • dt_from (Time::Time)

    The start time.

Returns:

  • (Array of Objects)


141
142
143
# File 'lib/models.rb', line 141

def points_since(dt_from)
	self.points_range(dt_from,Time.now)
end

#set_permissions(agent, read = false, write = false, admin = false) ⇒ Object

Sets permissions flag for this address, for a specific agent. The existing Chawk::Relationship will be destroyed and a new one created as specified. Write access is not yet checked.

Parameters:

  • agent (Chawk::Agent)

    the agent to give permission.

  • read (Boolean) (defaults to: false)

    true/false can the agent read this address.

  • write (Boolean) (defaults to: false)

    true/false can the agent write this address. (Read acces is required to write.)

  • admin (Boolean) (defaults to: false)

    does the agent have ownership/adnim rights for this address. (Read and write are granted if admin is as well.)



159
160
161
162
163
164
165
166
# File 'lib/models.rb', line 159

def set_permissions(agent,read=false,write=false,admin=false)
	relations.where(agent_id:agent.id).destroy_all
	if read || write || admin
		vals = {agent:agent,read:(read ? true : false),write:(write ? true : false),admin:(admin ? true : false)}
		relations.create(vals)
	end
	nil
end

#set_public_read(value) ⇒ Object

Sets public read flag for this address

Parameters:

  • value (Boolean)

    true if public reading is allowed, false if it is not.



147
148
149
150
151
# File 'lib/models.rb', line 147

def set_public_read(value)
	value = value ? true : false
	self.public_read = value
	save
end