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
12 {-# LANGUAGE DeriveGeneric #-}
13 {-# LANGUAGE TemplateHaskell #-}
14 {-# LANGUAGE OverloadedStrings #-}
15 {-# LANGUAGE FlexibleInstances #-}
16 -- {-# LANGUAGE DuplicateRecordFields #-}
18 module Gargantext.Types.Node where
20 import GHC.Generics (Generic)
22 import Control.Lens hiding (elements)
23 import qualified Control.Lens as L
25 import Data.Aeson (Value(),toJSON)
26 import Data.Aeson.TH (deriveJSON)
27 import Data.ByteString.Lazy (ByteString)
30 import Data.Text (Text, unpack)
31 import Data.Time (UTCTime)
32 import Data.Time.Segment (jour)
35 import Text.Read (read)
36 import Text.Show (Show())
40 import Test.QuickCheck.Arbitrary
41 import Test.QuickCheck (elements)
43 import Gargantext.Prelude
44 import Gargantext.Utils.Prefix (unPrefix)
46 ------------------------------------------------------------------------
47 data Status = Status { status_date :: Maybe UTCTime
48 , status_error :: Maybe Text
49 , status_action :: Maybe Text
50 , status_complete :: Maybe Bool
51 , status_progress :: Maybe Int
52 } deriving (Show, Generic)
53 $(deriveJSON (unPrefix "status_") ''Status)
55 instance Arbitrary Status where
56 arbitrary = elements [Status Nothing Nothing Nothing Nothing Nothing]
59 ------------------------------------------------------------------------
60 data HyperdataDocument = HyperdataDocument { hyperdataDocument_bdd :: Maybe Text
61 , hyperdataDocument_doi :: Maybe Text
62 , hyperdataDocument_url :: Maybe Text
63 , hyperdataDocument_page :: Maybe Int
64 , hyperdataDocument_title :: Maybe Text
65 , hyperdataDocument_authors :: Maybe Text
66 , hyperdataDocument_abstract :: Maybe Text
67 , hyperdataDocument_statuses :: Maybe [Status]
68 , hyperdataDocument_publication_date :: Maybe Text
69 , hyperdataDocument_publication_year :: Maybe Int
70 , hyperdataDocument_publication_month :: Maybe Int
71 , hyperdataDocument_publication_hour :: Maybe Int
72 , hyperdataDocument_publication_minute :: Maybe Int
73 , hyperdataDocument_publication_second :: Maybe Int
74 , hyperdataDocument_languageIso2 :: Maybe Text
75 } deriving (Show, Generic)
76 $(deriveJSON (unPrefix "hyperdataDocument_") ''HyperdataDocument)
78 hyperdataDocuments :: [HyperdataDocument]
79 hyperdataDocuments = [HyperdataDocument Nothing Nothing Nothing Nothing (Just "Title")
80 Nothing (Just "Abstract") Nothing Nothing
81 Nothing Nothing Nothing Nothing Nothing Nothing
85 instance Arbitrary HyperdataDocument where
86 arbitrary = elements hyperdataDocuments
88 ------------------------------------------------------------------------
89 data LanguageNodes = LanguageNodes { languageNodes___unknown__ :: [Int]}
90 deriving (Show, Generic)
91 $(deriveJSON (unPrefix "languageNodes_") ''LanguageNodes)
94 ------------------------------------------------------------------------
96 data Resource = Resource { resource_url :: Maybe Text
97 , resource_path :: Maybe Text
98 , resource_type :: Maybe Int
99 , resource_extracted :: Maybe Bool
100 } deriving (Show, Generic)
101 $(deriveJSON (unPrefix "resource_") ''Resource)
103 instance Arbitrary Resource where
104 arbitrary = elements [Resource Nothing Nothing Nothing Nothing]
106 data HyperdataCorpus = HyperdataCorpus { hyperdataCorpus_action :: Maybe Text
107 , hyperdataCorpus_statuses :: Maybe [Status]
108 , hyperdataCorpus_languages :: Maybe LanguageNodes
109 , hyperdataCorpus_resources :: Maybe [Resource]
110 , hyperdataCorpus_language_id :: Maybe Text
111 , hyperdataCorpus_skipped_docs :: Maybe [Int]
112 } deriving (Show, Generic)
113 $(deriveJSON (unPrefix "hyperdataCorpus_") ''HyperdataCorpus)
116 data HyperdataUser = HyperdataUser { hyperdataUser_language :: Maybe Text
117 } deriving (Show, Generic)
118 $(deriveJSON (unPrefix "hyperdataUser_") ''HyperdataUser)
122 data HyperdataFolder = HyperdataFolder { hyperdataFolder_preferences :: Maybe Text
123 } deriving (Show, Generic)
124 $(deriveJSON (unPrefix "hyperdataFolder_") ''HyperdataFolder)
127 data HyperdataProject = HyperdataProject { hyperdataProject_preferences :: Maybe Text
128 } deriving (Show, Generic)
129 $(deriveJSON (unPrefix "hyperdataProject_") ''HyperdataProject)
133 data HyperdataList = HyperdataList { hyperdataList_preferences :: Maybe Text
134 } deriving (Show, Generic)
135 $(deriveJSON (unPrefix "hyperdataList_") ''HyperdataList)
137 data HyperdataScore = HyperdataScore { hyperdataScore_preferences :: Maybe Text
138 } deriving (Show, Generic)
139 $(deriveJSON (unPrefix "hyperdataScore_") ''HyperdataScore)
143 data HyperdataFavorites = HyperdataFavorites { hyperdataFavorites_preferences :: Maybe Text
144 } deriving (Show, Generic)
145 $(deriveJSON (unPrefix "hyperdataFavorites_") ''HyperdataFavorites)
147 data HyperdataResource = HyperdataResource { hyperdataResource_preferences :: Maybe Text
148 } deriving (Show, Generic)
149 $(deriveJSON (unPrefix "hyperdataResource_") ''HyperdataResource)
153 -- TODO add the Graph Structure here
154 data HyperdataGraph = HyperdataGraph { hyperdataGraph_preferences :: Maybe Text
155 } deriving (Show, Generic)
156 $(deriveJSON (unPrefix "hyperdataGraph_") ''HyperdataGraph)
159 -- TODO add the Graph Structure here
160 data HyperdataPhylo = HyperdataPhylo { hyperdataPhylo_preferences :: Maybe Text
161 } deriving (Show, Generic)
162 $(deriveJSON (unPrefix "hyperdataPhylo_") ''HyperdataPhylo)
164 -- | TODO FEATURE: Notebook saved in the node (to work with Python or Haskell)
165 data HyperdataNotebook = HyperdataNotebook { hyperdataNotebook_preferences :: Maybe Text
166 } deriving (Show, Generic)
167 $(deriveJSON (unPrefix "hyperdataNotebook_") ''HyperdataNotebook)
171 -- | NodePoly indicates that Node has a Polymorphism Type
172 type Node json = NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime json -- NodeVector
174 -- type Node json = NodePoly NodeId NodeTypeId UserId ParentId NodeName UTCTime json
175 type NodeTypeId = Int
177 type NodeParentId = Int
178 type NodeUserId = Int
180 --type NodeVector = Vector
182 --type NodeUser = Node HyperdataUser
184 -- | Then a Node can be either a Folder or a Corpus or a Document
185 type NodeUser = Node HyperdataUser
186 type Folder = Node HyperdataFolder
187 type Project = Folder -- NP Node HyperdataProject ?
188 type Corpus = Node HyperdataCorpus
189 type Document = Node HyperdataDocument
191 ------------------------------------------------------------------------
192 data NodeType = NodeUser | Project | Corpus | Document | DocumentCopy
195 | Metrics | Occurrences
196 deriving (Show, Read, Eq, Generic)
198 instance FromJSON NodeType
199 instance ToJSON NodeType
201 instance FromHttpApiData NodeType
203 parseUrlPiece = Right . read . unpack
205 instance ToParamSchema NodeType
206 instance ToSchema NodeType
208 ------------------------------------------------------------------------
209 data NodePoly id typename userId parentId name date hyperdata = Node { node_id :: id
210 , node_typename :: typename
211 , node_userId :: userId
212 -- , nodeHashId :: hashId
213 , node_parentId :: parentId
216 , node_hyperdata :: hyperdata
217 -- , node_titleAbstract :: titleAbstract
218 } deriving (Show, Generic)
219 $(deriveJSON (unPrefix "node_") ''NodePoly)
221 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime Value) where
222 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) (toJSON ("{}"::Text))]
225 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime Value) where
226 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) (toJSON ("{}"::Text))]
228 instance Arbitrary (NodePoly NodeId NodeTypeId (Maybe NodeUserId) NodeParentId NodeName UTCTime HyperdataDocument) where
229 arbitrary = elements [Node 1 1 (Just 1) 1 "name" (jour 2018 01 01) ((hyperdataDocument))]
232 instance Arbitrary (NodePoly NodeId NodeTypeId NodeUserId (Maybe NodeParentId) NodeName UTCTime HyperdataDocument) where
233 arbitrary = elements [Node 1 1 1 (Just 1) "name" (jour 2018 01 01) hyperdataDocument]
236 ------------------------------------------------------------------------
237 hyperdataDocument :: HyperdataDocument
238 hyperdataDocument = case decode docExample of
240 Nothing -> HyperdataDocument Nothing Nothing Nothing Nothing
241 Nothing Nothing Nothing Nothing
242 Nothing Nothing Nothing Nothing
243 Nothing Nothing Nothing
244 docExample :: ByteString
245 docExample = "{\"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}"
248 instance ToSchema HyperdataDocument where
249 declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy
250 L.& mapped.schema.description ?~ "a document"
251 L.& mapped.schema.example ?~ toJSON hyperdataDocument
254 instance ToSchema (NodePoly NodeId NodeTypeId NodeUserId
255 (Maybe NodeParentId) NodeName
256 UTCTime HyperdataDocument
259 instance ToSchema (NodePoly NodeId NodeTypeId
261 NodeParentId NodeName
262 UTCTime HyperdataDocument
265 instance ToSchema Status