2 Module : Gargantext.Database.Query.Table.NodeNode
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Here is a longer description of this module, containing some
11 commentary with @some markup@.
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 {-# LANGUAGE Arrows #-}
17 {-# LANGUAGE FunctionalDependencies #-}
18 {-# LANGUAGE QuasiQuotes #-}
19 {-# LANGUAGE TemplateHaskell #-}
21 module Gargantext.Database.Query.Table.NodeNode
22 ( module Gargantext.Database.Schema.NodeNode
37 import Control.Arrow (returnA)
38 import Control.Lens (view, (^.))
39 import Data.Maybe (catMaybes)
40 import Data.Text (Text, splitOn)
41 import Database.PostgreSQL.Simple.SqlQQ (sql)
42 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
43 import qualified Database.PostgreSQL.Simple as PGS (Query, Only(..))
44 import qualified Opaleye as O
47 import Gargantext.Core
48 import Gargantext.Core.Types
49 import Gargantext.Database.Schema.NodeNode
50 import Gargantext.Database.Admin.Types.Hyperdata
51 import Gargantext.Database.Prelude
52 import Gargantext.Database.Schema.Node
53 import Gargantext.Prelude
56 queryNodeNodeTable :: Query NodeNodeRead
57 queryNodeNodeTable = queryTable nodeNodeTable
59 -- | not optimized (get all ngrams without filters)
60 _nodesNodes :: Cmd err [NodeNode]
61 _nodesNodes = runOpaQuery queryNodeNodeTable
63 ------------------------------------------------------------------------
64 -- | Basic NodeNode tools
65 getNodeNode :: NodeId -> Cmd err [NodeNode]
66 getNodeNode n = runOpaQuery (selectNodeNode $ pgNodeId n)
68 selectNodeNode :: Column PGInt4 -> Query NodeNodeRead
69 selectNodeNode n' = proc () -> do
70 ns <- queryNodeNodeTable -< ()
71 restrict -< _nn_node1_id ns .== n'
74 ------------------------------------------------------------------------
75 -- TODO (refactor with Children)
77 getNodeNodeWith :: NodeId -> proxy a -> Maybe NodeType -> Cmd err [a]
78 getNodeNodeWith pId _ maybeNodeType = runOpaQuery query
80 query = selectChildren pId maybeNodeType
82 selectChildren :: ParentId
85 selectChildren parentId maybeNodeType = proc () -> do
86 row@(Node nId typeName _ parent_id _ _ _) <- queryNodeTable -< ()
87 (NodeNode _ n1id n2id _ _) <- queryNodeNodeTable -< ()
89 let nodeType = maybe 0 toDBid maybeNodeType
90 restrict -< typeName .== pgInt4 nodeType
92 restrict -< (.||) (parent_id .== (pgNodeId parentId))
93 ( (.&&) (n1id .== pgNodeId parentId)
98 ------------------------------------------------------------------------
99 insertNodeNode :: [NodeNode] -> Cmd err Int
100 insertNodeNode ns = mkCmd $ \conn -> fromIntegral <$> (runInsert_ conn
101 $ Insert nodeNodeTable ns' rCount (Just DoNothing))
103 ns' :: [NodeNodeWrite]
104 ns' = map (\(NodeNode n1 n2 x y)
105 -> NodeNode (pgNodeId n1)
113 ------------------------------------------------------------------------
114 type Node1_Id = NodeId
115 type Node2_Id = NodeId
117 deleteNodeNode :: Node1_Id -> Node2_Id -> Cmd err Int
118 deleteNodeNode n1 n2 = mkCmd $ \conn ->
119 fromIntegral <$> runDelete conn nodeNodeTable
120 (\(NodeNode n1_id n2_id _ _) -> n1_id .== pgNodeId n1
121 .&& n2_id .== pgNodeId n2 )
123 ------------------------------------------------------------------------
124 -- | Favorite management
125 _nodeNodeCategory :: CorpusId -> DocId -> Int -> Cmd err [Int]
126 _nodeNodeCategory cId dId c = map (\(PGS.Only a) -> a) <$> runPGSQuery favQuery (c,cId,dId)
128 favQuery :: PGS.Query
129 favQuery = [sql|UPDATE nodes_nodes SET category = ?
130 WHERE node1_id = ? AND node2_id = ?
134 nodeNodesCategory :: [(CorpusId, DocId, Int)] -> Cmd err [Int]
135 nodeNodesCategory inputData = map (\(PGS.Only a) -> a)
136 <$> runPGSQuery catQuery (PGS.Only $ Values fields inputData)
138 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","int4","int4"]
139 catQuery :: PGS.Query
140 catQuery = [sql| UPDATE nodes_nodes as nn0
141 SET category = nn1.category
142 FROM (?) as nn1(node1_id,node2_id,category)
143 WHERE nn0.node1_id = nn1.node1_id
144 AND nn0.node2_id = nn1.node2_id
145 RETURNING nn1.node2_id
148 ------------------------------------------------------------------------
149 -- | Score management
150 _nodeNodeScore :: CorpusId -> DocId -> Int -> Cmd err [Int]
151 _nodeNodeScore cId dId c = map (\(PGS.Only a) -> a) <$> runPGSQuery scoreQuery (c,cId,dId)
153 scoreQuery :: PGS.Query
154 scoreQuery = [sql|UPDATE nodes_nodes SET score = ?
155 WHERE node1_id = ? AND node2_id = ?
159 nodeNodesScore :: [(CorpusId, DocId, Int)] -> Cmd err [Int]
160 nodeNodesScore inputData = map (\(PGS.Only a) -> a)
161 <$> runPGSQuery catScore (PGS.Only $ Values fields inputData)
163 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","int4","int4"]
164 catScore :: PGS.Query
165 catScore = [sql| UPDATE nodes_nodes as nn0
166 SET score = nn1.score
167 FROM (?) as nn1(node1_id, node2_id, score)
168 WHERE nn0.node1_id = nn1.node1_id
169 AND nn0.node2_id = nn1.node2_id
170 RETURNING nn1.node2_id
173 ------------------------------------------------------------------------
174 selectCountDocs :: HasDBid NodeType => CorpusId -> Cmd err Int
175 selectCountDocs cId = runCountOpaQuery (queryCountDocs cId)
177 queryCountDocs cId' = proc () -> do
178 (n, nn) <- joinInCorpus -< ()
179 restrict -< nn^.nn_node1_id .== (toNullable $ pgNodeId cId')
180 restrict -< nn^.nn_category .>= (toNullable $ pgInt4 1)
181 restrict -< n^.node_typename .== (pgInt4 $ toDBid NodeDocument)
187 -- | TODO use UTCTime fast
188 selectDocsDates :: HasDBid NodeType => CorpusId -> Cmd err [Text]
189 selectDocsDates cId = map (head' "selectDocsDates" . splitOn "-")
191 <$> map (view hd_publication_date)
194 selectDocs :: HasDBid NodeType => CorpusId -> Cmd err [HyperdataDocument]
195 selectDocs cId = runOpaQuery (queryDocs cId)
197 queryDocs :: HasDBid NodeType => CorpusId -> O.Query (Column PGJsonb)
198 queryDocs cId = proc () -> do
199 (n, nn) <- joinInCorpus -< ()
200 restrict -< nn^.nn_node1_id .== (toNullable $ pgNodeId cId)
201 restrict -< nn^.nn_category .>= (toNullable $ pgInt4 1)
202 restrict -< n^.node_typename .== (pgInt4 $ toDBid NodeDocument)
203 returnA -< view (node_hyperdata) n
205 selectDocNodes :: HasDBid NodeType =>CorpusId -> Cmd err [Node HyperdataDocument]
206 selectDocNodes cId = runOpaQuery (queryDocNodes cId)
208 queryDocNodes :: HasDBid NodeType =>CorpusId -> O.Query NodeRead
209 queryDocNodes cId = proc () -> do
210 (n, nn) <- joinInCorpus -< ()
211 restrict -< nn^.nn_node1_id .== (toNullable $ pgNodeId cId)
212 restrict -< nn^.nn_category .>= (toNullable $ pgInt4 1)
213 restrict -< n^.node_typename .== (pgInt4 $ toDBid NodeDocument)
216 joinInCorpus :: O.Query (NodeRead, NodeNodeReadNull)
217 joinInCorpus = leftJoin queryNodeTable queryNodeNodeTable cond
219 cond :: (NodeRead, NodeNodeRead) -> Column PGBool
220 cond (n, nn) = nn^.nn_node2_id .== (view node_id n)
222 joinOn1 :: O.Query (NodeRead, NodeNodeReadNull)
223 joinOn1 = leftJoin queryNodeTable queryNodeNodeTable cond
225 cond :: (NodeRead, NodeNodeRead) -> Column PGBool
226 cond (n, nn) = nn^.nn_node1_id .== n^.node_id
229 ------------------------------------------------------------------------
230 selectPublicNodes :: HasDBid NodeType => (Hyperdata a, QueryRunnerColumnDefault PGJsonb a)
231 => Cmd err [(Node a, Maybe Int)]
232 selectPublicNodes = runOpaQuery (queryWithType NodeFolderPublic)
234 queryWithType :: HasDBid NodeType =>NodeType -> O.Query (NodeRead, Column (Nullable PGInt4))
235 queryWithType nt = proc () -> do
236 (n, nn) <- joinOn1 -< ()
237 restrict -< n^.node_typename .== (pgInt4 $ toDBid nt)
238 returnA -< (n, nn^.nn_node2_id)