{-# OPTIONS_GHC -fno-warn-orphans #-}
{-# LANGUAGE TemplateHaskell   #-}
{-# LANGUAGE TypeOperators     #-}

module Gargantext.API.Admin.Orchestrator.Types
  where

import Control.Lens hiding (elements)
import Data.Aeson
import Data.Morpheus.Types
  ( GQLType
  , typeOptions )
import Data.Proxy
import Data.Swagger hiding (URL, url, port)
import Data.Text (Text)
import GHC.Generics hiding (to)
import Servant
import Servant.Job.Async
import Servant.Job.Types
import Servant.Job.Utils (jsonOptions)
import Test.QuickCheck (elements)
import Test.QuickCheck.Arbitrary

import qualified Gargantext.API.GraphQL.Utils as GQLU
import Gargantext.Core.Types (TODO(..))
import Gargantext.Prelude

------------------------------------------------------------------------
instance Arbitrary a => Arbitrary (JobStatus 'Safe a) where
  arbitrary :: Gen (JobStatus 'Safe a)
arbitrary = Text -> Gen (JobStatus 'Safe a)
forall a. HasCallStack => Text -> a
panic Text
"TODO"

instance Arbitrary a => Arbitrary (JobOutput a) where
  arbitrary :: Gen (JobOutput a)
arbitrary = a -> JobOutput a
forall a. a -> JobOutput a
JobOutput (a -> JobOutput a) -> Gen a -> Gen (JobOutput a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
forall a. Arbitrary a => Gen a
arbitrary

-- | Main Types
-- TODO IsidoreAuth
data ExternalAPIs = All
                  | PubMed
                  | HAL
                  | IsTex
                  | Isidore
  deriving (Int -> ExternalAPIs -> ShowS
[ExternalAPIs] -> ShowS
ExternalAPIs -> String
(Int -> ExternalAPIs -> ShowS)
-> (ExternalAPIs -> String)
-> ([ExternalAPIs] -> ShowS)
-> Show ExternalAPIs
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ExternalAPIs] -> ShowS
$cshowList :: [ExternalAPIs] -> ShowS
show :: ExternalAPIs -> String
$cshow :: ExternalAPIs -> String
showsPrec :: Int -> ExternalAPIs -> ShowS
$cshowsPrec :: Int -> ExternalAPIs -> ShowS
Show, ExternalAPIs -> ExternalAPIs -> Bool
(ExternalAPIs -> ExternalAPIs -> Bool)
-> (ExternalAPIs -> ExternalAPIs -> Bool) -> Eq ExternalAPIs
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ExternalAPIs -> ExternalAPIs -> Bool
$c/= :: ExternalAPIs -> ExternalAPIs -> Bool
== :: ExternalAPIs -> ExternalAPIs -> Bool
$c== :: ExternalAPIs -> ExternalAPIs -> Bool
Eq, Int -> ExternalAPIs
ExternalAPIs -> Int
ExternalAPIs -> [ExternalAPIs]
ExternalAPIs -> ExternalAPIs
ExternalAPIs -> ExternalAPIs -> [ExternalAPIs]
ExternalAPIs -> ExternalAPIs -> ExternalAPIs -> [ExternalAPIs]
(ExternalAPIs -> ExternalAPIs)
-> (ExternalAPIs -> ExternalAPIs)
-> (Int -> ExternalAPIs)
-> (ExternalAPIs -> Int)
-> (ExternalAPIs -> [ExternalAPIs])
-> (ExternalAPIs -> ExternalAPIs -> [ExternalAPIs])
-> (ExternalAPIs -> ExternalAPIs -> [ExternalAPIs])
-> (ExternalAPIs -> ExternalAPIs -> ExternalAPIs -> [ExternalAPIs])
-> Enum ExternalAPIs
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: ExternalAPIs -> ExternalAPIs -> ExternalAPIs -> [ExternalAPIs]
$cenumFromThenTo :: ExternalAPIs -> ExternalAPIs -> ExternalAPIs -> [ExternalAPIs]
enumFromTo :: ExternalAPIs -> ExternalAPIs -> [ExternalAPIs]
$cenumFromTo :: ExternalAPIs -> ExternalAPIs -> [ExternalAPIs]
enumFromThen :: ExternalAPIs -> ExternalAPIs -> [ExternalAPIs]
$cenumFromThen :: ExternalAPIs -> ExternalAPIs -> [ExternalAPIs]
enumFrom :: ExternalAPIs -> [ExternalAPIs]
$cenumFrom :: ExternalAPIs -> [ExternalAPIs]
fromEnum :: ExternalAPIs -> Int
$cfromEnum :: ExternalAPIs -> Int
toEnum :: Int -> ExternalAPIs
$ctoEnum :: Int -> ExternalAPIs
pred :: ExternalAPIs -> ExternalAPIs
$cpred :: ExternalAPIs -> ExternalAPIs
succ :: ExternalAPIs -> ExternalAPIs
$csucc :: ExternalAPIs -> ExternalAPIs
Enum, ExternalAPIs
ExternalAPIs -> ExternalAPIs -> Bounded ExternalAPIs
forall a. a -> a -> Bounded a
maxBound :: ExternalAPIs
$cmaxBound :: ExternalAPIs
minBound :: ExternalAPIs
$cminBound :: ExternalAPIs
Bounded, (forall x. ExternalAPIs -> Rep ExternalAPIs x)
-> (forall x. Rep ExternalAPIs x -> ExternalAPIs)
-> Generic ExternalAPIs
forall x. Rep ExternalAPIs x -> ExternalAPIs
forall x. ExternalAPIs -> Rep ExternalAPIs x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ExternalAPIs x -> ExternalAPIs
$cfrom :: forall x. ExternalAPIs -> Rep ExternalAPIs x
Generic)


-- | Main Instances
instance FromJSON ExternalAPIs
instance ToJSON ExternalAPIs

externalAPIs :: [ExternalAPIs]
externalAPIs :: [ExternalAPIs]
externalAPIs = [ExternalAPIs
forall a. Bounded a => a
minBound..ExternalAPIs
forall a. Bounded a => a
maxBound]

instance Arbitrary ExternalAPIs
  where
    arbitrary :: Gen ExternalAPIs
arbitrary = [ExternalAPIs] -> Gen ExternalAPIs
forall a. [a] -> Gen a
elements [ExternalAPIs]
externalAPIs

instance ToSchema ExternalAPIs

instance ToSchema URL where
  declareNamedSchema :: Proxy URL -> Declare (Definitions Schema) NamedSchema
declareNamedSchema Proxy URL
_ = Proxy TODO -> Declare (Definitions Schema) NamedSchema
forall a.
ToSchema a =>
Proxy a -> Declare (Definitions Schema) NamedSchema
declareNamedSchema (Proxy TODO
forall k (t :: k). Proxy t
Proxy :: Proxy TODO)

data ScraperInput = ScraperInput
  { ScraperInput -> Text
_scin_spider       :: !Text
  , ScraperInput -> Maybe Text
_scin_query        :: !(Maybe Text)
  , ScraperInput -> Text
_scin_user         :: !Text
  , ScraperInput -> Int
_scin_corpus       :: !Int
  , ScraperInput -> Maybe Int
_scin_report_every :: !(Maybe Int)
  , ScraperInput -> Maybe Int
_scin_limit        :: !(Maybe Int)
  , ScraperInput -> Maybe Text
_scin_local_file   :: !(Maybe Text)
  , ScraperInput -> Maybe Bool
_scin_count_only   :: !(Maybe Bool)
  }
  deriving (forall x. ScraperInput -> Rep ScraperInput x)
-> (forall x. Rep ScraperInput x -> ScraperInput)
-> Generic ScraperInput
forall x. Rep ScraperInput x -> ScraperInput
forall x. ScraperInput -> Rep ScraperInput x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ScraperInput x -> ScraperInput
$cfrom :: forall x. ScraperInput -> Rep ScraperInput x
Generic

makeLenses ''ScraperInput

instance FromJSON ScraperInput where
  parseJSON :: Value -> Parser ScraperInput
parseJSON = Options -> Value -> Parser ScraperInput
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Options -> Value -> Parser ScraperInput)
-> Options -> Value -> Parser ScraperInput
forall a b. (a -> b) -> a -> b
$ Text -> Options
jsonOptions Text
"_scin_"

-- Proposal to replace the Corpus.API.Query type which seems to generically named.

data ScraperEvent = ScraperEvent
  { ScraperEvent -> Maybe Text
_scev_message :: !(Maybe Text)
  , ScraperEvent -> Maybe Text
_scev_level   :: !(Maybe Text)
  , ScraperEvent -> Maybe Text
_scev_date    :: !(Maybe Text)
  }
  deriving (Int -> ScraperEvent -> ShowS
[ScraperEvent] -> ShowS
ScraperEvent -> String
(Int -> ScraperEvent -> ShowS)
-> (ScraperEvent -> String)
-> ([ScraperEvent] -> ShowS)
-> Show ScraperEvent
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ScraperEvent] -> ShowS
$cshowList :: [ScraperEvent] -> ShowS
show :: ScraperEvent -> String
$cshow :: ScraperEvent -> String
showsPrec :: Int -> ScraperEvent -> ShowS
$cshowsPrec :: Int -> ScraperEvent -> ShowS
Show, (forall x. ScraperEvent -> Rep ScraperEvent x)
-> (forall x. Rep ScraperEvent x -> ScraperEvent)
-> Generic ScraperEvent
forall x. Rep ScraperEvent x -> ScraperEvent
forall x. ScraperEvent -> Rep ScraperEvent x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ScraperEvent x -> ScraperEvent
$cfrom :: forall x. ScraperEvent -> Rep ScraperEvent x
Generic)

instance Arbitrary ScraperEvent where
  arbitrary :: Gen ScraperEvent
arbitrary = Maybe Text -> Maybe Text -> Maybe Text -> ScraperEvent
ScraperEvent (Maybe Text -> Maybe Text -> Maybe Text -> ScraperEvent)
-> Gen (Maybe Text)
-> Gen (Maybe Text -> Maybe Text -> ScraperEvent)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Maybe Text] -> Gen (Maybe Text)
forall a. [a] -> Gen a
elements [Maybe Text
forall a. Maybe a
Nothing, Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"test message"]
                           Gen (Maybe Text -> Maybe Text -> ScraperEvent)
-> Gen (Maybe Text) -> Gen (Maybe Text -> ScraperEvent)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Maybe Text] -> Gen (Maybe Text)
forall a. [a] -> Gen a
elements [Maybe Text
forall a. Maybe a
Nothing, Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"INFO", Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"WARN"]
                           Gen (Maybe Text -> ScraperEvent)
-> Gen (Maybe Text) -> Gen ScraperEvent
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Maybe Text] -> Gen (Maybe Text)
forall a. [a] -> Gen a
elements [Maybe Text
forall a. Maybe a
Nothing, Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"2018-04-18"]
instance ToJSON ScraperEvent where
  toJSON :: ScraperEvent -> Value
toJSON = Options -> ScraperEvent -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Options -> ScraperEvent -> Value)
-> Options -> ScraperEvent -> Value
forall a b. (a -> b) -> a -> b
$ Text -> Options
jsonOptions Text
"_scev_"
instance FromJSON ScraperEvent where
  parseJSON :: Value -> Parser ScraperEvent
