chawk 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29d1fbfc067f4fb5c6e95997d265cc9764987467
4
+ data.tar.gz: 93a8956cbe1bf6d3a135285a537ea7c5539e8678
5
+ SHA512:
6
+ metadata.gz: 0f78c30f040dbf79a534abe849a6aa4831965d1ca0e4b94dd5525e4ce1386795fb9b7ddeec299fbb608e565ec61c949a92770e1fb50895082a177b6bfa7b5b78
7
+ data.tar.gz: b72f8375b23223bdbef799936894acb72c99fee92932c023721c2d0a43153b54f45e214d06a075b0f910c4131bd82168c53ca116ce5bcf498a395239c9d4c0a0
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.sqlite3
19
+ .DS_Store
20
+ *.log
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ - 2.0.0
5
+ - 1.9.3
6
+
7
+ script: "rake test"
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in chawk.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Scott Russell (queuetue@gmail.com / queuetue.com)
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,148 @@
1
+ # Chawk
2
+
3
+ [![Gem Version][GV img]][Gem Version]
4
+ [![Build Status][BS img]][Build Status]
5
+ [![Dependency Status][DS img]][Dependency Status]
6
+ [![Code Climate][CC img]][Code Climate]
7
+
8
+ [Gem Version]: https://rubygems.org/gems/chawk
9
+ [Build Status]: https://travis-ci.org/queuetue/chawk-gem
10
+ [travis pull requests]: https://travis-ci.org/queuetue/chawk-gem/pull_requests
11
+ [Dependency Status]: https://gemnasium.com/queuetue/chawk-gem
12
+ [Code Climate]: https://codeclimate.com/github/queuetue/chawk-gem
13
+
14
+ [GV img]: https://badge.fury.io/rb/chawk-gem.png
15
+ [BS img]: https://travis-ci.org/queuetue/chawk-gem.png
16
+ [DS img]: https://gemnasium.com/queuetue/chawk-gem.png
17
+ [CC img]: https://codeclimate.com/github/queuetue/chawk-gem.png
18
+ [CS img]: https://coveralls.io/repos/queuetue/chawk/badge.png?branch=master
19
+
20
+ ## Description
21
+ Chawk is a database agnostic time-series database written in Ruby.
22
+
23
+ It tracks both both points (Integers) and values (String) in seperate datastores, and will eventually provide statistical and aggregate tools for numeric data.
24
+
25
+ This is the gem that powers the server, [chawk-server](http://github.com/queuetue/chawk-server "chawk-server")
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ gem 'chawk'
32
+
33
+ And then execute:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install chawk
40
+
41
+ ## Using Chawk
42
+
43
+ Setup
44
+
45
+ require 'chawk'
46
+ Chawk.setup 'sqlite::memory:'
47
+
48
+ DO NOT DO THIS ON AN EXISTING DATABASE, but the first time using a new database (Like this sqlite memory one that is destroyed at program exit) you should call:
49
+
50
+ DataMapper.auto_upgrade!
51
+
52
+ All Chawk data operations require an Agent. This can be used as the main actor in your code, or can be a proxy for your own User, etc through the foreign_id property.
53
+
54
+ agent = Chawk::Models::Agent.first(name:"Steve Austin") || Chawk::Models::Agent.new(name:"Steve Austin")
55
+
56
+ All data operations are performed through an Addr object.
57
+
58
+ addr = Chawk.addr(agent,"inventory/popcorn")
59
+
60
+ The Addr object has two store objects - values and points. **Points** are integers and allow mathematical and statistical operations. **Values** are strings and are intended for storing informational or serialized time series data.:
61
+
62
+ addr.values << "This is a test."
63
+ addr.values.last
64
+ => #<Chawk::Models::Value @id=...
65
+ @observed_at=...
66
+ @recorded_at=#<DateTime: ...>
67
+ @meta=nil
68
+ @value="This is a test."
69
+ @node_id=...
70
+ @agent_id=...>
71
+
72
+ addr.values << ["AND","SO","IS","THIS"]
73
+ addr.values.last
74
+ => #<Chawk::Models::Value ... @value="THIS">
75
+ addr.values.last(10)
76
+ => [#<Chawk::Models::Value ... @value="This is a test.">,
77
+ #<Chawk::Models::Value ... @value="AND">,
78
+ #<Chawk::Models::Value ... @value="SO">,
79
+ #<Chawk::Models::Value ... @value="IS">,
80
+ #<Chawk::Models::Value ... @value="THIS">]
81
+
82
+ Addr can also return ranges from the past using the range method or the last method:
83
+
84
+ addr.values.range(Time.now-2000,Time.now-1000)
85
+ => [#<Chawk::Models::Value ... @value="ROCK">,
86
+ #<Chawk::Models::Value ... @value="AROUND">]
87
+
88
+ addr.values.since(Time.now-1000)
89
+ => [#<Chawk::Models::Value ... @value="THE.">,
90
+ #<Chawk::Models::Value ... @value="CLOCK">]
91
+
92
+ These same methods also work for points:
93
+
94
+ addr.points << [10,9,8,7,6,5]
95
+ addr.points.last
96
+ => #<Chawk::Models::Point ... @value=5>
97
+ addr.points.last(2)
98
+ => [#<Chawk::Models::Point ... @value=6>, #<Chawk::Models::Point ... @value=5>]
99
+
100
+ Points can also use the increment and decrement operators
101
+
102
+ addr.points.last
103
+ => #<Chawk::Models::Point ... @value=5>
104
+ addr.points + 10
105
+ addr.pointslast
106
+ => #<Chawk::Models::Point ... @value=15>
107
+
108
+ As well as max and min
109
+
110
+ addr.points.max
111
+ => 15
112
+ addr + 10
113
+ addr.points.last
114
+ => 1
115
+
116
+
117
+ ## Contributing
118
+
119
+ 1. Fork it ( http://github.com/<my-github-username>/chawk/fork )
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create new Pull Request
124
+
125
+ ## License
126
+
127
+ Copyright (c) 2014 Scott Russell (queuetue@gmail.com / queuetue.com)
128
+
129
+ MIT License
130
+
131
+ Permission is hereby granted, free of charge, to any person obtaining
132
+ a copy of this software and associated documentation files (the
133
+ "Software"), to deal in the Software without restriction, including
134
+ without limitation the rights to use, copy, modify, merge, publish,
135
+ distribute, sublicense, and/or sell copies of the Software, and to
136
+ permit persons to whom the Software is furnished to do so, subject to
137
+ the following conditions:
138
+
139
+ The above copyright notice and this permission notice shall be
140
+ included in all copies or substantial portions of the Software.
141
+
142
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
143
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
144
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
145
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
146
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
147
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
148
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib' << 'test'
6
+ test.pattern = 'test/**/*_test.rb'
7
+ end
8
+
9
+ task :default => [:test]
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chawk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "chawk"
8
+ spec.version = Chawk::VERSION
9
+ spec.authors = ["Scott Russell"]
10
+ spec.email = ["queuetue@gmail.com"]
11
+ spec.summary = %q{Time Series Storage Server}
12
+ spec.description = %q{Time Series Storage Server}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "dm-sqlite-adapter", "1.2.0"
22
+ spec.add_runtime_dependency "dm-postgres-adapter","1.2.0"
23
+ spec.add_runtime_dependency "data_mapper", "1.2.0"
24
+ spec.add_runtime_dependency "dm-is-tree", "1.2.0"
25
+ spec.add_runtime_dependency "dm-aggregates", "1.2.0"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.5"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency('minitest', '5.3.0')
30
+ spec.add_development_dependency('rack-test', "0.6.2")
31
+ spec.add_development_dependency('json', "1.8.1")
32
+ spec.add_development_dependency('coveralls', "0.7.0")
33
+ spec.add_development_dependency('fakeweb')
34
+
35
+ end
@@ -0,0 +1,89 @@
1
+ require 'vaddr'
2
+ require 'paddr'
3
+ module Chawk
4
+ class Addr
5
+ attr_reader :path, :node, :agent
6
+ def initialize(agent,path)
7
+ @path = path
8
+ @agent = agent
9
+
10
+ unless path.is_a?(String)
11
+ #DataMapper.logger.debug "NOT A STRING"
12
+ raise ArgumentError
13
+ end
14
+
15
+ unless path =~ /^[\w\/\:\\]+$/
16
+ #DataMapper.logger.debug "BAD MATCH"
17
+ raise ArgumentError
18
+ end
19
+
20
+ @node = find_or_create_addr(path)
21
+
22
+ unless @node
23
+ #DataMapper.logger.debug "NOT A NODE"
24
+ raise ArgumentError
25
+ end
26
+ end
27
+
28
+ def address
29
+ @path
30
+ end
31
+
32
+
33
+ def values()
34
+ Chawk::Vaddr.new(self)
35
+ end
36
+
37
+ def points()
38
+ Chawk::Paddr.new(self)
39
+ end
40
+
41
+ def public_read=(value,options={})
42
+ value = value ? true : false
43
+ #DataMapper.logger.debug "MAKING #{@node.address} PUBLIC (#{value})"
44
+ @node.update(public_read:value)
45
+ end
46
+
47
+ def set_permissions(agent,read=false,write=false,admin=false)
48
+ #DataMapper.logger.debug "REVOKING #{@node.address} / #{@agent.name} "
49
+ @node.relations.all(agent:agent).destroy()
50
+ if read || write || admin
51
+ vals = {agent:agent,read:(read ? true : false),write:(write ? true : false),admin:(admin ? true : false)}
52
+ #DataMapper.logger.debug "SET PERMISSIONS #{@node.address} / #{vals} "
53
+ @node.relations.create(vals)
54
+ end
55
+ nil
56
+ end
57
+
58
+ def check_node_security(node)
59
+ if node.public_read
60
+ #DataMapper.logger.debug "NODE IS PUBLIC ACCESSABLE -- #{@agent.name} - #{@agent.id}"
61
+ return node
62
+ end
63
+ rel = node.relations.first(agent:@agent)
64
+ if (rel && (rel.read || rel.admin))
65
+ #DataMapper.logger.debug "NODE IS ACCESSABLE -- #{@agent.name} - #{@agent.id}"
66
+ return node
67
+ else
68
+ #DataMapper.logger.debug "NODE IS INACCESSABLE -- #{@agent.name} - #{@agent.id}"
69
+ raise SecurityError
70
+ end
71
+ end
72
+
73
+ def find_or_create_addr(addr)
74
+ #TODO also accept regex-tested string
75
+ raise ArgumentError unless addr.is_a?(String)
76
+
77
+ node = Chawk::Models::Node.first(address:self.address)
78
+ if node
79
+ node = check_node_security(node)
80
+ else
81
+ #DataMapper.logger.debug "NODE CREATED -- #{@agent.name} -- #{@agent.id}"
82
+ node = Chawk::Models::Node.create(address:self.address) if node.nil?
83
+ node.relations.create(agent:@agent,node:node,admin:true,read:true,write:true)
84
+ return node
85
+ end
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,46 @@
1
+ require "chawk/version"
2
+ require 'data_mapper'
3
+ require 'dm-is-tree'
4
+ require 'quantizer'
5
+ require 'models'
6
+ require 'addr'
7
+
8
+ module Chawk
9
+
10
+ @@ready = nil
11
+
12
+ def self.addr(agent,path)
13
+ if @@ready.nil?
14
+ raise "Chawk has not been setup yet."
15
+ end
16
+
17
+ if agent.is_a?(Chawk::Models::Agent) && path.is_a?(String)
18
+ return Chawk::Addr.new(agent,path)
19
+ else
20
+ raise ArgumentError
21
+ end
22
+ end
23
+
24
+ def self.clear_all_data!
25
+ if @@ready.nil?
26
+ raise "Chawk has not been setup yet."
27
+ end
28
+
29
+ Chawk::Models::Agent.all.destroy!
30
+ Chawk::Models::Relation.all.destroy!
31
+ Chawk::Models::AgentTag.all.destroy!
32
+ Chawk::Models::Tag.all.destroy!
33
+ Chawk::Models::Node.all.destroy!
34
+ Chawk::Models::Point.all.destroy!
35
+ Chawk::Models::Value.all.destroy!
36
+ end
37
+
38
+
39
+ def self.setup(database_url)
40
+ @@ready = true
41
+ adapter = DataMapper.setup(:default, database_url)
42
+ DataMapper::Model.raise_on_save_failure = true
43
+ DataMapper.finalize
44
+ nil
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module Chawk
2
+ VERSION = "0.1.10"
3
+ end
@@ -0,0 +1,87 @@
1
+ require 'data_mapper'
2
+ require 'dm-aggregates'
3
+ module Chawk
4
+ module Models
5
+
6
+ module Datum
7
+ def self.included(base)
8
+ base.class_eval do
9
+ include DataMapper::Resource
10
+ before :create, :set_timestamp
11
+
12
+ property :id, DataMapper::Property::Serial
13
+ property :observed_at, DataMapper::Property::Float
14
+ property :recorded_at, DataMapper::Property::DateTime
15
+ property :meta, DataMapper::Property::Text
16
+
17
+ belongs_to :node
18
+ belongs_to :agent
19
+
20
+ def set_timestamp
21
+ attribute_set(:recorded_at, DateTime.now )
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ class Agent
30
+ include DataMapper::Resource
31
+ property :id, Serial
32
+ property :foreign_id, Integer
33
+ property :name, String, length:200
34
+ has n, :tags
35
+ has n, :agent_tags
36
+ has n, :relations
37
+ end
38
+
39
+ class Relation
40
+ include DataMapper::Resource
41
+ property :id, Serial
42
+ property :admin, Boolean, default:false
43
+ property :read, Boolean, default:false
44
+ property :write, Boolean, default:false
45
+ belongs_to :agent
46
+ belongs_to :node
47
+ end
48
+
49
+ class AgentTag
50
+ include DataMapper::Resource
51
+ property :id, Serial
52
+ property :name, String, length:100
53
+ property :description, Text
54
+ property :managed, Boolean, default:false
55
+ belongs_to :agent
56
+ end
57
+
58
+ class Tag
59
+ include DataMapper::Resource
60
+ property :description, Text
61
+ property :id, Serial
62
+ property :name, String, length:100
63
+ end
64
+
65
+ class Node
66
+ include DataMapper::Resource
67
+ property :id, Serial
68
+ property :address, String, length:150
69
+ property :public_read, Boolean, default:false
70
+ property :public_write, Boolean, default:false
71
+
72
+ has n, :points
73
+ has n, :values
74
+ has n, :relations
75
+ end
76
+
77
+ class Point
78
+ include Chawk::Models::Datum
79
+ property :value, Integer
80
+ end
81
+
82
+ class Value
83
+ include Chawk::Models::Datum
84
+ property :value, Text
85
+ end
86
+ end
87
+ end