{-# LANGUAGE OverloadedStrings #-}

-- | Typed encoding and decoding for PostgreSQL queries.
--
--   * 'RowCodec' — a free applicative that describes how to decode result rows.
--     Each backend interprets it natively (postgresql-simple positional fields,
--     hasql typed decoders, orville named marshallers).
--
--   * 'Params' — a list of typed parameters for query execution.
--     Each 'SomeParam' pairs a 'ParamType' with its value, letting backends
--     encode without an untyped intermediary.
--
-- Both sides are built from the same 'Col' GADT, ensuring type consistency
-- between what we send and what we read back.
module Arbiter.Core.Codec
  ( -- * Column types
    Col (..)
  , NullCol (..)

    -- * Row decoding
  , RowCodec
  , col
  , ncol
  , pureVal
  , runCodec
  , codecColumns

    -- * Parameter encoding
  , ParamType (..)
  , SomeParam (..)
  , Params
  , pval
  , pnul
  , parr
  , pnarr

    -- * Job codecs
  , jobRowCodec
  , dlqRowCodec
  , countCodec
  , statsRowCodec
  ) where

import Control.Applicative.Free.Final (Ap, liftAp, runAp, runAp_)
import Data.Aeson (Value)
import Data.Int (Int32, Int64)
import Data.Maybe (isJust)
import Data.Text (Text)
import Data.Time (UTCTime)

import Arbiter.Core.Job.Types (DedupKey (..), Job (..))

-- | Scalar PostgreSQL column type. The GADT tag recovers the Haskell type.
data Col a where
  CInt4 :: Col Int32
  CInt8 :: Col Int64
  CText :: Col Text
  CBool :: Col Bool
  CTimestamptz :: Col UTCTime
  CJsonb :: Col Value
  CFloat8 :: Col Double

-- | A named column with nullability. Carries the column name for
-- backends that use name-based decoding (e.g. orville).
data NullCol a where
  NotNull :: Text -> Col a -> NullCol a
  Nullable :: Text -> Col a -> NullCol (Maybe a)

-- | Free applicative over 'NullCol'. Backends interpret this into
-- their native row parser by pattern-matching on each 'NullCol'.
type RowCodec = Ap NullCol

-- | A non-nullable column.
col :: Text -> Col a -> RowCodec a
col :: forall a. Text -> Col a -> RowCodec a
col Text
name Col a
c = NullCol a -> Ap NullCol a
forall (f :: * -> *) a. f a -> Ap f a
liftAp (Text -> Col a -> NullCol a
forall a. Text -> Col a -> NullCol a
NotNull Text
name Col a
c)

-- | A nullable column.
ncol :: Text -> Col a -> RowCodec (Maybe a)
ncol :: forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
name Col a
c = NullCol (Maybe a) -> Ap NullCol (Maybe a)
forall (f :: * -> *) a. f a -> Ap f a
liftAp (Text -> Col a -> NullCol (Maybe a)
forall a. Text -> Col a -> NullCol (Maybe a)
Nullable Text
name Col a
c)

-- | Inject a pure value (not read from the database).
pureVal :: a -> RowCodec a
pureVal :: forall a. a -> RowCodec a
pureVal = a -> Ap NullCol a
forall a. a -> RowCodec a
forall (f :: * -> *) a. Applicative f => a -> f a
pure

-- | Interpret a 'RowCodec' by providing a natural transformation
-- from 'NullCol' to some 'Applicative'.
runCodec :: (Applicative f) => (forall x. NullCol x -> f x) -> RowCodec a -> f a
runCodec :: forall (f :: * -> *) a.
Applicative f =>
(forall x. NullCol x -> f x) -> RowCodec a -> f a
runCodec = (forall x. NullCol x -> f x) -> Ap NullCol a -> f a
forall (g :: * -> *) (f :: * -> *) a.
Applicative g =>
(forall x. f x -> g x) -> Ap f a -> g a
runAp

-- | Extract the column names from a codec (in order).
codecColumns :: RowCodec a -> [Text]
codecColumns :: forall a. RowCodec a -> [Text]
codecColumns = (forall a. NullCol a -> [Text]) -> Ap NullCol a -> [Text]
forall m (f :: * -> *) b.
Monoid m =>
(forall a. f a -> m) -> Ap f b -> m
runAp_ NullCol a -> [Text]
forall a. NullCol a -> [Text]
colName
  where
    colName :: NullCol a -> [Text]
    colName :: forall a. NullCol a -> [Text]
colName (NotNull Text
name Col a
_) = [Text
name]
    colName (Nullable Text
name Col a
_) = [Text
name]

-- | How a parameter is shaped: scalar, nullable, or array.
data ParamType a where
  PScalar :: Col a -> ParamType a
  PNullable :: Col a -> ParamType (Maybe a)
  PArray :: Col a -> ParamType [a]
  PNullArray :: Col a -> ParamType [Maybe a]

-- | An existentially-typed parameter: a 'ParamType' paired with its value.
data SomeParam where
  SomeParam :: ParamType a -> a -> SomeParam

-- | Positional query parameters.
type Params = [SomeParam]

pval :: Col a -> a -> SomeParam
pval :: forall a. Col a -> a -> SomeParam
pval Col a
c a
v = ParamType a -> a -> SomeParam
forall a. ParamType a -> a -> SomeParam
SomeParam (Col a -> ParamType a
forall a. Col a -> ParamType a
PScalar Col a
c) a
v

pnul :: Col a -> Maybe a -> SomeParam
pnul :: forall a. Col a -> Maybe a -> SomeParam
pnul Col a
c Maybe a
v = ParamType (Maybe a) -> Maybe a -> SomeParam
forall a. ParamType a -> a -> SomeParam
SomeParam (Col a -> ParamType (Maybe a)
forall a. Col a -> ParamType (Maybe a)
PNullable Col a
c) Maybe a
v

parr :: Col a -> [a] -> SomeParam
parr :: forall a. Col a -> [a] -> SomeParam
parr Col a
c [a]
v = ParamType [a] -> [a] -> SomeParam
forall a. ParamType a -> a -> SomeParam
SomeParam (Col a -> ParamType [a]
forall a. Col a -> ParamType [a]
PArray Col a
c) [a]
v

pnarr :: Col a -> [Maybe a] -> SomeParam
pnarr :: forall a. Col a -> [Maybe a] -> SomeParam
pnarr Col a
c [Maybe a]
v = ParamType [Maybe a] -> [Maybe a] -> SomeParam
forall a. ParamType a -> a -> SomeParam
SomeParam (Col a -> ParamType [Maybe a]
forall a. Col a -> ParamType [Maybe a]
PNullArray Col a
c) [Maybe a]
v

-- ---------------------------------------------------------------------------
-- Job codecs
-- ---------------------------------------------------------------------------

jobRowCodec :: Text -> RowCodec (Job Value Int64 Text UTCTime)
jobRowCodec :: Text -> RowCodec (Job Value Int64 Text UTCTime)
jobRowCodec Text
queueName =
  Int64
-> Value
-> Text
-> Maybe Text
-> UTCTime
-> Maybe UTCTime
-> Int32
-> Maybe Text
-> Int32
-> Maybe UTCTime
-> Maybe UTCTime
-> Maybe DedupKey
-> Maybe Int32
-> Maybe Int64
-> Bool
-> Bool
-> Job Value Int64 Text UTCTime
forall payload key q insertedAt.
key
-> payload
-> q
-> Maybe Text
-> insertedAt
-> Maybe UTCTime
-> Int32
-> Maybe Text
-> Int32
-> Maybe UTCTime
-> Maybe UTCTime
-> Maybe DedupKey
-> Maybe Int32
-> Maybe Int64
-> Bool
-> Bool
-> Job payload key q insertedAt
Job
    (Int64
 -> Value
 -> Text
 -> Maybe Text
 -> UTCTime
 -> Maybe UTCTime
 -> Int32
 -> Maybe Text
 -> Int32
 -> Maybe UTCTime
 -> Maybe UTCTime
 -> Maybe DedupKey
 -> Maybe Int32
 -> Maybe Int64
 -> Bool
 -> Bool
 -> Job Value Int64 Text UTCTime)
-> Ap NullCol Int64
-> Ap
     NullCol
     (Value
      -> Text
      -> Maybe Text
      -> UTCTime
      -> Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Col Int64 -> Ap NullCol Int64
forall a. Text -> Col a -> RowCodec a
col Text
"id" Col Int64
CInt8
    Ap
  NullCol
  (Value
   -> Text
   -> Maybe Text
   -> UTCTime
   -> Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol Value
-> Ap
     NullCol
     (Text
      -> Maybe Text
      -> UTCTime
      -> Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Value -> Ap NullCol Value
forall a. Text -> Col a -> RowCodec a
col Text
"payload" Col Value
CJsonb
    Ap
  NullCol
  (Text
   -> Maybe Text
   -> UTCTime
   -> Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol Text
-> Ap
     NullCol
     (Maybe Text
      -> UTCTime
      -> Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Ap NullCol Text
forall a. a -> RowCodec a
pureVal Text
queueName
    Ap
  NullCol
  (Maybe Text
   -> UTCTime
   -> Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe Text)
-> Ap
     NullCol
     (UTCTime
      -> Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Text -> Ap NullCol (Maybe Text)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"group_key" Col Text
CText
    Ap
  NullCol
  (UTCTime
   -> Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol UTCTime
-> Ap
     NullCol
     (Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol UTCTime
forall a. Text -> Col a -> RowCodec a
col Text
"inserted_at" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe UTCTime)
-> Ap
     NullCol
     (Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol (Maybe UTCTime)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"updated_at" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol Int32
-> Ap
     NullCol
     (Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int32 -> Ap NullCol Int32
forall a. Text -> Col a -> RowCodec a
col Text
"attempts" Col Int32
CInt4
    Ap
  NullCol
  (Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe Text)
-> Ap
     NullCol
     (Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Text -> Ap NullCol (Maybe Text)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"last_error" Col Text
CText
    Ap
  NullCol
  (Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol Int32
-> Ap
     NullCol
     (Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int32 -> Ap NullCol Int32
forall a. Text -> Col a -> RowCodec a
col Text
"priority" Col Int32
CInt4
    Ap
  NullCol
  (Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe UTCTime)
-> Ap
     NullCol
     (Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol (Maybe UTCTime)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"last_attempted_at" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe UTCTime)
-> Ap
     NullCol
     (Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol (Maybe UTCTime)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"not_visible_until" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe DedupKey)
-> Ap
     NullCol
     (Maybe Int32
      -> Maybe Int64 -> Bool -> Bool -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Ap NullCol (Maybe DedupKey)
dedupKeyCodec
    Ap
  NullCol
  (Maybe Int32
   -> Maybe Int64 -> Bool -> Bool -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe Int32)
-> Ap
     NullCol
     (Maybe Int64 -> Bool -> Bool -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int32 -> Ap NullCol (Maybe Int32)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"max_attempts" Col Int32
CInt4
    Ap
  NullCol
  (Maybe Int64 -> Bool -> Bool -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe Int64)
-> Ap NullCol (Bool -> Bool -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int64 -> Ap NullCol (Maybe Int64)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"parent_id" Col Int64
CInt8
    Ap NullCol (Bool -> Bool -> Job Value Int64 Text UTCTime)
-> Ap NullCol Bool
-> Ap NullCol (Bool -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Maybe Value -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Value -> Bool)
-> Ap NullCol (Maybe Value) -> Ap NullCol Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Col Value -> Ap NullCol (Maybe Value)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"parent_state" Col Value
CJsonb)
    Ap NullCol (Bool -> Job Value Int64 Text UTCTime)
-> Ap NullCol Bool -> RowCodec (Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Bool -> Ap NullCol Bool
forall a. Text -> Col a -> RowCodec a
col Text
"suspended" Col Bool
CBool

dedupKeyCodec :: RowCodec (Maybe DedupKey)
dedupKeyCodec :: Ap NullCol (Maybe DedupKey)
dedupKeyCodec = Maybe Text -> Maybe Text -> Maybe DedupKey
forall {a}.
(Eq a, IsString a) =>
Maybe Text -> Maybe a -> Maybe DedupKey
toDedupKey (Maybe Text -> Maybe Text -> Maybe DedupKey)
-> Ap NullCol (Maybe Text)
-> Ap NullCol (Maybe Text -> Maybe DedupKey)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Col Text -> Ap NullCol (Maybe Text)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"dedup_key" Col Text
CText Ap NullCol (Maybe Text -> Maybe DedupKey)
-> Ap NullCol (Maybe Text) -> Ap NullCol (Maybe DedupKey)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Text -> Ap NullCol (Maybe Text)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"dedup_strategy" Col Text
CText
  where
    toDedupKey :: Maybe Text -> Maybe a -> Maybe DedupKey
toDedupKey Maybe Text
Nothing Maybe a
_ = Maybe DedupKey
forall a. Maybe a
Nothing
    toDedupKey (Just Text
k) (Just a
"replace") = DedupKey -> Maybe DedupKey
forall a. a -> Maybe a
Just (Text -> DedupKey
ReplaceDuplicate Text
k)
    toDedupKey (Just Text
k) Maybe a
_ = DedupKey -> Maybe DedupKey
forall a. a -> Maybe a
Just (Text -> DedupKey
IgnoreDuplicate Text
k)

dlqRowCodec :: Text -> RowCodec (Int64, UTCTime, Job Value Int64 Text UTCTime)
dlqRowCodec :: Text -> RowCodec (Int64, UTCTime, Job Value Int64 Text UTCTime)
dlqRowCodec Text
queueName =
  (,,)
    (Int64
 -> UTCTime
 -> Job Value Int64 Text UTCTime
 -> (Int64, UTCTime, Job Value Int64 Text UTCTime))
-> Ap NullCol Int64
-> Ap
     NullCol
     (UTCTime
      -> Job Value Int64 Text UTCTime
      -> (Int64, UTCTime, Job Value Int64 Text UTCTime))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Col Int64 -> Ap NullCol Int64
forall a. Text -> Col a -> RowCodec a
col Text
"id" Col Int64
CInt8
    Ap
  NullCol
  (UTCTime
   -> Job Value Int64 Text UTCTime
   -> (Int64, UTCTime, Job Value Int64 Text UTCTime))
-> Ap NullCol UTCTime
-> Ap
     NullCol
     (Job Value Int64 Text UTCTime
      -> (Int64, UTCTime, Job Value Int64 Text UTCTime))
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol UTCTime
forall a. Text -> Col a -> RowCodec a
col Text
"failed_at" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Job Value Int64 Text UTCTime
   -> (Int64, UTCTime, Job Value Int64 Text UTCTime))
-> RowCodec (Job Value Int64 Text UTCTime)
-> RowCodec (Int64, UTCTime, Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> RowCodec (Job Value Int64 Text UTCTime)
jobRowCodecWithJobId Text
queueName

jobRowCodecWithJobId :: Text -> RowCodec (Job Value Int64 Text UTCTime)
jobRowCodecWithJobId :: Text -> RowCodec (Job Value Int64 Text UTCTime)
jobRowCodecWithJobId Text
queueName =
  Int64
-> Value
-> Text
-> Maybe Text
-> UTCTime
-> Maybe UTCTime
-> Int32
-> Maybe Text
-> Int32
-> Maybe UTCTime
-> Maybe UTCTime
-> Maybe DedupKey
-> Maybe Int32
-> Maybe Int64
-> Bool
-> Bool
-> Job Value Int64 Text UTCTime
forall payload key q insertedAt.
key
-> payload
-> q
-> Maybe Text
-> insertedAt
-> Maybe UTCTime
-> Int32
-> Maybe Text
-> Int32
-> Maybe UTCTime
-> Maybe UTCTime
-> Maybe DedupKey
-> Maybe Int32
-> Maybe Int64
-> Bool
-> Bool
-> Job payload key q insertedAt
Job
    (Int64
 -> Value
 -> Text
 -> Maybe Text
 -> UTCTime
 -> Maybe UTCTime
 -> Int32
 -> Maybe Text
 -> Int32
 -> Maybe UTCTime
 -> Maybe UTCTime
 -> Maybe DedupKey
 -> Maybe Int32
 -> Maybe Int64
 -> Bool
 -> Bool
 -> Job Value Int64 Text UTCTime)
-> Ap NullCol Int64
-> Ap
     NullCol
     (Value
      -> Text
      -> Maybe Text
      -> UTCTime
      -> Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Col Int64 -> Ap NullCol Int64
forall a. Text -> Col a -> RowCodec a
col Text
"job_id" Col Int64
CInt8
    Ap
  NullCol
  (Value
   -> Text
   -> Maybe Text
   -> UTCTime
   -> Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol Value
-> Ap
     NullCol
     (Text
      -> Maybe Text
      -> UTCTime
      -> Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Value -> Ap NullCol Value
forall a. Text -> Col a -> RowCodec a
col Text
"payload" Col Value
CJsonb
    Ap
  NullCol
  (Text
   -> Maybe Text
   -> UTCTime
   -> Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol Text
-> Ap
     NullCol
     (Maybe Text
      -> UTCTime
      -> Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Ap NullCol Text
forall a. a -> RowCodec a
pureVal Text
queueName
    Ap
  NullCol
  (Maybe Text
   -> UTCTime
   -> Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe Text)
-> Ap
     NullCol
     (UTCTime
      -> Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Text -> Ap NullCol (Maybe Text)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"group_key" Col Text
CText
    Ap
  NullCol
  (UTCTime
   -> Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol UTCTime
-> Ap
     NullCol
     (Maybe UTCTime
      -> Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol UTCTime
forall a. Text -> Col a -> RowCodec a
col Text
"inserted_at" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Maybe UTCTime
   -> Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe UTCTime)
-> Ap
     NullCol
     (Int32
      -> Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol (Maybe UTCTime)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"updated_at" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Int32
   -> Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol Int32
-> Ap
     NullCol
     (Maybe Text
      -> Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int32 -> Ap NullCol Int32
forall a. Text -> Col a -> RowCodec a
col Text
"attempts" Col Int32
CInt4
    Ap
  NullCol
  (Maybe Text
   -> Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe Text)
-> Ap
     NullCol
     (Int32
      -> Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Text -> Ap NullCol (Maybe Text)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"last_error" Col Text
CText
    Ap
  NullCol
  (Int32
   -> Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol Int32
-> Ap
     NullCol
     (Maybe UTCTime
      -> Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int32 -> Ap NullCol Int32
forall a. Text -> Col a -> RowCodec a
col Text
"priority" Col Int32
CInt4
    Ap
  NullCol
  (Maybe UTCTime
   -> Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe UTCTime)
-> Ap
     NullCol
     (Maybe UTCTime
      -> Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol (Maybe UTCTime)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"last_attempted_at" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Maybe UTCTime
   -> Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe UTCTime)
-> Ap
     NullCol
     (Maybe DedupKey
      -> Maybe Int32
      -> Maybe Int64
      -> Bool
      -> Bool
      -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col UTCTime -> Ap NullCol (Maybe UTCTime)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"not_visible_until" Col UTCTime
CTimestamptz
    Ap
  NullCol
  (Maybe DedupKey
   -> Maybe Int32
   -> Maybe Int64
   -> Bool
   -> Bool
   -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe DedupKey)
-> Ap
     NullCol
     (Maybe Int32
      -> Maybe Int64 -> Bool -> Bool -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Ap NullCol (Maybe DedupKey)
dedupKeyCodec
    Ap
  NullCol
  (Maybe Int32
   -> Maybe Int64 -> Bool -> Bool -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe Int32)
-> Ap
     NullCol
     (Maybe Int64 -> Bool -> Bool -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int32 -> Ap NullCol (Maybe Int32)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"max_attempts" Col Int32
CInt4
    Ap
  NullCol
  (Maybe Int64 -> Bool -> Bool -> Job Value Int64 Text UTCTime)
-> Ap NullCol (Maybe Int64)
-> Ap NullCol (Bool -> Bool -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int64 -> Ap NullCol (Maybe Int64)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"parent_id" Col Int64
CInt8
    Ap NullCol (Bool -> Bool -> Job Value Int64 Text UTCTime)
-> Ap NullCol Bool
-> Ap NullCol (Bool -> Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Maybe Value -> Bool
forall a. Maybe a -> Bool
isJust (Maybe Value -> Bool)
-> Ap NullCol (Maybe Value) -> Ap NullCol Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Col Value -> Ap NullCol (Maybe Value)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"parent_state" Col Value
CJsonb)
    Ap NullCol (Bool -> Job Value Int64 Text UTCTime)
-> Ap NullCol Bool -> RowCodec (Job Value Int64 Text UTCTime)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Bool -> Ap NullCol Bool
forall a. Text -> Col a -> RowCodec a
col Text
"suspended" Col Bool
CBool

countCodec :: RowCodec Int64
countCodec :: Ap NullCol Int64
countCodec = Text -> Col Int64 -> Ap NullCol Int64
forall a. Text -> Col a -> RowCodec a
col Text
"count" Col Int64
CInt8

statsRowCodec :: RowCodec (Int64, Int64, Maybe Double)
statsRowCodec :: RowCodec (Int64, Int64, Maybe Double)
statsRowCodec =
  (,,)
    (Int64 -> Int64 -> Maybe Double -> (Int64, Int64, Maybe Double))
-> Ap NullCol Int64
-> Ap
     NullCol (Int64 -> Maybe Double -> (Int64, Int64, Maybe Double))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text -> Col Int64 -> Ap NullCol Int64
forall a. Text -> Col a -> RowCodec a
col Text
"total_jobs" Col Int64
CInt8
    Ap NullCol (Int64 -> Maybe Double -> (Int64, Int64, Maybe Double))
-> Ap NullCol Int64
-> Ap NullCol (Maybe Double -> (Int64, Int64, Maybe Double))
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Int64 -> Ap NullCol Int64
forall a. Text -> Col a -> RowCodec a
col Text
"visible_jobs" Col Int64
CInt8
    Ap NullCol (Maybe Double -> (Int64, Int64, Maybe Double))
-> Ap NullCol (Maybe Double)
-> RowCodec (Int64, Int64, Maybe Double)
forall a b. Ap NullCol (a -> b) -> Ap NullCol a -> Ap NullCol b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Text -> Col Double -> Ap NullCol (Maybe Double)
forall a. Text -> Col a -> RowCodec (Maybe a)
ncol Text
"oldest_job_age_seconds" Col Double
CFloat8