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 DeriveGeneric #-}
15 {-# LANGUAGE FlexibleInstances #-}
16 {-# LANGUAGE NoImplicitPrelude #-}
17 {-# LANGUAGE OverloadedStrings #-}
18 {-# LANGUAGE TemplateHaskell #-}
19 -- {-# LANGUAGE DuplicateRecordFields #-}
21 module Gargantext.Database.Types.Node where
23 import Prelude (Enum, Bounded, minBound, maxBound, mempty)
25 import GHC.Generics (Generic)
27 import Control.Lens hiding (elements)
28 import qualified Control.Lens as L
29 import Control.Applicative ((<*>))
32 import Data.Aeson (Value(),toJSON)
33 import Data.Aeson.TH (deriveJSON)
34 import Data.ByteString.Lazy (ByteString)
37 import Data.Text (Text, unpack)
38 import Data.Time (UTCTime)
39 import Data.Time.Segment (jour, timesAfter, Granularity(D))
42 import Text.Read (read)
43 import Text.Show (Show())
45 import Database.PostgreSQL.Simple.ToField (ToField, toField, toJSONField)
48 import Test.QuickCheck.Arbitrary
49 import Test.QuickCheck (elements)
51 import Gargantext.Prelude
52 import Gargantext.Core.Utils.Prefix (unPrefix)
54 ------------------------------------------------------------------------
56 type UTCTime' = UTCTime
58 instance Arbitrary UTCTime' where
59 arbitrary = elements $ timesAfter 100 D (jour 2000 01 01)
63 ------------------------------------------------------------------------
64 data Status = Status { status_failed :: Int
65 , status_succeeded :: Int
66 , status_remaining :: Int
67 } deriving (Show, Generic)
68 $(deriveJSON (unPrefix "status_") ''Status)
70 instance Arbitrary Status where
71 arbitrary = Status <$> arbitrary <*> arbitrary <*> arbitrary
73 ------------------------------------------------------------------------
74 data StatusV3 = StatusV3 { statusV3_error :: Maybe Text
75 , statusV3_action :: Maybe Text
76 } deriving (Show, Generic)
77 $(deriveJSON (unPrefix "statusV3_") ''StatusV3)
79 ------------------------------------------------------------------------
80 ------------------------------------------------------------------------
81 data HyperdataDocumentV3 = HyperdataDocumentV3 { hyperdataDocumentV3_publication_day :: Maybe Int
82 , hyperdataDocumentV3_language_iso2 :: Maybe Text
83 , hyperdataDocumentV3_publication_second :: Maybe Int
84 , hyperdataDocumentV3_publication_minute :: Maybe Int
85 , hyperdataDocumentV3_publication_month :: Maybe Int
86 , hyperdataDocumentV3_publication_hour :: Maybe Int
87 , hyperdataDocumentV3_error :: Maybe Text
88 , hyperdataDocumentV3_language_iso3 :: Maybe Text
89 , hyperdataDocumentV3_authors :: Maybe Text
90 , hyperdataDocumentV3_publication_year :: Maybe Int
91 , hyperdataDocumentV3_publication_date :: Maybe Text
92 , hyperdataDocumentV3_language_name :: Maybe Text
93 , hyperdataDocumentV3_statuses :: Maybe [StatusV3]
94 , hyperdataDocumentV3_realdate_full_ :: Maybe Text
95 , hyperdataDocumentV3_source :: Maybe Text
96 , hyperdataDocumentV3_abstract :: Maybe Text
97 , hyperdataDocumentV3_title :: Maybe Text
98 } deriving (Show, Generic)
99 $(deriveJSON (unPrefix "hyperdataDocumentV3_") ''HyperdataDocumentV3)
102 ------------------------------------------------------------------------
104 data HyperdataDocument = HyperdataDocument { _hyperdataDocument_bdd :: Maybe Text
105 , _hyperdataDocument_doi :: Maybe Int
106 , _hyperdataDocument_url :: Maybe Text
107 , _hyperdataDocument_uniqId :: Maybe Text
108 , _hyperdataDocument_page :: Maybe Int
109 , _hyperdataDocument_title :: Maybe Text
110 , _hyperdataDocument_authors :: Maybe Text
111 , _hyperdataDocument_source :: Maybe Text
112 , _hyperdataDocument_abstract :: Maybe Text
113 , _hyperdataDocument_publication_date :: Maybe Text
114 , _hyperdataDocument_publication_year :: Maybe Int
115 , _hyperdataDocument_publication_month :: Maybe Int
116 , _hyperdataDocument_publication_hour :: Maybe Int
117 , _hyperdataDocument_publication_minute :: Maybe Int
118 , _hyperdataDocument_publication_second :: Maybe Int
119 , _hyperdataDocument_language_iso2 :: Maybe Text
120 , _hyperdataDocument_language_iso3 :: Maybe Text
121 } deriving (Show, Generic)
122 $(deriveJSON (unPrefix "_hyperdataDocument_") ''HyperdataDocument)
123 $(makeLenses ''HyperdataDocument)
125 instance ToField HyperdataDocument where
126 toField = toJSONField
128 toHyperdataDocuments :: [(Text, Text)] -> [HyperdataDocument]
129 toHyperdataDocuments ts = map (\(t1,t2) -> HyperdataDocument Nothing Nothing Nothing Nothing Nothing (Just t1)
130 Nothing (Just t2) Nothing Nothing Nothing
131 Nothing Nothing Nothing Nothing Nothing Nothing
134 hyperdataDocuments :: [HyperdataDocument]
135 hyperdataDocuments = toHyperdataDocuments [ ("AI is big but less than crypto", "Troll System journal")
136 , ("Crypto is big but less than AI", "System Troll review" )
137 , ("Science is magic" , "Closed Source review")
138 , ("Open science for all" , "No Time" )
139 , ("Closed science for me" , "No Space" )
143 instance Arbitrary HyperdataDocument where
144 arbitrary = elements hyperdataDocuments
146 ------------------------------------------------------------------------
147 data LanguageNodes = LanguageNodes { languageNodes___unknown__ :: [Int]}
148 deriving (Show, Generic)
149 $(deriveJSON (unPrefix "languageNodes_") ''LanguageNodes)
151 ------------------------------------------------------------------------
152 -- level: debug | dev (fatal = critical)
153 data EventLevel = CRITICAL | FATAL | ERROR | WARNING | INFO | DEBUG
154 deriving (Show, Generic, Enum, Bounded)
156 instance FromJSON EventLevel
157 instance ToJSON EventLevel
159 instance Arbitrary EventLevel where
160 arbitrary = elements [minBound..maxBound]
162 ------------------------------------------------------------------------
164 data Event = Event { event_level :: EventLevel
165 , event_message :: Text
166 , event_date :: UTCTime
167 } deriving (Show, Generic)
168 $(deriveJSON (unPrefix "event_") ''Event)
170 instance Arbitrary Event where
171 arbitrary = Event <$> arbitrary <*> arbitrary <*> arbitrary
173 ------------------------------------------------------------------------
177 instance Arbitrary Text' where
178 arbitrary = elements ["ici", "la"]
180 data Resource = Resource { resource_path :: Maybe Text
181 , resource_scraper :: Maybe Text
182 , resource_query :: Maybe Text
183 , resource_events :: [Event]
184 , resource_status :: Status
185 , resource_date :: UTCTime'
186 } deriving (Show, Generic)
187 $(deriveJSON (unPrefix "resource_") ''Resource)
189 instance Arbitrary Resource where
190 arbitrary = Resource <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
192 ------------------------------------------------------------------------
194 data HyperdataCorpus = HyperdataCorpus { hyperdataCorpus_resources :: [Resource]
195 } deriving (Show, Generic)
196 $(deriveJSON (unPrefix "hyperdataCorpus_") ''HyperdataCorpus)
199 data HyperdataUser = HyperdataUser { hyperdataUser_language :: Maybe Text
200 } deriving (Show, Generic)
201 $(deriveJSON (unPrefix "hyperdataUser_") ''HyperdataUser)
205 data HyperdataFolder = HyperdataFolder { hyperdataFolder_preferences :: Maybe Text
206 } deriving (Show, Generic)
207 $(deriveJSON (unPrefix "hyperdataFolder_") ''HyperdataFolder)
210 data HyperdataProject = HyperdataProject { hyperdataProject_preferences :: Maybe Text
211 } deriving (Show, Generic)
212 $(deriveJSON (unPrefix "hyperdataProject_") ''HyperdataProject)
216 data HyperdataList = HyperdataList { hyperdataList_preferences :: Maybe Text
217 } deriving (Show, Generic)
218 $(deriveJSON (unPrefix "hyperdataList_") ''HyperdataList)
220 data HyperdataScore = HyperdataScore { hyperdataScore_preferences :: Maybe Text
221 } deriving (Show, Generic)
222 $(deriveJSON (unPrefix "hyperdataScore_") ''HyperdataScore)
226 data HyperdataFavorites = HyperdataFavorites { hyperdataFavorites_preferences :: Maybe Text
227 } deriving (Show, Generic)
228 $(deriveJSON (unPrefix "hyperdataFavorites_") ''HyperdataFavorites)
230 data HyperdataResource = HyperdataResource { hyperdataResource_preferences :: Maybe Text
231 } deriving (Show, Generic)
232 $(deriveJSON (unPrefix "hyperdataResource_") ''HyperdataResource)
236 -- TODO add the Graph Structure here
237 data HyperdataGraph = HyperdataGraph { hyperdataGraph_preferences :: Maybe Text
238 } deriving (Show, Generic)
239 $(deriveJSON (unPrefix "hyperdataGraph_") ''HyperdataGraph)
242 -- TODO add the Graph Structure here
243 data HyperdataPhylo = HyperdataPhylo { hyperdataPhylo_preferences :: Maybe Text
244 } deriving (Show, Generic)
245 $(deriveJSON (unPrefix "hyperdataPhylo_") ''HyperdataPhylo)
247 -- | TODO FEATURE: Notebook saved in the node (to work with Python or Haskell)
248 data HyperdataNotebook = HyperdataNotebook { hyperdataNotebook_preferences :: Maybe Text
249 } deriving (Show, Generic)
250 $(deriveJSON (unPrefix "hyperdataNotebook_") ''HyperdataNotebook)
254 -- | NodePoly indicates that Node has a Polymorphism Type
255 type Node json = NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime json -- NodeVector
257 -- type Node json = NodePoly NodeId NodeTypeId UserId ParentId NodeName UTCTime json
258 type NodeTypeId = Int
260 type NodeParentId = Int
261 type NodeUserId = Int
263 --type NodeVector = Vector
265 --type NodeUser = Node HyperdataUser
267 -- | Then a Node can be either a Folder or a Corpus or a Document
268 type NodeUser = Node HyperdataUser
269 type Folder = Node HyperdataFolder
270 type Project = Node HyperdataProject
271 type NodeCorpus = Node HyperdataCorpus
272 type NodeCorpusV3 = Node HyperdataCorpus
273 type Document = Node HyperdataDocument
275 ------------------------------------------------------------------------
276 data NodeType = NodeUser
279 | NodeCorpus | NodeCorpusV3 | Annuaire
280 | Document -- | Individu
281 | UserPage | Favorites
282 | Graph | Dashboard | Chart
287 deriving (Show, Read, Eq, Generic, Bounded, Enum)
289 allNodeTypes :: [NodeType]
290 allNodeTypes = [minBound ..]
292 instance FromJSON NodeType
293 instance ToJSON NodeType
295 instance FromHttpApiData NodeType
297 parseUrlPiece = Right . read . unpack
299 instance ToParamSchema NodeType
300 instance ToSchema NodeType
302 ------------------------------------------------------------------------
303 data NodePoly id typename userId parentId name date hyperdata = Node { node_id :: id
304 , node_typename :: typename
305 , node_userId :: userId
306 -- , nodeHashId :: hashId
307 , node_parentId :: parentId
310 , node_hyperdata :: hyperdata
311 -- , node_titleAbstract :: titleAbstract
312 } deriving (Show, Generic)
313 $(deriveJSON (unPrefix "node_") ''NodePoly)
318 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime Value) where
319 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (Object mempty)]
322 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime Value) where
323 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) (Object mempty)]
325 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime HyperdataDocument) where
326 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) ((hyperdataDocument))]
329 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime HyperdataDocument) where
330 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) hyperdataDocument]
333 ------------------------------------------------------------------------
334 hyperdataDocument :: HyperdataDocument
335 hyperdataDocument = case decode docExample of
337 Nothing -> HyperdataDocument Nothing Nothing Nothing Nothing
338 Nothing Nothing Nothing Nothing
339 Nothing Nothing Nothing Nothing
340 Nothing Nothing Nothing Nothing
342 docExample :: ByteString
343 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}"
346 instance ToSchema HyperdataDocument where
347 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
348 L.& mapped.schema.description ?~ "a document"
349 L.& mapped.schema.example ?~ toJSON hyperdataDocument
352 instance ToSchema Value where
353 declareNamedSchema proxy = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions proxy
354 L.& mapped.schema.description ?~ "a document"
355 L.& mapped.schema.example ?~ toJSON ("" :: Text) -- TODO
358 instance ToSchema (NodePoly NodeId NodeTypeId NodeUserId
359 (Maybe NodeParentId) NodeName
360 UTCTime HyperdataDocument
363 instance ToSchema (NodePoly NodeId NodeTypeId
365 NodeParentId NodeName
366 UTCTime HyperdataDocument
369 instance ToSchema (NodePoly NodeId NodeTypeId
371 NodeParentId NodeName
375 instance ToSchema (NodePoly NodeId NodeTypeId
377 (Maybe NodeParentId) NodeName
382 instance ToSchema Status