2 Module : Gargantext.Database.Types.Nodes
3 Description : Main Types of Nodes in Database
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
14 {-# LANGUAGE BangPatterns #-}
15 {-# LANGUAGE DeriveGeneric #-}
16 {-# LANGUAGE FlexibleInstances #-}
17 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
18 {-# LANGUAGE NoImplicitPrelude #-}
19 {-# LANGUAGE OverloadedStrings #-}
20 {-# LANGUAGE TemplateHaskell #-}
21 {-# LANGUAGE GeneralizedNewtypeDeriving #-}
22 -- {-# LANGUAGE DuplicateRecordFields #-}
24 module Gargantext.Database.Types.Node
27 import Prelude (Enum, Bounded, minBound, maxBound)
29 import GHC.Generics (Generic)
31 import Control.Lens hiding (elements, (&))
32 import Control.Applicative ((<*>))
33 import Control.Monad (mzero)
36 import Data.Aeson.Types (emptyObject)
37 import Data.Aeson (Object, toJSON)
38 import Data.Aeson.TH (deriveJSON)
39 import Data.ByteString.Lazy (ByteString)
42 import Data.Monoid (mempty)
43 import Data.Text (Text, unpack, pack)
44 import Data.Time (UTCTime)
45 import Data.Time.Segment (jour, timesAfter, Granularity(D))
48 import Text.Read (read)
49 import Text.Show (Show())
51 import Database.PostgreSQL.Simple.ToField (ToField, toField, toJSONField)
52 import Database.PostgreSQL.Simple.FromField (FromField, fromField)
55 import Test.QuickCheck.Arbitrary
56 import Test.QuickCheck (elements)
58 import Gargantext.Prelude
59 import Gargantext.Core.Utils.Prefix (unPrefix)
60 import Gargantext.Viz.Phylo (Phylo)
61 --import Gargantext.Database.Utils
62 ------------------------------------------------------------------------
63 newtype NodeId = NodeId Int
64 deriving (Show, Read, Generic, Num, Eq, Ord, Enum, ToJSONKey, FromJSONKey, ToJSON, FromJSON)
66 instance ToField NodeId where
67 toField (NodeId n) = toField n
70 instance FromField NodeId where
71 fromField field mdata = do
72 n <- fromField field mdata
74 then return $ NodeId n
77 instance ToSchema NodeId
79 -- type Node json = NodePoly NodeId NodeTypeId UserId ParentId NodeName UTCTime json
84 ------------------------------------------------------------------------
85 data NodePoly id typename userId
87 hyperdata = Node { _node_id :: id
88 , _node_typename :: typename
90 , _node_userId :: userId
91 , _node_parentId :: parentId
96 , _node_hyperdata :: hyperdata
97 } deriving (Show, Generic)
98 $(deriveJSON (unPrefix "_node_") ''NodePoly)
99 $(makeLenses ''NodePoly)
101 -- | NodePoly indicates that Node has a Polymorphism Type
102 type Node json = NodePoly NodeId NodeTypeId UserId (Maybe ParentId) NodeName UTCTime json
106 ------------------------------------------------------------------------
109 instance FromHttpApiData NodeId where
110 parseUrlPiece n = pure $ NodeId $ (read . cs) n
112 instance ToParamSchema NodeId
113 instance Arbitrary NodeId where
114 arbitrary = NodeId <$> arbitrary
116 type ParentId = NodeId
117 type CorpusId = NodeId
119 type DocumentId = NodeId
120 type DocId = DocumentId -- todo: remove this
122 type MasterCorpusId = CorpusId
123 type UserCorpusId = CorpusId
125 type GraphId = NodeId
126 type PhyloId = NodeId
127 type AnnuaireId = NodeId
128 type ContactId = NodeId
131 type MasterUserId = UserId
133 id2int :: NodeId -> Int
134 id2int (NodeId n) = n
137 type UTCTime' = UTCTime
139 instance Arbitrary UTCTime' where
140 arbitrary = elements $ timesAfter 100 D (jour 2000 01 01)
142 ------------------------------------------------------------------------
143 data Status = Status { status_failed :: !Int
144 , status_succeeded :: !Int
145 , status_remaining :: !Int
146 } deriving (Show, Generic)
147 $(deriveJSON (unPrefix "status_") ''Status)
149 instance Arbitrary Status where
150 arbitrary = Status <$> arbitrary <*> arbitrary <*> arbitrary
152 ------------------------------------------------------------------------
153 data StatusV3 = StatusV3 { statusV3_error :: !(Maybe Text)
154 , statusV3_action :: !(Maybe Text)
155 } deriving (Show, Generic)
156 $(deriveJSON (unPrefix "statusV3_") ''StatusV3)
157 ------------------------------------------------------------------------
159 -- Only Hyperdata types should be member of this type class.
161 ------------------------------------------------------------------------
162 data HyperdataDocumentV3 = HyperdataDocumentV3 { hyperdataDocumentV3_publication_day :: !(Maybe Int)
163 , hyperdataDocumentV3_language_iso2 :: !(Maybe Text)
164 , hyperdataDocumentV3_publication_second :: !(Maybe Int)
165 , hyperdataDocumentV3_publication_minute :: !(Maybe Int)
166 , hyperdataDocumentV3_publication_month :: !(Maybe Int)
167 , hyperdataDocumentV3_publication_hour :: !(Maybe Int)
168 , hyperdataDocumentV3_error :: !(Maybe Text)
169 , hyperdataDocumentV3_language_iso3 :: !(Maybe Text)
170 , hyperdataDocumentV3_authors :: !(Maybe Text)
171 , hyperdataDocumentV3_publication_year :: !(Maybe Int)
172 , hyperdataDocumentV3_publication_date :: !(Maybe Text)
173 , hyperdataDocumentV3_language_name :: !(Maybe Text)
174 , hyperdataDocumentV3_statuses :: !(Maybe [StatusV3])
175 , hyperdataDocumentV3_realdate_full_ :: !(Maybe Text)
176 , hyperdataDocumentV3_source :: !(Maybe Text)
177 , hyperdataDocumentV3_abstract :: !(Maybe Text)
178 , hyperdataDocumentV3_title :: !(Maybe Text)
179 } deriving (Show, Generic)
180 $(deriveJSON (unPrefix "hyperdataDocumentV3_") ''HyperdataDocumentV3)
183 instance Hyperdata HyperdataDocumentV3
185 ------------------------------------------------------------------------
186 data HyperdataDocument = HyperdataDocument { _hyperdataDocument_bdd :: !(Maybe Text)
187 , _hyperdataDocument_doi :: !(Maybe Text)
188 , _hyperdataDocument_url :: !(Maybe Text)
189 , _hyperdataDocument_uniqId :: !(Maybe Text)
190 , _hyperdataDocument_uniqIdBdd :: !(Maybe Text)
191 , _hyperdataDocument_page :: !(Maybe Int)
192 , _hyperdataDocument_title :: !(Maybe Text)
193 , _hyperdataDocument_authors :: !(Maybe Text)
194 , _hyperdataDocument_institutes :: !(Maybe Text)
195 , _hyperdataDocument_source :: !(Maybe Text)
196 , _hyperdataDocument_abstract :: !(Maybe Text)
197 , _hyperdataDocument_publication_date :: !(Maybe Text)
198 , _hyperdataDocument_publication_year :: !(Maybe Int)
199 , _hyperdataDocument_publication_month :: !(Maybe Int)
200 , _hyperdataDocument_publication_day :: !(Maybe Int)
201 , _hyperdataDocument_publication_hour :: !(Maybe Int)
202 , _hyperdataDocument_publication_minute :: !(Maybe Int)
203 , _hyperdataDocument_publication_second :: !(Maybe Int)
204 , _hyperdataDocument_language_iso2 :: !(Maybe Text)
205 } deriving (Show, Generic)
207 $(deriveJSON (unPrefix "_hyperdataDocument_") ''HyperdataDocument)
208 $(makeLenses ''HyperdataDocument)
210 class ToHyperdataDocument a where
211 toHyperdataDocument :: a -> HyperdataDocument
213 instance ToHyperdataDocument HyperdataDocument
215 toHyperdataDocument = identity
217 instance Eq HyperdataDocument where
218 (==) h1 h2 = (==) (_hyperdataDocument_uniqId h1) (_hyperdataDocument_uniqId h2)
220 instance Ord HyperdataDocument where
221 compare h1 h2 = compare (_hyperdataDocument_publication_date h1) (_hyperdataDocument_publication_date h2)
223 instance Hyperdata HyperdataDocument
225 instance ToField HyperdataDocument where
226 toField = toJSONField
228 instance Arbitrary HyperdataDocument where
229 arbitrary = elements arbitraryHyperdataDocuments
231 arbitraryHyperdataDocuments :: [HyperdataDocument]
232 arbitraryHyperdataDocuments =
233 map toHyperdataDocument' ([ ("AI is big but less than crypto", "Troll System journal")
234 , ("Crypto is big but less than AI", "System Troll review" )
235 , ("Science is magic" , "Closed Source review")
236 , ("Open science for all" , "No Time" )
237 , ("Closed science for me" , "No Space" )
240 toHyperdataDocument' (t1,t2) =
241 HyperdataDocument Nothing Nothing Nothing Nothing Nothing Nothing (Just t1)
242 Nothing Nothing (Just t2) Nothing Nothing Nothing Nothing Nothing
243 Nothing Nothing Nothing Nothing
245 ------------------------------------------------------------------------
246 data LanguageNodes = LanguageNodes { languageNodes___unknown__ :: [Int]}
247 deriving (Show, Generic)
248 $(deriveJSON (unPrefix "languageNodes_") ''LanguageNodes)
250 ------------------------------------------------------------------------
251 -- level: debug | dev (fatal = critical)
252 data EventLevel = CRITICAL | FATAL | ERROR | WARNING | INFO | DEBUG
253 deriving (Show, Generic, Enum, Bounded)
255 instance FromJSON EventLevel
256 instance ToJSON EventLevel
258 instance Arbitrary EventLevel where
259 arbitrary = elements [minBound..maxBound]
261 instance ToSchema EventLevel where
262 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
264 ------------------------------------------------------------------------
266 data Event = Event { event_level :: !EventLevel
267 , event_message :: !Text
268 , event_date :: !UTCTime
269 } deriving (Show, Generic)
270 $(deriveJSON (unPrefix "event_") ''Event)
272 instance Arbitrary Event where
273 arbitrary = Event <$> arbitrary <*> arbitrary <*> arbitrary
275 instance ToSchema Event where
276 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
278 ------------------------------------------------------------------------
279 instance Arbitrary Text where
280 arbitrary = elements $ map (\c -> pack [c]) ['a'..'z']
282 data Resource = Resource { resource_path :: !(Maybe Text)
283 , resource_scraper :: !(Maybe Text)
284 , resource_query :: !(Maybe Text)
285 , resource_events :: !([Event])
286 , resource_status :: !Status
287 , resource_date :: !UTCTime'
288 } deriving (Show, Generic)
289 $(deriveJSON (unPrefix "resource_") ''Resource)
291 instance Arbitrary Resource where
292 arbitrary = Resource <$> arbitrary
299 instance ToSchema Resource where
300 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
302 ------------------------------------------------------------------------
303 data HyperdataUser = HyperdataUser { hyperdataUser_language :: Maybe Text
304 } deriving (Show, Generic)
305 $(deriveJSON (unPrefix "hyperdataUser_") ''HyperdataUser)
307 instance Hyperdata HyperdataUser
308 ------------------------------------------------------------------------
309 data HyperdataFolder = HyperdataFolder { hyperdataFolder_desc :: Maybe Text
310 } deriving (Show, Generic)
311 $(deriveJSON (unPrefix "hyperdataFolder_") ''HyperdataFolder)
313 instance Hyperdata HyperdataFolder
314 ------------------------------------------------------------------------
315 data HyperdataCorpus = HyperdataCorpus { hyperdataCorpus_title :: !(Maybe Text)
316 , hyperdataCorpus_desc :: !(Maybe Text)
317 , hyperdataCorpus_query :: !(Maybe Text)
318 , hyperdataCorpus_authors :: !(Maybe Text)
319 , hyperdataCorpus_resources :: !(Maybe [Resource])
320 } deriving (Show, Generic)
321 $(deriveJSON (unPrefix "hyperdataCorpus_") ''HyperdataCorpus)
323 instance Hyperdata HyperdataCorpus
325 corpusExample :: ByteString
326 corpusExample = "" -- TODO
328 defaultCorpus :: HyperdataCorpus
329 defaultCorpus = (HyperdataCorpus (Just "Title") (Just "Descr") (Just "Bool query") (Just "Authors") Nothing)
331 hyperdataCorpus :: HyperdataCorpus
332 hyperdataCorpus = case decode corpusExample of
334 Nothing -> defaultCorpus
336 instance Arbitrary HyperdataCorpus where
337 arbitrary = pure hyperdataCorpus -- TODO
339 ------------------------------------------------------------------------
340 ------------------------------------------------------------------------
341 data HyperdataAnnuaire = HyperdataAnnuaire { hyperdataAnnuaire_title :: !(Maybe Text)
342 , hyperdataAnnuaire_desc :: !(Maybe Text)
343 } deriving (Show, Generic)
344 $(deriveJSON (unPrefix "hyperdataAnnuaire_") ''HyperdataAnnuaire)
346 instance Hyperdata HyperdataAnnuaire
348 hyperdataAnnuaire :: HyperdataAnnuaire
349 hyperdataAnnuaire = HyperdataAnnuaire (Just "Annuaire Title") (Just "Annuaire Description")
351 instance Arbitrary HyperdataAnnuaire where
352 arbitrary = pure hyperdataAnnuaire -- TODO
354 ------------------------------------------------------------------------
355 newtype HyperdataAny = HyperdataAny Object
356 deriving (Show, Generic, ToJSON, FromJSON)
358 instance Hyperdata HyperdataAny
360 instance Arbitrary HyperdataAny where
361 arbitrary = pure $ HyperdataAny mempty -- TODO produce arbitrary objects
362 ------------------------------------------------------------------------
364 data HyperdataList = HyperdataList { hyperdataList_preferences :: !(Maybe Text)
365 } deriving (Show, Generic)
366 $(deriveJSON (unPrefix "hyperdataList_") ''HyperdataList)
368 instance Hyperdata HyperdataList
370 instance Arbitrary HyperdataList where
371 arbitrary = elements [HyperdataList (Just "from list A")]
374 data HyperdataListModel = HyperdataListModel { _hlm_params :: !(Int, Int)
376 , _hlm_score :: !(Maybe Double)
377 } deriving (Show, Generic)
379 instance Hyperdata HyperdataListModel
380 instance Arbitrary HyperdataListModel where
381 arbitrary = elements [HyperdataListModel (100,100) "models/example.model" Nothing]
383 $(deriveJSON (unPrefix "_hlm_") ''HyperdataListModel)
384 $(makeLenses ''HyperdataListModel)
386 ------------------------------------------------------------------------
387 data HyperdataScore = HyperdataScore { hyperdataScore_preferences :: !(Maybe Text)
388 } deriving (Show, Generic)
389 $(deriveJSON (unPrefix "hyperdataScore_") ''HyperdataScore)
391 instance Hyperdata HyperdataScore
393 ------------------------------------------------------------------------
395 data HyperdataResource = HyperdataResource { hyperdataResource_preferences :: !(Maybe Text)
396 } deriving (Show, Generic)
397 $(deriveJSON (unPrefix "hyperdataResource_") ''HyperdataResource)
399 instance Hyperdata HyperdataResource
401 ------------------------------------------------------------------------
402 data HyperdataDashboard = HyperdataDashboard { hyperdataDashboard_preferences :: !(Maybe Text)
403 } deriving (Show, Generic)
404 $(deriveJSON (unPrefix "hyperdataDashboard_") ''HyperdataDashboard)
406 instance Hyperdata HyperdataDashboard
408 -- TODO add the Graph Structure here
409 data HyperdataGraph = HyperdataGraph { hyperdataGraph_preferences :: !(Maybe Text)
410 } deriving (Show, Generic)
411 $(deriveJSON (unPrefix "hyperdataGraph_") ''HyperdataGraph)
413 instance Hyperdata HyperdataGraph
415 ------------------------------------------------------------------------
417 -- TODO add the Graph Structure here
418 data HyperdataPhylo = HyperdataPhylo { hyperdataPhylo_preferences :: !(Maybe Text)
419 , hyperdataPhylo_data :: !(Maybe Phylo)
420 } deriving (Show, Generic)
421 $(deriveJSON (unPrefix "hyperdataPhylo_") ''HyperdataPhylo)
423 instance Hyperdata HyperdataPhylo
425 ------------------------------------------------------------------------
426 -- | TODO FEATURE: Notebook saved in the node
427 data HyperdataNotebook = HyperdataNotebook { hyperdataNotebook_preferences :: !(Maybe Text)
428 } deriving (Show, Generic)
429 $(deriveJSON (unPrefix "hyperdataNotebook_") ''HyperdataNotebook)
431 instance Hyperdata HyperdataNotebook
435 -- | Then a Node can be either a Folder or a Corpus or a Document
436 type NodeUser = Node HyperdataUser
437 type NodeFolder = Node HyperdataFolder
439 type NodeCorpus = Node HyperdataCorpus
442 data HyperData = HyperdataTexts { hd_texts :: Maybe Text }
443 | HyperdataList' { hd_lists :: Maybe Text}
444 deriving (Show, Generic)
446 $(deriveJSON (unPrefix "hd_") ''HyperData)
448 instance Hyperdata HyperData
451 type NodeTexts = Node HyperData
453 type NodeCorpusV3 = Node HyperdataCorpus
454 type NodeDocument = Node HyperdataDocument
456 type NodeAnnuaire = Node HyperdataAnnuaire
458 -- | Any others nodes
459 type NodeAny = Node HyperdataAny
461 ---- | Then a Node can be either a Graph or a Phylo or a Notebook
462 type NodeList = Node HyperdataList
463 type NodeGraph = Node HyperdataGraph
464 type NodePhylo = Node HyperdataPhylo
465 type NodeNotebook = Node HyperdataNotebook
466 ------------------------------------------------------------------------
467 data NodeType = NodeUser
469 | NodeCorpus | NodeCorpusV3 | NodeTexts | NodeDocument
470 | NodeAnnuaire | NodeContact
471 | NodeGraph | NodePhylo
472 | NodeDashboard | NodeChart
473 | NodeList | NodeListModel deriving (Show, Read, Eq, Generic, Bounded, Enum)
482 allNodeTypes :: [NodeType]
483 allNodeTypes = [minBound ..]
485 instance FromJSON NodeType
486 instance ToJSON NodeType
488 instance FromHttpApiData NodeType
490 parseUrlPiece = Right . read . unpack
492 instance ToParamSchema NodeType
493 instance ToSchema NodeType
496 data NodePolySearch id typename userId
498 hyperdata search = NodeSearch { _ns_id :: id
499 , _ns_typename :: typename
500 , _ns_userId :: userId
501 -- , nodeUniqId :: hashId
502 , _ns_parentId :: parentId
506 , _ns_hyperdata :: hyperdata
507 , _ns_search :: search
508 } deriving (Show, Generic)
509 $(deriveJSON (unPrefix "_ns_") ''NodePolySearch)
510 $(makeLenses ''NodePolySearch)
512 type NodeSearch json = NodePolySearch NodeId NodeTypeId UserId (Maybe ParentId) NodeName UTCTime json (Maybe TSVector)
513 ------------------------------------------------------------------------
516 instance (Arbitrary hyperdata
518 ,Arbitrary nodeTypeId
520 ,Arbitrary nodeParentId
521 ) => Arbitrary (NodePoly nodeId nodeTypeId userId nodeParentId
522 NodeName UTCTime hyperdata) where
523 --arbitrary = Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (arbitrary) (Just "")
524 arbitrary = Node <$> arbitrary <*> arbitrary <*> arbitrary
525 <*> arbitrary <*> arbitrary <*> arbitrary
528 instance (Arbitrary hyperdata
530 ,Arbitrary nodeTypeId
532 ,Arbitrary nodeParentId
533 ) => Arbitrary (NodePolySearch nodeId nodeTypeId userId nodeParentId
534 NodeName UTCTime hyperdata (Maybe TSVector)) where
535 --arbitrary = Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (arbitrary) (Just "")
536 arbitrary = NodeSearch <$> arbitrary <*> arbitrary <*> arbitrary
537 <*> arbitrary <*> arbitrary <*> arbitrary
538 <*> arbitrary <*> arbitrary
541 ------------------------------------------------------------------------
542 hyperdataDocument :: HyperdataDocument
543 hyperdataDocument = case decode docExample of
545 Nothing -> HyperdataDocument Nothing Nothing Nothing Nothing
546 Nothing Nothing Nothing Nothing
547 Nothing Nothing Nothing Nothing
548 Nothing Nothing Nothing Nothing
549 Nothing Nothing Nothing
550 docExample :: ByteString
551 docExample = "{\"doi\":\"sdfds\",\"publication_day\":6,\"language_iso2\":\"en\",\"publication_minute\":0,\"publication_month\":7,\"language_iso3\":\"eng\",\"publication_second\":0,\"authors\":\"Nils Hovdenak, Kjell Haram\",\"publication_year\":2012,\"publication_date\":\"2012-07-06 00:00:00+00:00\",\"language_name\":\"English\",\"realdate_full_\":\"2012 01 12\",\"source\":\"European journal of obstetrics, gynecology, and reproductive biology\",\"abstract\":\"The literature was searched for publications on minerals and vitamins during pregnancy and the possible influence of supplements on pregnancy outcome.\",\"title\":\"Influence of mineral and vitamin supplements on pregnancy outcome.\",\"publication_hour\":0}"
553 instance ToSchema HyperdataCorpus where
554 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
555 & mapped.schema.description ?~ "a corpus"
556 & mapped.schema.example ?~ toJSON hyperdataCorpus
558 instance ToSchema HyperdataAnnuaire where
559 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
560 & mapped.schema.description ?~ "an annuaire"
561 & mapped.schema.example ?~ toJSON hyperdataAnnuaire
563 instance ToSchema HyperdataDocument where
564 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
565 & mapped.schema.description ?~ "a document"
566 & mapped.schema.example ?~ toJSON hyperdataDocument
568 instance ToSchema HyperdataAny where
569 declareNamedSchema proxy =
570 pure $ genericNameSchema defaultSchemaOptions proxy mempty
571 & schema.description ?~ "a node"
572 & schema.example ?~ emptyObject -- TODO
575 instance ToSchema hyperdata =>
576 ToSchema (NodePoly NodeId NodeTypeId
582 instance ToSchema hyperdata =>
583 ToSchema (NodePoly NodeId NodeTypeId
585 (Maybe ParentId) NodeName
590 instance ToSchema hyperdata =>
591 ToSchema (NodePolySearch NodeId NodeTypeId
594 UTCTime hyperdata (Maybe TSVector)
597 instance ToSchema hyperdata =>
598 ToSchema (NodePolySearch NodeId NodeTypeId
600 (Maybe ParentId) NodeName
601 UTCTime hyperdata (Maybe TSVector)
605 instance ToSchema Status