2 Module : Gargantext.API.Prelude
3 Description : Server API main Types
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE ConstraintKinds #-}
13 {-# LANGUAGE TemplateHaskell #-}
14 {-# LANGUAGE MonoLocalBinds #-}
16 module Gargantext.API.Prelude
17 ( module Gargantext.API.Prelude
23 import Control.Concurrent (threadDelay)
24 import Control.Exception (Exception)
25 import Control.Lens (Prism', (#))
26 import Control.Lens.TH (makePrisms)
27 import Control.Monad.Except (ExceptT)
28 import Control.Monad.Reader (ReaderT)
29 import Control.Monad.Error.Class (MonadError(..))
30 import Crypto.JOSE.Error as Jose
31 import Data.Aeson.Types
32 import qualified Data.Text as Text
35 import Gargantext.API.Admin.Orchestrator.Types
36 import Gargantext.API.Admin.Types
37 import Gargantext.Core.NLP (HasNLPServer)
38 import Gargantext.Core.NodeStory
39 import Gargantext.Core.Mail.Types (HasMail)
40 import Gargantext.Core.Types
41 import Gargantext.Database.Prelude
42 import Gargantext.Database.Query.Table.Node.Error (NodeError(..), HasNodeError(..))
43 import Gargantext.Database.Query.Tree
44 import Gargantext.Prelude
45 import qualified Gargantext.Utils.Jobs.Monad as Jobs
47 import Servant.Job.Async
48 import Servant.Job.Core (HasServerError(..), serverError)
50 class HasJoseError e where
51 _JoseError :: Prism' e Jose.Error
53 joseError :: (MonadError e m, HasJoseError e) => Jose.Error -> m a
54 joseError = throwError . (_JoseError #)
56 type HasJobEnv' env = HasJobEnv env JobLog JobLog
59 ( HasConnectionPool env
60 , HasSettings env -- TODO rename HasDbSettings
61 , HasJobEnv env JobLog JobLog
74 -- , ToJSON err -- TODO this is arguable
78 type GargServerC env err m =
80 , HasNodeStory env err m
86 type GargServerT env err m api = GargServerC env err m => ServerT api m
88 type GargServer api = forall env err m. GargServerT env err m api
90 -- This is the concrete monad. It needs to be used as little as possible.
91 type GargM env err = ReaderT env (ExceptT err IO)
92 -- This is the server type using GargM. It needs to be used as little as possible.
93 -- Instead, prefer GargServer, GargServerT.
94 type GargServerM env err api = (EnvC env, ErrC err) => ServerT api (GargM env err)
96 -------------------------------------------------------------------
97 -- | This Type is needed to prepare the function before the GargServer
99 forall env err m. GargNoServer' env err m => m t
101 type GargNoServer' env err m =
103 , HasNodeStory env err m
108 -------------------------------------------------------------------
110 = GargNodeError NodeError
111 | GargTreeError TreeError
112 | GargInvalidError Validation
113 | GargJoseError Jose.Error
114 | GargServerError ServerError
115 | GargJobError Jobs.JobError
116 deriving (Show, Typeable)
118 makePrisms ''GargError
120 instance ToJSON GargError where
121 toJSON err = object [("error", String $ Text.pack $ show err)]
123 instance Exception GargError
125 instance HasNodeError GargError where
126 _NodeError = _GargNodeError
128 instance HasInvalidError GargError where
129 _InvalidError = _GargInvalidError
131 instance HasTreeError GargError where
132 _TreeError = _GargTreeError
134 instance HasServerError GargError where
135 _ServerError = _GargServerError
137 instance HasJoseError GargError where
138 _JoseError = _GargJoseError
140 ------------------------------------------------------------------------
143 simuLogs :: MonadBase IO m
147 simuLogs logStatus t = do
148 _ <- mapM (\n -> simuTask logStatus n t) $ take t [0,1..]
149 pure $ JobLog { _scst_succeeded = Just t
150 , _scst_failed = Just 0
151 , _scst_remaining = Just 0
152 , _scst_events = Just []
155 simuTask :: MonadBase IO m
160 simuTask logStatus cur total = do
161 let m = (10 :: Int) ^ (6 :: Int)
162 liftBase $ threadDelay (m*5)
164 let status = JobLog { _scst_succeeded = Just cur
165 , _scst_failed = Just 0
166 , _scst_remaining = (-) <$> Just total <*> Just cur
167 , _scst_events = Just []
169 -- printDebug "status" status