parseJSON = Options -> Value -> Parser ScraperEvent
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Options -> Value -> Parser ScraperEvent)
-> Options -> Value -> Parser ScraperEvent
forall a b. (a -> b) -> a -> b
$ Text -> Options
jsonOptions Text
"_scev_"
instance ToSchema ScraperEvent  -- TODO _scev_ prefix
instance GQLType ScraperEvent where
  typeOptions :: f ScraperEvent -> GQLTypeOptions -> GQLTypeOptions
typeOptions f ScraperEvent
_ = Text -> GQLTypeOptions -> GQLTypeOptions
GQLU.unPrefix Text
"_scev_"


data JobLog = JobLog
  { JobLog -> Maybe Int
_scst_succeeded :: !(Maybe Int)
  , JobLog -> Maybe Int
_scst_failed    :: !(Maybe Int)
  , JobLog -> Maybe Int
_scst_remaining :: !(Maybe Int)
  , JobLog -> Maybe [ScraperEvent]
_scst_events    :: !(Maybe [ScraperEvent])
  }
  deriving (Int -> JobLog -> ShowS
[JobLog] -> ShowS
JobLog -> String
(Int -> JobLog -> ShowS)
-> (JobLog -> String) -> ([JobLog] -> ShowS) -> Show JobLog
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [JobLog] -> ShowS
$cshowList :: [JobLog] -> ShowS
show :: JobLog -> String
$cshow :: JobLog -> String
showsPrec :: Int -> JobLog -> ShowS
$cshowsPrec :: Int -> JobLog -> ShowS
Show, (forall x. JobLog -> Rep JobLog x)
-> (forall x. Rep JobLog x -> JobLog) -> Generic JobLog
forall x. Rep JobLog x -> JobLog
forall x. JobLog -> Rep JobLog x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep JobLog x -> JobLog
$cfrom :: forall x. JobLog -> Rep JobLog x
Generic)

