]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Flow/List.hs
[DB] Node_NodeNgrams_NodeNgrams insertion in flowList (WIP).
[gargantext.git] / src / Gargantext / Database / Flow / List.hs
1 {-|
2 Module : Gargantext.Database.Flow.List
3 Description : List Flow
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
8 Portability : POSIX
9
10 -}
11
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
13
14 {-# LANGUAGE ConstraintKinds #-}
15 {-# LANGUAGE RankNTypes #-}
16 {-# LANGUAGE ConstrainedClassMethods #-}
17 {-# LANGUAGE ConstraintKinds #-}
18 {-# LANGUAGE DeriveGeneric #-}
19 {-# LANGUAGE FlexibleContexts #-}
20 {-# LANGUAGE InstanceSigs #-}
21 {-# LANGUAGE NoImplicitPrelude #-}
22 {-# LANGUAGE OverloadedStrings #-}
23
24 module Gargantext.Database.Flow.List
25 where
26 import Control.Monad (mapM_)
27 import Data.Map (Map, toList)
28 import Data.Maybe (Maybe(..), catMaybes)
29 import Gargantext.API.Ngrams (NgramsElement(..), putListNgrams)
30 import Gargantext.Database.Schema.Ngrams -- (insertNgrams, Ngrams(..), NgramsIndexed(..), indexNgrams, NgramsType(..), text2ngrams, ngramsTypeId)
31 import Gargantext.Database.Schema.NodeNgrams (NodeNgramsPoly(..), NodeNgramsW, listInsertDb, getCgramsId)
32 import Gargantext.Database.Schema.Node_NodeNgramsNodeNgrams -- (insert_Node_NodeNgrams_NodeNgrams, Node_NodeNgrams_NodeNgrams(..))
33 import Gargantext.Database.Types.Node -- (HyperdataDocument(..), NodeType(..), NodeId, UserId, ListId, CorpusId, RootId, MasterCorpusId, MasterUserId)
34 import Gargantext.Database.Flow.Types
35 import Gargantext.Prelude
36 import qualified Data.List as List
37 import qualified Data.Map as Map
38
39
40 -- FLOW LIST
41 -- | TODO check optimization
42 mapNodeIdNgrams :: [DocumentIdWithNgrams a]
43 -> Map Ngrams (Map NgramsType (Map NodeId Int))
44 mapNodeIdNgrams = Map.unionsWith (Map.unionWith (Map.unionWith (+))) . fmap f
45 where
46 f :: DocumentIdWithNgrams a
47 -> Map Ngrams (Map NgramsType (Map NodeId Int))
48 f d = fmap (fmap (Map.singleton nId)) $ document_ngrams d
49 where
50 nId = documentId $ documentWithId d
51
52 ------------------------------------------------------------------------
53 listInsert :: FlowCmdM env err m
54 => ListId
55 -> Map NgramsType [NgramsElement]
56 -> m ()
57 listInsert lId ngs = mapM_ (\(typeList, ngElmts)
58 -> putListNgrams lId typeList ngElmts
59 ) $ toList ngs
60
61 toNodeNgramsW :: ListId
62 -> [(NgramsType, [NgramsElement])]
63 -> [NodeNgramsW]
64 toNodeNgramsW l ngs = List.concat $ map (toNodeNgramsW' l) ngs
65 where
66 toNodeNgramsW' :: ListId
67 -> (NgramsType, [NgramsElement])
68 -> [NodeNgramsW]
69 toNodeNgramsW' l' (ngrams_type, elms) =
70 [ NodeNgrams Nothing l' list_type ngrams_terms' ngrams_type Nothing Nothing Nothing 0 |
71 (NgramsElement ngrams_terms' _size list_type _occ _root _parent _children) <- elms
72 ]
73
74 flowList :: FlowCmdM env err m
75 => CorpusId
76 -> ListId
77 -> Map NgramsType [NgramsElement]
78 -> m ListId
79 flowList _cId lId ngs = do
80 -- printDebug "listId flowList" lId
81 -- TODO save in database
82 mapCgramsId <- listInsertDb lId toNodeNgramsW (Map.toList ngs)
83 let toInsert = catMaybes [ (,) <$> (getCgramsId mapCgramsId ntype <$> parent)
84 <*> getCgramsId mapCgramsId ntype ngram
85 | (ntype, ngs') <- Map.toList ngs
86 , NgramsElement ngram _ _ _ _ parent _ <- ngs'
87 ]
88 _r <- insert_Node_NodeNgrams_NodeNgrams $ map (\(a,b) -> Node_NodeNgrams_NodeNgrams lId a b Nothing) toInsert
89 listInsert lId ngs
90 --trace (show $ List.filter (\n -> _ne_ngrams n == "versatile") $ List.concat $ Map.elems ngs) $ listInsert lId ngs
91 pure lId
92