-
Notifications
You must be signed in to change notification settings - Fork 15
Step examples
The following examples demonstrate basic functionality scripting techniques to use with the ruby scripting step.
The following script converts the existing field text
to upper case, and adds some new fields to the row stream. Please note how the output fields configuration matches the keys of the resulting hash.
The following script implements a simple counter and places is in output field counter
.
# initialize counter variable
my_counter ||= 0
# increment counter variable
my_counter += 1
# set field counter to the value of counter variable
$row["counter"] = my_counter
# evaluate to a row hash
$row
The following script duplicates any incoming rows.
# evaluate to an array of row hashes
[$row, $row]
The following script places a field from the previous row into the current row.
# initialize previous row variable
prev_row ||= nil
# if there's a previous row put its "testfield" value into "prev_value"
$row["prev_value"] = prev_row["testfield"] unless prev_row == nil
# remember this row as the previous row now
prev_row = $row
# evaluate to the row hash
$row
The following script logs a message for every processed row:
# write to basic log
$step.log_basic "I'm looking at a #{$row['animal']} here. Who let that in?"
# evaluate to the row to pass it on
$row
This script aggregates the total goals scored during a game of four quarters. Input rows come in consequtive groups of four, one per quarter. This script aggregates the results of the four quarters into a single game record.
# place the current row and the next 3 rows into an array
game = [$row] + $input.read(3)
total_goals = game.reduce(0) {|sum, row| sum + row["goals"]}
# evaluate to the game id and total goals
{
"game_id" => $row["game_id"],
"total_goals" => total_goals
}
This script parses an XML file using the ruby standard library, and writes each record it finds into the output stream.
require 'rexml/document'
include REXML
# The demo file is in <plugin-dir>/step/sample_files/system_monitor.xml
xml_file = File.join($step.plugin_dir, "step", "sample_files", "system_monitor.xml")
# put the xml file into the parser
doc = Document.new(File.new(xml_file))
# find the nodes and convert them to appropriate types
system_name = XPath.first(doc, "/data/system").text
times = XPath.match(doc, "/data/metric[@name = 'time']/value").map{|n| Time.at n.text.to_i}
active_cpu = XPath.match(doc, "/data/metric[@name = 'active_cpu']/value").map{|n| n.text.to_i}
cpu_util = XPath.match(doc, "/data/metric[@name = 'cpu_util']/value").map{|n| n.text.to_f}
# generate the output rows
(0...swap_util.size).each do |i|
$output << {
"system_name" => system_name,
"times" => times[i],
"active_cpu" => active_cpu[i],
"cpu_util" => cpu_util[i]
}
end
# evaluate to nil, all rows have been written
nil