2 Module : Gargantext.Database.Query.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-orphans #-}
12 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
14 {-# LANGUAGE Arrows #-}
15 {-# LANGUAGE FunctionalDependencies #-}
16 {-# LANGUAGE QuasiQuotes #-}
17 {-# LANGUAGE NoMonomorphismRestriction #-}
18 {-# LANGUAGE TemplateHaskell #-}
19 {-# LANGUAGE TypeFamilies #-}
20 ------------------------------------------------------------------------
21 module Gargantext.Database.Query.Facet
33 , FacetPairedReadNullAgg
38 import Control.Arrow (returnA)
39 import Control.Lens ((^.))
40 import Data.Aeson (FromJSON, ToJSON)
41 import Data.Aeson.TH (deriveJSON)
42 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
44 import Data.Text (Text)
45 import Data.Time (UTCTime)
46 import Data.Time.Segment (jour)
47 import Data.Typeable (Typeable)
48 import GHC.Generics (Generic)
50 import Prelude hiding (null, id, map, sum, not, read)
52 import Test.QuickCheck (elements)
53 import Test.QuickCheck.Arbitrary
54 import qualified Opaleye.Internal.Unpackspec()
56 import Gargantext.Core.Types
57 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger, wellNamedSchema)
58 import Gargantext.Database.Admin.Config (nodeTypeId)
59 import Gargantext.Database.Admin.Types.Hyperdata
60 import Gargantext.Database.Query.Filter
61 import Gargantext.Database.Query.Join (leftJoin5)
62 import Gargantext.Database.Query.Table.Ngrams
63 import Gargantext.Database.Query.Table.NodeNode
64 import Gargantext.Database.Query.Table.NodeNodeNgrams
65 import Gargantext.Database.Prelude
66 import Gargantext.Database.Schema.Node
68 ------------------------------------------------------------------------
71 -- data Facet = FacetDoc | FacetSources | FacetAuthors | FacetTerms
72 -- deriving (Show, Generic)
73 --instance FromJSON Facet
74 --instance ToJSON Facet
80 type FacetDoc = Facet NodeId UTCTime Title HyperdataDocument (Maybe Category) (Maybe Double)
81 -- type FacetSources = FacetDoc
82 -- type FacetAuthors = FacetDoc
83 -- type FacetTerms = FacetDoc
86 data Facet id created title hyperdata category ngramCount =
87 FacetDoc { facetDoc_id :: id
88 , facetDoc_created :: created
89 , facetDoc_title :: title
90 , facetDoc_hyperdata :: hyperdata
91 , facetDoc_category :: category
92 , facetDoc_score :: ngramCount
93 } deriving (Show, Generic)
95 data Facet id date hyperdata score =
96 FacetDoc { facetDoc_id :: id
97 , facetDoc_date :: date
98 , facetDoc_hyperdata :: hyperdata
99 , facetDoc_score :: score
100 } deriving (Show, Generic)
103 data Pair i l = Pair {_p_id :: i
105 } deriving (Show, Generic)
106 $(deriveJSON (unPrefix "_p_") ''Pair)
107 $(makeAdaptorAndInstance "pPair" ''Pair)
109 instance (Typeable i, Typeable l, ToSchema i, ToSchema l) => ToSchema (Pair i l) where
110 declareNamedSchema = wellNamedSchema "_p_"
111 instance (Arbitrary i, Arbitrary l) => Arbitrary (Pair i l) where
112 arbitrary = Pair <$> arbitrary <*> arbitrary
114 data FacetPaired id date hyperdata score =
115 FacetPaired {_fp_id :: id
117 ,_fp_hyperdata :: hyperdata
119 } deriving (Show, Generic)
120 $(deriveJSON (unPrefix "_fp_") ''FacetPaired)
121 $(makeAdaptorAndInstance "pFacetPaired" ''FacetPaired)
125 instance ( ToSchema id
133 ) => ToSchema (FacetPaired id date hyperdata score) where
134 declareNamedSchema = wellNamedSchema "_fp_"
136 instance ( Arbitrary id
138 , Arbitrary hyperdata
140 ) => Arbitrary (FacetPaired id date hyperdata score) where
141 arbitrary = FacetPaired <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
143 type FacetPairedRead = FacetPaired (Column PGInt4 )
144 (Column PGTimestamptz)
148 type FacetPairedReadNull = FacetPaired (Column (Nullable PGInt4) )
149 (Column (Nullable PGTimestamptz))
150 (Column (Nullable PGJsonb) )
151 (Column (Nullable PGInt4) )
153 type FacetPairedReadNullAgg = FacetPaired (Aggregator (Column (Nullable PGInt4) )
154 (Column (Nullable PGInt4) )
156 (Aggregator (Column (Nullable PGTimestamptz))
157 (Column (Nullable PGTimestamptz))
160 (Aggregator (Column (Nullable PGJsonb) )
161 (Column (Nullable PGJsonb) )
163 (Aggregator (Column (Nullable PGInt4) )
164 (Column (Nullable PGInt4) )
171 $(deriveJSON (unPrefix "facetDoc_") ''Facet)
173 -- | Documentation instance
174 instance ToSchema FacetDoc where
175 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "facetDoc_")
177 -- | Mock and Quickcheck instances
178 instance Arbitrary FacetDoc where
179 arbitrary = elements [ FacetDoc id' (jour year 01 01) t hp (Just cat) (Just ngramCount)
181 , year <- [1990..2000]
182 , t <- ["title", "another title"]
183 , hp <- arbitraryHyperdataDocuments
185 , ngramCount <- [3..100]
188 -- Facets / Views for the Front End
189 -- | Database instances
190 $(makeAdaptorAndInstance "pFacetDoc" ''Facet)
191 -- $(makeLensesWith abbreviatedFields ''Facet)
193 type FacetDocRead = Facet (Column PGInt4 )
194 (Column PGTimestamptz)
197 (Column (Nullable PGInt4)) -- Category
198 (Column (Nullable PGFloat8)) -- Score
200 -----------------------------------------------------------------------
201 -----------------------------------------------------------------------
202 data OrderBy = DateAsc | DateDesc
203 | TitleAsc | TitleDesc
204 | ScoreDesc | ScoreAsc
205 | SourceAsc | SourceDesc
206 deriving (Generic, Enum, Bounded, Read, Show)
208 instance FromHttpApiData OrderBy
210 parseUrlPiece "DateAsc" = pure DateAsc
211 parseUrlPiece "DateDesc" = pure DateDesc
212 parseUrlPiece "TitleAsc" = pure TitleAsc
213 parseUrlPiece "TitleDesc" = pure TitleDesc
214 parseUrlPiece "ScoreAsc" = pure ScoreAsc
215 parseUrlPiece "ScoreDesc" = pure ScoreDesc
216 parseUrlPiece "SourceAsc" = pure SourceAsc
217 parseUrlPiece "SourceDesc" = pure SourceDesc
218 parseUrlPiece _ = Left "Unexpected value of OrderBy"
220 instance ToParamSchema OrderBy
221 instance FromJSON OrderBy
222 instance ToJSON OrderBy
223 instance ToSchema OrderBy
224 instance Arbitrary OrderBy
226 arbitrary = elements [minBound..maxBound]
229 -- TODO-SECURITY check
232 runViewAuthorsDoc :: ContactId -> IsTrash -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> Cmd err [FacetDoc]
233 runViewAuthorsDoc cId t o l order = runOpaQuery $ filterWith o l order $ viewAuthorsDoc cId t ntId
238 viewAuthorsDoc :: ContactId -> IsTrash -> NodeType -> Query FacetDocRead
239 viewAuthorsDoc cId _ nt = proc () -> do
240 (doc,(_,(_,(_,contact)))) <- queryAuthorsDoc -< ()
242 {-nn <- queryNodeNodeTable -< ()
243 restrict -< nn_node1_id nn .== _node_id doc
244 -- restrict -< nn_delete nn .== (pgBool t)
247 restrict -< _node_id contact .== (toNullable $ pgNodeId cId)
248 restrict -< _node_typename doc .== (pgInt4 $ nodeTypeId nt)
250 returnA -< FacetDoc (_node_id doc)
253 (_node_hyperdata doc)
254 (toNullable $ pgInt4 1)
255 (toNullable $ pgDouble 1)
257 queryAuthorsDoc :: Query (NodeRead, (NodeNodeNgramsReadNull, (NgramsReadNull, (NodeNodeNgramsReadNull, NodeReadNull))))
258 queryAuthorsDoc = leftJoin5 queryNodeTable queryNodeNodeNgramsTable queryNgramsTable queryNodeNodeNgramsTable queryNodeTable cond12 cond23 cond34 cond45
260 cond12 :: (NodeNodeNgramsRead, NodeRead) -> Column PGBool
261 cond12 (nodeNgram, doc) = _node_id doc
262 .== _nnng_node1_id nodeNgram
264 cond23 :: (NgramsRead, (NodeNodeNgramsRead, NodeReadNull)) -> Column PGBool
265 cond23 (ngrams, (nodeNgram, _)) = ngrams^.ngrams_id
266 .== _nnng_ngrams_id nodeNgram
268 cond34 :: (NodeNodeNgramsRead, (NgramsRead, (NodeNodeNgramsReadNull, NodeReadNull))) -> Column PGBool
269 cond34 (nodeNgram2, (ngrams, (_,_)))= ngrams^.ngrams_id .== _nnng_ngrams_id nodeNgram2
271 cond45 :: (NodeRead, (NodeNodeNgramsRead, (NgramsReadNull, (NodeNodeNgramsReadNull, NodeReadNull)))) -> Column PGBool
272 cond45 (contact, (nodeNgram2, (_, (_,_)))) = _node_id contact .== _nnng_node1_id nodeNgram2
275 ------------------------------------------------------------------------
277 -- TODO-SECURITY check
278 runViewDocuments :: CorpusId -> IsTrash -> Maybe Offset -> Maybe Limit -> Maybe OrderBy -> Cmd err [FacetDoc]
279 runViewDocuments cId t o l order =
280 runOpaQuery $ filterWith o l order $ viewDocuments cId t ntId
282 ntId = nodeTypeId NodeDocument
284 viewDocuments :: CorpusId -> IsTrash -> NodeTypeId -> Query FacetDocRead
285 viewDocuments cId t ntId = proc () -> do
286 n <- queryNodeTable -< ()
287 nn <- queryNodeNodeTable -< ()
288 restrict -< n^.node_id .== nn^.nn_node2_id
289 restrict -< nn^.nn_node1_id .== (pgNodeId cId)
290 restrict -< n^.node_typename .== (pgInt4 ntId)
291 restrict -< if t then nn^.nn_category .== (pgInt4 0)
292 else nn^.nn_category .>= (pgInt4 1)
293 returnA -< FacetDoc (_node_id n)
297 (toNullable $ nn^.nn_category)
298 (toNullable $ nn^.nn_score)
300 ------------------------------------------------------------------------
301 filterWith :: (PGOrd date, PGOrd title, PGOrd score, hyperdata ~ Column SqlJsonb) =>
302 Maybe Gargantext.Core.Types.Offset
303 -> Maybe Gargantext.Core.Types.Limit
305 -> Select (Facet id (Column date) (Column title) hyperdata (Column score) ngramCount)
306 -> Select (Facet id (Column date) (Column title) hyperdata (Column score) ngramCount)
307 filterWith o l order q = limit' l $ offset' o $ orderBy (orderWith order) q
310 orderWith :: (PGOrd b1, PGOrd b2, PGOrd b3)
312 -> Order (Facet id (Column b1) (Column b2) (Column SqlJsonb) (Column b3) score)
313 orderWith (Just DateAsc) = asc facetDoc_created
314 orderWith (Just DateDesc) = desc facetDoc_created
316 orderWith (Just TitleAsc) = asc facetDoc_title
317 orderWith (Just TitleDesc) = desc facetDoc_title
319 orderWith (Just ScoreAsc) = asc facetDoc_category
320 orderWith (Just ScoreDesc) = desc facetDoc_category
322 orderWith (Just SourceAsc) = asc facetDoc_source
323 orderWith (Just SourceDesc) = desc facetDoc_source
325 orderWith _ = asc facetDoc_created
327 facetDoc_source :: PGIsJson a
328 => Facet id created title (Column a) favorite ngramCount
329 -> Column (Nullable PGText)
330 facetDoc_source x = toNullable (facetDoc_hyperdata x) .->> pgString "source"