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 NoImplicitPrelude #-}
13 {-# LANGUAGE TemplateHaskell #-}
14 {-# LANGUAGE DeriveGeneric #-}
15 {-# LANGUAGE RankNTypes #-}
16 {-# LANGUAGE FlexibleContexts #-}
18 module Gargantext.Viz.Chart
22 import Data.Text (Text)
23 import Data.List (unzip, sortOn)
24 import Data.Map (toList)
25 import GHC.Generics (Generic)
26 import Gargantext.Prelude
27 import Gargantext.Database.Config
28 import Gargantext.Database.Schema.NodeNode (selectDocsDates)
29 import Gargantext.Database.Utils
30 import Gargantext.Database.Types.Node (CorpusId)
31 import Gargantext.Database.Node.Select
32 import Gargantext.Text.Metrics.Count (occurrencesWith)
33 import Gargantext.Core.Types.Main
36 import Data.Maybe (catMaybes)
37 import qualified Data.Map as Map
38 import qualified Data.List as List
39 import Gargantext.API.Ngrams.Tools
40 import Gargantext.API.Ngrams.NTree
41 import Gargantext.Database.Metrics.NgramsByNode
42 import Gargantext.Database.Schema.Ngrams
43 import Gargantext.Database.Schema.Node
44 import Gargantext.Core.Types
45 import Gargantext.Database.Flow
50 data Chart = ChartHisto | ChartScatter | ChartPie
54 data Histo = Histo { histo_dates :: [Text]
55 , histo_count :: [Int]
59 histoData :: CorpusId -> Cmd err Histo
61 dates <- selectDocsDates cId
65 $ occurrencesWith identity dates
69 pieData :: FlowCmdM env err m
70 => CorpusId -> NgramsType -> ListType
72 pieData cId nt lt = do
73 ls' <- selectNodesWithUsername NodeList userMaster
74 ls <- map (_node_id) <$> getListsWithParentId cId
75 ts <- mapTermListRoot ls nt
77 dico = filterListWithRoot lt ts
78 terms = catMaybes $ List.concat $ map (\(a,b) -> [Just a, b]) $ Map.toList dico
79 group dico' x = case Map.lookup x dico' of
81 Just x' -> maybe x identity x'
83 (_total,mapTerms) <- countNodesByNgramsWith (group dico)
84 <$> getNodesByNgramsOnlyUser cId (ls' <> ls) nt terms
85 let (dates, count) = unzip $ map (\(t,(d,_)) -> (t, d)) $ Map.toList mapTerms
86 pure (Histo dates (map round count))
91 treeData :: FlowCmdM env err m
92 => CorpusId -> NgramsType -> ListType
94 treeData cId nt lt = do
95 ls' <- selectNodesWithUsername NodeList userMaster
96 ls <- map (_node_id) <$> getListsWithParentId cId
97 ts <- mapTermListRoot ls nt
100 dico = filterListWithRoot lt ts
101 terms = catMaybes $ List.concat $ map (\(a,b) -> [Just a, b]) $ Map.toList dico
103 cs' <- getNodesByNgramsOnlyUser cId (ls' <> ls) nt terms
105 m <- getListNgrams ls nt
106 pure $ toTree lt cs' m
109 treeData' :: FlowCmdM env ServantErr m
110 => CorpusId -> NgramsType -> ListType
112 treeData' cId nt lt = do
113 ls' <- selectNodesWithUsername NodeList userMaster
114 ls <- map (_node_id) <$> getListsWithParentId cId
115 ts <- mapTermListRoot ls nt
118 dico = filterListWithRoot lt ts
119 terms = catMaybes $ List.concat $ map (\(a,b) -> [Just a, b]) $ Map.toList dico
121 cs' <- getNodesByNgramsOnlyUser cId (ls' <> ls) nt terms
123 m <- getListNgrams ls nt
124 pure $ toTree lt cs' m