2 Module : Gargantext.Database.Facet
3 Description : Main requests of Node to the database
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
12 {-# LANGUAGE TemplateHaskell #-}
13 {-# LANGUAGE FlexibleInstances #-}
14 {-# LANGUAGE FlexibleContexts #-}
15 {-# LANGUAGE DeriveGeneric #-}
16 {-# LANGUAGE MultiParamTypeClasses #-}
17 {-# LANGUAGE FunctionalDependencies #-}
18 {-# LANGUAGE Arrows #-}
19 {-# OPTIONS_GHC -fno-warn-orphans #-}
21 module Gargantext.Database.Facet where
23 import Prelude hiding (null, id, map, sum, not)
25 import Gargantext.Types
26 import Gargantext.Types.Main (NodeType)
27 import Gargantext.Database.NodeNode
28 import Gargantext.Database.NodeNodeNgram
29 import Gargantext.Database.Node
30 import Gargantext.Database.Queries
31 import Gargantext.Utils.Prefix (unPrefix)
32 -- import Gargantext.Database.NodeNgram
34 -- import Data.Aeson (Value)
35 import Data.Aeson.TH (deriveJSON)
36 import Control.Arrow (returnA)
37 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
38 import Data.Maybe (Maybe)
39 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
40 import Data.Time (UTCTime)
41 import Database.PostgreSQL.Simple (Connection)
43 import Opaleye.Internal.Join (NullMaker)
45 import qualified Opaleye.Internal.Unpackspec()
46 import Data.Profunctor.Product.Default (Default)
50 type FacetDoc = Facet NodeId UTCTime HyperdataDocument Bool -- Double
52 data Facet id created hyperdata favorite = FacetDoc { facetDoc_id :: id
53 , facetDoc_created :: created
54 , facetDoc_hyperdata :: hyperdata
55 , facetDoc_favorite :: favorite
56 -- To be added: Double
57 -- , facetDoc_ngramCount :: ngramCount
59 $(deriveJSON (unPrefix "facetDoc_") ''Facet)
63 -- Facets / Views for the Front End
64 type FacetDocRead = Facet (Column PGInt4) (Column PGTimestamptz) (Column PGJsonb) (Column PGBool) -- (Column PGFloat8)
66 $(makeAdaptorAndInstance "pFacetDoc" ''Facet)
67 $(makeLensesWith abbreviatedFields ''Facet)
69 getDocFacet :: Connection -> Int -> Maybe NodeType -> Maybe Offset -> Maybe Limit -> IO [FacetDoc]
70 getDocFacet conn parentId nodeType maybeOffset maybeLimit =
71 runQuery conn $ selectDocFacet parentId nodeType maybeOffset maybeLimit
73 selectDocFacet :: ParentId -> Maybe NodeType -> Maybe Offset -> Maybe Limit -> Query FacetDocRead
74 selectDocFacet parentId maybeNodeType maybeOffset maybeLimit =
75 limit' maybeLimit $ offset' maybeOffset $ orderBy (asc facetDoc_created) $ selectDocFacet' parentId maybeNodeType
78 -- | Left join to the favorites
79 nodeNodeLeftJoin :: Query (NodeRead, NodeNodeReadNull)
80 nodeNodeLeftJoin = leftJoin queryNodeTable queryNodeNodeTable (eqNode)
82 eqNode (Node n1 _ _ _ _ _ _, NodeNode _ n2 _) = ((.==) n1 n2)
85 nodeNodeLeftJoin' :: (Column (Nullable PGInt4))
86 -> Query (NodeRead, NodeNodeReadNull)
87 nodeNodeLeftJoin' nId = leftJoin queryNodeTable queryNodeNodeTable (eqNode nId)
89 eqNode n (Node n1 _ _ _ _ _ _, NodeNode n1' n2 _)
90 = foldl (.&&) (pgBool True) [ ((.==) n1 n2)
94 nodeNodeLeftJoin'' :: Query (NodeRead, NodeRead, NodeNodeRead)
95 nodeNodeLeftJoin'' = join3 queryNodeTable queryNodeTable queryNodeNodeTable eqNode
97 eqNode (Node n1 _ _ _ _ _ _, Node n2 _ _ _ _ _ _, NodeNode n1' n2' _)
98 = foldl (.&&) (pgBool True) [ ((.==) n2 n2')
99 , ((.==) (toNullable n1) n1')
102 -- | Left join to the ngram count per document
103 nodeNodeNgramLeftJoin :: Query (NodeRead, NodeNodeNgramReadNull)
104 nodeNodeNgramLeftJoin = leftJoin queryNodeTable queryNodeNodeNgramTable (eqNode)
106 eqNode (Node n1 _ _ _ _ _ _, NodeNodeNgram n1' _ _ _) = ((.==) n1 n1')
109 nodeNodeNgramLeftJoin' :: Column (Nullable PGInt4)
110 -> Query (NodeRead, NodeNodeNgramReadNull)
111 nodeNodeNgramLeftJoin' nId = leftJoin queryNodeTable queryNodeNodeNgramTable (eqNode nId)
113 eqNode nId' (Node n1 _ _ _ _ _ _, NodeNodeNgram n1' n2 _ _)
114 = (.&&) ((.==) n1 n1')
115 ((.==) nId' (toNullable n2))
118 leftJoin3 :: (Default NullMaker (columnsL1, nullableColumnsR) nullableColumnsR1,
119 Default NullMaker columnsR nullableColumnsR,
120 Default Unpackspec columnsR columnsR,
121 Default Unpackspec nullableColumnsR nullableColumnsR,
122 Default Unpackspec columnsL1 columnsL1,
123 Default Unpackspec columnsL columnsL) =>
127 -> ((columnsL1, columnsR) -> Column PGBool)
128 -> ((columnsL, (columnsL1, nullableColumnsR)) -> Column PGBool)
129 -> Query (columnsL, nullableColumnsR1)
130 leftJoin3 q1 q2 q3 cond12 cond23 = leftJoin q3 (leftJoin q1 q2 cond12) cond23
132 -- | Building the facet
133 selectDocFacet' :: ParentId -> Maybe NodeType -> Query FacetDocRead
134 selectDocFacet' parentId _ = proc () -> do
135 node <- (proc () -> do
138 (Node _ favTypeId _ favParentId _ _ _) <- queryNodeTable -< ()
139 restrict -< favTypeId .== 15 .&& favParentId .== (toNullable $ pgInt4 parentId)
141 -- select nn.score from nodes n left join nodes_nodes nn on n.id = nn.node2_id where n.typename =4;
142 -- Selecting the documents and joining Favorite Node
143 (Node docId docTypeId _ docParentId _ created docHyperdata, NodeNode _ docTypeId' _) <- nodeNodeLeftJoin' (toNullable $ pgInt4 347537) -< ()
144 restrict -< docParentId .== (toNullable $ pgInt4 parentId)
145 let docTypeId'' = maybe 0 nodeTypeId (Just Document)
146 restrict -< if docTypeId'' > 0
147 then docTypeId .== (pgInt4 (docTypeId'' :: Int))
150 -- Getting favorite data
151 let isFav = ifThenElse (isNull docTypeId') (pgBool False) (pgBool True)
153 -- Ngram count by document
154 -- Counting the ngram
155 -- (Node occId occTypeId _ _ _ _ _, NodeNode _ _ _ count) <- nodeNodeNgramLeftJoin -< ()
156 -- restrict -< occId .== 347540
158 --returnA -< (FacetDoc n_id hyperdata isFav ngramCount)) -< ()
159 returnA -< (FacetDoc docId created docHyperdata isFav)) -< ()