]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Types/Node.hs
Use desc instead of descr and use defaultCorpus
[gargantext.git] / src / Gargantext / Database / Types / Node.hs
1 {-|
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
8 Portability : POSIX
9
10 -}
11
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
13
14 {-# LANGUAGE DeriveGeneric #-}
15 {-# LANGUAGE FlexibleInstances #-}
16 {-# LANGUAGE NoImplicitPrelude #-}
17 {-# LANGUAGE OverloadedStrings #-}
18 {-# LANGUAGE TemplateHaskell #-}
19 -- {-# LANGUAGE DuplicateRecordFields #-}
20
21 module Gargantext.Database.Types.Node where
22
23 import Prelude (Enum, Bounded, minBound, maxBound, mempty)
24
25 import GHC.Generics (Generic)
26
27 import Control.Lens hiding (elements)
28 import qualified Control.Lens as L
29 import Control.Applicative ((<*>))
30
31 import Data.Aeson
32 import Data.Aeson (Value(),toJSON)
33 import Data.Aeson.TH (deriveJSON)
34 import Data.ByteString.Lazy (ByteString)
35 import Data.Either
36 import Data.Eq (Eq)
37 import Data.Text (Text, unpack)
38 import Data.Time (UTCTime)
39 import Data.Time.Segment (jour, timesAfter, Granularity(D))
40 import Data.Swagger
41
42 import Text.Read (read)
43 import Text.Show (Show())
44
45 import Database.PostgreSQL.Simple.ToField (ToField, toField, toJSONField)
46 import Servant
47
48 import Test.QuickCheck.Arbitrary
49 import Test.QuickCheck (elements)
50
51 import Gargantext.Prelude
52 import Gargantext.Core.Utils.Prefix (unPrefix)
53
54 ------------------------------------------------------------------------
55
56 type UTCTime' = UTCTime
57
58 instance Arbitrary UTCTime' where
59 arbitrary = elements $ timesAfter 100 D (jour 2000 01 01)
60
61
62
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)
69
70 instance Arbitrary Status where
71 arbitrary = Status <$> arbitrary <*> arbitrary <*> arbitrary
72
73 ------------------------------------------------------------------------
74 data StatusV3 = StatusV3 { statusV3_error :: Maybe Text
75 , statusV3_action :: Maybe Text
76 } deriving (Show, Generic)
77 $(deriveJSON (unPrefix "statusV3_") ''StatusV3)
78
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)
100 ------------------------------------------------------------------------
101
102 data HyperdataDocument = HyperdataDocument { _hyperdataDocument_bdd :: Maybe Text
103 , _hyperdataDocument_doi :: Maybe Text
104 , _hyperdataDocument_url :: Maybe Text
105 , _hyperdataDocument_uniqId :: Maybe Text
106 , _hyperdataDocument_page :: Maybe Int
107 , _hyperdataDocument_title :: Maybe Text
108 , _hyperdataDocument_authors :: Maybe Text
109 , _hyperdataDocument_source :: Maybe Text
110 , _hyperdataDocument_abstract :: Maybe Text
111 , _hyperdataDocument_publication_date :: Maybe Text
112 , _hyperdataDocument_publication_year :: Maybe Int
113 , _hyperdataDocument_publication_month :: Maybe Int
114 , _hyperdataDocument_publication_day :: Maybe Int
115 , _hyperdataDocument_publication_hour :: Maybe Int
116 , _hyperdataDocument_publication_minute :: Maybe Int
117 , _hyperdataDocument_publication_second :: Maybe Int
118 , _hyperdataDocument_language_iso2 :: Maybe Text
119 } deriving (Show, Generic)
120 $(deriveJSON (unPrefix "_hyperdataDocument_") ''HyperdataDocument)
121 $(makeLenses ''HyperdataDocument)
122
123 instance ToField HyperdataDocument where
124 toField = toJSONField
125
126 toHyperdataDocuments :: [(Text, Text)] -> [HyperdataDocument]
127 toHyperdataDocuments ts = map (\(t1,t2) -> HyperdataDocument Nothing Nothing Nothing Nothing Nothing (Just t1)
128 Nothing (Just t2) Nothing Nothing Nothing
129 Nothing Nothing Nothing Nothing Nothing Nothing
130 ) ts
131
132 hyperdataDocuments :: [HyperdataDocument]
133 hyperdataDocuments = toHyperdataDocuments [ ("AI is big but less than crypto", "Troll System journal")
134 , ("Crypto is big but less than AI", "System Troll review" )
135 , ("Science is magic" , "Closed Source review")
136 , ("Open science for all" , "No Time" )
137 , ("Closed science for me" , "No Space" )
138 ]
139
140
141 instance Arbitrary HyperdataDocument where
142 arbitrary = elements hyperdataDocuments
143
144 ------------------------------------------------------------------------
145 data LanguageNodes = LanguageNodes { languageNodes___unknown__ :: [Int]}
146 deriving (Show, Generic)
147 $(deriveJSON (unPrefix "languageNodes_") ''LanguageNodes)
148
149 ------------------------------------------------------------------------
150 -- level: debug | dev (fatal = critical)
151 data EventLevel = CRITICAL | FATAL | ERROR | WARNING | INFO | DEBUG
152 deriving (Show, Generic, Enum, Bounded)
153
154 instance FromJSON EventLevel
155 instance ToJSON EventLevel
156
157 instance Arbitrary EventLevel where
158 arbitrary = elements [minBound..maxBound]
159
160 instance ToSchema EventLevel where
161 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
162
163 ------------------------------------------------------------------------
164
165 data Event = Event { event_level :: EventLevel
166 , event_message :: Text
167 , event_date :: UTCTime
168 } deriving (Show, Generic)
169 $(deriveJSON (unPrefix "event_") ''Event)
170
171 instance Arbitrary Event where
172 arbitrary = Event <$> arbitrary <*> arbitrary <*> arbitrary
173
174 instance ToSchema Event where
175 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
176
177 ------------------------------------------------------------------------
178
179 type Text' = Text
180
181 instance Arbitrary Text' where
182 arbitrary = elements ["ici", "la"]
183
184 data Resource = Resource { resource_path :: Maybe Text
185 , resource_scraper :: Maybe Text
186 , resource_query :: Maybe Text
187 , resource_events :: [Event]
188 , resource_status :: Status
189 , resource_date :: UTCTime'
190 } deriving (Show, Generic)
191 $(deriveJSON (unPrefix "resource_") ''Resource)
192
193 instance Arbitrary Resource where
194 arbitrary = Resource <$> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary <*> arbitrary
195
196 instance ToSchema Resource where
197 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
198
199 ------------------------------------------------------------------------
200
201 data Hyperdata a = Hyperdata { unHyperdata :: a}
202 $(deriveJSON (unPrefix "") ''Hyperdata)
203
204 data HyperdataUser = HyperdataUser { hyperdataUser_language :: Maybe Text
205 } deriving (Show, Generic)
206 $(deriveJSON (unPrefix "hyperdataUser_") ''HyperdataUser)
207
208
209 data HyperdataFolder = HyperdataFolder { hyperdataFolder_desc :: Maybe Text
210 } deriving (Show, Generic)
211 $(deriveJSON (unPrefix "hyperdataFolder_") ''HyperdataFolder)
212
213
214 data HyperdataCorpus = HyperdataCorpus { hyperdataCorpus_title :: Maybe Text
215 , hyperdataCorpus_desc :: Maybe Text
216 , hyperdataCorpus_query :: Maybe Text
217 , hyperdataCorpus_authors :: Maybe Text
218 , hyperdataCorpus_resources :: Maybe [Resource]
219 } deriving (Show, Generic)
220 $(deriveJSON (unPrefix "hyperdataCorpus_") ''HyperdataCorpus)
221
222 ------------------------------------------------------------------------
223 data HyperdataAnnuaire = HyperdataAnnuaire { hyperdataAnnuaire_title :: Maybe Text
224 , hyperdataAnnuaire_desc :: Maybe Text
225 } deriving (Show, Generic)
226 $(deriveJSON (unPrefix "hyperdataAnnuaire_") ''HyperdataAnnuaire)
227 ------------------------------------------------------------------------
228 data HyperdataContact = HyperdataContact { hyperdataContact_name :: Maybe Text
229 , hyperdataContact_mail :: Maybe Text
230 } deriving (Show, Generic)
231 $(deriveJSON (unPrefix "hyperdataContact_") ''HyperdataContact)
232 ------------------------------------------------------------------------
233
234 data HyperdataList = HyperdataList { hyperdataList_preferences :: Maybe Text
235 } deriving (Show, Generic)
236 $(deriveJSON (unPrefix "hyperdataList_") ''HyperdataList)
237
238 data HyperdataScore = HyperdataScore { hyperdataScore_preferences :: Maybe Text
239 } deriving (Show, Generic)
240 $(deriveJSON (unPrefix "hyperdataScore_") ''HyperdataScore)
241
242
243 data HyperdataResource = HyperdataResource { hyperdataResource_preferences :: Maybe Text
244 } deriving (Show, Generic)
245 $(deriveJSON (unPrefix "hyperdataResource_") ''HyperdataResource)
246
247
248
249 -- TODO add the Graph Structure here
250 data HyperdataGraph = HyperdataGraph { hyperdataGraph_preferences :: Maybe Text
251 } deriving (Show, Generic)
252 $(deriveJSON (unPrefix "hyperdataGraph_") ''HyperdataGraph)
253
254
255 -- TODO add the Graph Structure here
256 data HyperdataPhylo = HyperdataPhylo { hyperdataPhylo_preferences :: Maybe Text
257 } deriving (Show, Generic)
258 $(deriveJSON (unPrefix "hyperdataPhylo_") ''HyperdataPhylo)
259
260 -- | TODO FEATURE: Notebook saved in the node (to work with Python or Haskell)
261 data HyperdataNotebook = HyperdataNotebook { hyperdataNotebook_preferences :: Maybe Text
262 } deriving (Show, Generic)
263 $(deriveJSON (unPrefix "hyperdataNotebook_") ''HyperdataNotebook)
264
265
266
267 -- | NodePoly indicates that Node has a Polymorphism Type
268 type Node json = NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime json -- NodeVector
269
270 -- type Node json = NodePoly NodeId NodeTypeId UserId ParentId NodeName UTCTime json
271 type NodeTypeId = Int
272 type NodeId = Int
273 type NodeParentId = Int
274 type NodeUserId = Int
275 type NodeName = Text
276 --type NodeVector = Vector
277
278 --type NodeUser = Node HyperdataUser
279
280 -- | Then a Node can be either a Folder or a Corpus or a Document
281 type NodeUser = Node HyperdataUser
282 type NodeFolder = Node HyperdataFolder
283
284 type NodeCorpus = Node HyperdataCorpus
285 type NodeCorpusV3 = Node HyperdataCorpus
286 type NodeDocument = Node HyperdataDocument
287
288 type NodeAnnuaire = Node HyperdataAnnuaire
289 type NodeContact = Node HyperdataContact
290
291 ---- | Then a Node can be either a Graph or a Phylo or a Notebook
292 type NodeGraph = Node HyperdataGraph
293 type NodePhylo = Node HyperdataPhylo
294 type NodeNotebook = Node HyperdataNotebook
295
296 ------------------------------------------------------------------------
297 data NodeType = NodeUser
298 | NodeFolder
299 | NodeCorpus | NodeCorpusV3 | NodeDocument
300 | NodeAnnuaire | NodeContact
301 -- | NodeOccurrences
302 | NodeGraph
303 | NodeDashboard | NodeChart
304 -- | Classification
305 -- | Lists
306 -- | Metrics
307 deriving (Show, Read, Eq, Generic, Bounded, Enum)
308
309 allNodeTypes :: [NodeType]
310 allNodeTypes = [minBound ..]
311
312 instance FromJSON NodeType
313 instance ToJSON NodeType
314
315 instance FromHttpApiData NodeType
316 where
317 parseUrlPiece = Right . read . unpack
318
319 instance ToParamSchema NodeType
320 instance ToSchema NodeType
321
322 ------------------------------------------------------------------------
323 data NodePoly id typename userId parentId name date hyperdata = Node { _node_id :: id
324 , _node_typename :: typename
325 , _node_userId :: userId
326 -- , nodeUniqId :: hashId
327 , _node_parentId :: parentId
328 , _node_name :: name
329 , _node_date :: date
330 , _node_hyperdata :: hyperdata
331 } deriving (Show, Generic)
332 $(deriveJSON (unPrefix "_node_") ''NodePoly)
333 $(makeLenses ''NodePoly)
334
335
336
337 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime Value) where
338 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (Object mempty)]
339
340
341 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime Value) where
342 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) (Object mempty)]
343
344 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime HyperdataDocument) where
345 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) ((hyperdataDocument))]
346
347 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime HyperdataDocument) where
348 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) hyperdataDocument]
349
350 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime HyperdataCorpus) where
351 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) hyperdataCorpus]
352
353 ------------------------------------------------------------------------
354 hyperdataDocument :: HyperdataDocument
355 hyperdataDocument = case decode docExample of
356 Just hp -> hp
357 Nothing -> HyperdataDocument Nothing Nothing Nothing Nothing
358 Nothing Nothing Nothing Nothing
359 Nothing Nothing Nothing Nothing
360 Nothing Nothing Nothing Nothing
361 Nothing
362 docExample :: ByteString
363 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}"
364
365 corpusExample :: ByteString
366 corpusExample = "" -- TODO
367
368 defaultCorpus :: HyperdataCorpus
369 defaultCorpus = (HyperdataCorpus (Just "Title") (Just "Descr") (Just "Bool query") (Just "Authors") Nothing)
370
371 hyperdataCorpus :: HyperdataCorpus
372 hyperdataCorpus = case decode corpusExample of
373 Just hp -> hp
374 Nothing -> defaultCorpus
375
376 instance ToSchema HyperdataCorpus where
377 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
378 L.& mapped.schema.description ?~ "a corpus"
379 L.& mapped.schema.example ?~ toJSON hyperdataCorpus
380
381
382 instance ToSchema HyperdataDocument where
383 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
384 L.& mapped.schema.description ?~ "a document"
385 L.& mapped.schema.example ?~ toJSON hyperdataDocument
386
387
388 instance ToSchema Value where
389 declareNamedSchema proxy = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions proxy
390 L.& mapped.schema.description ?~ "a document"
391 L.& mapped.schema.example ?~ toJSON ("" :: Text) -- TODO
392
393
394 instance ToSchema (NodePoly NodeId NodeTypeId NodeUserId
395 (Maybe NodeParentId) NodeName
396 UTCTime HyperdataDocument
397 )
398
399 instance ToSchema (NodePoly NodeId NodeTypeId
400 (Maybe NodeUserId)
401 NodeParentId NodeName
402 UTCTime HyperdataDocument
403 )
404
405 instance ToSchema (NodePoly NodeId NodeTypeId
406 (Maybe NodeUserId)
407 NodeParentId NodeName
408 UTCTime HyperdataCorpus
409 )
410
411 instance ToSchema (NodePoly NodeId NodeTypeId
412 (NodeUserId)
413 (Maybe NodeParentId) NodeName
414 UTCTime HyperdataCorpus
415 )
416
417 instance ToSchema (NodePoly NodeId NodeTypeId
418 (Maybe NodeUserId)
419 NodeParentId NodeName
420 UTCTime Value
421 )
422
423 instance ToSchema (NodePoly NodeId NodeTypeId
424 (NodeUserId)
425 (Maybe NodeParentId) NodeName
426 UTCTime Value
427 )
428
429
430 instance ToSchema Status
431
432