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 #-}
23 -- {-# LANGUAGE DuplicateRecordFields #-}
25 module Gargantext.Database.Types.Node
28 import Prelude (Enum, Bounded, minBound, maxBound)
30 import GHC.Generics (Generic)
32 import Control.Lens hiding (elements, (&))
33 import Control.Applicative ((<*>))
34 import Control.Monad (mzero)
37 import Data.Aeson.Types (emptyObject)
38 import Data.Aeson (Object, toJSON)
39 import Data.Aeson.TH (deriveJSON)
40 import Data.ByteString.Lazy (ByteString)
43 import Data.Monoid (mempty)
44 import Data.Text (Text, unpack)
45 import Data.Time (UTCTime)
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)
57 import Test.QuickCheck.Instances.Time ()
58 import Test.QuickCheck.Instances.Text ()
60 import Gargantext.Prelude
61 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
62 import Gargantext.Viz.Phylo (Phylo)
63 --import Gargantext.Database.Utils
64 ------------------------------------------------------------------------
66 newtype NodeId = NodeId Int
67 deriving (Show, Read, Generic, Num, Eq, Ord, Enum, ToJSONKey, FromJSONKey, ToJSON, FromJSON)
69 instance ToField NodeId where
70 toField (NodeId n) = toField n
72 instance FromField NodeId where
73 fromField field mdata = do
74 n <- fromField field mdata
76 then return $ NodeId n
79 instance ToSchema NodeId
86 ------------------------------------------------------------------------
87 data NodePoly id typename userId
89 hyperdata = Node { _node_id :: id
90 , _node_typename :: typename
92 , _node_userId :: userId
93 , _node_parentId :: parentId
98 , _node_hyperdata :: hyperdata
99 } deriving (Show, Generic)
100 $(deriveJSON (unPrefix "_node_") ''NodePoly)
101 $(makeLenses ''NodePoly)
103 -- | NodePoly indicates that Node has a Polymorphism Type
104 type Node json = NodePoly NodeId NodeTypeId UserId (Maybe ParentId) NodeName UTCTime json
107 ------------------------------------------------------------------------
110 instance FromHttpApiData NodeId where
111 parseUrlPiece n = pure $ NodeId $ (read . cs) n
113 instance ToParamSchema NodeId
114 instance Arbitrary NodeId where
115 arbitrary = NodeId <$> arbitrary
117 type ParentId = NodeId
118 type CorpusId = NodeId
120 type DocumentId = NodeId
123 type MasterCorpusId = CorpusId
124 type UserCorpusId = CorpusId
126 type GraphId = NodeId
127 type PhyloId = NodeId
128 type AnnuaireId = NodeId
129 type ContactId = NodeId
132 type MasterUserId = UserId
134 id2int :: NodeId -> Int
135 id2int (NodeId n) = n
137 ------------------------------------------------------------------------
138 data Status = Status { status_failed :: !Int
139 , status_succeeded :: !Int
140 , status_remaining :: !Int
141 } deriving (Show, Generic)
142 $(deriveJSON (unPrefix "status_") ''Status)
144 instance Arbitrary Status where
145 arbitrary = Status <$> arbitrary <*> arbitrary <*> arbitrary
147 ------------------------------------------------------------------------
148 data StatusV3 = StatusV3 { statusV3_error :: !(Maybe Text)
149 , statusV3_action :: !(Maybe Text)
150 } deriving (Show, Generic)
151 $(deriveJSON (unPrefix "statusV3_") ''StatusV3)
152 ------------------------------------------------------------------------
154 -- Only Hyperdata types should be member of this type class.
156 ------------------------------------------------------------------------
157 data HyperdataDocumentV3 = HyperdataDocumentV3 { hyperdataDocumentV3_publication_day :: !(Maybe Int)
158 , hyperdataDocumentV3_language_iso2 :: !(Maybe Text)
159 , hyperdataDocumentV3_publication_second :: !(Maybe Int)
160 , hyperdataDocumentV3_publication_minute :: !(Maybe Int)
161 , hyperdataDocumentV3_publication_month :: !(Maybe Int)
162 , hyperdataDocumentV3_publication_hour :: !(Maybe Int)
163 , hyperdataDocumentV3_error :: !(Maybe Text)
164 , hyperdataDocumentV3_language_iso3 :: !(Maybe Text)
165 , hyperdataDocumentV3_authors :: !(Maybe Text)
166 , hyperdataDocumentV3_publication_year :: !(Maybe Int)
167 , hyperdataDocumentV3_publication_date :: !(Maybe Text)
168 , hyperdataDocumentV3_language_name :: !(Maybe Text)
169 , hyperdataDocumentV3_statuses :: !(Maybe [StatusV3])
170 , hyperdataDocumentV3_realdate_full_ :: !(Maybe Text)
171 , hyperdataDocumentV3_source :: !(Maybe Text)
172 , hyperdataDocumentV3_abstract :: !(Maybe Text)
173 , hyperdataDocumentV3_title :: !(Maybe Text)
174 } deriving (Show, Generic)
175 $(deriveJSON (unPrefix "hyperdataDocumentV3_") ''HyperdataDocumentV3)
178 instance Hyperdata HyperdataDocumentV3
180 ------------------------------------------------------------------------
181 data HyperdataDocument = HyperdataDocument { _hyperdataDocument_bdd :: !(Maybe Text)
182 , _hyperdataDocument_doi :: !(Maybe Text)
183 , _hyperdataDocument_url :: !(Maybe Text)
184 , _hyperdataDocument_uniqId :: !(Maybe Text)
185 , _hyperdataDocument_uniqIdBdd :: !(Maybe Text)
186 , _hyperdataDocument_page :: !(Maybe Int)
187 , _hyperdataDocument_title :: !(Maybe Text)
188 , _hyperdataDocument_authors :: !(Maybe Text)
189 , _hyperdataDocument_institutes :: !(Maybe Text)
190 , _hyperdataDocument_source :: !(Maybe Text)
191 , _hyperdataDocument_abstract :: !(Maybe Text)
192 , _hyperdataDocument_publication_date :: !(Maybe Text)
193 , _hyperdataDocument_publication_year :: !(Maybe Int)
194 , _hyperdataDocument_publication_month :: !(Maybe Int)
195 , _hyperdataDocument_publication_day :: !(Maybe Int)
196 , _hyperdataDocument_publication_hour :: !(Maybe Int)
197 , _hyperdataDocument_publication_minute :: !(Maybe Int)
198 , _hyperdataDocument_publication_second :: !(Maybe Int)
199 , _hyperdataDocument_language_iso2 :: !(Maybe Text)
200 } deriving (Show, Generic)
202 $(deriveJSON (unPrefix "_hyperdataDocument_") ''HyperdataDocument)
203 $(makeLenses ''HyperdataDocument)
205 class ToHyperdataDocument a where
206 toHyperdataDocument :: a -> HyperdataDocument
208 instance ToHyperdataDocument HyperdataDocument
210 toHyperdataDocument = identity
212 instance Eq HyperdataDocument where
213 (==) h1 h2 = (==) (_hyperdataDocument_uniqId h1) (_hyperdataDocument_uniqId h2)
215 instance Ord HyperdataDocument where
216 compare h1 h2 = compare (_hyperdataDocument_publication_date h1) (_hyperdataDocument_publication_date h2)
218 instance Hyperdata HyperdataDocument
220 instance ToField HyperdataDocument where
221 toField = toJSONField
223 instance Arbitrary HyperdataDocument where
224 arbitrary = elements arbitraryHyperdataDocuments
226 arbitraryHyperdataDocuments :: [HyperdataDocument]
227 arbitraryHyperdataDocuments =
228 map toHyperdataDocument' ([ ("AI is big but less than crypto", "Troll System journal")
229 , ("Crypto is big but less than AI", "System Troll review" )
230 , ("Science is magic" , "Closed Source review")
231 , ("Open science for all" , "No Time" )
232 , ("Closed science for me" , "No Space" )
235 toHyperdataDocument' (t1,t2) =
236 HyperdataDocument Nothing Nothing Nothing Nothing Nothing Nothing (Just t1)
237 Nothing Nothing (Just t2) Nothing Nothing Nothing Nothing Nothing
238 Nothing Nothing Nothing Nothing
240 ------------------------------------------------------------------------
241 data LanguageNodes = LanguageNodes { languageNodes___unknown__ :: [Int]}
242 deriving (Show, Generic)
243 $(deriveJSON (unPrefix "languageNodes_") ''LanguageNodes)
245 ------------------------------------------------------------------------
246 -- level: debug | dev (fatal = critical)
247 data EventLevel = CRITICAL | FATAL | ERROR | WARNING | INFO | DEBUG
248 deriving (Show, Generic, Enum, Bounded)
250 instance FromJSON EventLevel
251 instance ToJSON EventLevel
253 instance Arbitrary EventLevel where
254 arbitrary = elements [minBound..maxBound]
256 instance ToSchema EventLevel where
257 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
259 ------------------------------------------------------------------------
261 data Event = Event { event_level :: !EventLevel
262 , event_message :: !Text
263 , event_date :: !UTCTime
264 } deriving (Show, Generic)
265 $(deriveJSON (unPrefix "event_") ''Event)
267 instance Arbitrary Event where
268 arbitrary = Event <$> arbitrary <*> arbitrary <*> arbitrary
270 instance ToSchema Event where
271 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "event_")
273 ------------------------------------------------------------------------
275 data Resource = Resource { resource_path :: !(Maybe Text)
276 , resource_scraper :: !(Maybe Text)
277 , resource_query :: !(Maybe Text)
278 , resource_events :: !([Event])
279 , resource_status :: !Status
280 , resource_date :: !UTCTime
281 } deriving (Show, Generic)
282 $(deriveJSON (unPrefix "resource_") ''Resource)
284 instance Arbitrary Resource where
285 arbitrary = Resource <$> arbitrary
292 instance ToSchema Resource where
293 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "resource_")
295 ------------------------------------------------------------------------
296 data HyperdataUser = HyperdataUser { hyperdataUser_language :: !(Maybe Text)
297 } deriving (Show, Generic)
298 $(deriveJSON (unPrefix "hyperdataUser_") ''HyperdataUser)
300 instance Hyperdata HyperdataUser
301 ------------------------------------------------------------------------
302 ------------------------------------------------------------------------
303 ------------------------------------------------------------------------
304 ------------------------------------------------------------------------
305 data CodeType = JSON | Markdown | Haskell
307 instance ToJSON CodeType
308 instance FromJSON CodeType
309 instance ToSchema CodeType
311 ------------------------------------------------------------------------
312 data CorpusField = MarkdownField { _cf_text :: !Text }
313 | JsonField { _cf_title :: !Text
316 , _cf_authors :: !Text
317 -- , _cf_resources :: ![Resource]
319 | HaskellField { _cf_haskell :: !Text }
322 $(deriveJSON (unPrefix "_cf_") ''CorpusField)
323 $(makeLenses ''CorpusField)
325 defaultCorpusField :: CorpusField
326 defaultCorpusField = MarkdownField "# title"
328 instance ToSchema CorpusField where
329 declareNamedSchema proxy =
330 genericDeclareNamedSchema (unPrefixSwagger "_cf_") proxy
331 & mapped.schema.description ?~ "CorpusField"
332 & mapped.schema.example ?~ toJSON defaultCorpusField
334 ------------------------------------------------------------------------
335 data HyperdataField a =
336 HyperdataField { _hf_type :: !CodeType
340 $(deriveJSON (unPrefix "_hf_") ''HyperdataField)
341 $(makeLenses ''HyperdataField)
343 defaultHyperdataField :: HyperdataField CorpusField
344 defaultHyperdataField = HyperdataField Markdown "name" defaultCorpusField
346 instance (ToSchema a) => ToSchema (HyperdataField a) where
348 genericDeclareNamedSchema (unPrefixSwagger "_hf_")
349 -- & mapped.schema.description ?~ "HyperdataField"
350 -- & mapped.schema.example ?~ toJSON defaultHyperdataField
352 ------------------------------------------------------------------------
353 data HyperdataCorpus =
354 HyperdataCorpus { _hc_fields :: ![HyperdataField CorpusField] }
356 $(deriveJSON (unPrefix "_hc_") ''HyperdataCorpus)
357 $(makeLenses ''HyperdataCorpus)
359 instance Hyperdata HyperdataCorpus
361 corpusExample :: ByteString
362 corpusExample = "" -- TODO
364 defaultCorpus :: HyperdataCorpus
365 defaultCorpus = HyperdataCorpus [ HyperdataField JSON "Mandatory fields" (JsonField "Title" "Descr" "Bool query" "Authors")
366 , HyperdataField Markdown "Optional Text" (MarkdownField "# title\n## subtitle")
369 hyperdataCorpus :: HyperdataCorpus
370 hyperdataCorpus = case decode corpusExample of
372 Nothing -> defaultCorpus
374 instance Arbitrary HyperdataCorpus where
375 arbitrary = pure hyperdataCorpus -- TODO
377 ------------------------------------------------------------------------
379 data HyperdataList = HyperdataList {hd_list :: !(Maybe Text)
380 } deriving (Show, Generic)
381 $(deriveJSON (unPrefix "hd_") ''HyperdataList)
383 instance Hyperdata HyperdataList
385 ------------------------------------------------------------------------
386 data HyperdataAnnuaire = HyperdataAnnuaire { hyperdataAnnuaire_title :: !(Maybe Text)
387 , hyperdataAnnuaire_desc :: !(Maybe Text)
388 } deriving (Show, Generic)
389 $(deriveJSON (unPrefix "hyperdataAnnuaire_") ''HyperdataAnnuaire)
391 instance Hyperdata HyperdataAnnuaire
393 hyperdataAnnuaire :: HyperdataAnnuaire
394 hyperdataAnnuaire = HyperdataAnnuaire (Just "Annuaire Title") (Just "Annuaire Description")
396 instance Arbitrary HyperdataAnnuaire where
397 arbitrary = pure hyperdataAnnuaire -- TODO
399 ------------------------------------------------------------------------
400 newtype HyperdataAny = HyperdataAny Object
401 deriving (Show, Generic, ToJSON, FromJSON)
403 instance Hyperdata HyperdataAny
405 instance Arbitrary HyperdataAny where
406 arbitrary = pure $ HyperdataAny mempty -- TODO produce arbitrary objects
407 ------------------------------------------------------------------------
410 instance Arbitrary HyperdataList' where
411 arbitrary = elements [HyperdataList' (Just "from list A")]
415 data HyperdataListModel = HyperdataListModel { _hlm_params :: !(Int, Int)
417 , _hlm_score :: !(Maybe Double)
418 } deriving (Show, Generic)
420 instance Hyperdata HyperdataListModel
421 instance Arbitrary HyperdataListModel where
422 arbitrary = elements [HyperdataListModel (100,100) "models/example.model" Nothing]
424 $(deriveJSON (unPrefix "_hlm_") ''HyperdataListModel)
425 $(makeLenses ''HyperdataListModel)
427 ------------------------------------------------------------------------
428 data HyperdataScore = HyperdataScore { hyperdataScore_preferences :: !(Maybe Text)
429 } deriving (Show, Generic)
430 $(deriveJSON (unPrefix "hyperdataScore_") ''HyperdataScore)
432 instance Hyperdata HyperdataScore
434 ------------------------------------------------------------------------
436 data HyperdataResource = HyperdataResource { hyperdataResource_preferences :: !(Maybe Text)
437 } deriving (Show, Generic)
438 $(deriveJSON (unPrefix "hyperdataResource_") ''HyperdataResource)
440 instance Hyperdata HyperdataResource
442 ------------------------------------------------------------------------
443 data HyperdataDashboard = HyperdataDashboard { hyperdataDashboard_preferences :: !(Maybe Text)
444 } deriving (Show, Generic)
445 $(deriveJSON (unPrefix "hyperdataDashboard_") ''HyperdataDashboard)
447 instance Hyperdata HyperdataDashboard
449 ------------------------------------------------------------------------
451 -- TODO add the Graph Structure here
452 data HyperdataPhylo = HyperdataPhylo { hyperdataPhylo_preferences :: !(Maybe Text)
453 , hyperdataPhylo_data :: !(Maybe Phylo)
454 } deriving (Show, Generic)
455 $(deriveJSON (unPrefix "hyperdataPhylo_") ''HyperdataPhylo)
457 instance Hyperdata HyperdataPhylo
459 ------------------------------------------------------------------------
460 -- | TODO FEATURE: Notebook saved in the node
461 data HyperdataNotebook = HyperdataNotebook { hyperdataNotebook_preferences :: !(Maybe Text)
462 } deriving (Show, Generic)
463 $(deriveJSON (unPrefix "hyperdataNotebook_") ''HyperdataNotebook)
465 instance Hyperdata HyperdataNotebook
469 data HyperData = HyperdataTexts { hd_preferences :: Maybe Text }
470 | HyperdataList' { hd_preferences :: Maybe Text}
471 deriving (Show, Generic)
473 $(deriveJSON (unPrefix "hd_") ''HyperData)
475 instance Hyperdata HyperData
479 ------------------------------------------------------------------------
480 -- | Then a Node can be either a Folder or a Corpus or a Document
481 data NodeType = NodeUser
483 | NodeFolderShared | NodeTeam
487 | NodeCorpus | NodeCorpusV3 | NodeTexts | NodeDocument
488 | NodeAnnuaire | NodeContact
489 | NodeGraph | NodePhylo
490 | NodeDashboard | NodeChart | NodeNoteBook
491 | NodeList | NodeListModel
493 deriving (Show, Read, Eq, Generic, Bounded, Enum)
502 allNodeTypes :: [NodeType]
503 allNodeTypes = [minBound ..]
505 instance FromJSON NodeType
506 instance ToJSON NodeType
508 instance FromHttpApiData NodeType
510 parseUrlPiece = Right . read . unpack
512 instance ToParamSchema NodeType
513 instance ToSchema NodeType
516 data NodePolySearch id typename userId
518 hyperdata search = NodeSearch { _ns_id :: id
519 , _ns_typename :: typename
520 , _ns_userId :: userId
521 -- , nodeUniqId :: shaId
522 , _ns_parentId :: parentId
526 , _ns_hyperdata :: hyperdata
527 , _ns_search :: search
528 } deriving (Show, Generic)
529 $(deriveJSON (unPrefix "_ns_") ''NodePolySearch)
530 $(makeLenses ''NodePolySearch)
532 type NodeSearch json = NodePolySearch NodeId NodeTypeId UserId (Maybe ParentId) NodeName UTCTime json (Maybe TSVector)
533 ------------------------------------------------------------------------
536 instance (Arbitrary hyperdata
538 ,Arbitrary nodeTypeId
540 ,Arbitrary nodeParentId
541 ) => Arbitrary (NodePoly nodeId nodeTypeId userId nodeParentId
542 NodeName UTCTime hyperdata) where
543 --arbitrary = Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (arbitrary) (Just "")
544 arbitrary = Node <$> arbitrary <*> arbitrary <*> arbitrary
545 <*> arbitrary <*> arbitrary <*> arbitrary
548 instance (Arbitrary hyperdata
550 ,Arbitrary nodeTypeId
552 ,Arbitrary nodeParentId
553 ) => Arbitrary (NodePolySearch nodeId nodeTypeId userId nodeParentId
554 NodeName UTCTime hyperdata (Maybe TSVector)) where
555 --arbitrary = Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (arbitrary) (Just "")
556 arbitrary = NodeSearch <$> arbitrary <*> arbitrary <*> arbitrary
557 <*> arbitrary <*> arbitrary <*> arbitrary
558 <*> arbitrary <*> arbitrary
561 ------------------------------------------------------------------------
562 hyperdataDocument :: HyperdataDocument
563 hyperdataDocument = case decode docExample of
565 Nothing -> HyperdataDocument Nothing Nothing Nothing Nothing
566 Nothing Nothing Nothing Nothing
567 Nothing Nothing Nothing Nothing
568 Nothing Nothing Nothing Nothing
569 Nothing Nothing Nothing
570 docExample :: ByteString
571 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}"
573 instance ToSchema HyperdataCorpus where
574 declareNamedSchema proxy =
575 genericDeclareNamedSchema (unPrefixSwagger "_hc_") proxy
576 & mapped.schema.description ?~ "Corpus"
577 & mapped.schema.example ?~ toJSON hyperdataCorpus
579 instance ToSchema HyperdataAnnuaire where
580 declareNamedSchema proxy =
581 genericDeclareNamedSchema (unPrefixSwagger "hyperdataAnnuaire_") proxy
582 & mapped.schema.description ?~ "an annuaire"
583 & mapped.schema.example ?~ toJSON hyperdataAnnuaire
585 instance ToSchema HyperdataDocument where
586 declareNamedSchema proxy =
587 genericDeclareNamedSchema (unPrefixSwagger "_hyperdataDocument_") proxy
588 & mapped.schema.description ?~ "a document"
589 & mapped.schema.example ?~ toJSON hyperdataDocument
591 instance ToSchema HyperdataAny where
592 declareNamedSchema proxy =
593 pure $ genericNameSchema defaultSchemaOptions proxy mempty
594 & schema.description ?~ "a node"
595 & schema.example ?~ emptyObject -- TODO
598 instance ToSchema hyperdata =>
599 ToSchema (NodePoly NodeId NodeTypeId
604 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_node_")
606 instance ToSchema hyperdata =>
607 ToSchema (NodePoly NodeId NodeTypeId
609 (Maybe ParentId) NodeName
612 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_node_")
615 instance ToSchema hyperdata =>
616 ToSchema (NodePolySearch NodeId NodeTypeId
619 UTCTime hyperdata (Maybe TSVector)
621 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_ns_")
623 instance ToSchema hyperdata =>
624 ToSchema (NodePolySearch NodeId NodeTypeId
626 (Maybe ParentId) NodeName
627 UTCTime hyperdata (Maybe TSVector)
629 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_ns_")
632 instance ToSchema Status where
633 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "status_")