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 DuplicateRecordFields #-}
23 module Gargantext.Database.Types.Node where
25 import Prelude (Enum, Bounded, minBound, maxBound)
27 import GHC.Generics (Generic)
29 import Control.Lens hiding (elements)
30 import qualified Control.Lens as L
31 import Control.Applicative ((<*>))
34 import Data.Aeson.Types (emptyObject)
35 import Data.Aeson (Object, toJSON)
36 import Data.Aeson.TH (deriveJSON)
37 import Data.ByteString.Lazy (ByteString)
40 import Data.Monoid (mempty)
41 import Data.Text (Text, unpack)
42 import Data.Time (UTCTime)
43 import Data.Time.Segment (jour, timesAfter, Granularity(D))
46 import Text.Read (read)
47 import Text.Show (Show())
49 import Database.PostgreSQL.Simple.ToField (ToField, toField, toJSONField)
52 import Test.QuickCheck.Arbitrary
53 import Test.QuickCheck (elements)
55 import Gargantext.Prelude
56 import Gargantext.Core.Utils.Prefix (unPrefix)
58 ------------------------------------------------------------------------
60 type UTCTime' = UTCTime
62 instance Arbitrary UTCTime' where
63 arbitrary = elements $ timesAfter 100 D (jour 2000 01 01)
67 ------------------------------------------------------------------------
68 data Status = Status { status_failed :: Int
69 , status_succeeded :: Int
70 , status_remaining :: Int
71 } deriving (Show, Generic)
72 $(deriveJSON (unPrefix "status_") ''Status)
74 instance Arbitrary Status where
75 arbitrary = Status <$> arbitrary <*> arbitrary <*> arbitrary
77 ------------------------------------------------------------------------
78 data StatusV3 = StatusV3 { statusV3_error :: Maybe Text
79 , statusV3_action :: Maybe Text
80 } deriving (Show, Generic)
81 $(deriveJSON (unPrefix "statusV3_") ''StatusV3)
82 ------------------------------------------------------------------------
84 -- Only Hyperdata types should be member of this type class.
87 ------------------------------------------------------------------------
88 data HyperdataDocumentV3 = HyperdataDocumentV3 { hyperdataDocumentV3_publication_day :: !(Maybe Int)
89 , hyperdataDocumentV3_language_iso2 :: !(Maybe Text)
90 , hyperdataDocumentV3_publication_second :: !(Maybe Int)
91 , hyperdataDocumentV3_publication_minute :: !(Maybe Int)
92 , hyperdataDocumentV3_publication_month :: !(Maybe Int)
93 , hyperdataDocumentV3_publication_hour :: !(Maybe Int)
94 , hyperdataDocumentV3_error :: !(Maybe Text)
95 , hyperdataDocumentV3_language_iso3 :: !(Maybe Text)
96 , hyperdataDocumentV3_authors :: !(Maybe Text)
97 , hyperdataDocumentV3_publication_year :: !(Maybe Int)
98 , hyperdataDocumentV3_publication_date :: !(Maybe Text)
99 , hyperdataDocumentV3_language_name :: !(Maybe Text)
100 , hyperdataDocumentV3_statuses :: !(Maybe [StatusV3])
101 , hyperdataDocumentV3_realdate_full_ :: !(Maybe Text)
102 , hyperdataDocumentV3_source :: !(Maybe Text)
103 , hyperdataDocumentV3_abstract :: !(Maybe Text)
104 , hyperdataDocumentV3_title :: !(Maybe Text)
105 } deriving (Show, Generic)
106 $(deriveJSON (unPrefix "hyperdataDocumentV3_") ''HyperdataDocumentV3)
108 instance Hyperdata HyperdataDocumentV3
109 ------------------------------------------------------------------------
111 data HyperdataDocument = HyperdataDocument { _hyperdataDocument_bdd :: Maybe Text
112 , _hyperdataDocument_doi :: Maybe Text
113 , _hyperdataDocument_url :: Maybe Text
114 , _hyperdataDocument_uniqId :: Maybe Text
115 , _hyperdataDocument_uniqIdBdd :: Maybe Text
116 , _hyperdataDocument_page :: Maybe Int
117 , _hyperdataDocument_title :: Maybe Text
118 , _hyperdataDocument_authors :: Maybe Text
119 , _hyperdataDocument_source :: Maybe Text
120 , _hyperdataDocument_abstract :: Maybe Text
121 , _hyperdataDocument_publication_date :: Maybe Text
122 , _hyperdataDocument_publication_year :: Maybe Int
123 , _hyperdataDocument_publication_month :: Maybe Int
124 , _hyperdataDocument_publication_day :: Maybe Int
125 , _hyperdataDocument_publication_hour :: Maybe Int
126 , _hyperdataDocument_publication_minute :: Maybe Int
127 , _hyperdataDocument_publication_second :: Maybe Int
128 , _hyperdataDocument_language_iso2 :: Maybe Text
129 } deriving (Show, Generic)
130 $(deriveJSON (unPrefix "_hyperdataDocument_") ''HyperdataDocument)
131 $(makeLenses ''HyperdataDocument)
133 instance Hyperdata HyperdataDocument
135 instance ToField HyperdataDocument where
136 toField = toJSONField
138 toHyperdataDocuments :: [(Text, Text)] -> [HyperdataDocument]
139 toHyperdataDocuments ts = map (\(t1,t2) -> HyperdataDocument Nothing Nothing Nothing Nothing Nothing Nothing (Just t1)
140 Nothing (Just t2) Nothing Nothing Nothing Nothing Nothing
141 Nothing Nothing Nothing Nothing
144 hyperdataDocuments :: [HyperdataDocument]
145 hyperdataDocuments = toHyperdataDocuments [ ("AI is big but less than crypto", "Troll System journal")
146 , ("Crypto is big but less than AI", "System Troll review" )
147 , ("Science is magic" , "Closed Source review")
148 , ("Open science for all" , "No Time" )
149 , ("Closed science for me" , "No Space" )
153 instance Arbitrary HyperdataDocument where
154 arbitrary = elements hyperdataDocuments
156 ------------------------------------------------------------------------
157 data LanguageNodes = LanguageNodes { languageNodes___unknown__ :: [Int]}
158 deriving (Show, Generic)
159 $(deriveJSON (unPrefix "languageNodes_") ''LanguageNodes)
161 ------------------------------------------------------------------------
162 -- level: debug | dev (fatal = critical)
163 data EventLevel = CRITICAL | FATAL | ERROR | WARNING | INFO | DEBUG
164 deriving (Show, Generic, Enum, Bounded)
166 instance FromJSON EventLevel
167 instance ToJSON EventLevel
169 instance Arbitrary EventLevel where
170 arbitrary = elements [minBound..maxBound]
172 instance ToSchema EventLevel where
173 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
175 ------------------------------------------------------------------------
177 data Event = Event { event_level :: EventLevel
178 , event_message :: Text
179 , event_date :: UTCTime
180 } deriving (Show, Generic)
181 $(deriveJSON (unPrefix "event_") ''Event)
183 instance Arbitrary Event where
184 arbitrary = Event <$> arbitrary <*> arbitrary <*> arbitrary
186 instance ToSchema Event where
187 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
189 ------------------------------------------------------------------------
193 instance Arbitrary Text' where
194 arbitrary = elements ["ici", "la"]
196 data Resource = Resource { resource_path :: Maybe Text
197 , resource_scraper :: Maybe Text
198 , resource_query :: Maybe Text
199 , resource_events :: [Event]
200 , resource_status :: Status
201 , resource_date :: UTCTime'
202 } deriving (Show, Generic)
203 $(deriveJSON (unPrefix "resource_") ''Resource)
205 instance Arbitrary Resource where
206 arbitrary = Resource <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
208 instance ToSchema Resource where
209 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
211 ------------------------------------------------------------------------
213 data HyperdataUser = HyperdataUser { hyperdataUser_language :: Maybe Text
214 } deriving (Show, Generic)
215 $(deriveJSON (unPrefix "hyperdataUser_") ''HyperdataUser)
217 instance Hyperdata HyperdataUser
219 data HyperdataFolder = HyperdataFolder { hyperdataFolder_desc :: Maybe Text
220 } deriving (Show, Generic)
221 $(deriveJSON (unPrefix "hyperdataFolder_") ''HyperdataFolder)
223 instance Hyperdata HyperdataFolder
225 data HyperdataCorpus = HyperdataCorpus { hyperdataCorpus_title :: Maybe Text
226 , hyperdataCorpus_desc :: Maybe Text
227 , hyperdataCorpus_query :: Maybe Text
228 , hyperdataCorpus_authors :: Maybe Text
229 , hyperdataCorpus_resources :: Maybe [Resource]
230 } deriving (Show, Generic)
231 $(deriveJSON (unPrefix "hyperdataCorpus_") ''HyperdataCorpus)
233 instance Hyperdata HyperdataCorpus
235 corpusExample :: ByteString
236 corpusExample = "" -- TODO
238 defaultCorpus :: HyperdataCorpus
239 defaultCorpus = (HyperdataCorpus (Just "Title") (Just "Descr") (Just "Bool query") (Just "Authors") Nothing)
241 hyperdataCorpus :: HyperdataCorpus
242 hyperdataCorpus = case decode corpusExample of
244 Nothing -> defaultCorpus
246 instance Arbitrary HyperdataCorpus where
247 arbitrary = pure hyperdataCorpus -- TODO
249 ------------------------------------------------------------------------
250 data HyperdataAnnuaire = HyperdataAnnuaire { hyperdataAnnuaire_title :: Maybe Text
251 , hyperdataAnnuaire_desc :: Maybe Text
252 } deriving (Show, Generic)
253 $(deriveJSON (unPrefix "hyperdataAnnuaire_") ''HyperdataAnnuaire)
255 instance Hyperdata HyperdataAnnuaire
257 hyperdataAnnuaire :: HyperdataAnnuaire
258 hyperdataAnnuaire = HyperdataAnnuaire (Just "Annuaire Title") (Just "Annuaire Description")
260 instance Arbitrary HyperdataAnnuaire where
261 arbitrary = pure hyperdataAnnuaire -- TODO
263 ------------------------------------------------------------------------
264 data HyperdataContact = HyperdataContact { hyperdataContact_name :: Maybe Text
265 , hyperdataContact_mail :: Maybe Text
266 } deriving (Show, Generic)
267 $(deriveJSON (unPrefix "hyperdataContact_") ''HyperdataContact)
269 instance Hyperdata HyperdataContact
270 ------------------------------------------------------------------------
271 newtype HyperdataAny = HyperdataAny Object
272 deriving (Show, Generic, ToJSON, FromJSON)
274 instance Hyperdata HyperdataAny
276 instance Arbitrary HyperdataAny where
277 arbitrary = pure $ HyperdataAny mempty -- TODO produce arbitrary objects
278 ------------------------------------------------------------------------
280 data HyperdataList = HyperdataList { hyperdataList_preferences :: Maybe Text
281 } deriving (Show, Generic)
282 $(deriveJSON (unPrefix "hyperdataList_") ''HyperdataList)
284 instance Hyperdata HyperdataList
286 data HyperdataScore = HyperdataScore { hyperdataScore_preferences :: Maybe Text
287 } deriving (Show, Generic)
288 $(deriveJSON (unPrefix "hyperdataScore_") ''HyperdataScore)
290 instance Hyperdata HyperdataScore
293 data HyperdataResource = HyperdataResource { hyperdataResource_preferences :: Maybe Text
294 } deriving (Show, Generic)
295 $(deriveJSON (unPrefix "hyperdataResource_") ''HyperdataResource)
297 instance Hyperdata HyperdataResource
300 -- TODO add the Graph Structure here
301 data HyperdataGraph = HyperdataGraph { hyperdataGraph_preferences :: Maybe Text
302 } deriving (Show, Generic)
303 $(deriveJSON (unPrefix "hyperdataGraph_") ''HyperdataGraph)
305 instance Hyperdata HyperdataGraph
307 -- TODO add the Graph Structure here
308 data HyperdataPhylo = HyperdataPhylo { hyperdataPhylo_preferences :: Maybe Text
309 } deriving (Show, Generic)
310 $(deriveJSON (unPrefix "hyperdataPhylo_") ''HyperdataPhylo)
312 instance Hyperdata HyperdataPhylo
314 -- | TODO FEATURE: Notebook saved in the node (to work with Python or Haskell)
315 data HyperdataNotebook = HyperdataNotebook { hyperdataNotebook_preferences :: Maybe Text
316 } deriving (Show, Generic)
317 $(deriveJSON (unPrefix "hyperdataNotebook_") ''HyperdataNotebook)
319 instance Hyperdata HyperdataNotebook
323 -- | NodePoly indicates that Node has a Polymorphism Type
324 type Node json = NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime json -- NodeVector
326 -- type Node json = NodePoly NodeId NodeTypeId UserId ParentId NodeName UTCTime json
327 type NodeTypeId = Int
329 type NodeParentId = Int
330 type NodeUserId = Int
332 --type NodeVector = Vector
334 --type NodeUser = Node HyperdataUser
336 type NodeAny = Node HyperdataAny
338 -- | Then a Node can be either a Folder or a Corpus or a Document
339 type NodeUser = Node HyperdataUser
340 type NodeFolder = Node HyperdataFolder
342 type NodeCorpus = Node HyperdataCorpus
343 type NodeCorpusV3 = Node HyperdataCorpus
344 type NodeDocument = Node HyperdataDocument
346 type NodeAnnuaire = Node HyperdataAnnuaire
347 type NodeContact = Node HyperdataContact
349 ---- | Then a Node can be either a Graph or a Phylo or a Notebook
350 type NodeGraph = Node HyperdataGraph
351 type NodePhylo = Node HyperdataPhylo
352 type NodeNotebook = Node HyperdataNotebook
354 ------------------------------------------------------------------------
355 data NodeType = NodeUser
357 | NodeCorpus | NodeCorpusV3 | NodeDocument
358 | NodeAnnuaire | NodeContact
361 | NodeDashboard | NodeChart
365 deriving (Show, Read, Eq, Generic, Bounded, Enum)
367 allNodeTypes :: [NodeType]
368 allNodeTypes = [minBound ..]
370 instance FromJSON NodeType
371 instance ToJSON NodeType
373 instance FromHttpApiData NodeType
375 parseUrlPiece = Right . read . unpack
377 instance ToParamSchema NodeType
378 instance ToSchema NodeType
380 ------------------------------------------------------------------------
381 data NodePoly id typename userId parentId name date hyperdata = Node { _node_id :: id
382 , _node_typename :: typename
383 , _node_userId :: userId
384 -- , nodeUniqId :: hashId
385 , _node_parentId :: parentId
388 , _node_hyperdata :: hyperdata
389 } deriving (Show, Generic)
390 $(deriveJSON (unPrefix "_node_") ''NodePoly)
391 $(makeLenses ''NodePoly)
393 instance Arbitrary hyperdata => Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime hyperdata) where
394 arbitrary = Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) <$> arbitrary
396 instance Arbitrary hyperdata => Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime hyperdata) where
397 arbitrary = Node 1 1 1 (Just 1) "name" (jour 2018 01 01) <$> arbitrary
399 ------------------------------------------------------------------------
400 hyperdataDocument :: HyperdataDocument
401 hyperdataDocument = case decode docExample of
403 Nothing -> HyperdataDocument Nothing Nothing Nothing Nothing
404 Nothing Nothing Nothing Nothing
405 Nothing Nothing Nothing Nothing
406 Nothing Nothing Nothing Nothing
408 docExample :: ByteString
409 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}"
411 instance ToSchema HyperdataCorpus where
412 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
413 L.& mapped.schema.description ?~ "a corpus"
414 L.& mapped.schema.example ?~ toJSON hyperdataCorpus
417 instance ToSchema HyperdataAnnuaire where
418 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
419 L.& mapped.schema.description ?~ "an annuaire"
420 L.& mapped.schema.example ?~ toJSON hyperdataAnnuaire
423 instance ToSchema HyperdataDocument where
424 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
425 L.& mapped.schema.description ?~ "a document"
426 L.& mapped.schema.example ?~ toJSON hyperdataDocument
429 instance ToSchema HyperdataAny where
430 declareNamedSchema proxy =
431 pure $ genericNameSchema defaultSchemaOptions proxy mempty
432 L.& schema.description ?~ "a node"
433 L.& schema.example ?~ emptyObject -- TODO
436 instance ToSchema hyperdata =>
437 ToSchema (NodePoly NodeId NodeTypeId
439 NodeParentId NodeName
443 instance ToSchema hyperdata =>
444 ToSchema (NodePoly NodeId NodeTypeId
446 (Maybe NodeParentId) NodeName
452 instance ToSchema Status