{-# 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
{ WriteParams -> Socket
_socket :: !Socket
, WriteParams -> SockAddr
_sockAddr :: !SockAddr
, WriteParams -> Precision 'WriteRequest
_precision :: !(Precision 'WriteRequest)
}
writeParams :: Socket -> SockAddr -> WriteParams
writeParams :: Socket -> SockAddr -> WriteParams
writeParams Socket
_socket SockAddr
_sockAddr = WriteParams :: Socket -> SockAddr -> Precision 'WriteRequest -> WriteParams
WriteParams
{ _precision :: Precision 'WriteRequest
_precision = Precision 'WriteRequest
forall (ty :: RequestType). Precision ty
Nanosecond
, Socket
SockAddr
_sockAddr :: SockAddr
_socket :: Socket
_sockAddr :: SockAddr
_socket :: Socket
..
}
write
:: Timestamp time
=> WriteParams
-> Line time
-> IO ()
write :: WriteParams -> Line time -> IO ()
write p :: WriteParams
p@WriteParams {Precision 'WriteRequest
_precision :: Precision 'WriteRequest
_precision :: WriteParams -> Precision 'WriteRequest
_precision} =
WriteParams -> ByteString -> IO ()
writeByteString WriteParams
p (ByteString -> IO ())
-> (Line time -> ByteString) -> Line time -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (time -> Int64) -> Line time -> ByteString
forall time. (time -> Int64) -> Line time -> ByteString
encodeLine (Precision 'WriteRequest -> time -> Int64
forall time.
Timestamp time =>
Precision 'WriteRequest -> time -> Int64
roundTo Precision 'WriteRequest
_precision)
writeBatch
:: (Timestamp time, Foldable f)
=> WriteParams
-> f (Line time)
-> IO ()
writeBatch :: WriteParams -> f (Line time) -> IO ()
writeBatch p :: WriteParams
p@WriteParams {Precision 'WriteRequest
_precision :: Precision 'WriteRequest
_precision :: WriteParams -> Precision 'WriteRequest
_precision} =
WriteParams -> ByteString -> IO ()
writeByteString WriteParams
p (ByteString -> IO ())
-> (f (Line time) -> ByteString) -> f (Line time) -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (time -> Int64) -> f (Line time) -> ByteString
forall (f :: * -> *) time.
Foldable f =>
(time -> Int64) -> f (Line time) -> ByteString
encodeLines (Precision 'WriteRequest -> time -> Int64
forall time.
Timestamp time =>
Precision 'WriteRequest -> time -> Int64
roundTo Precision 'WriteRequest
_precision)
writeByteString :: WriteParams -> BL.ByteString -> IO ()
writeByteString :: WriteParams -> ByteString -> IO ()
writeByteString WriteParams {Socket
SockAddr
Precision 'WriteRequest
_precision :: Precision 'WriteRequest
_sockAddr :: SockAddr
_socket :: Socket
_precision :: WriteParams -> Precision 'WriteRequest
_sockAddr :: WriteParams -> SockAddr
_socket :: WriteParams -> Socket
..} ByteString
payload =
Socket -> [ByteString] -> SockAddr -> IO ()
sendManyTo Socket
_socket (ByteString -> [ByteString]
BL.toChunks ByteString
payload) SockAddr
_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 :: (Precision 'WriteRequest -> f (Precision 'WriteRequest))
-> WriteParams -> f WriteParams
precision = (Precision 'WriteRequest -> f (Precision 'WriteRequest))
-> WriteParams -> f WriteParams
Lens' WriteParams (Precision 'WriteRequest)
Database.InfluxDB.Write.UDP.precision