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
21 import Data.List (unzip, sortOn)
22 import Data.Map (toList)
23 import Data.Text (Text)
24 import GHC.Generics (Generic)
25 import Gargantext.Core.Types.Main
26 import Gargantext.Database.Admin.Config
27 import Gargantext.Database.Admin.Types.Node (CorpusId)
28 import Gargantext.Database.Prelude
29 import Gargantext.Database.Query.Table.Node
30 import Gargantext.Database.Query.Table.Node.Select
31 import Gargantext.Database.Query.Table.NodeNode (selectDocsDates)
32 import Gargantext.Database.Schema.Node
33 import Gargantext.Prelude
34 import Gargantext.Text.Metrics.Count (occurrencesWith)
37 import Data.Maybe (catMaybes)
38 import Gargantext.API.Ngrams.NTree
39 import Gargantext.API.Ngrams.Tools
40 import Gargantext.Core.Types
41 import Gargantext.Database.Action.Flow
42 import Gargantext.Database.Action.Metrics.NgramsByNode
43 import Gargantext.Database.Schema.Ngrams
45 import qualified Data.List as List
46 import qualified Data.Map as Map
49 data Chart = ChartHisto | ChartScatter | ChartPie
53 data Histo = Histo { histo_dates :: [Text]
54 , histo_count :: [Int]
58 histoData :: CorpusId -> Cmd err Histo
60 dates <- selectDocsDates cId
64 $ occurrencesWith identity dates
68 pieData :: FlowCmdM env err m
69 => CorpusId -> NgramsType -> ListType
71 pieData cId nt lt = do
72 ls' <- selectNodesWithUsername NodeList userMaster
73 ls <- map (_node_id) <$> getListsWithParentId cId
74 ts <- mapTermListRoot ls nt <$> getRepo
76 dico = filterListWithRoot lt ts
77 terms = catMaybes $ List.concat $ map (\(a,b) -> [Just a, b]) $ Map.toList dico
78 group dico' x = case Map.lookup x dico' of
80 Just x' -> maybe x identity x'
82 (_total,mapTerms) <- countNodesByNgramsWith (group dico)
83 <$> getNodesByNgramsOnlyUser cId (ls' <> ls) nt terms
84 let (dates, count) = unzip $ map (\(t,(d,_)) -> (t, d)) $ Map.toList mapTerms
85 pure (Histo dates (map round count))
90 treeData :: FlowCmdM env err m
91 => CorpusId -> NgramsType -> ListType
93 treeData cId nt lt = do
94 ls' <- selectNodesWithUsername NodeList userMaster
95 ls <- map (_node_id) <$> getListsWithParentId cId
96 ts <- mapTermListRoot ls nt <$> getRepo
99 dico = filterListWithRoot lt ts
100 terms = catMaybes $ List.concat $ map (\(a,b) -> [Just a, b]) $ Map.toList dico
102 cs' <- getNodesByNgramsOnlyUser cId (ls' <> ls) nt terms
104 m <- getListNgrams ls nt
105 pure $ toTree lt cs' m
108 treeData' :: FlowCmdM env ServerError m
109 => CorpusId -> NgramsType -> ListType
111 treeData' cId nt lt = do
112 ls' <- selectNodesWithUsername NodeList userMaster
113 ls <- map (_node_id) <$> getListsWithParentId cId
114 ts <- mapTermListRoot ls nt <$> getRepo
117 dico = filterListWithRoot lt ts
118 terms = catMaybes $ List.concat $ map (\(a,b) -> [Just a, b]) $ Map.toList dico
120 cs' <- getNodesByNgramsOnlyUser cId (ls' <> ls) nt terms
122 m <- getListNgrams ls nt
123 pure $ toTree lt cs' m