makeLenses ''JobLog

instance Arbitrary JobLog where
  arbitrary :: Gen JobLog
arbitrary = Maybe Int
-> Maybe Int -> Maybe Int -> Maybe [ScraperEvent] -> JobLog
JobLog
           (Maybe Int
 -> Maybe Int -> Maybe Int -> Maybe [ScraperEvent] -> JobLog)
-> Gen (Maybe Int)
-> Gen (Maybe Int -> Maybe Int -> Maybe [ScraperEvent] -> JobLog)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen (Maybe Int)
forall a. Arbitrary a => Gen a
arbitrary
           Gen (Maybe Int -> Maybe Int -> Maybe [ScraperEvent] -> JobLog)
-> Gen (Maybe Int)
-> Gen (Maybe Int -> Maybe [ScraperEvent] -> JobLog)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen (Maybe Int)
forall a. Arbitrary a => Gen a
arbitrary
           Gen (Maybe Int -> Maybe [ScraperEvent] -> JobLog)
-> Gen (Maybe Int) -> Gen (Maybe [ScraperEvent] -> JobLog)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen (Maybe Int)
forall a. Arbitrary a => Gen a
arbitrary
           Gen (Maybe [ScraperEvent] -> JobLog)
-> Gen (Maybe [ScraperEvent]) -> Gen JobLog
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Gen (Maybe [ScraperEvent])
forall a. Arbitrary a => Gen a
arbitrary
instance ToJSON JobLog where
  toJSON :: JobLog -> Value
