Commit d027a9a4 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[API] simulate logs and update async api ok

parent bc17efc9
...@@ -128,7 +128,6 @@ type NodeAPI a = Get '[JSON] (Node a) ...@@ -128,7 +128,6 @@ type NodeAPI a = Get '[JSON] (Node a)
:<|> "table" :> TableApi :<|> "table" :> TableApi
:<|> "ngrams" :> TableNgramsApi :<|> "ngrams" :> TableNgramsApi
:<|> "update" :> Update.API
:<|> "category" :> CatApi :<|> "category" :> CatApi
:<|> "search" :> SearchDocsAPI :<|> "search" :> SearchDocsAPI
:<|> "share" :> Share.API :<|> "share" :> Share.API
...@@ -146,6 +145,7 @@ type NodeAPI a = Get '[JSON] (Node a) ...@@ -146,6 +145,7 @@ type NodeAPI a = Get '[JSON] (Node a)
:<|> "tree" :> TreeApi :<|> "tree" :> TreeApi
:<|> "phylo" :> PhyloAPI :<|> "phylo" :> PhyloAPI
-- :<|> "add" :> NodeAddAPI -- :<|> "add" :> NodeAddAPI
:<|> "update" :> Update.API
-- TODO-ACCESS: check userId CanRenameNode nodeId -- TODO-ACCESS: check userId CanRenameNode nodeId
-- TODO-EVENTS: NodeRenamed RenameNode or re-use some more general NodeEdited... -- TODO-EVENTS: NodeRenamed RenameNode or re-use some more general NodeEdited...
...@@ -203,7 +203,6 @@ nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode ...@@ -203,7 +203,6 @@ nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode
:<|> tableApi id' :<|> tableApi id'
:<|> apiNgramsTableCorpus id' :<|> apiNgramsTableCorpus id'
:<|> Update.api id'
:<|> catApi id' :<|> catApi id'
:<|> searchDocs id' :<|> searchDocs id'
:<|> Share.api id' :<|> Share.api id'
...@@ -220,6 +219,7 @@ nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode ...@@ -220,6 +219,7 @@ nodeAPI p uId id' = withAccess (Proxy :: Proxy (NodeAPI a)) Proxy uId (PathNode
:<|> phyloAPI id' uId :<|> phyloAPI id' uId
-- :<|> nodeAddAPI id' -- :<|> nodeAddAPI id'
-- :<|> postUpload id' -- :<|> postUpload id'
:<|> Update.api uId id'
------------------------------------------------------------------------ ------------------------------------------------------------------------
data RenameNode = RenameNode { r_name :: Text } data RenameNode = RenameNode { r_name :: Text }
......
...@@ -38,17 +38,17 @@ type Api = Summary "New Annuaire endpoint" ...@@ -38,17 +38,17 @@ type Api = Summary "New Annuaire endpoint"
------------------------------------------------------------------------ ------------------------------------------------------------------------
------------------------------------------------------------------------ ------------------------------------------------------------------------
data WithForm = WithForm data AnnuaireWithForm = AnnuaireWithForm
{ _wf_filetype :: !NewFile.FileType { _wf_filetype :: !NewFile.FileType
, _wf_data :: !Text , _wf_data :: !Text
, _wf_lang :: !(Maybe Lang) , _wf_lang :: !(Maybe Lang)
} deriving (Eq, Show, Generic) } deriving (Eq, Show, Generic)
makeLenses ''WithForm makeLenses ''AnnuaireWithForm
instance FromForm WithForm instance FromForm AnnuaireWithForm
instance FromJSON WithForm where instance FromJSON AnnuaireWithForm where
parseJSON = genericParseJSON $ jsonOptions "_wf_" parseJSON = genericParseJSON $ jsonOptions "_wf_"
instance ToSchema WithForm where instance ToSchema AnnuaireWithForm where
declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_") declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -62,15 +62,15 @@ type AddWithForm = Summary "Add with FormUrlEncoded to annuaire endpoint" ...@@ -62,15 +62,15 @@ type AddWithForm = Summary "Add with FormUrlEncoded to annuaire endpoint"
:> "add" :> "add"
:> "form" :> "form"
:> "async" :> "async"
:> AsyncJobs ScraperStatus '[FormUrlEncoded] WithForm ScraperStatus :> AsyncJobs ScraperStatus '[FormUrlEncoded] AnnuaireWithForm ScraperStatus
------------------------------------------------------------------------ ------------------------------------------------------------------------
addToAnnuaireWithForm :: FlowCmdM env err m addToAnnuaireWithForm :: FlowCmdM env err m
=> AnnuaireId => AnnuaireId
-> WithForm -> AnnuaireWithForm
-> (ScraperStatus -> m ()) -> (ScraperStatus -> m ())
-> m ScraperStatus -> m ScraperStatus
addToAnnuaireWithForm _cid (WithForm ft _d _l) logStatus = do addToAnnuaireWithForm _cid (AnnuaireWithForm ft _d _l) logStatus = do
printDebug "ft" ft printDebug "ft" ft
......
...@@ -150,18 +150,18 @@ instance ToSchema WithQuery where ...@@ -150,18 +150,18 @@ instance ToSchema WithQuery where
declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wq_") declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wq_")
------------------------------------------------------- -------------------------------------------------------
data WithForm = WithForm data NewWithForm = NewWithForm
{ _wf_filetype :: !FileType { _wf_filetype :: !FileType
, _wf_data :: !Text , _wf_data :: !Text
, _wf_lang :: !(Maybe Lang) , _wf_lang :: !(Maybe Lang)
, _wf_name :: !Text , _wf_name :: !Text
} deriving (Eq, Show, Generic) } deriving (Eq, Show, Generic)
makeLenses ''WithForm makeLenses ''NewWithForm
instance FromForm WithForm instance FromForm NewWithForm
instance FromJSON WithForm where instance FromJSON NewWithForm where
parseJSON = genericParseJSON $ jsonOptions "_wf_" parseJSON = genericParseJSON $ jsonOptions "_wf_"
instance ToSchema WithForm where instance ToSchema NewWithForm where
declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_") declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -193,7 +193,7 @@ type AddWithForm = Summary "Add with FormUrlEncoded to corpus endpoint" ...@@ -193,7 +193,7 @@ type AddWithForm = Summary "Add with FormUrlEncoded to corpus endpoint"
:> "add" :> "add"
:> "form" :> "form"
:> "async" :> "async"
:> AsyncJobs ScraperStatus '[FormUrlEncoded] WithForm ScraperStatus :> AsyncJobs ScraperStatus '[FormUrlEncoded] NewWithForm ScraperStatus
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -229,10 +229,10 @@ addToCorpusWithQuery u cid (WithQuery q dbs l _nid) logStatus = do ...@@ -229,10 +229,10 @@ addToCorpusWithQuery u cid (WithQuery q dbs l _nid) logStatus = do
addToCorpusWithForm :: FlowCmdM env err m addToCorpusWithForm :: FlowCmdM env err m
=> User => User
-> CorpusId -> CorpusId
-> WithForm -> NewWithForm
-> (ScraperStatus -> m ()) -> (ScraperStatus -> m ())
-> m ScraperStatus -> m ScraperStatus
addToCorpusWithForm user cid (WithForm ft d l _n) logStatus = do addToCorpusWithForm user cid (NewWithForm ft d l _n) logStatus = do
let let
parse = case ft of parse = case ft of
......
...@@ -16,18 +16,24 @@ Portability : POSIX ...@@ -16,18 +16,24 @@ Portability : POSIX
module Gargantext.API.Node.Update module Gargantext.API.Node.Update
where where
import Prelude (Enum, Bounded, minBound, maxBound)
import Data.Aeson import Data.Aeson
import Data.Swagger import Data.Swagger
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Gargantext.API.Admin.Orchestrator.Types (ScraperStatus(..))
import Gargantext.API.Node.Corpus.New (AsyncJobs)
import Gargantext.API.Prelude (GargServer, simuLogs)
import Gargantext.Database.Action.Flow.Types (FlowCmdM)
import Gargantext.Database.Admin.Types.Node import Gargantext.Database.Admin.Types.Node
import Gargantext.Database.Prelude import Gargantext.Prelude (Ord, Eq, (<$>), ($), liftBase, (.))
import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..)) import Prelude (Enum, Bounded, minBound, maxBound)
import Gargantext.Prelude
import Servant import Servant
import Servant.Job.Async (JobFunction(..), serveJobsAPI)
import Test.QuickCheck (elements) import Test.QuickCheck (elements)
import Test.QuickCheck.Arbitrary import Test.QuickCheck.Arbitrary
------------------------------------------------------------------------ ------------------------------------------------------------------------
data UpdateNodeParams = UpdateNodeParamsList { methodList :: Method } data UpdateNodeParams = UpdateNodeParamsList { methodList :: Method }
| UpdateNodeParamsGraph { methodGraph :: GraphMetric } | UpdateNodeParamsGraph { methodGraph :: GraphMetric }
...@@ -89,16 +95,21 @@ instance Arbitrary Charts where ...@@ -89,16 +95,21 @@ instance Arbitrary Charts where
arbitrary = elements [ minBound .. maxBound ] arbitrary = elements [ minBound .. maxBound ]
------------------------------------------------------------------------ ------------------------------------------------------------------------
api :: HasNodeError err api :: UserId -> NodeId -> GargServer API
=> NodeId api uId nId =
serveJobsAPI $
JobFunction (\p logs -> updateNode uId nId p (liftBase . logs))
updateNode :: FlowCmdM env err m
=> UserId
-> NodeId
-> UpdateNodeParams -> UpdateNodeParams
-> Cmd err Int -> (ScraperStatus -> m ())
api _nId (UpdateNodeParamsList _meth) = pure 1 -> m ScraperStatus
api _nId (UpdateNodeParamsGraph _meth) = pure 1 updateNode _uId _nId _ logStatus = do
api _nId (UpdateNodeParamsTexts _meth) = pure 1 simuLogs logStatus 100
api _nId (UpdateNodeParamsBoard _meth) = pure 1
------------------------------------------------------------------------ ------------------------------------------------------------------------
type API = Summary " Share Node with username" type API = Summary " Share Node with username"
:> ReqBody '[JSON] UpdateNodeParams :> AsyncJobs ScraperStatus '[JSON] UpdateNodeParams ScraperStatus
:> Post '[JSON] Int
...@@ -22,6 +22,7 @@ module Gargantext.API.Prelude ...@@ -22,6 +22,7 @@ module Gargantext.API.Prelude
) )
where where
import Control.Concurrent (threadDelay)
import Control.Exception (Exception) import Control.Exception (Exception)
import Control.Lens (Prism', (#)) import Control.Lens (Prism', (#))
import Control.Lens.TH (makePrisms) import Control.Lens.TH (makePrisms)
...@@ -36,9 +37,9 @@ import Gargantext.API.Admin.Orchestrator.Types ...@@ -36,9 +37,9 @@ import Gargantext.API.Admin.Orchestrator.Types
import Gargantext.API.Admin.Settings import Gargantext.API.Admin.Settings
import Gargantext.API.Ngrams import Gargantext.API.Ngrams
import Gargantext.Core.Types import Gargantext.Core.Types
import Gargantext.Database.Query.Tree
import Gargantext.Database.Query.Table.Node.Error (NodeError(..), HasNodeError(..))
import Gargantext.Database.Prelude import Gargantext.Database.Prelude
import Gargantext.Database.Query.Table.Node.Error (NodeError(..), HasNodeError(..))
import Gargantext.Database.Query.Tree
import Gargantext.Prelude import Gargantext.Prelude
import Servant import Servant
import Servant.Job.Async (HasJobEnv) import Servant.Job.Async (HasJobEnv)
...@@ -146,3 +147,45 @@ instance HasServerError GargError where ...@@ -146,3 +147,45 @@ instance HasServerError GargError where
instance HasJoseError GargError where instance HasJoseError GargError where
_JoseError = _GargJoseError _JoseError = _GargJoseError
------------------------------------------------------------------------
-- | Utils
-- | Simulate logs
simuLogs :: MonadBase IO m
=> (ScraperStatus -> m a)
-> Int
-> m ScraperStatus
simuLogs logStatus t = do
let task = ScraperStatus { _scst_succeeded = Just 0
, _scst_failed = Just 0
, _scst_remaining = Just 0
, _scst_events = Just []
}
f <- foldM' (\status n -> simuTask logStatus status n t) task $ take t [1..]
pure f
simuTask :: MonadBase IO m
=> (ScraperStatus -> m a)
-> ScraperStatus
-> Int
-> Int
-> m ScraperStatus
simuTask logStatus (ScraperStatus s f _r e) n t = do
let
m = (10 :: Int) ^ (6 :: Int)
_ <- liftBase $ threadDelay ( m * 10)
let status = ScraperStatus { _scst_succeeded = (+) <$> s <*> Just n
, _scst_failed = f
, _scst_remaining = (-) <$> Just t <*> s
, _scst_events = e
}
printDebug "status" status
_ <- logStatus status
pure status
...@@ -59,6 +59,7 @@ import Protolude ( Bool(True, False), Int, Int64, Double, Integer ...@@ -59,6 +59,7 @@ import Protolude ( Bool(True, False), Int, Int64, Double, Integer
, compare , compare
, on , on
, panic , panic
, seq
) )
import Prelude (Enum, Bounded, minBound, maxBound, putStrLn) import Prelude (Enum, Bounded, minBound, maxBound, putStrLn)
...@@ -307,4 +308,12 @@ lookup2 a b m = do ...@@ -307,4 +308,12 @@ lookup2 a b m = do
m' <- lookup a m m' <- lookup a m
lookup b m' lookup b m'
-----------------------------------------------
foldM' :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
foldM' _ z [] = return z
foldM' f z (x:xs) = do
z' <- f z x
z' `seq` foldM' f z' xs
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment