metricdb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,10 @@
1
+ #Metric DB
2
+
3
+ A semantic layer for db interaction when dealing with metrics
4
+
5
+ ##install
6
+ gem install metricdb
7
+
8
+ ##Examples
9
+
10
+ See the tests for a reasonable run down on usage.
data/lib/MetricDb.rb ADDED
@@ -0,0 +1,4 @@
1
+
2
+ require "MetricDb/MetricBag"
3
+ require "MetricDb/Metric"
4
+ require "MetricDb/Server"
@@ -0,0 +1,18 @@
1
+ require "MetricDb"
2
+
3
+ module MetricDb
4
+
5
+ def MetricDb.Db( uri )
6
+ uri = URI.parse( uri ) if uri.is_a? String
7
+
8
+ case uri.scheme
9
+ when "inmemory"
10
+ require "MetricDb/InMemory"
11
+ return MetricDb::InMemory.new( uri )
12
+
13
+ else
14
+ abort("Scheme, #{uri.scheme}, not recognised when configuring creating db connection");
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,4 @@
1
+ require "FluidDb/Db"
2
+
3
+ require "MetricDb/FluidDb/Metric"
4
+ require "MetricDb/FluidDb/Server"
@@ -0,0 +1,58 @@
1
+ module MetricDb
2
+
3
+ require "MetricDb/Metric"
4
+
5
+ class FluidDb_Metric<Metric
6
+ def initialize( db, id, name )
7
+ @db = db
8
+ @id = id.to_i
9
+ @name = name
10
+ end
11
+
12
+ def massageArray( input )
13
+ return Hash["value", input["value"].to_i, "timestamp", Time.parse( input["timestamp"] )]
14
+ end
15
+ def massage( input )
16
+ return massageArray( input ) if input.is_a? Hash
17
+
18
+ l = Array.new
19
+ input.each do |r|
20
+ l << massageArray( r )
21
+ end
22
+ return l
23
+ end
24
+
25
+
26
+ def q( sql, params )
27
+ return massage( @db.queryForResultset(sql, params ))
28
+ end
29
+
30
+ def push( value )
31
+ @db.execute( "INSERT INTO value_tbl( metric_id, value ) VALUES ( ?, ? )", [@id, value] )
32
+ return true
33
+ end
34
+
35
+ def get()
36
+ r = @db.queryForArray( "SELECT value, timestamp FROM value_tbl WHERE metric_id = ? ORDER BY timestamp DESC LIMIT 1", [@id] )
37
+ return massage(r)
38
+ rescue FluidDb::NoDataFoundError=>e
39
+ return nil
40
+ end
41
+
42
+ def range( dateRange )
43
+ return q( "SELECT value, timestamp FROM value_tbl WHERE metric_id = ? AND timestamp >= ? AND timestamp <= ? ORDER BY timestamp DESC", [@id, dateRange.begin, dateRange.end ] )
44
+ end
45
+
46
+ def after( t )
47
+ return q( "SELECT value, timestamp FROM value_tbl WHERE metric_id = ? AND timestamp > ? ORDER BY timestamp DESC", [@id, t ] )
48
+ end
49
+
50
+ def firstAfter( t )
51
+ return massage(@db.queryForArray( "SELECT value, timestamp FROM value_tbl WHERE metric_id = ? AND timestamp > ? ORDER BY timestamp ASC LIMIT 1", [@id, t ] ))
52
+ rescue FluidDb::NoDataFoundError=>e
53
+ return nil
54
+ end
55
+
56
+ end
57
+
58
+ end
@@ -0,0 +1,28 @@
1
+ module MetricDb
2
+
3
+ require "MetricDb/Server"
4
+
5
+ class FluidDb_Server<Server
6
+
7
+ def initialize( uri )
8
+ super()
9
+ @db = FluidDb.Db( uri )
10
+ end
11
+
12
+ def getNewMetric( name )
13
+ id = nil
14
+ begin
15
+ id = @db.queryForValue( "SELECT id FROM metric_tbl WHERE name = ?", [name] )
16
+
17
+ rescue FluidDb::NoDataFoundError=>e
18
+
19
+ id = @db.queryForValue( "SELECT NEXTVAL( 'metric_seq' )", [] )
20
+ @db.execute( "INSERT INTO metric_tbl( id, name ) VALUES ( ?, ? )", [id.to_i, name])
21
+ end
22
+
23
+ return FluidDb_Metric.new( @db, id, name )
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -0,0 +1,2 @@
1
+ require "MetricDb/InMemory/Metric"
2
+ require "MetricDb/InMemory/Server"
@@ -0,0 +1,49 @@
1
+ module MetricDb
2
+
3
+ require "MetricDb/Metric"
4
+
5
+ class InMemory_Metric<Metric
6
+ def initialize
7
+ @list = Array.new
8
+ end
9
+
10
+ def push( value )
11
+ @list.unshift Hash["value", value, "timestamp", Time.now]
12
+ return true
13
+ end
14
+
15
+ def get()
16
+ return @list[0]
17
+ end
18
+
19
+ def range( dateRange )
20
+ l = Array.new
21
+ @list.each do |r|
22
+ l << r if dateRange.cover?(r["timestamp"])
23
+ end
24
+
25
+ return l
26
+ end
27
+
28
+ def after( t )
29
+ l = Array.new
30
+ @list.each do |r|
31
+ l << r if r["timestamp"] > t
32
+ end
33
+
34
+ return l
35
+ end
36
+
37
+ def firstAfter( t )
38
+ last = nil
39
+ @list.each do |r|
40
+ last = r if r["timestamp"] > t
41
+ break if r["timestamp"] <= t
42
+ end
43
+
44
+ return last
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -0,0 +1,13 @@
1
+ module MetricDb
2
+
3
+ require "MetricDb/Server"
4
+
5
+ class InMemory_Server<Server
6
+
7
+ def getNewMetric( name )
8
+ return InMemory_Metric.new
9
+ end
10
+
11
+ end
12
+
13
+ end
@@ -0,0 +1,6 @@
1
+ module MetricDb
2
+
3
+ class Metric
4
+ end
5
+
6
+ end
@@ -0,0 +1,39 @@
1
+ module MetricDb
2
+
3
+ class MetricBag
4
+
5
+ def initialize( list )
6
+ @l = list
7
+ end
8
+
9
+ def push( value )
10
+ answer = Array.new
11
+ @l.each do |metric|
12
+ answer << metric.push(value)
13
+ end
14
+ return answer
15
+ end
16
+
17
+ def get()
18
+ answer = Array.new
19
+ @l.each do |metric|
20
+ answer << metric.get()["value"]
21
+ end
22
+
23
+ return answer[0] if answer.length == 1
24
+ return answer
25
+ end
26
+
27
+ def method_missing( methodName, *args )
28
+ answer = Array.new
29
+ @l.each do |metric|
30
+ answer << metric.send( methodName, args[0] )
31
+ end
32
+
33
+ return answer[0] if answer.length == 1
34
+
35
+ return answer
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,25 @@
1
+ module MetricDb
2
+
3
+ require "MetricDb/MetricBag"
4
+
5
+ class Server
6
+
7
+ def initialize
8
+ @h = Hash.new
9
+ end
10
+
11
+ def getNewMetric( name )
12
+ raise "Method, getNewMetric, needs to be implemented"
13
+ end
14
+
15
+ def [](*args)
16
+ l = Array.new
17
+ args.each do |name|
18
+ @h[name] = self.getNewMetric( name ) if @h[name].nil?
19
+ l << @h[name]
20
+ end
21
+ return MetricBag.new(l)
22
+ end
23
+ end
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: metricdb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Guy Irvine
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-07 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A semantic layer for db interaction
15
+ email: guy@guyirvine.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/MetricDb/Db.rb
21
+ - lib/MetricDb/FluidDb/Metric.rb
22
+ - lib/MetricDb/FluidDb/Server.rb
23
+ - lib/MetricDb/FluidDb.rb
24
+ - lib/MetricDb/InMemory/Metric.rb
25
+ - lib/MetricDb/InMemory/Server.rb
26
+ - lib/MetricDb/InMemory.rb
27
+ - lib/MetricDb/Metric.rb
28
+ - lib/MetricDb/MetricBag.rb
29
+ - lib/MetricDb/Server.rb
30
+ - lib/MetricDb.rb
31
+ - README.md
32
+ homepage: http://rubygems.org/gems/metricdb
33
+ licenses: []
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubyforge_project:
52
+ rubygems_version: 1.8.11
53
+ signing_key:
54
+ specification_version: 3
55
+ summary: MetricDB
56
+ test_files: []
57
+ has_rdoc: