{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Honeycomb.API.Types where

import Chronos
    ( builder_YmdHMS,
      timeToDatetime,
      w3c,
      Datetime,
      SubsecondPrecision(SubsecondPrecisionFixed),
      Time )
import Data.Aeson
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.Builder as TB
import Data.Word

data Event = Event
  { Event -> Maybe Word64
eventSampleRate :: !(Maybe Word64)
  , Event -> Maybe Time
eventTimestamp :: !(Maybe Time)
  , Event -> Object
eventData :: !Object
  }

instance ToJSON Event where
  toJSON :: Event -> Value
toJSON Event{Maybe Word64
Maybe Time
Object
eventData :: Object
eventTimestamp :: Maybe Time
eventSampleRate :: Maybe Word64
eventData :: Event -> Object
eventTimestamp :: Event -> Maybe Time
eventSampleRate :: Event -> Maybe Word64
..} = [Pair] -> Value
object forall a b. (a -> b) -> a -> b
$ (Key
"data" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Object
eventData) forall a. a -> [a] -> [a]
: forall a. [Maybe a] -> [a]
catMaybes
    [ (\Time
x -> Key
"time" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Datetime -> Text
encodeRFC3339 (Time -> Datetime
timeToDatetime Time
x)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Time
eventTimestamp
    , (Key
"samplerate" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Word64
eventSampleRate
    ]
  toEncoding :: Event -> Encoding
toEncoding Event{Maybe Word64
Maybe Time
Object
eventData :: Object
eventTimestamp :: Maybe Time
eventSampleRate :: Maybe Word64
eventData :: Event -> Object
eventTimestamp :: Event -> Maybe Time
eventSampleRate :: Event -> Maybe Word64
..} = Series -> Encoding
pairs forall a b. (a -> b) -> a -> b
$ forall a. Monoid a => [a] -> a
mconcat
    [ Key
"data" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Object
eventData
    , forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (\Time
x -> Key
"time" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= Datetime -> Text
encodeRFC3339 (Time -> Datetime
timeToDatetime Time
x)) Maybe Time
eventTimestamp
    , forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. Monoid a => a
mempty (Key
"samplerate" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.=) Maybe Word64
eventSampleRate
    ]

-- | Construct a 'Text' 'TB.Builder' corresponding to the ISO-8601
--   encoding of the given 'Datetime'.
builderRFC3339 :: Datetime -> TB.Builder
builderRFC3339 :: Datetime -> Builder
builderRFC3339 Datetime
dt = SubsecondPrecision -> DatetimeFormat -> Datetime -> Builder
builder_YmdHMS (Int -> SubsecondPrecision
SubsecondPrecisionFixed Int
8) DatetimeFormat
w3c Datetime
dt forall a. Semigroup a => a -> a -> a
<> Builder
"Z"

-- | Construct 'Text' corresponding to the ISO-8601
--   encoding of the given 'Datetime'.
encodeRFC3339 :: Datetime -> Text
encodeRFC3339 :: Datetime -> Text
encodeRFC3339 = Text -> Text
LT.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> Text
TB.toLazyText forall b c a. (b -> c) -> (a -> b) -> a -> c
. Datetime -> Builder
builderRFC3339