2 Module : Gargantext.Viz.Phylo.API
3 Description : Phylo API
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 RankNTypes #-}
15 {-# LANGUAGE DataKinds #-}
16 {-# LANGUAGE NoImplicitPrelude #-}
17 {-# LANGUAGE OverloadedStrings #-} -- allows to write Text literals
18 {-# LANGUAGE OverloadedLists #-} -- allows to write Map and HashMap as lists
19 {-# LANGUAGE TypeOperators #-}
20 {-# LANGUAGE FlexibleInstances #-}
21 {-# LANGUAGE MultiParamTypeClasses #-}
23 module Gargantext.Viz.Phylo.API
26 import Data.String.Conversions
27 --import Control.Monad.Reader (ask)
28 import qualified Data.ByteString as DB
29 import qualified Data.ByteString.Lazy as DBL
31 import Gargantext.API.Types
32 import Gargantext.API.Utils (swaggerOptions)
33 import Gargantext.Database.Types.Node (PhyloId, ListId, CorpusId)
34 import Gargantext.Database.Schema.Node (insertNodes, nodePhyloW, getNodePhylo)
35 import Gargantext.Database.Types.Node -- (NodePhylo(..))
36 import Gargantext.Prelude
37 import Gargantext.Viz.Phylo
38 import Gargantext.Viz.Phylo.Main
39 import Gargantext.Viz.Phylo.Example
40 import Gargantext.API.Ngrams (TODO(..))
42 import Test.QuickCheck (elements)
43 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
44 import Web.HttpApiData (parseUrlPiece, readTextData)
45 import Control.Monad.IO.Class (liftIO)
46 import Network.HTTP.Media ((//), (/:))
48 ------------------------------------------------------------------------
49 type PhyloAPI = Summary "Phylo API"
55 phyloAPI :: PhyloId -> UserId -> GargServer PhyloAPI
56 phyloAPI n u = getPhylo n
61 newtype SVG = SVG DB.ByteString
65 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
67 instance Show SVG where
70 instance Accept SVG where
71 contentType _ = "SVG" // "image/svg+xml" /: ("charset", "utf-8")
73 instance Show a => MimeRender PlainText a where
74 mimeRender _ val = cs ("" <> show val)
76 instance MimeRender SVG SVG where
77 mimeRender _ (SVG s) = DBL.fromStrict s
79 ------------------------------------------------------------------------
80 type GetPhylo = QueryParam "listId" ListId
81 :> QueryParam "level" Level
82 :> QueryParam "minSizeBranch" MinSizeBranch
83 {- :> QueryParam "filiation" Filiation
84 :> QueryParam "childs" Bool
85 :> QueryParam "depth" Level
86 :> QueryParam "metrics" [Metric]
87 :> QueryParam "periodsInf" Int
88 :> QueryParam "periodsSup" Int
89 :> QueryParam "minNodes" Int
90 :> QueryParam "taggers" [Tagger]
91 :> QueryParam "sort" Sort
92 :> QueryParam "order" Order
93 :> QueryParam "export" ExportMode
94 :> QueryParam "display" DisplayMode
95 :> QueryParam "verbose" Bool
100 -- Add real text processing
101 -- Fix Filter parameters
102 getPhylo :: PhyloId -> GargServer GetPhylo
103 --getPhylo phId _lId l msb _f _b _l' _ms _x _y _z _ts _s _o _e _d _b' = do
104 getPhylo phId _lId l msb = do
105 phNode <- getNodePhylo phId
107 level = maybe 2 identity l
108 branc = maybe 2 identity msb
109 maybePhylo = hyperdataPhylo_data $ _node_hyperdata phNode
111 p <- liftIO $ viewPhylo2Svg $ viewPhylo level branc $ maybe phyloFromQuery identity maybePhylo
113 ------------------------------------------------------------------------
114 type PostPhylo = QueryParam "listId" ListId
115 -- :> ReqBody '[JSON] PhyloQueryBuild
116 :> (Post '[JSON] NodeId)
118 postPhylo :: CorpusId -> UserId -> GargServer PostPhylo
119 postPhylo n userId _lId = do
120 -- TODO get Reader settings
123 -- _vrs = Just ("1" :: Text)
124 -- _sft = Just (Software "Gargantext" "4")
125 -- _prm = initPhyloParam vrs sft (Just q)
127 pId <- insertNodes [nodePhyloW (Just "Phylo") (Just $ HyperdataPhylo Nothing (Just phy)) n userId]
128 pure $ NodeId (fromIntegral pId)
130 ------------------------------------------------------------------------
131 -- | DELETE Phylo == delete a node
132 ------------------------------------------------------------------------
133 ------------------------------------------------------------------------
135 type PutPhylo = (Put '[JSON] Phylo )
136 --putPhylo :: PhyloId -> Maybe ListId -> PhyloQueryBuild -> Phylo
137 putPhylo :: PhyloId -> GargServer PutPhylo
143 instance Arbitrary PhyloView
145 arbitrary = elements [phyloView]
147 -- | TODO add phyloGroup ex
148 instance Arbitrary PhyloGroup
150 arbitrary = elements []
152 instance Arbitrary Phylo
154 arbitrary = elements [phylo]
156 instance ToSchema Cluster
157 instance ToSchema EdgeType
158 instance ToSchema Filiation
159 instance ToSchema Filter
160 instance ToSchema FisParams
161 instance ToSchema HammingParams
162 instance ToSchema LouvainParams
163 instance ToSchema Metric
164 instance ToSchema Order
165 instance ToSchema Phylo
166 instance ToSchema PhyloFis
167 instance ToSchema PhyloBranch
168 instance ToSchema PhyloEdge
169 instance ToSchema PhyloGroup
170 instance ToSchema PhyloLevel
171 instance ToSchema PhyloNode
172 instance ToSchema PhyloParam
173 instance ToSchema PhyloFoundations
174 instance ToSchema PhyloPeriod
175 instance ToSchema PhyloQueryBuild
176 instance ToSchema PhyloView
177 instance ToSchema RCParams
178 instance ToSchema LBParams
179 instance ToSchema SBParams
180 instance ToSchema Software
181 instance ToSchema WLJParams
184 instance ToParamSchema Order
185 instance FromHttpApiData Order
187 parseUrlPiece = readTextData
190 instance ToParamSchema Metric
191 instance FromHttpApiData [Metric]
193 parseUrlPiece = readTextData
194 instance FromHttpApiData Metric
196 parseUrlPiece = readTextData
199 instance ToParamSchema DisplayMode
200 instance FromHttpApiData DisplayMode
202 parseUrlPiece = readTextData
205 instance ToParamSchema ExportMode
206 instance FromHttpApiData ExportMode
208 parseUrlPiece = readTextData
211 instance FromHttpApiData Sort
213 parseUrlPiece = readTextData
214 instance ToParamSchema Sort
217 instance ToSchema Proximity
219 declareNamedSchema = genericDeclareNamedSchemaUnrestricted
223 instance FromHttpApiData [Tagger]
225 parseUrlPiece = readTextData
226 instance FromHttpApiData Tagger
228 parseUrlPiece = readTextData
229 instance ToParamSchema Tagger
231 instance FromHttpApiData Filiation
233 parseUrlPiece = readTextData
234 instance ToParamSchema Filiation