Module: Chawk

Defined in:
lib/chawk.rb,
lib/models.rb,
lib/quantizer.rb,
lib/chawk/version.rb

Overview

Chawk is a gem for storing and retrieving time seris data.

Defined Under Namespace

Modules: Models, Quantizer

Constant Summary collapse

VERSION =

The current version of Chawk

"0.2.0"

Class Method Summary collapse

Class Method Details

.addr(agent, key) ⇒ Chawk::Addr

The primary method for retrieving an Addr. If a key does not exist, it will be created and the current agent will be set as an admin for it.

Parameters:

  • agent (Chawk::Agent)

    the agent whose permission will be used for this request

  • key (String)

    the string address this addr can be found in the database.

Returns:

  • (Chawk::Addr)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chawk.rb', line 41

def self.addr(agent,key)

	unless key =~ /^[\w\:\$\!\@\*\[\]\~\(\)]+$/
		raise ArgumentError, "Key can only contain [A-Za-z0-9_:$!@*[]~()] (#{key})"
	end

	unless agent.is_a?(Chawk::Models::Agent) 
		raise ArgumentError, 'Agent must be a Chawk::Models::Agent instance'
	end

	unless key.is_a?(String)
		raise ArgumentError, 'key must be a string.'
	end

	node = find_or_create_node(agent,key)

	unless node
		raise ArgumentError,"No node was returned."
	end

	node.agent = agent
	return node
end

.bulk_add_points(agent, data) ⇒ Object

Insert data for multiple addresses at once. Format should be a hash of valid data sets keyed by address example: ‘key1’=>[1,2,3,4,5],‘key2’=>[6,7,8,9]

Parameters:

  • agent (Chawk::Agent)

    the agent whose permission will be used for this request

  • data (Hash)

    the bulk data to be inserted



69
70
71
72
73
74
75
# File 'lib/chawk.rb', line 69

def self.bulk_add_points(agent, data)
	data.keys.each do |key|
		dset = data[key]
		daddr = addr(agent,key)
		daddr.add_points dset
	end
end

.check_node_security(agent, node) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/chawk.rb', line 7

def self.check_node_security(agent,node)

	if node.public_read
		return node
	end

	rel = node.relations.where(agent_id:agent.id).first

	if (rel && (rel.read || rel.admin))
		return node
	else
		raise SecurityError,"You do not have permission to access this node. #{agent} #{rel}"
	end
end

.clear_all_data!Object

Deletes all data in the database. Very dangerous. Backup often!



79
80
81
82
83
84
85
# File 'lib/chawk.rb', line 79

def self.clear_all_data!
	Chawk::Models::Agent.destroy_all
	Chawk::Models::Relation.destroy_all
	Chawk::Models::Node.destroy_all
	Chawk::Models::Point.destroy_all
	Chawk::Models::Value.destroy_all
end

.find_or_create_node(agent, key) ⇒ Object

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chawk.rb', line 22

def self.find_or_create_node(agent,key)
	#TODO also accept regex-tested string
	raise(ArgumentError,"Key must be a string.") unless key.is_a?(String)

	node = Chawk::Models::Node.where(key:key).first
	if node
		node = check_node_security(agent,node)
	else
		node = Chawk::Models::Node.create(key:key) if node.nil?
		node.relations.create(agent:agent,node:node,admin:true,read:true,write:true)
		return node
	end
end