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.Database.Types.Node (PhyloId, ListId, CorpusId)
33 import Gargantext.Database.Schema.Node (insertNodes, nodePhyloW, getNodePhylo)
34 import Gargantext.Database.Types.Node -- (NodePhylo(..))
35 import Gargantext.Prelude
36 import Gargantext.Viz.Phylo
37 import Gargantext.Viz.Phylo.Main
38 import Gargantext.Viz.Phylo.Example
39 import Gargantext.API.Ngrams (TODO(..))
41 import Test.QuickCheck (elements)
42 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
43 import Web.HttpApiData (parseUrlPiece, readTextData)
44 import Control.Monad.IO.Class (liftIO)
45 import Network.HTTP.Media ((//), (/:))
47 ------------------------------------------------------------------------
48 type PhyloAPI = Summary "Phylo API"
54 phyloAPI :: PhyloId -> UserId -> GargServer PhyloAPI
55 phyloAPI n u = getPhylo n
60 newtype SVG = SVG DB.ByteString
64 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
66 instance Show SVG where
69 instance Accept SVG where
70 contentType _ = "SVG" // "image/svg+xml" /: ("charset", "utf-8")
72 instance Show a => MimeRender PlainText a where
73 mimeRender _ val = cs ("" <> show val)
75 instance MimeRender SVG SVG where
76 mimeRender _ (SVG s) = DBL.fromStrict s
78 ------------------------------------------------------------------------
79 type GetPhylo = QueryParam "listId" ListId
80 :> QueryParam "level" Level
81 :> QueryParam "minSizeBranch" MinSizeBranch
82 {- :> QueryParam "filiation" Filiation
83 :> QueryParam "childs" Bool
84 :> QueryParam "depth" Level
85 :> QueryParam "metrics" [Metric]
86 :> QueryParam "periodsInf" Int
87 :> QueryParam "periodsSup" Int
88 :> QueryParam "minNodes" Int
89 :> QueryParam "taggers" [Tagger]
90 :> QueryParam "sort" Sort
91 :> QueryParam "order" Order
92 :> QueryParam "export" ExportMode
93 :> QueryParam "display" DisplayMode
94 :> QueryParam "verbose" Bool
99 -- Add real text processing
100 -- Fix Filter parameters
101 getPhylo :: PhyloId -> GargServer GetPhylo
102 --getPhylo phId _lId l msb _f _b _l' _ms _x _y _z _ts _s _o _e _d _b' = do
103 getPhylo phId _lId l msb = do
104 phNode <- getNodePhylo phId
106 level = maybe 2 identity l
107 branc = maybe 2 identity msb
108 maybePhylo = hyperdataPhylo_data $ _node_hyperdata phNode
110 p <- liftIO $ viewPhylo2Svg $ viewPhylo level branc $ maybe phyloFromQuery identity maybePhylo
112 ------------------------------------------------------------------------
113 type PostPhylo = QueryParam "listId" ListId
114 -- :> ReqBody '[JSON] PhyloQueryBuild
115 :> (Post '[JSON] NodeId)
117 postPhylo :: CorpusId -> UserId -> GargServer PostPhylo
118 postPhylo n userId _lId = do
119 -- TODO get Reader settings
122 -- _vrs = Just ("1" :: Text)
123 -- _sft = Just (Software "Gargantext" "4")
124 -- _prm = initPhyloParam vrs sft (Just q)
126 pId <- insertNodes [nodePhyloW (Just "Phylo") (Just $ HyperdataPhylo Nothing (Just phy)) n userId]
127 pure $ NodeId (fromIntegral pId)
129 ------------------------------------------------------------------------
130 -- | DELETE Phylo == delete a node
131 ------------------------------------------------------------------------
132 ------------------------------------------------------------------------
134 type PutPhylo = (Put '[JSON] Phylo )
135 --putPhylo :: PhyloId -> Maybe ListId -> PhyloQueryBuild -> Phylo
136 putPhylo :: PhyloId -> GargServer PutPhylo
142 instance Arbitrary PhyloView
144 arbitrary = elements [phyloView]
146 -- | TODO add phyloGroup ex
147 instance Arbitrary PhyloGroup
149 arbitrary = elements []
151 instance Arbitrary Phylo
153 arbitrary = elements [phylo]
155 instance ToSchema Order
157 instance ToParamSchema Order
158 instance FromHttpApiData Order
160 parseUrlPiece = readTextData
163 instance ToParamSchema Metric
164 instance FromHttpApiData [Metric]
166 parseUrlPiece = readTextData
167 instance FromHttpApiData Metric
169 parseUrlPiece = readTextData
172 instance ToParamSchema DisplayMode
173 instance FromHttpApiData DisplayMode
175 parseUrlPiece = readTextData
178 instance ToParamSchema ExportMode
179 instance FromHttpApiData ExportMode
181 parseUrlPiece = readTextData
184 instance FromHttpApiData Sort
186 parseUrlPiece = readTextData
187 instance ToParamSchema Sort
189 instance FromHttpApiData [Tagger]
191 parseUrlPiece = readTextData
192 instance FromHttpApiData Tagger
194 parseUrlPiece = readTextData
195 instance ToParamSchema Tagger
197 instance FromHttpApiData Filiation
199 parseUrlPiece = readTextData
200 instance ToParamSchema Filiation