2 Module : Gargantext.API.Corpus.New
3 Description : New corpus API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 New corpus means either:
12 - new data in existing corpus
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE TemplateHaskell #-}
17 {-# LANGUAGE DeriveGeneric #-}
18 {-# LANGUAGE DataKinds #-}
19 {-# LANGUAGE TypeOperators #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE FlexibleContexts #-}
22 {-# LANGUAGE RankNTypes #-}
23 {-# OPTIONS_GHC -fno-warn-orphans #-}
25 module Gargantext.API.Corpus.New
28 --import Gargantext.Text.Corpus.Parsers (parseFile, FileFormat(..))
29 import Control.Lens hiding (elements)
31 import Data.Aeson.TH (deriveJSON)
32 import Data.Maybe (fromMaybe)
35 import Data.Text (Text)
36 import GHC.Generics (Generic)
37 import Gargantext.API.Corpus.New.File
38 import Gargantext.API.Orchestrator.Types
39 import Gargantext.Core (Lang(..))
40 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
41 import Gargantext.Database.Flow (FlowCmdM, flowCorpus)
42 import Gargantext.Database.Flow (flowCorpusSearchInDatabase)
43 import Gargantext.Database.Types.Node (CorpusId)
44 import Gargantext.Database.Types.Node (ToHyperdataDocument(..))
45 import Gargantext.Core.Types.Individu (UserId, User(..))
46 import Gargantext.Prelude
47 import qualified Gargantext.Text.Corpus.Parsers as Parser (FileFormat(..), parseFormat)
48 import Gargantext.Text.Terms (TermType(..))
50 import Servant.API.Flatten (Flat)
51 import Servant.Job.Core
52 import Servant.Job.Types
53 import Servant.Job.Utils (jsonOptions)
54 import Servant.Multipart
55 import Test.QuickCheck (elements)
56 import Test.QuickCheck.Arbitrary
57 import Web.FormUrlEncoded (FromForm)
58 import qualified Gargantext.Text.Corpus.API as API
60 data Query = Query { query_query :: Text
61 , query_corpus_id :: Int
62 , query_databases :: [API.ExternalAPIs]
64 deriving (Eq, Show, Generic)
66 deriveJSON (unPrefix "query_") 'Query
68 instance Arbitrary Query where
69 arbitrary = elements [ Query q n fs
72 , fs <- take 3 $ repeat API.externalAPIs
75 instance ToSchema Query where
76 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "query_")
78 ------------------------------------------------------------------------
83 type PostApi = Summary "New Corpus endpoint"
84 :> ReqBody '[JSON] Query
85 :> Post '[JSON] CorpusId
86 type GetApi = Get '[JSON] ApiInfo
88 -- | TODO manage several apis
90 -- TODO this is only the POST
91 api :: (FlowCmdM env err m) => UserId -> Query -> m CorpusId
92 api uid (Query q _ as) = do
93 cId <- case head as of
94 Nothing -> flowCorpusSearchInDatabase (UserDBId uid) EN q
95 Just API.All -> flowCorpusSearchInDatabase (UserDBId uid) EN q
97 docs <- liftBase $ API.get a q (Just 1000)
98 cId' <- flowCorpus (UserDBId uid) (Left q) (Multi EN) [docs]
103 ------------------------------------------------
104 data ApiInfo = ApiInfo { api_info :: [API.ExternalAPIs]}
106 instance Arbitrary ApiInfo where
107 arbitrary = ApiInfo <$> arbitrary
109 deriveJSON (unPrefix "") 'ApiInfo
111 instance ToSchema ApiInfo
113 info :: FlowCmdM env err m => UserId -> m ApiInfo
114 info _u = pure $ ApiInfo API.externalAPIs
116 ------------------------------------------------------------------------
117 ------------------------------------------------------------------------
118 data WithQuery = WithQuery
120 , _wq_databases :: ![ExternalAPIs]
121 , _wq_lang :: !(Maybe Lang)
125 makeLenses ''WithQuery
126 instance FromJSON WithQuery where
127 parseJSON = genericParseJSON $ jsonOptions "_wq_"
128 instance ToSchema WithQuery where
129 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wq_")
131 -------------------------------------------------------
132 data WithForm = WithForm
133 { _wf_filetype :: !FileType
135 , _wf_lang :: !(Maybe Lang)
137 } deriving (Eq, Show, Generic)
139 makeLenses ''WithForm
140 instance FromForm WithForm
141 instance FromJSON WithForm where
142 parseJSON = genericParseJSON $ jsonOptions "_wf_"
143 instance ToSchema WithForm where
144 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_wf_")
146 ------------------------------------------------------------------------
147 type AsyncJobs event ctI input output =
148 Flat (AsyncJobsAPI' 'Unsafe 'Safe ctI '[JSON] Maybe event input output)
149 ------------------------------------------------------------------------
151 type Upload = Summary "Corpus Upload endpoint"
153 :> Capture "corpus_id" CorpusId
154 :<|> "addWithquery" :> AsyncJobsAPI ScraperStatus WithQuery ScraperStatus
155 :<|> "addWithfile" :> AsyncJobs ScraperStatus '[FormUrlEncoded] WithForm ScraperStatus
157 type AddWithQuery = Summary "Add with Query to corpus endpoint"
159 :> Capture "corpus_id" CorpusId
163 :> AsyncJobsAPI ScraperStatus WithQuery ScraperStatus
165 type AddWithFile = Summary "Add with MultipartData to corpus endpoint"
167 :> Capture "corpus_id" CorpusId
170 :> MultipartForm Mem (MultipartData Mem)
171 :> QueryParam "fileType" FileType
173 :> AsyncJobs ScraperStatus '[JSON] () ScraperStatus
175 type AddWithForm = Summary "Add with FormUrlEncoded to corpus endpoint"
177 :> Capture "corpus_id" CorpusId
181 :> AsyncJobs ScraperStatus '[FormUrlEncoded] WithForm ScraperStatus
183 ------------------------------------------------------------------------
184 -- TODO WithQuery also has a corpus id
185 addToCorpusWithQuery :: FlowCmdM env err m
189 -> (ScraperStatus -> m ())
191 addToCorpusWithQuery u cid (WithQuery q _dbs l) logStatus = do
193 logStatus ScraperStatus { _scst_succeeded = Just 10
194 , _scst_failed = Just 2
195 , _scst_remaining = Just 138
196 , _scst_events = Just []
198 printDebug "addToCorpusWithQuery" cid
200 -- TODO if cid is folder -> create Corpus
201 -- if cid is corpus -> add to corpus
202 -- if cid is root -> create corpus in Private
203 cids <- flowCorpusSearchInDatabase u (maybe EN identity l) q
204 printDebug "corpus id" cids
206 pure ScraperStatus { _scst_succeeded = Just 137
207 , _scst_failed = Just 13
208 , _scst_remaining = Just 0
209 , _scst_events = Just []
212 addToCorpusWithFile :: FlowCmdM env err m
216 -> (ScraperStatus -> m ())
218 addToCorpusWithFile cid input filetype logStatus = do
219 logStatus ScraperStatus { _scst_succeeded = Just 10
220 , _scst_failed = Just 2
221 , _scst_remaining = Just 138
222 , _scst_events = Just []
224 printDebug "addToCorpusWithFile" cid
225 _h <- postUpload cid filetype input
227 pure ScraperStatus { _scst_succeeded = Just 137
228 , _scst_failed = Just 13
229 , _scst_remaining = Just 0
230 , _scst_events = Just []
233 {- | Model to fork the flow
234 -- This is not really optimized since it increases the need RAM
235 -- and freezes the whole system
236 -- This is mainly for documentation (see a better solution in the function below)
237 -- Each process has to be tailored
238 addToCorpusWithForm' :: FlowCmdM env err m
241 -> (ScraperStatus -> m ())
243 addToCorpusWithForm' cid (WithForm ft d l) logStatus = do
244 newStatus <- liftBase newEmptyMVar
245 s <- addToCorpusWithForm cid (WithForm ft d l) logStatus
246 _ <- liftBase $ forkIO $ putMVar newStatus s
247 s' <- liftBase $ takeMVar newStatus
250 addToCorpusWithForm :: FlowCmdM env err m
254 -> (ScraperStatus -> m ())
256 addToCorpusWithForm user cid (WithForm ft d l _n) logStatus = do
260 CSV_HAL -> Parser.parseFormat Parser.CsvHal
261 CSV -> Parser.parseFormat Parser.CsvGargV3
262 WOS -> Parser.parseFormat Parser.WOS
263 PresseRIS -> Parser.parseFormat Parser.RisPresse
265 logStatus ScraperStatus { _scst_succeeded = Just 1
266 , _scst_failed = Just 0
267 , _scst_remaining = Just 1
268 , _scst_events = Just []
271 printDebug "Parsing corpus: " cid
273 -- TODO granularity of the logStatus
274 docs <- liftBase $ splitEvery 500
278 printDebug "Parsing corpus finished : " cid
279 printDebug "Starting extraction : " cid
281 -- TODO granularity of the logStatus
282 _cid' <- flowCorpus user
284 (Multi $ fromMaybe EN l)
285 (map (map toHyperdataDocument) docs)
287 printDebug "Extraction finished : " cid
289 pure ScraperStatus { _scst_succeeded = Just 2
290 , _scst_failed = Just 0
291 , _scst_remaining = Just 0
292 , _scst_events = Just []