{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
-{-# OPTIONS_GHC -fno-warn-orphans #-}
module Gargantext.API.Node.Corpus.New
where
+-- import Servant.Multipart
+-- import Test.QuickCheck (elements)
import Control.Lens hiding (elements, Empty)
import Data.Aeson
import Data.Aeson.TH (deriveJSON)
import Data.Swagger
import Data.Text (Text)
import GHC.Generics (Generic)
-import Servant
-import Servant.Job.Core
-import Servant.Job.Types
-import Servant.Job.Utils (jsonOptions)
--- import Servant.Multipart
--- import Test.QuickCheck (elements)
-import Test.QuickCheck.Arbitrary
-import Web.FormUrlEncoded (FromForm)
-
-import Gargantext.API.Admin.Orchestrator.Types (JobLog(..))
-import qualified Gargantext.API.Admin.Orchestrator.Types as T
+import Gargantext.API.Admin.Orchestrator.Types (JobLog(..), AsyncJobs)
+import Gargantext.API.Admin.Types (HasSettings)
import Gargantext.API.Node.Corpus.New.File
+import Gargantext.API.Node.Types
import Gargantext.Core (Lang(..){-, allLangs-})
import Gargantext.Core.Types.Individu (User(..))
import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
-import Gargantext.Database.Action.Flow (FlowCmdM, flowCorpus, getDataText, flowDataText, TermType(..), DataOrigin(..){-, allDataOrigins-})
+import Gargantext.Database.Action.Flow (flowCorpus, getDataText, flowDataText, TermType(..), DataOrigin(..){-, allDataOrigins-})
+import Gargantext.Database.Action.Flow.Types (FlowCmdM)
+import Gargantext.Database.Action.Mail (sendMail)
+import Gargantext.Database.Action.Node (mkNodeWithParent)
+import Gargantext.Database.Action.User (getUserId)
import Gargantext.Database.Admin.Types.Hyperdata
-import Gargantext.Database.Admin.Types.Node (CorpusId, UserId)
+import Gargantext.Database.Admin.Types.Node (CorpusId, NodeType(..), UserId)
+import Gargantext.Database.Query.Table.Node (getNodeWith)
+import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
+import Gargantext.Database.Schema.Node (node_hyperdata)
import Gargantext.Prelude
-import qualified Gargantext.Text.Corpus.API as API
-import qualified Gargantext.Text.Corpus.Parsers as Parser (FileFormat(..), parseFormat)
+import Servant
+import Servant.Job.Utils (jsonOptions)
+import Test.QuickCheck.Arbitrary
+import qualified Data.Text as T
+import qualified Gargantext.API.Admin.Orchestrator.Types as T
+import qualified Gargantext.Core.Text.Corpus.API as API
+import qualified Gargantext.Core.Text.Corpus.Parsers as Parser (FileFormat(..), parseFormat)
+import qualified Gargantext.Database.GargDB as GargDB
------------------------------------------------------------------------
{-
makeLenses ''WithQuery
instance FromJSON WithQuery where
parseJSON = genericParseJSON $ jsonOptions "_wq_"
+instance ToJSON WithQuery where
+ toJSON = genericToJSON $ jsonOptions "_wq_"
instance ToSchema WithQuery where
declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wq_")
--------------------------------------------------------
-data NewWithForm = NewWithForm
- { _wf_filetype :: !FileType
- , _wf_data :: !Text
- , _wf_lang :: !(Maybe Lang)
- , _wf_name :: !Text
- } deriving (Eq, Show, Generic)
-
-makeLenses ''NewWithForm
-instance FromForm NewWithForm
-instance FromJSON NewWithForm where
- parseJSON = genericParseJSON $ jsonOptions "_wf_"
-instance ToSchema NewWithForm where
- declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")
-
-------------------------------------------------------------------------
-type AsyncJobs event ctI input output =
- AsyncJobsAPI' 'Unsafe 'Safe ctI '[JSON] Maybe event input output
------------------------------------------------------------------------
type AddWithQuery = Summary "Add with Query to corpus endpoint"
:> AsyncJobs JobLog '[JSON] () JobLog
-}
-type AddWithForm = Summary "Add with FormUrlEncoded to corpus endpoint"
- :> "corpus"
- :> Capture "corpus_id" CorpusId
- :> "add"
- :> "form"
- :> "async"
- :> AsyncJobs JobLog '[FormUrlEncoded] NewWithForm JobLog
-
------------------------------------------------------------------------
-- TODO WithQuery also has a corpus id
=> User
-> CorpusId
-> WithQuery
+ -> Maybe Integer
-> (JobLog -> m ())
-> m JobLog
-addToCorpusWithQuery u cid (WithQuery q dbs l _nid) logStatus = do
+addToCorpusWithQuery user cid (WithQuery q dbs l _nid) maybeLimit logStatus = do
-- TODO ...
logStatus JobLog { _scst_succeeded = Just 0
- , _scst_failed = Just 0
- , _scst_remaining = Just 5
- , _scst_events = Just []
- }
- printDebug "addToCorpusWithQuery" cid
+ , _scst_failed = Just 0
+ , _scst_remaining = Just 5
+ , _scst_events = Just []
+ }
+ printDebug "addToCorpusWithQuery" (cid, dbs)
-- TODO add cid
-- TODO if cid is folder -> create Corpus
-- if cid is corpus -> add to corpus
-- if cid is root -> create corpus in Private
- txts <- mapM (\db -> getDataText db (Multi l) q (Just 10000)) [database2origin dbs]
+ txts <- mapM (\db -> getDataText db (Multi l) q maybeLimit) [database2origin dbs]
logStatus JobLog { _scst_succeeded = Just 2
- , _scst_failed = Just 0
- , _scst_remaining = Just 1
- , _scst_events = Just []
- }
+ , _scst_failed = Just 0
+ , _scst_remaining = Just 1
+ , _scst_events = Just []
+ }
- cids <- mapM (\txt -> flowDataText u txt (Multi l) cid) txts
+ cids <- mapM (\txt -> flowDataText user txt (Multi l) cid) txts
printDebug "corpus id" cids
+ printDebug "sending email" ("xxxxxxxxxxxxxxxxxxxxx" :: Text)
+ sendMail user
-- TODO ...
pure JobLog { _scst_succeeded = Just 3
- , _scst_failed = Just 0
- , _scst_remaining = Just 0
- , _scst_events = Just []
- }
+ , _scst_failed = Just 0
+ , _scst_remaining = Just 0
+ , _scst_events = Just []
+ }
+
+
+type AddWithForm = Summary "Add with FormUrlEncoded to corpus endpoint"
+ :> "corpus"
+ :> Capture "corpus_id" CorpusId
+ :> "add"
+ :> "form"
+ :> "async"
+ :> AsyncJobs JobLog '[FormUrlEncoded] NewWithForm JobLog
addToCorpusWithForm :: FlowCmdM env err m
=> User
-> m JobLog
addToCorpusWithForm user cid (NewWithForm ft d l _n) logStatus = do
- printDebug "Parsing corpus: " cid
+ printDebug "[addToCorpusWithForm] Parsing corpus: " cid
+ printDebug "[addToCorpusWithForm] fileType" ft
logStatus JobLog { _scst_succeeded = Just 0
- , _scst_failed = Just 0
- , _scst_remaining = Just 2
- , _scst_events = Just []
- }
+ , _scst_failed = Just 0
+ , _scst_remaining = Just 2
+ , _scst_events = Just []
+ }
let
parse = case ft of
CSV_HAL -> Parser.parseFormat Parser.CsvHal
printDebug "Parsing corpus finished : " cid
logStatus JobLog { _scst_succeeded = Just 1
- , _scst_failed = Just 0
- , _scst_remaining = Just 1
- , _scst_events = Just []
- }
+ , _scst_failed = Just 0
+ , _scst_remaining = Just 1
+ , _scst_events = Just []
+ }
printDebug "Starting extraction : " cid
(map (map toHyperdataDocument) docs)
printDebug "Extraction finished : " cid
+ printDebug "sending email" ("xxxxxxxxxxxxxxxxxxxxx" :: Text)
+ sendMail user
+
pure JobLog { _scst_succeeded = Just 2
- , _scst_failed = Just 0
- , _scst_remaining = Just 0
- , _scst_events = Just []
- }
+ , _scst_failed = Just 0
+ , _scst_remaining = Just 0
+ , _scst_events = Just []
+ }
{-
addToCorpusWithFile :: FlowCmdM env err m
-}
+
+type AddWithFile = Summary "Add with FileUrlEncoded to corpus endpoint"
+ :> "corpus"
+ :> Capture "corpus_id" CorpusId
+ :> "add"
+ :> "file"
+ :> "async"
+ :> AsyncJobs JobLog '[FormUrlEncoded] NewWithFile JobLog
+
+addToCorpusWithFile :: (HasSettings env, FlowCmdM env err m)
+ => User
+ -> CorpusId
+ -> NewWithFile
+ -> (JobLog -> m ())
+ -> m JobLog
+addToCorpusWithFile user cid nwf@(NewWithFile _d _l fName) logStatus = do
+
+ printDebug "[addToCorpusWithFile] Uploading file to corpus: " cid
+ logStatus JobLog { _scst_succeeded = Just 0
+ , _scst_failed = Just 0
+ , _scst_remaining = Just 1
+ , _scst_events = Just []
+ }
+
+ fPath <- GargDB.writeFile nwf
+ printDebug "[addToCorpusWithFile] File saved as: " fPath
+
+ uId <- getUserId user
+ nIds <- mkNodeWithParent NodeFile (Just cid) uId fName
+
+ _ <- case nIds of
+ [nId] -> do
+ node <- getNodeWith nId (Proxy :: Proxy HyperdataFile)
+ let hl = node ^. node_hyperdata
+ _ <- updateHyperdata nId $ hl { _hff_name = fName
+ , _hff_path = T.pack fPath }
+
+ printDebug "[addToCorpusWithFile] Created node with id: " nId
+ _ -> pure ()
+
+ printDebug "[addToCorpusWithFile] File upload to corpus finished: " cid
+
+ printDebug "sending email" ("xxxxxxxxxxxxxxxxxxxxx" :: Text)
+ sendMail user
+
+ pure $ JobLog { _scst_succeeded = Just 1
+ , _scst_failed = Just 0
+ , _scst_remaining = Just 0
+ , _scst_events = Just []
+ }