module Hasura.RQL.DDL.ScheduledTrigger
  ( runCreateCronTrigger,
    runDeleteCronTrigger,
    dropCronTriggerInMetadata,
    resolveCronTrigger,
    runCreateScheduledEvent,
    runDeleteScheduledEvent,
    runGetScheduledEvents,
    runGetEventInvocations,
    populateInitialCronTriggerEvents,
    runGetCronTriggers,
  )
where

import Data.Aeson qualified as J
import Data.Environment qualified as Env
import Data.HashMap.Strict qualified as Map
import Data.HashMap.Strict.InsOrd qualified as OMap
import Data.Time.Clock qualified as C
import Data.URL.Template (printURLTemplate)
import Hasura.Base.Error
import Hasura.EncJSON
import Hasura.Eventing.ScheduledTrigger
import Hasura.Metadata.Class
import Hasura.Prelude
import Hasura.RQL.DDL.EventTrigger (getHeaderInfosFromConf)
import Hasura.RQL.Types.Common
import Hasura.RQL.Types.EventTrigger
import Hasura.RQL.Types.Metadata
import Hasura.RQL.Types.Metadata.Object
import Hasura.RQL.Types.ScheduledTrigger
import Hasura.RQL.Types.SchemaCache
import Hasura.RQL.Types.SchemaCache.Build
import System.Cron.Types (CronSchedule)

populateInitialCronTriggerEvents ::
  ( MonadIO m,
    MonadMetadataStorageQueryAPI m
  ) =>
  CronSchedule ->
  TriggerName ->
  m ()
populateInitialCronTriggerEvents :: CronSchedule -> TriggerName -> m ()
populateInitialCronTriggerEvents CronSchedule
schedule TriggerName
triggerName = do
  UTCTime
currentTime <- IO UTCTime -> m UTCTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
C.getCurrentTime
  let scheduleTimes :: [UTCTime]
scheduleTimes = UTCTime -> Int -> CronSchedule -> [UTCTime]
generateScheduleTimes UTCTime
currentTime Int
100 CronSchedule
schedule
  [CronEventSeed] -> m ()
forall (m :: * -> *).
MonadMetadataStorage m =>
[CronEventSeed] -> m ()
insertCronEvents ([CronEventSeed] -> m ()) -> [CronEventSeed] -> m ()
forall a b. (a -> b) -> a -> b
$ (UTCTime -> CronEventSeed) -> [UTCTime] -> [CronEventSeed]
forall a b. (a -> b) -> [a] -> [b]
map (TriggerName -> UTCTime -> CronEventSeed
CronEventSeed TriggerName
triggerName) [UTCTime]
scheduleTimes
  () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()

-- | runCreateCronTrigger will update a existing cron trigger when the 'replace'
--   value is set to @true@ and when replace is @false@ a new cron trigger will
--   be created
runCreateCronTrigger ::
  ( CacheRWM m,
    MonadIO m,
    MetadataM m,
    MonadMetadataStorageQueryAPI m
  ) =>
  CreateCronTrigger ->
  m EncJSON
runCreateCronTrigger :: CreateCronTrigger -> m EncJSON
runCreateCronTrigger CreateCronTrigger {Bool
[HeaderConf]
Maybe Value
Maybe Text
Maybe MetadataResponseTransform
Maybe RequestTransform
CronSchedule
InputWebhook
TriggerName
STRetryConf
_cctResponseTransform :: CreateCronTrigger -> Maybe MetadataResponseTransform
_cctRequestTransform :: CreateCronTrigger -> Maybe RequestTransform
_cctReplace :: CreateCronTrigger -> Bool
_cctComment :: CreateCronTrigger -> Maybe Text
_cctIncludeInMetadata :: CreateCronTrigger -> Bool
_cctHeaders :: CreateCronTrigger -> [HeaderConf]
_cctRetryConf :: CreateCronTrigger -> STRetryConf
_cctPayload :: CreateCronTrigger -> Maybe Value
_cctCronSchedule :: CreateCronTrigger -> CronSchedule
_cctWebhook :: CreateCronTrigger -> InputWebhook
_cctName :: CreateCronTrigger -> TriggerName
_cctResponseTransform :: Maybe MetadataResponseTransform
_cctRequestTransform :: Maybe RequestTransform
_cctReplace :: Bool
_cctComment :: Maybe Text
_cctIncludeInMetadata :: Bool
_cctHeaders :: [HeaderConf]
_cctRetryConf :: STRetryConf
_cctPayload :: Maybe Value
_cctCronSchedule :: CronSchedule
_cctWebhook :: InputWebhook
_cctName :: TriggerName
..} = do
  let q :: CronTriggerMetadata
q =
        TriggerName
-> InputWebhook
-> CronSchedule
-> Maybe Value
-> STRetryConf
-> [HeaderConf]
-> Bool
-> Maybe Text
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> CronTriggerMetadata
CronTriggerMetadata
          TriggerName
_cctName
          InputWebhook
_cctWebhook
          CronSchedule
_cctCronSchedule
          Maybe Value
_cctPayload
          STRetryConf
_cctRetryConf
          [HeaderConf]
_cctHeaders
          Bool
_cctIncludeInMetadata
          Maybe Text
_cctComment
          Maybe RequestTransform
_cctRequestTransform
          Maybe MetadataResponseTransform
_cctResponseTransform
  case Bool
_cctReplace of
    Bool
True -> CronTriggerMetadata -> m EncJSON
forall (m :: * -> *).
(CacheRWM m, MonadIO m, MetadataM m,
 MonadMetadataStorageQueryAPI m) =>
CronTriggerMetadata -> m EncJSON
updateCronTrigger CronTriggerMetadata
q
    Bool
False -> do
      HashMap TriggerName CronTriggerInfo
cronTriggersMap <- SchemaCache -> HashMap TriggerName CronTriggerInfo
scCronTriggers (SchemaCache -> HashMap TriggerName CronTriggerInfo)
-> m SchemaCache -> m (HashMap TriggerName CronTriggerInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
      case TriggerName
-> HashMap TriggerName CronTriggerInfo -> Maybe CronTriggerInfo
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
Map.lookup (CronTriggerMetadata -> TriggerName
ctName CronTriggerMetadata
q) HashMap TriggerName CronTriggerInfo
cronTriggersMap of
        Maybe CronTriggerInfo
Nothing -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
        Just CronTriggerInfo
_ ->
          Code -> Text -> m ()
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
AlreadyExists (Text -> m ()) -> Text -> m ()
forall a b. (a -> b) -> a -> b
$
            Text
"cron trigger with name: "
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName -> Text
triggerNameToTxt (CronTriggerMetadata -> TriggerName
ctName CronTriggerMetadata
q)
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" already exists"

      let metadataObj :: MetadataObjId
metadataObj = TriggerName -> MetadataObjId
MOCronTrigger TriggerName
_cctName
          metadata :: CronTriggerMetadata
metadata =
            TriggerName
-> InputWebhook
-> CronSchedule
-> Maybe Value
-> STRetryConf
-> [HeaderConf]
-> Bool
-> Maybe Text
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> CronTriggerMetadata
CronTriggerMetadata
              TriggerName
_cctName
              InputWebhook
_cctWebhook
              CronSchedule
_cctCronSchedule
              Maybe Value
_cctPayload
              STRetryConf
_cctRetryConf
              [HeaderConf]
_cctHeaders
              Bool
_cctIncludeInMetadata
              Maybe Text
_cctComment
              Maybe RequestTransform
_cctRequestTransform
              Maybe MetadataResponseTransform
_cctResponseTransform
      MetadataObjId -> MetadataModifier -> m ()
forall (m :: * -> *).
(QErrM m, CacheRWM m, MetadataM m) =>
MetadataObjId -> MetadataModifier -> m ()
buildSchemaCacheFor MetadataObjId
metadataObj (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
        (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
          (CronTriggers -> Identity CronTriggers)
-> Metadata -> Identity Metadata
Lens' Metadata CronTriggers
metaCronTriggers ((CronTriggers -> Identity CronTriggers)
 -> Metadata -> Identity Metadata)
-> (CronTriggers -> CronTriggers) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ TriggerName -> CronTriggerMetadata -> CronTriggers -> CronTriggers
forall k v.
(Eq k, Hashable k) =>
k -> v -> InsOrdHashMap k v -> InsOrdHashMap k v
OMap.insert TriggerName
_cctName CronTriggerMetadata
metadata
      CronSchedule -> TriggerName -> m ()
forall (m :: * -> *).
(MonadIO m, MonadMetadataStorageQueryAPI m) =>
CronSchedule -> TriggerName -> m ()
populateInitialCronTriggerEvents CronSchedule
_cctCronSchedule TriggerName
_cctName
      EncJSON -> m EncJSON
forall (m :: * -> *) a. Monad m => a -> m a
return EncJSON
successMsg

resolveCronTrigger ::
  (QErrM m) =>
  Env.Environment ->
  CronTriggerMetadata ->
  m CronTriggerInfo
resolveCronTrigger :: Environment -> CronTriggerMetadata -> m CronTriggerInfo
resolveCronTrigger Environment
env CronTriggerMetadata {Bool
[HeaderConf]
Maybe Value
Maybe Text
Maybe MetadataResponseTransform
Maybe RequestTransform
CronSchedule
InputWebhook
TriggerName
STRetryConf
ctResponseTransform :: CronTriggerMetadata -> Maybe MetadataResponseTransform
ctRequestTransform :: CronTriggerMetadata -> Maybe RequestTransform
ctComment :: CronTriggerMetadata -> Maybe Text
ctIncludeInMetadata :: CronTriggerMetadata -> Bool
ctHeaders :: CronTriggerMetadata -> [HeaderConf]
ctRetryConf :: CronTriggerMetadata -> STRetryConf
ctPayload :: CronTriggerMetadata -> Maybe Value
ctSchedule :: CronTriggerMetadata -> CronSchedule
ctWebhook :: CronTriggerMetadata -> InputWebhook
ctResponseTransform :: Maybe MetadataResponseTransform
ctRequestTransform :: Maybe RequestTransform
ctComment :: Maybe Text
ctIncludeInMetadata :: Bool
ctHeaders :: [HeaderConf]
ctRetryConf :: STRetryConf
ctPayload :: Maybe Value
ctSchedule :: CronSchedule
ctWebhook :: InputWebhook
ctName :: TriggerName
ctName :: CronTriggerMetadata -> TriggerName
..} = do
  ResolvedWebhook
webhookInfo <- Environment -> InputWebhook -> m ResolvedWebhook
forall (m :: * -> *).
QErrM m =>
Environment -> InputWebhook -> m ResolvedWebhook
resolveWebhook Environment
env InputWebhook
ctWebhook
  [EventHeaderInfo]
headerInfo <- Environment -> [HeaderConf] -> m [EventHeaderInfo]
forall (m :: * -> *).
QErrM m =>
Environment -> [HeaderConf] -> m [EventHeaderInfo]
getHeaderInfosFromConf Environment
env [HeaderConf]
ctHeaders
  let urlTemplate :: Text
urlTemplate = URLTemplate -> Text
printURLTemplate (URLTemplate -> Text) -> URLTemplate -> Text
forall a b. (a -> b) -> a -> b
$ InputWebhook -> URLTemplate
unInputWebhook InputWebhook
ctWebhook
  CronTriggerInfo -> m CronTriggerInfo
forall (f :: * -> *) a. Applicative f => a -> f a
pure (CronTriggerInfo -> m CronTriggerInfo)
-> CronTriggerInfo -> m CronTriggerInfo
forall a b. (a -> b) -> a -> b
$
    TriggerName
-> CronSchedule
-> Maybe Value
-> STRetryConf
-> EnvRecord ResolvedWebhook
-> [EventHeaderInfo]
-> Maybe Text
-> Maybe RequestTransform
-> Maybe MetadataResponseTransform
-> CronTriggerInfo
CronTriggerInfo
      TriggerName
ctName
      CronSchedule
ctSchedule
      Maybe Value
ctPayload
      STRetryConf
ctRetryConf
      (Text -> ResolvedWebhook -> EnvRecord ResolvedWebhook
forall a. Text -> a -> EnvRecord a
EnvRecord Text
urlTemplate ResolvedWebhook
webhookInfo)
      [EventHeaderInfo]
headerInfo
      Maybe Text
ctComment
      Maybe RequestTransform
ctRequestTransform
      Maybe MetadataResponseTransform
ctResponseTransform

updateCronTrigger ::
  ( CacheRWM m,
    MonadIO m,
    MetadataM m,
    MonadMetadataStorageQueryAPI m
  ) =>
  CronTriggerMetadata ->
  m EncJSON
updateCronTrigger :: CronTriggerMetadata -> m EncJSON
updateCronTrigger CronTriggerMetadata
cronTriggerMetadata = do
  let triggerName :: TriggerName
triggerName = CronTriggerMetadata -> TriggerName
ctName CronTriggerMetadata
cronTriggerMetadata
  TriggerName -> m ()
forall (m :: * -> *).
(CacheRM m, MonadError QErr m) =>
TriggerName -> m ()
checkExists TriggerName
triggerName
  MetadataObjId -> MetadataModifier -> m ()
forall (m :: * -> *).
(QErrM m, CacheRWM m, MetadataM m) =>
MetadataObjId -> MetadataModifier -> m ()
buildSchemaCacheFor (TriggerName -> MetadataObjId
MOCronTrigger TriggerName
triggerName) (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
    (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$
      (CronTriggers -> Identity CronTriggers)
-> Metadata -> Identity Metadata
Lens' Metadata CronTriggers
metaCronTriggers ((CronTriggers -> Identity CronTriggers)
 -> Metadata -> Identity Metadata)
-> (CronTriggers -> CronTriggers) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ TriggerName -> CronTriggerMetadata -> CronTriggers -> CronTriggers
forall k v.
(Eq k, Hashable k) =>
k -> v -> InsOrdHashMap k v -> InsOrdHashMap k v
OMap.insert TriggerName
triggerName CronTriggerMetadata
cronTriggerMetadata
  ClearCronEvents -> m ()
forall (m :: * -> *).
MonadMetadataStorageQueryAPI m =>
ClearCronEvents -> m ()
dropFutureCronEvents (ClearCronEvents -> m ()) -> ClearCronEvents -> m ()
forall a b. (a -> b) -> a -> b
$ TriggerName -> ClearCronEvents
SingleCronTrigger TriggerName
triggerName
  UTCTime
currentTime <- IO UTCTime -> m UTCTime
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO UTCTime
C.getCurrentTime
  let scheduleTimes :: [UTCTime]
scheduleTimes = UTCTime -> Int -> CronSchedule -> [UTCTime]
generateScheduleTimes UTCTime
currentTime Int
100 (CronSchedule -> [UTCTime]) -> CronSchedule -> [UTCTime]
forall a b. (a -> b) -> a -> b
$ CronTriggerMetadata -> CronSchedule
ctSchedule CronTriggerMetadata
cronTriggerMetadata
  [CronEventSeed] -> m ()
forall (m :: * -> *).
MonadMetadataStorage m =>
[CronEventSeed] -> m ()
insertCronEvents ([CronEventSeed] -> m ()) -> [CronEventSeed] -> m ()
forall a b. (a -> b) -> a -> b
$ (UTCTime -> CronEventSeed) -> [UTCTime] -> [CronEventSeed]
forall a b. (a -> b) -> [a] -> [b]
map (TriggerName -> UTCTime -> CronEventSeed
CronEventSeed TriggerName
triggerName) [UTCTime]
scheduleTimes
  EncJSON -> m EncJSON
forall (f :: * -> *) a. Applicative f => a -> f a
pure EncJSON
successMsg

runDeleteCronTrigger ::
  ( CacheRWM m,
    MetadataM m,
    MonadMetadataStorageQueryAPI m
  ) =>
  ScheduledTriggerName ->
  m EncJSON
runDeleteCronTrigger :: ScheduledTriggerName -> m EncJSON
runDeleteCronTrigger (ScheduledTriggerName TriggerName
stName) = do
  TriggerName -> m ()
forall (m :: * -> *).
(CacheRM m, MonadError QErr m) =>
TriggerName -> m ()
checkExists TriggerName
stName
  m () -> m ()
forall (m :: * -> *) a. (QErrM m, CacheRM m) => m a -> m a
withNewInconsistentObjsCheck (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
    MetadataModifier -> m ()
forall (m :: * -> *).
(MetadataM m, CacheRWM m) =>
MetadataModifier -> m ()
buildSchemaCache (MetadataModifier -> m ()) -> MetadataModifier -> m ()
forall a b. (a -> b) -> a -> b
$
      TriggerName -> MetadataModifier
dropCronTriggerInMetadata TriggerName
stName
  ClearCronEvents -> m ()
forall (m :: * -> *).
MonadMetadataStorageQueryAPI m =>
ClearCronEvents -> m ()
dropFutureCronEvents (ClearCronEvents -> m ()) -> ClearCronEvents -> m ()
forall a b. (a -> b) -> a -> b
$ TriggerName -> ClearCronEvents
SingleCronTrigger TriggerName
stName
  EncJSON -> m EncJSON
forall (m :: * -> *) a. Monad m => a -> m a
return EncJSON
successMsg

dropCronTriggerInMetadata :: TriggerName -> MetadataModifier
dropCronTriggerInMetadata :: TriggerName -> MetadataModifier
dropCronTriggerInMetadata TriggerName
name =
  (Metadata -> Metadata) -> MetadataModifier
MetadataModifier ((Metadata -> Metadata) -> MetadataModifier)
-> (Metadata -> Metadata) -> MetadataModifier
forall a b. (a -> b) -> a -> b
$ (CronTriggers -> Identity CronTriggers)
-> Metadata -> Identity Metadata
Lens' Metadata CronTriggers
metaCronTriggers ((CronTriggers -> Identity CronTriggers)
 -> Metadata -> Identity Metadata)
-> (CronTriggers -> CronTriggers) -> Metadata -> Metadata
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ TriggerName -> CronTriggers -> CronTriggers
forall k v.
(Eq k, Hashable k) =>
k -> InsOrdHashMap k v -> InsOrdHashMap k v
OMap.delete TriggerName
name

runCreateScheduledEvent ::
  (MonadMetadataStorageQueryAPI m) =>
  CreateScheduledEvent ->
  m EncJSON
runCreateScheduledEvent :: CreateScheduledEvent -> m EncJSON
runCreateScheduledEvent CreateScheduledEvent
scheduledEvent = do
  EventId
eid <- CreateScheduledEvent -> m EventId
forall (m :: * -> *).
MonadMetadataStorageQueryAPI m =>
CreateScheduledEvent -> m EventId
createOneOffScheduledEvent CreateScheduledEvent
scheduledEvent
  EncJSON -> m EncJSON
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EncJSON -> m EncJSON) -> EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$ Value -> EncJSON
forall a. ToJSON a => a -> EncJSON
encJFromJValue (Value -> EncJSON) -> Value -> EncJSON
forall a b. (a -> b) -> a -> b
$ [Pair] -> Value
J.object [Key
"message" Key -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
J..= Text -> Value
J.String Text
"success", Key
"event_id" Key -> EventId -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
J..= EventId
eid]

checkExists :: (CacheRM m, MonadError QErr m) => TriggerName -> m ()
checkExists :: TriggerName -> m ()
checkExists TriggerName
name = do
  HashMap TriggerName CronTriggerInfo
cronTriggersMap <- SchemaCache -> HashMap TriggerName CronTriggerInfo
scCronTriggers (SchemaCache -> HashMap TriggerName CronTriggerInfo)
-> m SchemaCache -> m (HashMap TriggerName CronTriggerInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m SchemaCache
forall (m :: * -> *). CacheRM m => m SchemaCache
askSchemaCache
  m CronTriggerInfo -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m CronTriggerInfo -> m ()) -> m CronTriggerInfo -> m ()
forall a b. (a -> b) -> a -> b
$
    Maybe CronTriggerInfo -> m CronTriggerInfo -> m CronTriggerInfo
forall (m :: * -> *) a. Applicative m => Maybe a -> m a -> m a
onNothing (TriggerName
-> HashMap TriggerName CronTriggerInfo -> Maybe CronTriggerInfo
forall k v. (Eq k, Hashable k) => k -> HashMap k v -> Maybe v
Map.lookup TriggerName
name HashMap TriggerName CronTriggerInfo
cronTriggersMap) (m CronTriggerInfo -> m CronTriggerInfo)
-> m CronTriggerInfo -> m CronTriggerInfo
forall a b. (a -> b) -> a -> b
$
      Code -> Text -> m CronTriggerInfo
forall (m :: * -> *) a. QErrM m => Code -> Text -> m a
throw400 Code
NotExists (Text -> m CronTriggerInfo) -> Text -> m CronTriggerInfo
forall a b. (a -> b) -> a -> b
$
        Text
"cron trigger with name: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> TriggerName -> Text
triggerNameToTxt TriggerName
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" does not exist"

runDeleteScheduledEvent ::
  (MonadMetadataStorageQueryAPI m) => DeleteScheduledEvent -> m EncJSON
runDeleteScheduledEvent :: DeleteScheduledEvent -> m EncJSON
runDeleteScheduledEvent DeleteScheduledEvent {EventId
ScheduledEventType
_dseEventId :: DeleteScheduledEvent -> EventId
_dseType :: DeleteScheduledEvent -> ScheduledEventType
_dseEventId :: EventId
_dseType :: ScheduledEventType
..} = do
  EventId -> ScheduledEventType -> m ()
forall (m :: * -> *).
MonadMetadataStorageQueryAPI m =>
EventId -> ScheduledEventType -> m ()
dropEvent EventId
_dseEventId ScheduledEventType
_dseType
  EncJSON -> m EncJSON
forall (f :: * -> *) a. Applicative f => a -> f a
pure EncJSON
successMsg

runGetScheduledEvents ::
  ( CacheRM m,
    MonadMetadataStorageQueryAPI m
  ) =>
  GetScheduledEvents ->
  m EncJSON
runGetScheduledEvents :: GetScheduledEvents -> m EncJSON
runGetScheduledEvents GetScheduledEvents
gse = do
  case GetScheduledEvents -> ScheduledEvent
_gseScheduledEvent GetScheduledEvents
gse of
    ScheduledEvent
SEOneOff -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    SECron TriggerName
name -> TriggerName -> m ()
forall (m :: * -> *).
(CacheRM m, MonadError QErr m) =>
TriggerName -> m ()
checkExists TriggerName
name
  Value -> EncJSON
forall a. ToJSON a => a -> EncJSON
encJFromJValue (Value -> EncJSON) -> m Value -> m EncJSON
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GetScheduledEvents -> m Value
forall (m :: * -> *).
MonadMetadataStorageQueryAPI m =>
GetScheduledEvents -> m Value
fetchScheduledEvents GetScheduledEvents
gse

runGetEventInvocations ::
  ( CacheRM m,
    MonadMetadataStorageQueryAPI m
  ) =>
  GetEventInvocations ->
  m EncJSON
runGetEventInvocations :: GetEventInvocations -> m EncJSON
runGetEventInvocations GetEventInvocations {ScheduledEventPagination
GetInvocationsBy
_geiPagination :: GetEventInvocations -> ScheduledEventPagination
_geiInvocationsBy :: GetEventInvocations -> GetInvocationsBy
_geiPagination :: ScheduledEventPagination
_geiInvocationsBy :: GetInvocationsBy
..} = do
  case GetInvocationsBy
_geiInvocationsBy of
    GIBEventId EventId
_ ScheduledEventType
_ -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
    GIBEvent ScheduledEvent
event -> case ScheduledEvent
event of
      ScheduledEvent
SEOneOff -> () -> m ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      SECron TriggerName
name -> TriggerName -> m ()
forall (m :: * -> *).
(CacheRM m, MonadError QErr m) =>
TriggerName -> m ()
checkExists TriggerName
name
  WithTotalCount Int
count [ScheduledEventInvocation]
invocations <- GetInvocationsBy
-> ScheduledEventPagination
-> m (WithTotalCount [ScheduledEventInvocation])
forall (m :: * -> *).
MonadMetadataStorageQueryAPI m =>
GetInvocationsBy
-> ScheduledEventPagination
-> m (WithTotalCount [ScheduledEventInvocation])
fetchInvocations GetInvocationsBy
_geiInvocationsBy ScheduledEventPagination
_geiPagination
  EncJSON -> m EncJSON
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EncJSON -> m EncJSON) -> EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$
    Value -> EncJSON
forall a. ToJSON a => a -> EncJSON
encJFromJValue (Value -> EncJSON) -> Value -> EncJSON
forall a b. (a -> b) -> a -> b
$
      [Pair] -> Value
J.object
        [ Key
"invocations" Key -> [ScheduledEventInvocation] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
J..= [ScheduledEventInvocation]
invocations,
          Key
"count" Key -> Int -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
J..= Int
count
        ]

-- | Metadata API handler to retrieve all the cron triggers from the metadata
runGetCronTriggers :: MetadataM m => m EncJSON
runGetCronTriggers :: m EncJSON
runGetCronTriggers = do
  [CronTriggerMetadata]
cronTriggers <- CronTriggers -> [CronTriggerMetadata]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (CronTriggers -> [CronTriggerMetadata])
-> (Metadata -> CronTriggers) -> Metadata -> [CronTriggerMetadata]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Metadata -> CronTriggers
_metaCronTriggers (Metadata -> [CronTriggerMetadata])
-> m Metadata -> m [CronTriggerMetadata]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> m Metadata
forall (m :: * -> *). MetadataM m => m Metadata
getMetadata
  EncJSON -> m EncJSON
forall (f :: * -> *) a. Applicative f => a -> f a
pure (EncJSON -> m EncJSON) -> EncJSON -> m EncJSON
forall a b. (a -> b) -> a -> b
$
    Value -> EncJSON
forall a. ToJSON a => a -> EncJSON
encJFromJValue (Value -> EncJSON) -> Value -> EncJSON
forall a b. (a -> b) -> a -> b
$
      [Pair] -> Value
J.object
        [Key
"cron_triggers" Key -> [CronTriggerMetadata] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
J..= [CronTriggerMetadata]
cronTriggers]