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 (mapM_)
28 import Control.Monad.Except (ExceptT)
29 import Control.Monad.Reader (ReaderT)
30 import Control.Monad.Error.Class (MonadError(..))
31 import Crypto.JOSE.Error as Jose
32 import Data.Aeson.Types
33 import qualified Data.Text as Text
36 import Gargantext.API.Admin.Orchestrator.Types
37 import Gargantext.API.Admin.Types
38 import Gargantext.Core.NLP (HasNLPServer)
39 import Gargantext.Core.NodeStory
40 import Gargantext.Core.Mail.Types (HasMail)
41 import Gargantext.Core.Types
42 import Gargantext.Database.Prelude
43 import Gargantext.Database.Query.Table.Node.Error (NodeError(..), HasNodeError(..))
44 import Gargantext.Database.Query.Tree
45 import Gargantext.Prelude
46 import Gargantext.Utils.Jobs.Monad (MonadJobStatus(..), JobHandle)
47 import qualified Gargantext.Utils.Jobs.Monad as Jobs
49 import Servant.Job.Async
50 import Servant.Job.Core (HasServerError(..), serverError)
51 import qualified Servant.Job.Types as SJ
53 class HasJoseError e where
54 _JoseError :: Prism' e Jose.Error
56 joseError :: (MonadError e m, HasJoseError e) => Jose.Error -> m a
57 joseError = throwError . (_JoseError #)
59 type HasJobEnv' env = HasJobEnv env JobLog JobLog
62 ( HasConnectionPool env
63 , HasSettings env -- TODO rename HasDbSettings
64 , HasJobEnv env JobLog JobLog
77 -- , ToJSON err -- TODO this is arguable
81 type GargServerC env err m =
83 , HasNodeStory env err m
89 type GargServerT env err m api = GargServerC env err m => ServerT api m
91 type GargServer api = forall env err m. GargServerT env err m api
93 -- This is the concrete monad. It needs to be used as little as possible.
94 type GargM env err = ReaderT env (ExceptT err IO)
95 -- This is the server type using GargM. It needs to be used as little as possible.
96 -- Instead, prefer GargServer, GargServerT.
97 type GargServerM env err api = (EnvC env, ErrC err) => ServerT api (GargM env err)
99 -------------------------------------------------------------------
100 -- | This Type is needed to prepare the function before the GargServer
101 type GargNoServer t =
102 forall env err m. GargNoServer' env err m => m t
104 type GargNoServer' env err m =
106 , HasNodeStory env err m
111 -------------------------------------------------------------------
113 = GargNodeError NodeError
114 | GargTreeError TreeError
115 | GargInvalidError Validation
116 | GargJoseError Jose.Error
117 | GargServerError ServerError
118 | GargJobError Jobs.JobError
119 deriving (Show, Typeable)
121 makePrisms ''GargError
123 instance ToJSON GargError where
124 toJSON (GargJobError s) =
125 object [ ("status", toJSON SJ.IsFailure)
126 , ("log", emptyArray)
128 , ("error", String $ Text.pack $ show s) ]
131 Jobs.InvalidMacID i -> i
133 toJSON err = object [("error", String $ Text.pack $ show err)]
135 instance Exception GargError
137 instance HasNodeError GargError where
138 _NodeError = _GargNodeError
140 instance HasInvalidError GargError where
141 _InvalidError = _GargInvalidError
143 instance HasTreeError GargError where
144 _TreeError = _GargTreeError
146 instance HasServerError GargError where
147 _ServerError = _GargServerError
149 instance HasJoseError GargError where
150 _JoseError = _GargJoseError
152 ------------------------------------------------------------------------
155 simuLogs :: (MonadBase IO m, MonadJobStatus m) => JobHandle m -> Int -> m ()
156 simuLogs jobHandle t = do
157 markStarted t jobHandle
158 mapM_ (const simuTask) $ take t ([0,1..] :: [Int])
159 markComplete jobHandle
162 let m = (10 :: Int) ^ (6 :: Int)
163 liftBase $ threadDelay (m*5)
164 markProgress 1 jobHandle