2 Module : Gargantext.Viz.Chart
3 Description : Graph utils
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE TemplateHaskell #-}
14 module Gargantext.Viz.Chart
17 import Data.List (unzip, sortOn)
18 import Data.Map (toList)
19 import qualified Data.List as List
20 import qualified Data.Map as Map
21 import Data.Maybe (catMaybes)
23 import Gargantext.Core.Types.Main
24 import Gargantext.Database.Admin.Config
25 import Gargantext.Database.Admin.Types.Node (CorpusId)
26 import Gargantext.Database.Prelude
27 import Gargantext.Database.Query.Table.Node
28 import Gargantext.Database.Query.Table.Node.Select
29 import Gargantext.Database.Query.Table.NodeNode (selectDocsDates)
30 import Gargantext.Database.Schema.Node
31 import Gargantext.Prelude
32 import Gargantext.Text.Metrics.Count (occurrencesWith)
35 import Gargantext.API.Ngrams.NTree
36 import Gargantext.API.Ngrams.Tools
37 import Gargantext.Core.Types
38 import Gargantext.Database.Action.Flow
39 import Gargantext.Database.Action.Metrics.NgramsByNode
40 import Gargantext.Database.Schema.Ngrams
41 import Gargantext.Viz.Types
43 histoData :: CorpusId -> Cmd err Histo
45 dates <- selectDocsDates cId
49 $ occurrencesWith identity dates
53 chartData :: FlowCmdM env err m
54 => CorpusId -> NgramsType -> ListType
56 chartData cId nt lt = do
57 ls' <- selectNodesWithUsername NodeList userMaster
58 ls <- map (_node_id) <$> getListsWithParentId cId
59 ts <- mapTermListRoot ls nt <$> getRepo
61 dico = filterListWithRoot lt ts
62 terms = catMaybes $ List.concat $ map (\(a,b) -> [Just a, b]) $ Map.toList dico
63 group dico' x = case Map.lookup x dico' of
65 Just x' -> maybe x identity x'
67 (_total,mapTerms) <- countNodesByNgramsWith (group dico)
68 <$> getNodesByNgramsOnlyUser cId (ls' <> ls) nt terms
69 let (dates, count) = unzip $ map (\(t,(d,_)) -> (t, d)) $ Map.toList mapTerms
70 pure (Histo dates (map round count))
73 treeData :: FlowCmdM env err m
74 => CorpusId -> NgramsType -> ListType
76 treeData cId nt lt = do
77 ls' <- selectNodesWithUsername NodeList userMaster
78 ls <- map (_node_id) <$> getListsWithParentId cId
79 ts <- mapTermListRoot ls nt <$> getRepo
82 dico = filterListWithRoot lt ts
83 terms = catMaybes $ List.concat $ map (\(a,b) -> [Just a, b]) $ Map.toList dico
85 cs' <- getNodesByNgramsOnlyUser cId (ls' <> ls) nt terms
87 m <- getListNgrams ls nt
88 pure $ toTree lt cs' m