{-# LANGUAGE DataKinds #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
module Database.InfluxDB.Write.UDP
(
write
, writeBatch
, writeByteString
, WriteParams
, writeParams
, socket
, sockAddr
, Types.precision
) where
import Control.Lens
import Network.Socket (SockAddr, Socket)
import Network.Socket.ByteString (sendManyTo)
import qualified Data.ByteString.Lazy as BL
import Database.InfluxDB.Line
import Database.InfluxDB.Types as Types
data WriteParams = WriteParams
{ _socket :: !Socket
, _sockAddr :: !SockAddr
, _precision :: !(Precision 'WriteRequest)
}
writeParams :: Socket -> SockAddr -> WriteParams
writeParams _socket _sockAddr = WriteParams
{ _precision = Nanosecond
, ..
}
write
:: Timestamp time
=> WriteParams
-> Line time
-> IO ()
write p@WriteParams {_precision} =
writeByteString p . encodeLine (roundTo _precision)
writeBatch
:: (Timestamp time, Foldable f)
=> WriteParams
-> f (Line time)
-> IO ()
writeBatch p@WriteParams {_precision} =
writeByteString p . encodeLines (roundTo _precision)
writeByteString :: WriteParams -> BL.ByteString -> IO ()
writeByteString WriteParams {..} payload =
sendManyTo _socket (BL.toChunks payload) _sockAddr
makeLensesWith (lensRules & generateSignatures .~ False) ''WriteParams
socket :: Lens' WriteParams Socket
sockAddr :: Lens' WriteParams SockAddr
precision :: Lens' WriteParams (Precision 'WriteRequest)
instance HasPrecision 'WriteRequest WriteParams where
precision = Database.InfluxDB.Write.UDP.precision