json2graphite 0.0.6 → 0.0.7
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.
- data/bin/json2graphite +1 -1
- data/lib/json2graphite.rb +31 -24
- metadata +2 -2
data/bin/json2graphite
CHANGED
data/lib/json2graphite.rb
CHANGED
@@ -1,40 +1,47 @@
|
|
1
1
|
module Json2Graphite
|
2
2
|
module_function
|
3
3
|
|
4
|
-
#
|
5
|
-
#
|
4
|
+
# Converts a hash of hashes into dot notaion used by graphite targets. Takes
|
5
|
+
# optional time argument, defaulting to now.
|
6
6
|
|
7
|
-
def
|
8
|
-
obj.each do |key,value|
|
9
|
-
case value
|
10
|
-
when Hash
|
11
|
-
walk_the_forrest(value, [*path, key], &blk)
|
12
|
-
else
|
13
|
-
blk.call("#{[*path, key].join('.')}",value)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# Converts a hash of hashes into dot notaion used by graphite targets and
|
19
|
-
# outputs to STDOUT. Takes optional time argument, defaulting to now.
|
20
|
-
|
21
|
-
def to_graphite (hash, time=Time.now.to_i)
|
7
|
+
def dump(hash, time = Time.now.to_i)
|
22
8
|
data = []
|
23
9
|
raise "Hash was not received" unless hash.is_a? Hash
|
24
|
-
walk_the_forrest(hash)
|
10
|
+
walk_the_forrest(hash) do |target, value|
|
25
11
|
data << "#{target} #{value} #{time}"
|
26
|
-
|
12
|
+
end
|
27
13
|
data
|
28
14
|
end
|
29
15
|
|
30
|
-
|
31
|
-
def get_graphite (hash, time=Time.now.to_i)
|
32
|
-
raise "Hash was not received" unless hash.is_a? Hash
|
16
|
+
def dump_as_hash(hash, time = Time.now.to_i)
|
33
17
|
data = []
|
34
|
-
|
18
|
+
raise "Hash was not received" unless hash.is_a? Hash
|
19
|
+
walk_the_forrest(hash) do |target, value|
|
35
20
|
data << { :target => target, :value => value, :time => time }
|
36
|
-
|
21
|
+
end
|
37
22
|
data
|
38
23
|
end
|
39
24
|
|
25
|
+
# For backward compatibility with other things
|
26
|
+
alias_method :to_graphite, :dump
|
27
|
+
alias_method :get_graphite, :dump_as_hash
|
28
|
+
|
29
|
+
# DOCUMENT THIS!!!
|
30
|
+
def walk_the_forrest (obj=self, path=[], &blk)
|
31
|
+
obj.each do |key,value|
|
32
|
+
case value
|
33
|
+
when Hash
|
34
|
+
walk_the_forrest(value, [key, *path].reverse, &blk)
|
35
|
+
else
|
36
|
+
blk.call("#{path.join('.')}.#{key}",value)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Hash
|
43
|
+
|
44
|
+
def to_graphite
|
45
|
+
Json2Graphite.dump(self)
|
46
|
+
end
|
40
47
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json2graphite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11
|
12
|
+
date: 2012-12-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|