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.NodeContext
22 ( module Gargantext.Database.Schema.NodeContext
23 , queryNodeContextTable
27 , nodeContextsCategory
32 , selectPublicContexts
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(..))
44 import qualified Database.PostgreSQL.Simple as PGS (Query, Only(..))
45 import qualified Opaleye as O
47 import Gargantext.Core
48 import Gargantext.Core.Types
49 import Gargantext.Database.Admin.Types.Hyperdata
50 import Gargantext.Database.Prelude
51 import Gargantext.Database.Schema.Context
52 import Gargantext.Database.Schema.Node
53 import Gargantext.Database.Schema.NodeContext
54 import Gargantext.Prelude
56 queryNodeContextTable :: Select NodeContextRead
57 queryNodeContextTable = selectTable nodeContextTable
59 -- | not optimized (get all ngrams without filters)
60 _nodesContexts :: Cmd err [NodeContext]
61 _nodesContexts = runOpaQuery queryNodeContextTable
63 ------------------------------------------------------------------------
64 -- | Basic NodeContext tools
65 getNodeContext :: NodeId -> Cmd err [NodeContext]
66 getNodeContext n = runOpaQuery (selectNodeContext $ pgNodeId n)
68 selectNodeContext :: Column SqlInt4 -> Select NodeContextRead
69 selectNodeContext n' = proc () -> do
70 ns <- queryNodeContextTable -< ()
71 restrict -< _nc_node_id ns .== n'
74 ------------------------------------------------------------------------
75 insertNodeContext :: [NodeContext] -> Cmd err Int
76 insertNodeContext ns = mkCmd $ \conn -> fromIntegral <$> (runInsert_ conn
77 $ Insert nodeContextTable ns' rCount (Just DoNothing))
79 ns' :: [NodeContextWrite]
80 ns' = map (\(NodeContext i n c x y)
81 -> NodeContext (sqlInt4 <$> i)
89 ------------------------------------------------------------------------
91 type Context_Id = NodeId
93 deleteNodeContext :: Node_Id -> Context_Id -> Cmd err Int
94 deleteNodeContext n c = mkCmd $ \conn ->
95 fromIntegral <$> runDelete_ conn
96 (Delete nodeContextTable
97 (\(NodeContext _ n_id c_id _ _) -> n_id .== pgNodeId n
98 .&& c_id .== pgNodeId c
103 ------------------------------------------------------------------------
104 -- | Favorite management
105 nodeContextsCategory :: [(CorpusId, DocId, Int)] -> Cmd err [Int]
106 nodeContextsCategory inputData = map (\(PGS.Only a) -> a)
107 <$> runPGSQuery catSelect (PGS.Only $ Values fields inputData)
109 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","int4","int4"]
110 catSelect :: PGS.Query
111 catSelect = [sql| UPDATE nodes_contexts as nn0
112 SET category = nn1.category
113 FROM (?) as nn1(node_id,context_id,category)
114 WHERE nn0.node_id = nn1.node_id
115 AND nn0.context_id = nn1.context_id
116 RETURNING nn1.node_id
119 ------------------------------------------------------------------------
120 -- | Score management
121 nodeContextsScore :: [(CorpusId, DocId, Int)] -> Cmd err [Int]
122 nodeContextsScore inputData = map (\(PGS.Only a) -> a)
123 <$> runPGSQuery catScore (PGS.Only $ Values fields inputData)
125 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","int4","int4"]
126 catScore :: PGS.Query
127 catScore = [sql| UPDATE nodes_contexts as nn0
128 SET score = nn1.score
129 FROM (?) as nn1(node_id, context_id, score)
130 WHERE nn0.node_id = nn1.node_id
131 AND nn0.context_id = nn1.context_id
132 RETURNING nn1.context_id
136 ------------------------------------------------------------------------
137 selectCountDocs :: HasDBid NodeType => CorpusId -> Cmd err Int
138 selectCountDocs cId = runCountOpaQuery (queryCountDocs cId)
140 queryCountDocs cId' = proc () -> do
141 (c, nc) <- joinInCorpus -< ()
142 restrict -< nc^.nc_node_id .== (toNullable $ pgNodeId cId')
143 restrict -< nc^.nc_category .>= (toNullable $ sqlInt4 1)
144 restrict -< c^.context_typename .== (sqlInt4 $ toDBid NodeDocument)
148 -- | TODO use UTCTime fast
149 selectDocsDates :: HasDBid NodeType => CorpusId -> Cmd err [Text]
150 selectDocsDates cId = map (head' "selectDocsDates" . splitOn "-")
152 <$> map (view hd_publication_date)
155 selectDocs :: HasDBid NodeType => CorpusId -> Cmd err [HyperdataDocument]
156 selectDocs cId = runOpaQuery (queryDocs cId)
158 queryDocs :: HasDBid NodeType => CorpusId -> O.Select (Column SqlJsonb)
159 queryDocs cId = proc () -> do
160 (c, nn) <- joinInCorpus -< ()
161 restrict -< nn^.nc_node_id .== (toNullable $ pgNodeId cId)
162 restrict -< nn^.nc_category .>= (toNullable $ sqlInt4 1)
163 restrict -< c^.context_typename .== (sqlInt4 $ toDBid NodeDocument)
164 returnA -< view (context_hyperdata) c
166 selectDocNodes :: HasDBid NodeType => CorpusId -> Cmd err [Context HyperdataDocument]
167 selectDocNodes cId = runOpaQuery (queryDocNodes cId)
169 queryDocNodes :: HasDBid NodeType => CorpusId -> O.Select ContextRead
170 queryDocNodes cId = proc () -> do
171 (c, nc) <- joinInCorpus -< ()
172 restrict -< nc^.nc_node_id .== (toNullable $ pgNodeId cId)
173 restrict -< nc^.nc_category .>= (toNullable $ sqlInt4 1)
174 restrict -< c^.context_typename .== (sqlInt4 $ toDBid NodeDocument)
177 joinInCorpus :: O.Select (ContextRead, NodeContextReadNull)
178 joinInCorpus = leftJoin queryContextTable queryNodeContextTable cond
180 cond :: (ContextRead, NodeContextRead) -> Column SqlBool
181 cond (c, nc) = c^.context_id .== nc^.nc_context_id
184 joinOn1 :: O.Select (NodeRead, NodeContextReadNull)
185 joinOn1 = leftJoin queryNodeTable queryNodeContextTable cond
187 cond :: (NodeRead, NodeContextRead) -> Column SqlBool
188 cond (n, nc) = nc^.nc_node_id .== n^.node_id
191 ------------------------------------------------------------------------
192 selectPublicContexts :: HasDBid NodeType => (Hyperdata a, DefaultFromField SqlJsonb a)
193 => Cmd err [(Node a, Maybe Int)]
194 selectPublicContexts = runOpaQuery (queryWithType NodeFolderPublic)
196 queryWithType :: HasDBid NodeType =>NodeType -> O.Select (NodeRead, Column (Nullable SqlInt4))
197 queryWithType nt = proc () -> do
198 (n, nc) <- joinOn1 -< ()
199 restrict -< n^.node_typename .== (sqlInt4 $ toDBid nt)
200 returnA -< (n, nc^.nc_context_id)