{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
module Database.InfluxDB.Line
(
Line(Line)
, measurement
, tagSet
, fieldSet
, timestamp
, buildLine
, buildLines
, encodeLine
, encodeLines
, LineField
, Field(..)
, Precision(..)
) where
import Data.List (intersperse)
import Data.Int (Int64)
import Data.Monoid
import Prelude
import Control.Lens
import Data.Map (Map)
import Data.Text (Text)
import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Lazy as L
import qualified Data.Map.Strict as Map
import qualified Data.Text.Encoding as TE
import Database.InfluxDB.Internal.Text
import Database.InfluxDB.Types
data Line time = Line
{ Line time -> Measurement
_measurement :: !Measurement
, Line time -> Map Key Key
_tagSet :: !(Map Key Key)
, Line time -> Map Key LineField
_fieldSet :: !(Map Key LineField)
, Line time -> Maybe time
_timestamp :: !(Maybe time)
} deriving Int -> Line time -> ShowS
[Line time] -> ShowS
Line time -> String
(Int -> Line time -> ShowS)
-> (Line time -> String)
-> ([Line time] -> ShowS)
-> Show (Line time)
forall time. Show time => Int -> Line time -> ShowS
forall time. Show time => [Line time] -> ShowS
forall time. Show time => Line time -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Line time] -> ShowS
$cshowList :: forall time. Show time => [Line time] -> ShowS
show :: Line time -> String
$cshow :: forall time. Show time => Line time -> String
showsPrec :: Int -> Line time -> ShowS
$cshowsPrec :: forall time. Show time => Int -> Line time -> ShowS
Show
encodeLine
:: (time -> Int64)
-> Line time
-> L.ByteString
encodeLine :: (time -> Int64) -> Line time -> ByteString
encodeLine time -> Int64
toTimestamp = Builder -> ByteString
B.toLazyByteString (Builder -> ByteString)
-> (Line time -> Builder) -> Line time -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (time -> Int64) -> Line time -> Builder
forall time. (time -> Int64) -> Line time -> Builder
buildLine time -> Int64
toTimestamp
encodeLines
:: Foldable f
=> (time -> Int64)
-> f (Line time)
-> L.ByteString
encodeLines :: (time -> Int64) -> f (Line time) -> ByteString
encodeLines time -> Int64
toTimestamp = Builder -> ByteString
B.toLazyByteString (Builder -> ByteString)
-> (f (Line time) -> Builder) -> f (Line time) -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (time -> Int64) -> f (Line time) -> Builder
forall (f :: * -> *) time.
Foldable f =>
(time -> Int64) -> f (Line time) -> Builder
buildLines time -> Int64
toTimestamp
buildLine
:: (time -> Int64)
-> Line time
-> B.Builder
buildLine :: (time -> Int64) -> Line time -> Builder
buildLine time -> Int64
toTimestamp Line {Maybe time
Map Key LineField
Map Key Key
Measurement
_timestamp :: Maybe time
_fieldSet :: Map Key LineField
_tagSet :: Map Key Key
_measurement :: Measurement
_timestamp :: forall time. Line time -> Maybe time
_fieldSet :: forall time. Line time -> Map Key LineField
_tagSet :: forall time. Line time -> Map Key Key
_measurement :: forall time. Line time -> Measurement
..} =
Builder
key Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
" " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
fields Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder -> (Builder -> Builder) -> Maybe Builder -> Builder
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Builder
"" (Builder
" " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<>) Maybe Builder
timestamp
where
measurement :: Builder
measurement = Text -> Builder
TE.encodeUtf8Builder (Text -> Builder) -> Text -> Builder
forall a b. (a -> b) -> a -> b
$ Measurement -> Text
escapeMeasurement Measurement
_measurement
tags :: Builder
tags = (Key -> Builder) -> Map Key Key -> Builder
forall t. (t -> Builder) -> Map Key t -> Builder
buildMap (Text -> Builder
TE.encodeUtf8Builder (Text -> Builder) -> (Key -> Text) -> Key -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Key -> Text
escapeKey) Map Key Key
_tagSet
key :: Builder
key = if Map Key Key -> Bool
forall k a. Map k a -> Bool
Map.null Map Key Key
_tagSet
then Builder
measurement
else Builder
measurement Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"," Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
tags
fields :: Builder
fields = (LineField -> Builder) -> Map Key LineField -> Builder
forall t. (t -> Builder) -> Map Key t -> Builder
buildMap LineField -> Builder
buildFieldValue Map Key LineField
_fieldSet
timestamp :: Maybe Builder
timestamp = Int64 -> Builder
B.int64Dec (Int64 -> Builder) -> (time -> Int64) -> time -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. time -> Int64
toTimestamp (time -> Builder) -> Maybe time -> Maybe Builder
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe time
_timestamp
buildMap :: (t -> Builder) -> Map Key t -> Builder
buildMap t -> Builder
encodeVal =
[Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat ([Builder] -> Builder)
-> (Map Key t -> [Builder]) -> Map Key t -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> [Builder] -> [Builder]
forall a. a -> [a] -> [a]
intersperse Builder
"," ([Builder] -> [Builder])
-> (Map Key t -> [Builder]) -> Map Key t -> [Builder]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Key, t) -> Builder) -> [(Key, t)] -> [Builder]
forall a b. (a -> b) -> [a] -> [b]
map (Key, t) -> Builder
encodeKeyVal ([(Key, t)] -> [Builder])
-> (Map Key t -> [(Key, t)]) -> Map Key t -> [Builder]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Map Key t -> [(Key, t)]
forall k a. Map k a -> [(k, a)]
Map.toList
where
encodeKeyVal :: (Key, t) -> Builder
encodeKeyVal (Key
name, t
val) = [Builder] -> Builder
forall a. Monoid a => [a] -> a
mconcat
[ Text -> Builder
TE.encodeUtf8Builder (Text -> Builder) -> Text -> Builder
forall a b. (a -> b) -> a -> b
$ Key -> Text
escapeKey Key
name
, Builder
"="
, t -> Builder
encodeVal t
val
]
escapeKey :: Key -> Text
escapeKey :: Key -> Text
escapeKey (Key Text
text) = Text -> Text
escapeCommas (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
escapeEqualSigns (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
escapeSpaces Text
text
escapeMeasurement :: Measurement -> Text
escapeMeasurement :: Measurement -> Text
escapeMeasurement (Measurement Text
text) = Text -> Text
escapeCommas (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
escapeSpaces Text
text
escapeStringField :: Text -> Text
escapeStringField :: Text -> Text
escapeStringField = Text -> Text
escapeDoubleQuotes (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
escapeBackslashes
buildFieldValue :: LineField -> B.Builder
buildFieldValue :: LineField -> Builder
buildFieldValue = \case
FieldInt Int64
i -> Int64 -> Builder
B.int64Dec Int64
i Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"i"
FieldFloat Double
d -> Double -> Builder
B.doubleDec Double
d
FieldString Text
t -> Builder
"\"" Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Text -> Builder
TE.encodeUtf8Builder (Text -> Text
escapeStringField Text
t) Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"\""
FieldBool Bool
b -> if Bool
b then Builder
"true" else Builder
"false"
buildLines
:: Foldable f
=> (time -> Int64)
-> f (Line time)
-> B.Builder
buildLines :: (time -> Int64) -> f (Line time) -> Builder
buildLines time -> Int64
toTimestamp = (Line time -> Builder) -> f (Line time) -> Builder
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ((Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Builder
"\n") (Builder -> Builder)
-> (Line time -> Builder) -> Line time -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (time -> Int64) -> Line time -> Builder
forall time. (time -> Int64) -> Line time -> Builder
buildLine time -> Int64
toTimestamp)
makeLensesWith (lensRules & generateSignatures .~ False) ''Line
measurement :: Lens' (Line time) Measurement
tagSet :: Lens' (Line time) (Map Key Key)
fieldSet :: Lens' (Line time) (Map Key LineField)
timestamp :: Lens' (Line time) (Maybe time)