toJSON = Options -> JobLog -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON (Options -> JobLog -> Value) -> Options -> JobLog -> Value
forall a b. (a -> b) -> a -> b
$ Text -> Options
jsonOptions Text
"_scst_"
instance FromJSON JobLog where
  parseJSON :: Value -> Parser JobLog
parseJSON = Options -> Value -> Parser JobLog
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON (Options -> Value -> Parser JobLog)
-> Options -> Value -> Parser JobLog
forall a b. (a -> b) -> a -> b
$ Text -> Options
jsonOptions Text
"_scst_"
instance ToSchema JobLog -- TODO _scst_ prefix
instance GQLType JobLog where
  typeOptions :: f JobLog -> GQLTypeOptions -> GQLTypeOptions
typeOptions f JobLog
_ = Text -> GQLTypeOptions -> GQLTypeOptions
GQLU.unPrefix Text
"_scst_"

instance ToSchema ScraperInput  -- TODO _scin_ prefix

instance ToParamSchema Offset -- where
  -- toParamSchema = panic "TODO"

instance ToParamSchema Limit -- where
  -- toParamSchema = panic "TODO"

type ScrapersEnv = JobEnv JobLog JobLog

type ScraperAPI  = AsyncJobsAPI JobLog ScraperInput  JobLog

------------------------------------------------------------------------
type AsyncJobs event ctI input output =
  AsyncJobsAPI' 'Unsafe 'Safe ctI '[JSON] Maybe event input output