]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Types/Node.hs
[SPECS/DOC] Adding Swagger Documentation, first draft POC.
[gargantext.git] / src / Gargantext / Types / Node.hs
1 {-|
2 Module : Gargantext.Types.Nodes
3 Description : Main Types of Nodes
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 {-# LANGUAGE DeriveGeneric #-}
13 {-# LANGUAGE TemplateHaskell #-}
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE FlexibleInstances #-}
16 -- {-# LANGUAGE DuplicateRecordFields #-}
17
18 module Gargantext.Types.Node where
19
20 import GHC.Generics (Generic)
21
22 import Data.Aeson
23 import Data.Aeson (Value(),toJSON)
24 import Data.Aeson.TH (deriveJSON)
25 import Data.Either
26 import Data.Eq (Eq)
27 import Data.Text (Text, unpack)
28 import Data.Time (UTCTime)
29 import Data.Time.Segment (jour)
30 import Data.Swagger
31 import Data.Maybe (fromJust)
32
33 import Text.Read (read)
34 import Text.Show (Show())
35
36 import Servant
37
38 import Test.QuickCheck.Arbitrary
39 import Test.QuickCheck (elements)
40
41 import Gargantext.Prelude
42 import Gargantext.Utils.Prefix (unPrefix)
43
44 ------------------------------------------------------------------------
45 data Status = Status { status_date :: Maybe UTCTime
46 , status_error :: Maybe Text
47 , status_action :: Maybe Text
48 , status_complete :: Maybe Bool
49 , status_progress :: Maybe Int
50 } deriving (Show, Generic)
51 $(deriveJSON (unPrefix "status_") ''Status)
52
53 instance Arbitrary Status where
54 arbitrary = elements [Status Nothing Nothing Nothing Nothing Nothing]
55
56
57 ------------------------------------------------------------------------
58 data HyperdataDocument = HyperdataDocument { hyperdataDocument_bdd :: Maybe Text
59 , hyperdataDocument_doi :: Maybe Text
60 , hyperdataDocument_url :: Maybe Text
61 , hyperdataDocument_page :: Maybe Int
62 , hyperdataDocument_title :: Maybe Text
63 , hyperdataDocument_authors :: Maybe Text
64 , hyperdataDocument_abstract :: Maybe Text
65 , hyperdataDocument_statuses :: Maybe [Status]
66 , hyperdataDocument_publication_date :: Maybe Text
67 , hyperdataDocument_publication_year :: Maybe Double
68 , hyperdataDocument_publication_month :: Maybe Double
69 , hyperdataDocument_publication_hour :: Maybe Double
70 , hyperdataDocument_publication_minute :: Maybe Double
71 , hyperdataDocument_publication_second :: Maybe Double
72 , hyperdataDocument_languageIso2 :: Maybe Text
73 } deriving (Show, Generic)
74 $(deriveJSON (unPrefix "hyperdataDocument_") ''HyperdataDocument)
75
76 hyperdataDocuments :: [HyperdataDocument]
77 hyperdataDocuments = [HyperdataDocument Nothing Nothing Nothing Nothing (Just "Title")
78 Nothing (Just "Abstract") Nothing Nothing
79 Nothing Nothing Nothing Nothing Nothing Nothing
80 ]
81
82
83 instance Arbitrary HyperdataDocument where
84 arbitrary = elements hyperdataDocuments
85
86 ------------------------------------------------------------------------
87 data LanguageNodes = LanguageNodes { languageNodes___unknown__ :: [Int]}
88 deriving (Show, Generic)
89 $(deriveJSON (unPrefix "languageNodes_") ''LanguageNodes)
90
91
92 ------------------------------------------------------------------------
93
94 data Resource = Resource { resource_Url :: Maybe Text
95 , resource_Path :: Maybe Text
96 , resource_Type :: Maybe Int
97 , resource_Extracted :: Maybe Bool
98 } deriving (Show, Generic)
99 $(deriveJSON (unPrefix "resource_") ''Resource)
100
101 instance Arbitrary Resource where
102 arbitrary = elements [Resource Nothing Nothing Nothing Nothing]
103
104 data HyperdataCorpus = HyperdataCorpus { hyperdataCorpus_Action :: Maybe Text
105 , hyperdataCorpus_Statuses :: Maybe [Status]
106 , hyperdataCorpus_Languages :: Maybe LanguageNodes
107 , hyperdataCorpus_Resources :: Maybe [Resource]
108 , hyperdataCorpus_Language_id :: Maybe Text
109 , hyperdataCorpus_Skipped_docs :: Maybe [Int]
110 } deriving (Show, Generic)
111 $(deriveJSON (unPrefix "hyperdataCorpus_") ''HyperdataCorpus)
112
113
114 data HyperdataUser = HyperdataUser { hyperdataUser_language :: Maybe Text
115 } deriving (Show, Generic)
116 $(deriveJSON (unPrefix "hyperdataUser_") ''HyperdataUser)
117
118 -- Preferences ?
119
120 data HyperdataFolder = HyperdataFolder { hyperdataFolder_Preferences :: Maybe Text
121 } deriving (Show, Generic)
122 $(deriveJSON (unPrefix "hyperdataFolder_") ''HyperdataFolder)
123
124
125 data HyperdataProject = HyperdataProject { hyperdataProject_Preferences :: Maybe Text
126 } deriving (Show, Generic)
127 $(deriveJSON (unPrefix "hyperdataProject_") ''HyperdataProject)
128
129
130
131 data HyperdataList = HyperdataList { hyperdataList_Preferences :: Maybe Text
132 } deriving (Show, Generic)
133 $(deriveJSON (unPrefix "hyperdataList_") ''HyperdataList)
134
135 data HyperdataScore = HyperdataScore { hyperdataScore_Preferences :: Maybe Text
136 } deriving (Show, Generic)
137 $(deriveJSON (unPrefix "hyperdataScore_") ''HyperdataScore)
138
139
140
141 data HyperdataFavorites = HyperdataFavorites { hyperdataFavorites_Preferences :: Maybe Text
142 } deriving (Show, Generic)
143 $(deriveJSON (unPrefix "hyperdataFavorites_") ''HyperdataFavorites)
144
145 data HyperdataResource = HyperdataResource { hyperdataResource_Preferences :: Maybe Text
146 } deriving (Show, Generic)
147 $(deriveJSON (unPrefix "hyperdataResource_") ''HyperdataResource)
148
149
150
151 -- TODO add the Graph Structure here
152 data HyperdataGraph = HyperdataGraph { hyperdataGraph_Preferences :: Maybe Text
153 } deriving (Show, Generic)
154 $(deriveJSON (unPrefix "hyperdataGraph_") ''HyperdataGraph)
155
156
157 -- TODO add the Graph Structure here
158 data HyperdataPhylo = HyperdataPhylo { hyperdataPhylo_Preferences :: Maybe Text
159 } deriving (Show, Generic)
160 $(deriveJSON (unPrefix "hyperdataPhylo_") ''HyperdataPhylo)
161
162 -- | TODO FEATURE: Notebook saved in the node (to work with Python or Haskell)
163 data HyperdataNotebook = HyperdataNotebook { hyperdataNotebook_Preferences :: Maybe Text
164 } deriving (Show, Generic)
165 $(deriveJSON (unPrefix "hyperdataNotebook_") ''HyperdataNotebook)
166
167
168
169 -- | NodePoly indicates that Node has a Polymorphism Type
170 type Node json = NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime json -- NodeVector
171
172 -- type Node json = NodePoly NodeId NodeTypeId UserId ParentId NodeName UTCTime json
173 type NodeTypeId = Int
174 type NodeId = Int
175 type NodeParentId = Int
176 type NodeUserId = Int
177 type NodeName = Text
178 --type NodeVector = Vector
179
180 --type NodeUser = Node HyperdataUser
181
182 -- | Then a Node can be either a Folder or a Corpus or a Document
183 type NodeUser = Node HyperdataUser
184 type Folder = Node HyperdataFolder
185 type Project = Folder -- NP Node HyperdataProject ?
186 type Corpus = Node HyperdataCorpus
187 type Document = Node HyperdataDocument
188
189 ------------------------------------------------------------------------
190 data NodeType = NodeUser | Project | Corpus | Document | DocumentCopy
191 | Classification
192 | Lists
193 | Metrics | Occurrences
194 deriving (Show, Read, Eq, Generic)
195
196 instance FromJSON NodeType
197 instance ToJSON NodeType
198
199 instance FromHttpApiData NodeType
200 where
201 parseUrlPiece = Right . read . unpack
202
203 instance ToParamSchema NodeType
204 instance ToSchema NodeType
205
206 ------------------------------------------------------------------------
207 data NodePoly id typename userId parentId name date hyperdata = Node { node_id :: id
208 , node_typename :: typename
209 , node_userId :: userId
210 -- , nodeHashId :: hashId
211 , node_parentId :: parentId
212 , node_name :: name
213 , node_date :: date
214 , node_hyperdata :: hyperdata
215 -- , node_titleAbstract :: titleAbstract
216 } deriving (Show, Generic)
217 $(deriveJSON (unPrefix "node_") ''NodePoly)
218
219 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime Value) where
220 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (toJSON ("{}"::Text))]
221
222
223 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime Value) where
224 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) (toJSON ("{}"::Text))]
225
226 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime HyperdataDocument) where
227 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) ((hyperdataDocument))]
228
229
230 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime HyperdataDocument) where
231 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) hyperdataDocument]
232
233 hyperdataDocument :: HyperdataDocument
234 hyperdataDocument = fromJust $ decode "{\"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\",\"statuses\":[],\"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}"
235
236
237 instance ToSchema HyperdataDocument where
238 declareNamedSchema = genericDeclareNamedSchemaUnrestricted defaultSchemaOptions
239
240 instance ToSchema (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime HyperdataDocument)
241 instance ToSchema (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime HyperdataDocument)
242
243 instance ToSchema Status
244
245