2 Module : Gargantext.Database.Schema.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 FlexibleInstances #-}
18 {-# LANGUAGE FunctionalDependencies #-}
19 {-# LANGUAGE QuasiQuotes #-}
20 {-# LANGUAGE MultiParamTypeClasses #-}
21 {-# LANGUAGE NoImplicitPrelude #-}
22 {-# LANGUAGE OverloadedStrings #-}
23 {-# LANGUAGE RankNTypes #-}
24 {-# LANGUAGE TemplateHaskell #-}
26 module Gargantext.Database.Schema.NodeNode where
28 import Control.Lens (view)
29 import qualified Database.PostgreSQL.Simple as PGS (Query, Only(..))
30 import Database.PostgreSQL.Simple.Types (Values(..), QualifiedIdentifier(..))
31 import Database.PostgreSQL.Simple.SqlQQ (sql)
32 import Control.Lens.TH (makeLensesWith, abbreviatedFields)
33 import Data.Maybe (Maybe, catMaybes)
34 import Data.Text (Text, splitOn)
35 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
36 import Gargantext.Database.Schema.Node
37 import Gargantext.Core.Types
38 import Gargantext.Database.Utils
39 import Gargantext.Database.Config (nodeTypeId)
40 import Gargantext.Database.Types.Node (CorpusId, DocId)
41 import Gargantext.Prelude
43 import Control.Arrow (returnA)
44 import qualified Opaleye as O
46 data NodeNodePoly node1_id node2_id score fav del
47 = NodeNode { nn_node1_id :: node1_id
48 , nn_node2_id :: node2_id
54 type NodeNodeWrite = NodeNodePoly (Column (PGInt4))
56 (Maybe (Column (PGFloat8)))
57 (Maybe (Column (PGBool)))
58 (Maybe (Column (PGBool)))
60 type NodeNodeRead = NodeNodePoly (Column (PGInt4))
66 type NodeNodeReadNull = NodeNodePoly (Column (Nullable PGInt4))
67 (Column (Nullable PGInt4))
68 (Column (Nullable PGFloat8))
69 (Column (Nullable PGBool))
70 (Column (Nullable PGBool))
72 type NodeNode = NodeNodePoly Int Int (Maybe Double) (Maybe Bool) (Maybe Bool)
74 $(makeAdaptorAndInstance "pNodeNode" ''NodeNodePoly)
75 $(makeLensesWith abbreviatedFields ''NodeNodePoly)
77 nodeNodeTable :: Table NodeNodeWrite NodeNodeRead
78 nodeNodeTable = Table "nodes_nodes" (pNodeNode
79 NodeNode { nn_node1_id = required "node1_id"
80 , nn_node2_id = required "node2_id"
81 , nn_score = optional "score"
82 , nn_favorite = optional "favorite"
83 , nn_delete = optional "delete"
87 queryNodeNodeTable :: Query NodeNodeRead
88 queryNodeNodeTable = queryTable nodeNodeTable
91 -- | not optimized (get all ngrams without filters)
92 nodesNodes :: Cmd err [NodeNode]
93 nodesNodes = runOpaQuery queryNodeNodeTable
95 instance QueryRunnerColumnDefault (Nullable PGInt4) Int where
96 queryRunnerColumnDefault = fieldQueryRunnerColumn
98 instance QueryRunnerColumnDefault PGFloat8 (Maybe Double) where
99 queryRunnerColumnDefault = fieldQueryRunnerColumn
101 instance QueryRunnerColumnDefault PGBool (Maybe Bool) where
102 queryRunnerColumnDefault = fieldQueryRunnerColumn
105 ------------------------------------------------------------------------
106 -- | Favorite management
107 nodeToFavorite :: CorpusId -> DocId -> Bool -> Cmd err [Int]
108 nodeToFavorite cId dId b = map (\(PGS.Only a) -> a) <$> runPGSQuery favQuery (b,cId,dId)
110 favQuery :: PGS.Query
111 favQuery = [sql|UPDATE nodes_nodes SET favorite = ?
112 WHERE node1_id = ? AND node2_id = ?
116 nodesToFavorite :: [(CorpusId,DocId,Bool)] -> Cmd err [Int]
117 nodesToFavorite inputData = map (\(PGS.Only a) -> a)
118 <$> runPGSQuery trashQuery (PGS.Only $ Values fields inputData)
120 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","int4","bool"]
121 trashQuery :: PGS.Query
122 trashQuery = [sql| UPDATE nodes_nodes as old SET
123 favorite = new.favorite
124 from (?) as new(node1_id,node2_id,favorite)
125 WHERE old.node1_id = new.node1_id
126 AND old.node2_id = new.node2_id
127 RETURNING new.node2_id
130 ------------------------------------------------------------------------
131 -- | TODO use UTCTime fast
132 selectDocsDates :: CorpusId -> Cmd err [Text]
133 selectDocsDates cId =
134 map (head' "selectDocsDates" . splitOn "-")
136 <$> map (view hyperdataDocument_publication_date)
140 selectDocs :: CorpusId -> Cmd err [HyperdataDocument]
141 selectDocs cId = runOpaQuery (queryDocs cId)
143 queryDocs :: CorpusId -> O.Query (Column PGJsonb)
144 queryDocs cId = proc () -> do
145 (n, nn) <- joinInCorpus -< ()
146 restrict -< ( nn_node1_id nn) .== (toNullable $ pgNodeId cId)
147 restrict -< ( nn_delete nn) .== (toNullable $ pgBool False)
148 restrict -< (_node_typename n) .== (pgInt4 $ nodeTypeId NodeDocument)
149 returnA -< view (node_hyperdata) n
152 joinInCorpus :: O.Query (NodeRead, NodeNodeReadNull)
153 joinInCorpus = leftJoin queryNodeTable queryNodeNodeTable cond
155 cond :: (NodeRead, NodeNodeRead) -> Column PGBool
156 cond (n, nn) = nn_node2_id nn .== (view node_id n)
159 ------------------------------------------------------------------------
160 -- | Trash management
161 nodeToTrash :: CorpusId -> DocId -> Bool -> Cmd err [PGS.Only Int]
162 nodeToTrash cId dId b = runPGSQuery trashQuery (b,cId,dId)
164 trashQuery :: PGS.Query
165 trashQuery = [sql|UPDATE nodes_nodes SET delete = ?
166 WHERE node1_id = ? AND node2_id = ?
171 nodesToTrash :: [(CorpusId,DocId,Bool)] -> Cmd err [Int]
172 nodesToTrash input = map (\(PGS.Only a) -> a)
173 <$> runPGSQuery trashQuery (PGS.Only $ Values fields input)
175 fields = map (\t-> QualifiedIdentifier Nothing t) ["int4","int4","bool"]
176 trashQuery :: PGS.Query
177 trashQuery = [sql| UPDATE nodes_nodes as old SET
179 from (?) as new(node1_id,node2_id,delete)
180 WHERE old.node1_id = new.node1_id
181 AND old.node2_id = new.node2_id
182 RETURNING new.node2_id
185 -- | /!\ Really remove nodes in the Corpus or Annuaire
186 emptyTrash :: CorpusId -> Cmd err [PGS.Only Int]
187 emptyTrash cId = runPGSQuery delQuery (PGS.Only cId)
189 delQuery :: PGS.Query
190 delQuery = [sql|DELETE from nodes_nodes n
195 ------------------------------------------------------------------------