Skip to content

Commit

Permalink
refactor: split point model and serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kimburgess committed Dec 12, 2019
1 parent 2ec8718 commit 9dbd6c4
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 58 deletions.
21 changes: 0 additions & 21 deletions spec/flux/data_point_spec.cr
Expand Up @@ -26,25 +26,4 @@ describe Flux::DataPoint do
point.tags.should eq({:test => "bar"})
end
end

describe "#to_s" 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
point.to_s.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
point.to_s.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"
point.to_s.should eq("foo,test=bar a=1 #{ts}")
end
end
end
24 changes: 24 additions & 0 deletions spec/flux/line_protocol_spec.cr
@@ -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
41 changes: 4 additions & 37 deletions src/flux/data_point.cr
@@ -1,4 +1,4 @@
# Model and serialization tools for InfluxDB data points.
# Model for InfluxDB data points.
struct Flux::DataPoint
alias TagSet = Hash(Symbol, String | Symbol)

Expand Down Expand Up @@ -28,41 +28,8 @@ struct Flux::DataPoint
tags[key] = value
end

# Serializes the point to InfluxDB line protocol.
# See https://v2.docs.influxdata.com/v2.0/reference/syntax/line-protocol/
def to_s(io : IO)
io << @measurement

@tags.try(
&.each do |k, v|
io << ','
io << k
io << '='
io << v
end
)

io << ' '

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 << timestamp.to_unix
# Checks if any tags are defined for the point.
def tagged?
!@tags.nil?
end
end
50 changes: 50 additions & 0 deletions src/flux/line_protocol.cr
@@ -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

0 comments on commit 9dbd6c4

Please sign in to comment.