51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/irm.rb', line 51
def create_cq
hash = YAML.load_file("#{options[:database]}.yaml")
hash["retention_policies"].each_index do |retention_policy_index|
retention_policy = hash["retention_policies"][retention_policy_index]
command = "CREATE RETENTION POLICY #{retention_policy["name"]} ON #{options[:database]} DURATION #{retention_policy["duration"]} REPLICATION 1"
command << " DEFAULT" if retention_policy_index == 0
puts command
end
hash["measurements"].each_pair do |measurement,measurement_hash|
retention_policy = hash["retention_policies"][0]
retention_name = retention_policy["name"]
retention_time = retention_policy["duration"]
prefix = "SELECT "
fields = measurement_hash["fields"].collect do |field, function|
"#{function}(#{field}) AS #{field}"
end.join(", ")
suffix = %Q| INTO #{options[:database]}."#{retention_name}".#{measurement} FROM #{options[:database]}."default".#{measurement} WHERE time > '2016-03-05' AND time < now() |
tags = ""
tags = %Q|GROUP BY time(15s), "#{measurement_hash["tags"].join(%Q[", "])}" fill(none)| unless measurement_hash["tags"].to_a.empty?
puts "#{prefix}#{fields}#{suffix}#{tags}"
hash["retention_policies"][1..-1].each_index do |retention_policy_index|
retention_policy = hash["retention_policies"][retention_policy_index+1]
retention_name = retention_policy["name"]
retention_time = retention_policy["precision"]
prefix = "CREATE CONTINUOUS QUERY #{measurement}_#{retention_name} ON #{options[:database]} BEGIN SELECT "
fields = measurement_hash["fields"].collect do |field, function|
"#{function}(#{field}) AS #{field}"
end.join(", ")
suffix = %Q| INTO #{options[:database]}."#{retention_name}".#{measurement} FROM #{options[:database]}."#{hash["retention_policies"][retention_policy_index]["name"]}".#{measurement} GROUP BY time(#{retention_time}) |
tags = ""
tags = %Q|, "#{measurement_hash["tags"].join(%Q[", "])}"| unless measurement_hash["tags"].to_a.empty?
puts "#{prefix}#{fields}#{suffix}#{tags} END"
end
puts "#################################### Done with: #{measurement}"
end
end
|