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 Control.Monad.Reader (ask)
27 import qualified Data.ByteString as DB
28 import qualified Data.ByteString.Lazy.Char8 as DBL (pack)
29 import Data.Text (Text)
30 import Data.Map (empty)
32 import Gargantext.API.Types
33 import Gargantext.Database.Types.Node (PhyloId, ListId, CorpusId)
34 import Gargantext.Prelude
35 import Gargantext.Viz.Phylo
36 import Gargantext.Viz.Phylo.Main
37 import Gargantext.Viz.Phylo.Aggregates
38 import Gargantext.Viz.Phylo.Example
39 import Gargantext.Viz.Phylo.Tools
40 --import Gargantext.Viz.Phylo.View.ViewMaker
41 import Gargantext.Viz.Phylo.LevelMaker
43 import Servant.Job.Utils (swaggerOptions)
44 import Test.QuickCheck (elements)
45 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
46 import Web.HttpApiData (parseUrlPiece, readTextData)
47 import Control.Monad.IO.Class (liftIO)
48 import Network.HTTP.Media ((//), (/:))
50 ------------------------------------------------------------------------
51 type PhyloAPI = Summary "Phylo API"
57 phyloAPI :: PhyloId -> GargServer PhyloAPI
58 phyloAPI n = getPhylo' n
62 newtype SVG = SVG DB.ByteString
66 declareNamedSchema = undefined
67 --genericDeclareNamedSchemaUnrestricted (swaggerOptions "")
69 instance Show SVG where
72 instance Accept SVG where
73 contentType _ = "SVG" // "image/svg+xml" /: ("charset", "utf-8")
75 instance Show a => MimeRender PlainText a where
76 mimeRender _ val = cs ("" <> show val)
78 instance Show a => MimeRender SVG a where
79 mimeRender _ val = DBL.pack $ show val
81 ------------------------------------------------------------------------
82 type GetPhylo = QueryParam "listId" ListId
83 :> QueryParam "level" Level
84 :> QueryParam "filiation" Filiation
85 :> QueryParam "childs" Bool
86 :> QueryParam "depth" Level
87 :> QueryParam "metrics" [Metric]
88 :> QueryParam "periodsInf" Int
89 :> QueryParam "periodsSup" Int
90 :> QueryParam "minNodes" Int
91 :> QueryParam "taggers" [Tagger]
92 :> QueryParam "sort" Sort
93 :> QueryParam "order" Order
94 :> QueryParam "export" ExportMode
95 :> QueryParam "display" DisplayMode
96 :> QueryParam "verbose" Bool
100 -- Add real text processing
101 -- Fix Filter parameters
103 getPhylo :: PhyloId -> GargServer GetPhylo
104 getPhylo _phyloId _lId l f b l' ms x y z ts s o e d b' = do
106 fs' = maybe (Just []) (\p -> Just [p]) $ LonelyBranch <$> (LBParams <$> x <*> y <*> z)
108 q = initPhyloQueryView l f b l' ms fs' ts so e d b'
109 -- | TODO remove phylo for real data here
110 pure (toPhyloView q phylo)
111 -- TODO remove phylo for real data here
114 getPhylo' :: PhyloId -> GargServer GetPhylo
115 getPhylo' _phyloId _lId _l _f _b _l' _ms _x _y _z _ts _s _o _e _d _b' = do
116 p <- liftIO $ viewPhylo2Svg phyloView
118 ------------------------------------------------------------------------
120 type PutPhylo = (Put '[JSON] Phylo )
121 --putPhylo :: PhyloId -> Maybe ListId -> PhyloQueryBuild -> Phylo
122 putPhylo :: PhyloId -> GargServer PutPhylo
125 ------------------------------------------------------------------------
126 type PostPhylo = QueryParam "listId" ListId
127 :> ReqBody '[JSON] PhyloQueryBuild
128 :> (Post '[JSON] Phylo)
130 postPhylo :: CorpusId -> GargServer PostPhylo
131 postPhylo _n _lId q = do
132 -- TODO get Reader settings
135 vrs = Just ("1" :: Text)
136 sft = Just (Software "Gargantext" "4")
137 prm = initPhyloParam vrs sft (Just q)
138 pure (toPhyloBase q prm (parseDocs (initFoundationsRoots actants) corpus) termList empty)
141 ------------------------------------------------------------------------
142 -- | DELETE Phylo == delete a node
143 ------------------------------------------------------------------------
149 instance Arbitrary PhyloView
151 arbitrary = elements [phyloView]
153 -- | TODO add phyloGroup ex
154 instance Arbitrary PhyloGroup
156 arbitrary = elements []
158 instance Arbitrary Phylo
160 arbitrary = elements [phylo]
163 instance ToSchema Cluster
164 instance ToSchema EdgeType
165 instance ToSchema Filiation
166 instance ToSchema Filter
167 instance ToSchema FisParams
168 instance ToSchema HammingParams
169 instance ToSchema LouvainParams
170 instance ToSchema Metric
171 instance ToSchema Order
172 instance ToSchema Phylo
173 instance ToSchema PhyloFis
174 instance ToSchema PhyloBranch
175 instance ToSchema PhyloEdge
176 instance ToSchema PhyloGroup
177 instance ToSchema PhyloLevel
178 instance ToSchema PhyloNode
179 instance ToSchema PhyloParam
180 instance ToSchema PhyloFoundations
181 instance ToSchema PhyloPeriod
182 instance ToSchema PhyloQueryBuild
183 instance ToSchema PhyloView
184 instance ToSchema RCParams
185 instance ToSchema LBParams
186 instance ToSchema SBParams
187 instance ToSchema Software
188 instance ToSchema WLJParams
191 instance ToParamSchema Order
192 instance FromHttpApiData Order
194 parseUrlPiece = readTextData
197 instance ToParamSchema Metric
198 instance FromHttpApiData [Metric]
200 parseUrlPiece = readTextData
201 instance FromHttpApiData Metric
203 parseUrlPiece = readTextData
206 instance ToParamSchema DisplayMode
207 instance FromHttpApiData DisplayMode
209 parseUrlPiece = readTextData
212 instance ToParamSchema ExportMode
213 instance FromHttpApiData ExportMode
215 parseUrlPiece = readTextData
218 instance FromHttpApiData Sort
220 parseUrlPiece = readTextData
221 instance ToParamSchema Sort
224 instance ToSchema Proximity
226 declareNamedSchema = genericDeclareNamedSchemaUnrestricted
230 instance FromHttpApiData [Tagger]
232 parseUrlPiece = readTextData
233 instance FromHttpApiData Tagger
235 parseUrlPiece = readTextData
236 instance ToParamSchema Tagger
238 instance FromHttpApiData Filiation
240 parseUrlPiece = readTextData
241 instance ToParamSchema Filiation