]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Node/Corpus/Export.hs
[NodeStory] NodeStory Integration, compilation with warning ok (WIP)
[gargantext.git] / src / Gargantext / API / Node / Corpus / Export.hs
1 {-|
2 Module : Gargantext.API.Node.Corpus.Export
3 Description : Get Metrics from Storage (Database like)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 Main exports of Gargantext:
11 - corpus
12 - document and ngrams
13 - lists
14 -}
15
16 module Gargantext.API.Node.Corpus.Export
17 where
18
19 import Data.Map (Map)
20 import Data.Maybe (fromMaybe)
21 import Data.Set (Set)
22 import Gargantext.API.Node.Corpus.Export.Types
23 import Gargantext.API.Ngrams.Types
24 import Gargantext.API.Ngrams.Tools (filterListWithRoot, mapTermListRoot, getRepo')
25 import Gargantext.API.Prelude (GargNoServer)
26 import Gargantext.Prelude.Crypto.Hash (hash)
27 import Gargantext.Core.Types
28 import Gargantext.Core.NodeStory
29 import Gargantext.Database.Action.Metrics.NgramsByNode (getNgramsByNodeOnlyUser)
30 import Gargantext.Database.Admin.Config (userMaster)
31 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataDocument(..))
32 import Gargantext.Database.Prelude (Cmd)
33 import Gargantext.Database.Query.Table.Node
34 import Gargantext.Database.Query.Table.Node.Error (HasNodeError)
35 import Gargantext.Database.Query.Table.Node.Select (selectNodesWithUsername)
36 import Gargantext.Database.Query.Table.NodeNode (selectDocNodes)
37 import Gargantext.Database.Schema.Ngrams (NgramsType(..))
38 import Gargantext.Database.Schema.Node (_node_id, _node_hyperdata)
39 import Gargantext.Prelude
40 import qualified Data.List as List
41 import qualified Data.Map as Map
42 import qualified Data.Set as Set
43 import qualified Data.HashMap.Strict as HashMap
44
45 --------------------------------------------------
46 -- | Hashes are ordered by Set
47 getCorpus :: CorpusId
48 -> Maybe ListId
49 -> Maybe NgramsType
50 -> GargNoServer Corpus
51 getCorpus cId lId nt' = do
52
53 let
54 nt = case nt' of
55 Nothing -> NgramsTerms
56 Just t -> t
57
58 ns <- Map.fromList
59 <$> map (\n -> (_node_id n, n))
60 <$> selectDocNodes cId
61
62 repo <- getRepo' [fromMaybe (panic "[Gargantext.API.Node.Corpus.Export]") lId]
63 ngs <- getNodeNgrams cId lId nt repo
64 let -- uniqId is hash computed already for each document imported in database
65 r = Map.intersectionWith (\a b -> Document a (Ngrams (Set.toList b) (hash b)) (d_hash a b)
66 ) ns (Map.map (Set.map unNgramsTerm) ngs)
67 where
68 d_hash a b = hash [ fromMaybe "" (_hd_uniqId $ _node_hyperdata a)
69 , hash b
70 ]
71 pure $ Corpus (Map.elems r) (hash $ List.map _d_hash
72 $ Map.elems r
73 )
74
75 getNodeNgrams :: HasNodeError err
76 => CorpusId
77 -> Maybe ListId
78 -> NgramsType
79 -> NodeListStory
80 -> Cmd err (Map NodeId (Set NgramsTerm))
81 getNodeNgrams cId lId' nt repo = do
82 lId <- case lId' of
83 Nothing -> defaultList cId
84 Just l -> pure l
85
86 lIds <- selectNodesWithUsername NodeList userMaster
87 let ngs = filterListWithRoot MapTerm $ mapTermListRoot [lId] nt repo
88 -- TODO HashMap
89 r <- getNgramsByNodeOnlyUser cId (lIds <> [lId]) nt (HashMap.keys ngs)
90 pure r
91
92 -- TODO
93 -- Exports List
94 -- Version number of the list