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 #-}
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
34 , FacetPairedReadNullAgg
39 import Control.Arrow (returnA)
40 import Control.Lens ((^.))
41 import Data.Aeson (FromJSON, ToJSON)
42 import Data.Aeson.TH (deriveJSON)
43 import Data.Profunctor.Product.TH (makeAdaptorAndInstance)
45 import qualified Data.Text as T
46 import Data.Time (UTCTime)
47 import Data.Time.Segment (jour)
49 import Protolude hiding (null, map, sum, not)
51 import Test.QuickCheck (elements)
52 import Test.QuickCheck.Arbitrary
53 import qualified Opaleye.Internal.Unpackspec()
55 import Gargantext.Core
56 import Gargantext.Core.Types
57 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger, wellNamedSchema)
58 import Gargantext.Database.Admin.Types.Hyperdata
59 import Gargantext.Database.Query.Filter
60 import Gargantext.Database.Query.Join (leftJoin5)
61 import Gargantext.Database.Query.Table.Ngrams
62 import Gargantext.Database.Query.Table.NodeNode
63 import Gargantext.Database.Query.Table.NodeNodeNgrams
64 import Gargantext.Database.Prelude
65 import Gargantext.Database.Schema.Node
67 ------------------------------------------------------------------------
70 -- data Facet = FacetDoc | FacetSources | FacetAuthors | FacetTerms
71 -- deriving (Show, Generic)
72 --instance FromJSON Facet
73 --instance ToJSON Facet
79 type FacetDoc = Facet NodeId UTCTime Title HyperdataDocument (Maybe Category) (Maybe Double) (Maybe Double)
80 -- type FacetSources = FacetDoc
81 -- type FacetAuthors = FacetDoc
82 -- type FacetTerms = FacetDoc
85 data Facet id created title hyperdata category ngramCount score =
86 FacetDoc { facetDoc_id :: id
87 , facetDoc_created :: created
88 , facetDoc_title :: title
89 , facetDoc_hyperdata :: hyperdata
90 , facetDoc_category :: category
91 , facetDoc_ngramCount :: ngramCount
92 , facetDoc_score :: score
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 {
106 } deriving (Show, Generic)
107 $(deriveJSON (unPrefix "_p_") ''Pair)
108 $(makeAdaptorAndInstance "pPair" ''Pair)
110 instance (Typeable i, Typeable l, ToSchema i, ToSchema l) => ToSchema (Pair i l) where
111 declareNamedSchema = wellNamedSchema "_p_"
112 instance (Arbitrary i, Arbitrary l) => Arbitrary (Pair i l) where
113 arbitrary = Pair <$> arbitrary <*> arbitrary
115 data FacetPaired id date hyperdata score =
116 FacetPaired {_fp_id :: id
118 ,_fp_hyperdata :: hyperdata
120 } deriving (Show, Generic)
121 $(deriveJSON (unPrefix "_fp_") ''FacetPaired)
122 $(makeAdaptorAndInstance "pFacetPaired" ''FacetPaired)
126 instance ( ToSchema id
134 ) => ToSchema (FacetPaired id date hyperdata score) where
135 declareNamedSchema = wellNamedSchema "_fp_"
137 instance ( Arbitrary id
139 , Arbitrary hyperdata
141 ) => Arbitrary (FacetPaired id date hyperdata score) where
142 arbitrary = FacetPaired <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
144 type FacetPairedRead = FacetPaired (Column PGInt4 )
145 (Column PGTimestamptz)
149 type FacetPairedReadNull = FacetPaired (Column (Nullable PGInt4) )
150 (Column (Nullable PGTimestamptz))
151 (Column (Nullable PGJsonb) )
152 (Column (Nullable PGInt4) )
154 type FacetPairedReadNullAgg = FacetPaired (Aggregator (Column (Nullable PGInt4) )
155 (Column (Nullable PGInt4) )
157 (Aggregator (Column (Nullable PGTimestamptz))
158 (Column (Nullable PGTimestamptz))
161 (Aggregator (Column (Nullable PGJsonb) )
162 (Column (Nullable PGJsonb) )
164 (Aggregator (Column (Nullable PGInt4) )
165 (Column (Nullable PGInt4) )
172 $(deriveJSON (unPrefix "facetDoc_") ''Facet)
174 -- | Documentation instance
175 instance ToSchema FacetDoc where
176 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "facetDoc_")
178 -- | Mock and Quickcheck instances
179 instance Arbitrary FacetDoc where
180 arbitrary = elements [ FacetDoc id' (jour year 01 01) t hp (Just cat) (Just ngramCount) (Just score)
182 , year <- [1990..2000]
183 , t <- ["title", "another title"]
184 , hp <- arbitraryHyperdataDocuments
186 , ngramCount <- [3..100]
190 -- Facets / Views for the Front End
191 -- | Database instances
192 $(makeAdaptorAndInstance "pFacetDoc" ''Facet)
193 -- $(makeLensesWith abbreviatedFields ''Facet)
195 type FacetDocRead = Facet (Column PGInt4 )
196 (Column PGTimestamptz)
199 (Column (Nullable PGInt4)) -- Category
200 (Column (Nullable PGFloat8)) -- Ngrams Count
201 (Column (Nullable PGFloat8)) -- Score
203 -----------------------------------------------------------------------
204 -----------------------------------------------------------------------
205 data OrderBy = DateAsc | DateDesc
206 | TitleAsc | TitleDesc
207 | ScoreDesc | ScoreAsc
208 | SourceAsc | SourceDesc
209 deriving (Generic, Enum, Bounded, Read, Show)
211 instance FromHttpApiData OrderBy
213 parseUrlPiece "DateAsc" = pure DateAsc
214 parseUrlPiece "DateDesc" = pure DateDesc
215 parseUrlPiece "TitleAsc" = pure TitleAsc
216 parseUrlPiece "TitleDesc" = pure TitleDesc
217 parseUrlPiece "ScoreAsc" = pure ScoreAsc
218 parseUrlPiece "ScoreDesc" = pure ScoreDesc
219 parseUrlPiece "SourceAsc" = pure SourceAsc
220 parseUrlPiece "SourceDesc" = pure SourceDesc
221 parseUrlPiece _ = Left "Unexpected value of OrderBy"
223 instance ToParamSchema OrderBy
224 instance FromJSON OrderBy
225 instance ToJSON OrderBy
226 instance ToSchema OrderBy
227 instance Arbitrary OrderBy
229 arbitrary = elements [minBound..maxBound]
232 -- TODO-SECURITY check
235 runViewAuthorsDoc :: HasDBid NodeType
241 -> Cmd err [FacetDoc]
242 runViewAuthorsDoc cId t o l order = runOpaQuery $ filterWith o l order $ viewAuthorsDoc cId t ntId
247 viewAuthorsDoc :: HasDBid NodeType
251 -> Query FacetDocRead
252 viewAuthorsDoc cId _ nt = proc () -> do
253 (doc,(_,(_,(_,contact')))) <- queryAuthorsDoc -< ()
255 {-nn <- queryNodeNodeTable -< ()
256 restrict -< nn_node1_id nn .== _node_id doc
257 -- restrict -< nn_delete nn .== (pgBool t)
260 restrict -< _node_id contact' .== (toNullable $ pgNodeId cId)
261 restrict -< _node_typename doc .== (pgInt4 $ toDBid nt)
263 returnA -< FacetDoc (_node_id doc)
266 (_node_hyperdata doc)
267 (toNullable $ pgInt4 1)
268 (toNullable $ pgDouble 1)
269 (toNullable $ pgDouble 1)
271 queryAuthorsDoc :: Query (NodeRead, (NodeNodeNgramsReadNull, (NgramsReadNull, (NodeNodeNgramsReadNull, NodeReadNull))))
272 queryAuthorsDoc = leftJoin5 queryNodeTable queryNodeNodeNgramsTable queryNgramsTable queryNodeNodeNgramsTable queryNodeTable cond12 cond23 cond34 cond45
274 cond12 :: (NodeNodeNgramsRead, NodeRead) -> Column PGBool
275 cond12 (nodeNgram, doc) = _node_id doc
276 .== _nnng_node1_id nodeNgram
278 cond23 :: (NgramsRead, (NodeNodeNgramsRead, NodeReadNull)) -> Column PGBool
279 cond23 (ngrams', (nodeNgram, _)) = ngrams'^.ngrams_id
280 .== _nnng_ngrams_id nodeNgram
282 cond34 :: (NodeNodeNgramsRead, (NgramsRead, (NodeNodeNgramsReadNull, NodeReadNull))) -> Column PGBool
283 cond34 (nodeNgram2, (ngrams', (_,_)))= ngrams'^.ngrams_id .== _nnng_ngrams_id nodeNgram2
285 cond45 :: (NodeRead, (NodeNodeNgramsRead, (NgramsReadNull, (NodeNodeNgramsReadNull, NodeReadNull)))) -> Column PGBool
286 cond45 (contact', (nodeNgram2', (_, (_,_)))) = _node_id contact' .== _nnng_node1_id nodeNgram2'
289 ------------------------------------------------------------------------
291 -- TODO-SECURITY check
292 runViewDocuments :: HasDBid NodeType
299 -> Cmd err [FacetDoc]
300 runViewDocuments cId t o l order query = do
301 runOpaQuery $ filterWith o l order sqlQuery
303 ntId = toDBid NodeDocument
304 sqlQuery = viewDocuments cId t ntId query
306 runCountDocuments :: HasDBid NodeType => CorpusId -> IsTrash -> Maybe Text -> Cmd err Int
307 runCountDocuments cId t mQuery = do
308 runCountOpaQuery sqlQuery
310 sqlQuery = viewDocuments cId t (toDBid NodeDocument) mQuery
313 viewDocuments :: CorpusId
317 -> Query FacetDocRead
318 viewDocuments cId t ntId mQuery = proc () -> do
319 n <- queryNodeTable -< ()
320 nn <- queryNodeNodeTable -< ()
321 restrict -< n^.node_id .== nn^.nn_node2_id
322 restrict -< nn^.nn_node1_id .== (pgNodeId cId)
323 restrict -< n^.node_typename .== (pgInt4 ntId)
324 restrict -< if t then nn^.nn_category .== (pgInt4 0)
325 else nn^.nn_category .>= (pgInt4 1)
327 let query = (fromMaybe "" mQuery)
328 iLikeQuery = T.intercalate "" ["%", query, "%"]
329 restrict -< (n^.node_name) `ilike` (pgStrictText iLikeQuery)
331 returnA -< FacetDoc (_node_id n)
335 (toNullable $ nn^.nn_category)
336 (toNullable $ nn^.nn_score)
337 (toNullable $ nn^.nn_score)
339 ------------------------------------------------------------------------
340 filterWith :: (PGOrd date, PGOrd title, PGOrd category, PGOrd score, hyperdata ~ Column SqlJsonb) =>
341 Maybe Gargantext.Core.Types.Offset
342 -> Maybe Gargantext.Core.Types.Limit
344 -> Select (Facet id (Column date) (Column title) hyperdata (Column category) ngramCount (Column score))
345 -> Select (Facet id (Column date) (Column title) hyperdata (Column category) ngramCount (Column score))
346 filterWith o l order q = limit' l $ offset' o $ orderBy (orderWith order) q
349 orderWith :: (PGOrd b1, PGOrd b2, PGOrd b3)
351 -> Order (Facet id (Column b1) (Column b2) (Column SqlJsonb) (Column b3) ngramCount score)
352 orderWith (Just DateAsc) = asc facetDoc_created
353 orderWith (Just DateDesc) = desc facetDoc_created
355 orderWith (Just TitleAsc) = asc facetDoc_title
356 orderWith (Just TitleDesc) = desc facetDoc_title
358 orderWith (Just ScoreAsc) = asc facetDoc_category
359 orderWith (Just ScoreDesc) = desc facetDoc_category
361 orderWith (Just SourceAsc) = asc facetDoc_source
362 orderWith (Just SourceDesc) = desc facetDoc_source
364 orderWith _ = asc facetDoc_created
366 facetDoc_source :: PGIsJson a
367 => Facet id created title (Column a) favorite ngramCount score
368 -> Column (Nullable PGText)
369 facetDoc_source x = toNullable (facetDoc_hyperdata x) .->> pgString "source"