Skip to content
Browse files

Managing field names not supported by RRDtool.

  • Loading branch information...
1 parent 1f8cff7 commit 9dcee4bbef22fd4b2b94f7b1165a3797bcdcc6fd @JEG2 committed Jun 17, 2008
Showing with 44 additions and 6 deletions.
  1. +19 −6 lib/rrdb.rb
  2. +25 −0 test/tc_update.rb
View
25 lib/rrdb.rb
@@ -3,6 +3,8 @@
class RRDB
VERSION = "0.0.1"
+ class FieldNameConflictError < RuntimeError; end
+
def self.run_command(command)
output = `#{command} 2>&1`
if $?.success?
@@ -38,6 +40,10 @@ def self.config(hash_or_key = nil)
:data_sources => "GAUGE:600:U:U",
:round_robin_archives => Array.new
+ def self.field_name(name)
+ name.to_s.delete("^a-zA-Z0-9_")[0..18]
+ end
+
def initialize(id)
@id = id
end
@@ -57,15 +63,22 @@ def step
end
def update(time, data)
- p data
- data = Hash[*data.map { |f, v| [f.to_s, v] }.flatten]
+ safe_data = Hash[*data.map { |f, v| [self.class.field_name(f), v] }.flatten]
+ if safe_data.size != data.size or
+ safe_data.keys.any? { |f| not f.size.between?(1, 19) }
+ raise FieldNameConflictError,
+ "Your field names cannot be unambiguously converted to RRDtool " +
+ "field names (1 to 19 [a-zA-Z0-9_] characters)."
+ end
if File.exist? path
- claim_new_fields(data.keys)
+ claim_new_fields(safe_data.keys)
else
- p "Creating..."
- create_database(time, data.keys)
+ create_database(time, safe_data.keys)
+ end
+ params = fields.map do |f|
+ safe_data[f].send(safe_data[f].to_s =~ /\A\d+\./ ? :to_f : :to_i)
end
- rrdtool(:update, "'#{time.to_i}:#{fields.map { |f| data[f].send(data[f].to_s =~ /\A\d+\./ ? :to_f : :to_i) }.join(':')}'")
+ rrdtool(:update, "'#{time.to_i}:#{params.join(':')}'")
end
def fetch(field, range = Hash.new)
View
25 test/tc_update.rb
@@ -18,4 +18,29 @@ def test_update_claims_new_fields_as_needed
assert(@db.fields.include?(field), "Field not claimed in database.")
end
end
+
+ def test_unsupported_characters_are_removed_from_names
+ @db.update(Time.now, "a'b=" => 4)
+ assert(@db.fields.include?("ab"), "Trimmed field name not found.")
+ end
+
+ def test_long_field_names_are_shortened_on_entry
+ @db.update(Time.now, ("a".."z").to_a.join => 5)
+ assert( @db.fields.include?(("a".."z").to_a.join[0..18]),
+ "Shortened field name not found." )
+ end
+
+ def test_field_names_must_be_at_least_one_supported_character_long
+ assert_raise(RRDB::FieldNameConflictError) do
+ @db.update(Time.now, String.new => 6)
+ end
+ assert_raise(RRDB::FieldNameConflictError) do
+ @db.update(Time.now, "!@\#$%^&*" => 7)
+ end
+ end
+
+ def test_you_can_retrieve_the_field_name_used
+ assert_equal( ("a".."z").to_a.join[0..18],
+ RRDB.field_name(("a".."z").to_a.join.gsub(/\b/, "'")) )
+ end
end

0 comments on commit 9dcee4b

Please sign in to comment.
Something went wrong with that request. Please try again.