2 Module : Gargantext.Database.Flow
3 Description : Database Flow
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 -- check userId CanFillUserCorpus userCorpusId
12 -- check masterUserId CanFillMasterCorpus masterCorpusId
14 -- TODO-ACCESS: check uId CanInsertDoc pId && checkDocType nodeType
15 -- TODO-EVENTS: InsertedNodes
18 {-# OPTIONS_GHC -fno-warn-orphans #-}
20 {-# LANGUAGE ConstraintKinds #-}
21 {-# LANGUAGE ConstrainedClassMethods #-}
22 {-# LANGUAGE ConstraintKinds #-}
23 {-# LANGUAGE InstanceSigs #-}
24 {-# LANGUAGE TemplateHaskell #-}
26 module Gargantext.Database.Action.Flow -- (flowDatabase, ngrams2list)
38 , getOrMk_RootWithCorpus
47 import Control.Lens ((^.), view, _Just, makeLenses)
48 import Data.Aeson.TH (deriveJSON)
50 import Data.List (concat)
51 import qualified Data.Map as Map
52 import Data.Map (Map, lookup)
53 import Data.Maybe (catMaybes)
56 import Data.Text (splitOn, intercalate)
57 import Data.Traversable (traverse)
58 import Data.Tuple.Extra (first, second)
59 import GHC.Generics (Generic)
60 import System.FilePath (FilePath)
62 import Gargantext.Core (Lang(..))
63 import Gargantext.Core.Ext.IMT (toSchoolName)
64 import Gargantext.Core.Ext.IMTUser (deserialiseImtUsersFromFile)
65 import Gargantext.Core.Flow.Types
66 import Gargantext.Core.Text
67 import Gargantext.Core.Text.Corpus.Parsers (parseFile, FileFormat)
68 import Gargantext.Core.Text.List (buildNgramsLists)
69 import Gargantext.Core.Text.Group (StopSize(..), GroupParams(..))
70 import Gargantext.Core.Text.Terms
71 import Gargantext.Core.Text.Terms.Mono.Stem.En (stemIt)
72 import Gargantext.Core.Types (Terms(..))
73 import Gargantext.Core.Types.Individu (User(..))
74 import Gargantext.Core.Types.Main
75 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
76 import Gargantext.Database.Action.Flow.List
77 import Gargantext.Database.Action.Flow.Types
78 import Gargantext.Database.Action.Flow.Utils (insertDocNgrams)
79 import Gargantext.Database.Action.Search (searchDocInDatabase)
80 import Gargantext.Database.Admin.Config (userMaster, corpusMasterName)
81 import Gargantext.Database.Admin.Types.Hyperdata
82 import Gargantext.Database.Admin.Types.Node -- (HyperdataDocument(..), NodeType(..), NodeId, UserId, ListId, CorpusId, RootId, MasterCorpusId, MasterUserId)
83 import Gargantext.Database.Prelude
84 import Gargantext.Database.Query.Table.Ngrams
85 import Gargantext.Database.Query.Table.Node
86 import Gargantext.Database.Query.Table.Node.Document.Insert -- (insertDocuments, ReturnId(..), addUniqIdsDoc, addUniqIdsContact, ToDbData(..))
87 import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
88 import Gargantext.Database.Query.Table.NodeNgrams (listInsertDb , getCgramsId)
89 import Gargantext.Database.Query.Table.NodeNodeNgrams2
90 import Gargantext.Database.Query.Tree.Root (getOrMkRoot, getOrMk_RootWithCorpus)
91 import Gargantext.Database.Schema.Node (NodePoly(..))
92 import Gargantext.Prelude
93 import Gargantext.Prelude.Crypto.Hash (Hash)
94 import qualified Gargantext.Core.Text.Corpus.API as API
95 import qualified Gargantext.Database.Query.Table.Node.Document.Add as Doc (add)
97 ------------------------------------------------------------------------
98 -- TODO use internal with API name (could be old data)
99 data DataOrigin = InternalOrigin { _do_api :: API.ExternalAPIs }
100 | ExternalOrigin { _do_api :: API.ExternalAPIs }
102 deriving (Generic, Eq)
104 makeLenses ''DataOrigin
105 deriveJSON (unPrefix "_do_") ''DataOrigin
106 instance ToSchema DataOrigin where
107 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_do_")
109 allDataOrigins :: [DataOrigin]
110 allDataOrigins = map InternalOrigin API.externalAPIs
111 <> map ExternalOrigin API.externalAPIs
114 data DataText = DataOld ![NodeId]
115 | DataNew ![[HyperdataDocument]]
117 -- TODO use the split parameter in config file
118 getDataText :: FlowCmdM env err m
124 getDataText (ExternalOrigin api) la q li = liftBase $ DataNew
126 <$> API.get api (_tt_lang la) q li
127 getDataText (InternalOrigin _) _la q _li = do
128 (_masterUserId, _masterRootId, cId) <- getOrMk_RootWithCorpus
129 (UserName userMaster)
131 (Nothing :: Maybe HyperdataCorpus)
132 ids <- map fst <$> searchDocInDatabase cId (stemIt q)
135 -------------------------------------------------------------------------------
136 flowDataText :: ( FlowCmdM env err m
143 flowDataText u (DataOld ids) tt cid = flowCorpusUser (_tt_lang tt) u (Right [cid]) corpusType ids
145 corpusType = (Nothing :: Maybe HyperdataCorpus)
146 flowDataText u (DataNew txt) tt cid = flowCorpus u (Right [cid]) tt txt
148 ------------------------------------------------------------------------
150 flowAnnuaire :: (FlowCmdM env err m)
152 -> Either CorpusName [CorpusId]
156 flowAnnuaire u n l filePath = do
157 docs <- liftBase $ (( splitEvery 500 <$> deserialiseImtUsersFromFile filePath) :: IO [[HyperdataContact]])
158 flow (Nothing :: Maybe HyperdataAnnuaire) u n l docs
160 ------------------------------------------------------------------------
161 flowCorpusFile :: (FlowCmdM env err m)
163 -> Either CorpusName [CorpusId]
164 -> Limit -- Limit the number of docs (for dev purpose)
165 -> TermType Lang -> FileFormat -> FilePath
167 flowCorpusFile u n l la ff fp = do
168 docs <- liftBase ( splitEvery 500
172 flowCorpus u n la (map (map toHyperdataDocument) docs)
174 ------------------------------------------------------------------------
175 -- | TODO improve the needed type to create/update a corpus
176 -- (For now, Either is enough)
177 flowCorpus :: (FlowCmdM env err m, FlowCorpus a)
179 -> Either CorpusName [CorpusId]
183 flowCorpus = flow (Nothing :: Maybe HyperdataCorpus)
186 flow :: ( FlowCmdM env err m
192 -> Either CorpusName [CorpusId]
196 flow c u cn la docs = do
197 -- TODO if public insertMasterDocs else insertUserDocs
198 ids <- traverse (insertMasterDocs c la) docs
199 flowCorpusUser (la ^. tt_lang) u cn c (concat ids)
201 ------------------------------------------------------------------------
202 flowCorpusUser :: ( FlowCmdM env err m
207 -> Either CorpusName [CorpusId]
211 flowCorpusUser l user corpusName ctype ids = do
213 (userId, _rootId, userCorpusId) <- getOrMk_RootWithCorpus user corpusName ctype
214 listId <- getOrMkList userCorpusId userId
215 -- _cooc <- insertDefaultNode NodeListCooc listId userId
216 -- TODO: check if present already, ignore
217 _ <- Doc.add userCorpusId ids
219 _tId <- insertDefaultNode NodeTexts userCorpusId userId
220 -- printDebug "Node Text Ids:" tId
223 (masterUserId, _masterRootId, masterCorpusId) <- getOrMk_RootWithCorpus (UserName userMaster) (Left "") ctype
224 ngs <- buildNgramsLists user (GroupParams l 2 3 (StopSize 3)) userCorpusId masterCorpusId
225 _userListId <- flowList_DbRepo listId ngs
226 _mastListId <- getOrMkList masterCorpusId masterUserId
227 -- _ <- insertOccsUpdates userCorpusId mastListId
228 -- printDebug "userListId" userListId
230 _ <- insertDefaultNode NodeDashboard userCorpusId userId
231 _ <- insertDefaultNode NodeGraph userCorpusId userId
232 --_ <- mkPhylo userCorpusId userId
234 -- _ <- mkAnnuaire rootUserId userId
238 insertMasterDocs :: ( FlowCmdM env err m
246 insertMasterDocs c lang hs = do
247 (masterUserId, _, masterCorpusId) <- getOrMk_RootWithCorpus (UserName userMaster) (Left corpusMasterName) c
248 (ids', documentsWithId) <- insertDocs masterUserId masterCorpusId (map (toNode masterUserId masterCorpusId) hs )
249 _ <- Doc.add masterCorpusId ids'
251 -- create a corpus with database name (CSV or PubMed)
252 -- add documents to the corpus (create node_node link)
253 -- this will enable global database monitoring
255 -- maps :: IO Map Ngrams (Map NgramsType (Map NodeId Int))
256 mapNgramsDocs <- mapNodeIdNgrams
257 <$> documentIdWithNgrams (extractNgramsT $ withLang lang documentsWithId) documentsWithId
259 terms2id <- insertNgrams $ Map.keys mapNgramsDocs
261 let indexedNgrams = Map.mapKeys (indexNgrams terms2id) mapNgramsDocs
264 lId <- getOrMkList masterCorpusId masterUserId
265 mapCgramsId <- listInsertDb lId toNodeNgramsW'
266 $ map (first _ngramsTerms . second Map.keys)
267 $ Map.toList mapNgramsDocs
269 _return <- insertNodeNodeNgrams2
270 $ catMaybes [ NodeNodeNgrams2 <$> Just nId
271 <*> getCgramsId mapCgramsId ngrams_type (_ngramsTerms terms'')
272 <*> Just (fromIntegral w :: Double)
273 | (terms'', mapNgramsTypes) <- Map.toList mapNgramsDocs
274 , (ngrams_type, mapNodeIdWeight) <- Map.toList mapNgramsTypes
275 , (nId, w) <- Map.toList mapNodeIdWeight
278 -- _cooc <- insertDefaultNode NodeListCooc lId masterUserId
280 _ <- insertDocNgrams lId indexedNgrams
283 ------------------------------------------------------------------------
284 -- TODO Type NodeDocumentUnicised
285 insertDocs :: ( FlowCmdM env err m
292 -> m ([DocId], [DocumentWithId a])
293 insertDocs uId cId hs = do
294 let docs = map addUniqId hs
295 newIds <- insertDb uId cId docs
296 -- printDebug "newIds" newIds
298 newIds' = map reId newIds
299 documentsWithId = mergeData (toInserted newIds) (Map.fromList $ map viewUniqId' docs)
300 _ <- Doc.add cId newIds'
301 pure (newIds', documentsWithId)
305 ------------------------------------------------------------------------
306 viewUniqId' :: UniqId a
309 viewUniqId' d = maybe err (\h -> (h,d)) (view uniqId d)
311 err = panic "[ERROR] Database.Flow.toInsert"
314 toInserted :: [ReturnId]
317 Map.fromList . map (\r -> (reUniqId r, r) )
318 . filter (\r -> reInserted r == True)
320 mergeData :: Map Hash ReturnId
322 -> [DocumentWithId a]
323 mergeData rs = catMaybes . map toDocumentWithId . Map.toList
325 toDocumentWithId (sha,hpd) =
326 DocumentWithId <$> fmap reId (lookup sha rs)
329 ------------------------------------------------------------------------
330 instance HasText HyperdataContact
333 ------------------------------------------------------------------------
334 ------------------------------------------------------------------------
335 documentIdWithNgrams :: HasNodeError err
337 -> Cmd err (Map Ngrams (Map NgramsType Int)))
338 -> [DocumentWithId a]
339 -> Cmd err [DocumentIdWithNgrams a]
340 documentIdWithNgrams f = traverse toDocumentIdWithNgrams
342 toDocumentIdWithNgrams d = do
343 e <- f $ documentData d
344 pure $ DocumentIdWithNgrams d e
346 ------------------------------------------------------------------------
347 instance ExtractNgramsT HyperdataContact
349 extractNgramsT l hc = filterNgramsT 255 <$> extract l hc
351 extract :: TermType Lang -> HyperdataContact
352 -> Cmd err (Map Ngrams (Map NgramsType Int))
354 let authors = map text2ngrams
355 $ maybe ["Nothing"] (\a -> [a])
356 $ view (hc_who . _Just . cw_lastName) hc'
358 pure $ Map.fromList $ [(a', Map.singleton Authors 1) | a' <- authors ]
360 instance HasText HyperdataDocument
362 hasText h = catMaybes [ _hd_title h
367 instance ExtractNgramsT HyperdataDocument
369 extractNgramsT :: TermType Lang
371 -> Cmd err (Map Ngrams (Map NgramsType Int))
372 extractNgramsT lang hd = filterNgramsT 255 <$> extractNgramsT' lang hd
374 extractNgramsT' :: TermType Lang
376 -> Cmd err (Map Ngrams (Map NgramsType Int))
377 extractNgramsT' lang' doc = do
378 let source = text2ngrams
379 $ maybe "Nothing" identity
382 institutes = map text2ngrams
383 $ maybe ["Nothing"] (map toSchoolName . (splitOn ", "))
386 authors = map text2ngrams
387 $ maybe ["Nothing"] (splitOn ", ")
390 terms' <- map text2ngrams
391 <$> map (intercalate " " . _terms_label)
393 <$> liftBase (extractTerms lang' $ hasText doc)
395 pure $ Map.fromList $ [(source, Map.singleton Sources 1)]
396 <> [(i', Map.singleton Institutes 1) | i' <- institutes ]
397 <> [(a', Map.singleton Authors 1) | a' <- authors ]
398 <> [(t', Map.singleton NgramsTerms 1) | t' <- terms' ]
400 instance (ExtractNgramsT a, HasText a) => ExtractNgramsT (Node a)
402 extractNgramsT l (Node _ _ _ _ _ _ _ h) = extractNgramsT l h
404 instance HasText a => HasText (Node a)
406 hasText (Node _ _ _ _ _ _ _ h) = hasText h