Module: Chawk

Defined in:
lib/chawk.rb,
lib/node.rb,
lib/range.rb,
lib/models.rb,
lib/quantizer.rb,
lib/aggregator.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.4.0"

Class Method Summary collapse

Class Method Details

.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



92
93
94
95
96
97
98
# File 'lib/chawk.rb', line 92

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

.check_node_public_security(node, access) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/chawk.rb', line 26

def self.check_node_public_security(node, access)
	case access
	when :read
		node.public_read == true
	when :write
		node.public_write == true
	end
end

.check_node_relations_security(rel, access) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chawk.rb', line 11

def self.check_node_relations_security(rel, access)
	if (rel && (rel.read || rel.admin))
		case access
		when :read
			(rel.read or rel.admin)
		when :write
			(rel.write or rel.write)
		when :admin
			rel.admin
		when :full
			(rel.read && rel.write && rel.admin)
		end
	end
end

.check_node_security(agent, node, access = :full) ⇒ Object

Raises:

  • (SecurityError)


35
36
37
38
39
40
41
42
# File 'lib/chawk.rb', line 35

def self.check_node_security(agent,node,access=:full)

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

	return node if check_node_relations_security(rel,access) || check_node_public_security(node,access)

	raise SecurityError,"You do not have permission to access this node. #{agent} #{rel} #{access}"
end

.clear_all_data!Object

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



102
103
104
105
106
107
108
# File 'lib/chawk.rb', line 102

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, access = :full) ⇒ Object

Raises:

  • (ArgumentError)


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

def self.find_or_create_node(agent,key,access=:full)
	#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,access)
	else
		node = Chawk::Models::Node.create(key:key) if node.nil?
		node.set_permissions(agent,true,true,true)
	end
	node.access = access
	return node
end

.node(agent, key, access = :full) ⇒ Chawk::Node

The primary method for retrieving an Node. 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 node can be found in the database.

Returns:

  • (Chawk::Node)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/chawk.rb', line 64

def self.node(agent,key,access=:full)

	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,access)

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

	node.agent = agent
	return node
end