Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split point model and serialization
- Loading branch information
1 parent
2ec8718
commit 9dbd6c4
Showing
4 changed files
with
78 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require "../spec_helper" | ||
|
||
describe Flux::LineProtocol do | ||
describe ".serialize" do | ||
time = Time.now | ||
ts = time.to_unix | ||
|
||
it "serializes a simple point to line protocol" do | ||
point = Flux::DataPoint.new "foo", time, a: 1 | ||
Flux::LineProtocol.serialize(point).should eq("foo a=1 #{ts}") | ||
end | ||
|
||
it "serializes a multi-field point to line protocol" do | ||
point = Flux::DataPoint.new "foo", time, a: 1, b: true | ||
Flux::LineProtocol.serialize(point).should eq("foo a=1,b=t #{ts}") | ||
end | ||
|
||
it "serializes when tags are present" do | ||
point = Flux::DataPoint.new "foo", time, a: 1 | ||
point.tag :test, "bar" | ||
Flux::LineProtocol.serialize(point).should eq("foo,test=bar a=1 #{ts}") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require "./data_point" | ||
|
||
# Tools for InfluxDB line protocol marshalling. | ||
# See https://v2.docs.influxdata.com/v2.0/reference/syntax/line-protocol/ | ||
# TODO: escape special chars | ||
module Flux::LineProtocol | ||
# Appends *point* onto *io* in line protocol format. | ||
def self.serialize(point : DataPoint, io : IO) : Nil | ||
io << point.measurement | ||
|
||
if point.tagged? | ||
point.tags.each do |k, v| | ||
io << ',' | ||
io << k | ||
io << '=' | ||
io << v | ||
end | ||
end | ||
|
||
io << ' ' | ||
|
||
point.fields.join(',', io) do |(k, v), field| | ||
field << k | ||
field << '=' | ||
case v | ||
when String | ||
field << '"' | ||
field << v | ||
field << '"' | ||
when true | ||
field << 't' | ||
when false | ||
field << 'f' | ||
else | ||
field << v | ||
end | ||
end | ||
|
||
io << ' ' | ||
|
||
io << point.timestamp.to_unix | ||
end | ||
|
||
# Serializes *point* to a line protocol row. | ||
def self.serialize(point : DataPoint) : String | ||
String.build do |io| | ||
serialize point, io | ||
end | ||
end | ||
end |