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.NodeStory
38 import Gargantext.Core.Mail.Types (HasMail)
39 import Gargantext.Core.Types
40 import Gargantext.Database.Prelude
41 import Gargantext.Database.Query.Table.Node.Error (NodeError(..), HasNodeError(..))
42 import Gargantext.Database.Query.Tree
43 import Gargantext.Prelude
44 import qualified Gargantext.Utils.Jobs.Monad as Jobs
46 import Servant.Job.Async
47 import Servant.Job.Core (HasServerError(..), serverError)
49 class HasJoseError e where
50 _JoseError :: Prism' e Jose.Error
52 joseError :: (MonadError e m, HasJoseError e) => Jose.Error -> m a
53 joseError = throwError . (_JoseError #)
55 type HasJobEnv' env = HasJobEnv env JobLog JobLog
58 ( HasConnectionPool env
59 , HasSettings env -- TODO rename HasDbSettings
60 , HasJobEnv env JobLog JobLog
72 -- , ToJSON err -- TODO this is arguable
76 type GargServerC env err m =
78 , HasNodeStory env err m
84 type GargServerT env err m api = GargServerC env err m => ServerT api m
86 type GargServer api = forall env err m. GargServerT env err m api
88 -- This is the concrete monad. It needs to be used as little as possible.
89 type GargM env err = ReaderT env (ExceptT err IO)
90 -- This is the server type using GargM. It needs to be used as little as possible.
91 -- Instead, prefer GargServer, GargServerT.
92 type GargServerM env err api = (EnvC env, ErrC err) => ServerT api (GargM env err)
94 -------------------------------------------------------------------
95 -- | This Type is needed to prepare the function before the GargServer
97 forall env err m. GargNoServer' env err m => m t
99 type GargNoServer' env err m =
101 , HasNodeStory env err m
106 -------------------------------------------------------------------
108 = GargNodeError NodeError
109 | GargTreeError TreeError
110 | GargInvalidError Validation
111 | GargJoseError Jose.Error
112 | GargServerError ServerError
113 | GargJobError Jobs.JobError
114 deriving (Show, Typeable)
116 makePrisms ''GargError
118 instance ToJSON GargError where
119 toJSON err = object [("error", String $ Text.pack $ show err)]
121 instance Exception GargError
123 instance HasNodeError GargError where
124 _NodeError = _GargNodeError
126 instance HasInvalidError GargError where
127 _InvalidError = _GargInvalidError
129 instance HasTreeError GargError where
130 _TreeError = _GargTreeError
132 instance HasServerError GargError where
133 _ServerError = _GargServerError
135 instance HasJoseError GargError where
136 _JoseError = _GargJoseError
138 ------------------------------------------------------------------------
141 simuLogs :: MonadBase IO m
145 simuLogs logStatus t = do
146 _ <- mapM (\n -> simuTask logStatus n t) $ take t [0,1..]
147 pure $ JobLog { _scst_succeeded = Just t
148 , _scst_failed = Just 0
149 , _scst_remaining = Just 0
150 , _scst_events = Just []
153 simuTask :: MonadBase IO m
158 simuTask logStatus cur total = do
159 let m = (10 :: Int) ^ (6 :: Int)
160 liftBase $ threadDelay (m*5)
162 let status = JobLog { _scst_succeeded = Just cur
163 , _scst_failed = Just 0
164 , _scst_remaining = (-) <$> Just total <*> Just cur
165 , _scst_events = Just []
167 printDebug "status" status