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 FlexibleContexts #-}
21 {-# LANGUAGE FlexibleInstances #-}
22 {-# LANGUAGE MultiParamTypeClasses #-}
24 module Gargantext.Viz.Phylo.API
27 import Data.String.Conversions
28 --import Control.Monad.Reader (ask)
29 import qualified Data.ByteString as DB
30 import qualified Data.ByteString.Lazy as DBL
32 import Gargantext.API.Types
33 import Gargantext.Database.Action.Query.Node (insertNodes, nodePhyloW, getNodePhylo)
34 import Gargantext.Database.Admin.Types.Node -- (PhyloId, ListId, CorpusId, UserId, NodeId(..))
35 import Gargantext.Prelude
36 import Gargantext.Viz.Phylo
37 import Gargantext.Viz.Phylo.Main
38 import Gargantext.Viz.Phylo.Example
39 import Gargantext.Core.Types (TODO(..))
41 import Test.QuickCheck (elements)
42 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
43 import Web.HttpApiData (parseUrlPiece, readTextData)
44 import Network.HTTP.Media ((//), (/:))
46 ------------------------------------------------------------------------
47 type PhyloAPI = Summary "Phylo API"
53 phyloAPI :: PhyloId -> UserId -> GargServer PhyloAPI
54 phyloAPI n u = getPhylo n
59 newtype SVG = SVG DB.ByteString
63 declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
65 instance Show SVG where
68 instance Accept SVG where
69 contentType _ = "SVG" // "image/svg+xml" /: ("charset", "utf-8")
71 instance Show a => MimeRender PlainText a where
72 mimeRender _ val = cs ("" <> show val)
74 instance MimeRender SVG SVG where
75 mimeRender _ (SVG s) = DBL.fromStrict s
77 ------------------------------------------------------------------------
78 type GetPhylo = QueryParam "listId" ListId
79 :> QueryParam "level" Level
80 :> QueryParam "minSizeBranch" MinSizeBranch
81 {- :> QueryParam "filiation" Filiation
82 :> QueryParam "childs" Bool
83 :> QueryParam "depth" Level
84 :> QueryParam "metrics" [Metric]
85 :> QueryParam "periodsInf" Int
86 :> QueryParam "periodsSup" Int
87 :> QueryParam "minNodes" Int
88 :> QueryParam "taggers" [Tagger]
89 :> QueryParam "sort" Sort
90 :> QueryParam "order" Order
91 :> QueryParam "export" ExportMode
92 :> QueryParam "display" DisplayMode
93 :> QueryParam "verbose" Bool
98 -- Add real text processing
99 -- Fix Filter parameters
100 getPhylo :: PhyloId -> GargServer GetPhylo
101 --getPhylo phId _lId l msb _f _b _l' _ms _x _y _z _ts _s _o _e _d _b' = do
102 getPhylo phId _lId l msb = do
103 phNode <- getNodePhylo phId
105 level = maybe 2 identity l
106 branc = maybe 2 identity msb
107 maybePhylo = hyperdataPhylo_data $ _node_hyperdata phNode
109 p <- liftBase $ viewPhylo2Svg $ viewPhylo level branc $ maybe phyloFromQuery identity maybePhylo
111 ------------------------------------------------------------------------
112 type PostPhylo = QueryParam "listId" ListId
113 -- :> ReqBody '[JSON] PhyloQueryBuild
114 :> (Post '[JSON] NodeId)
116 postPhylo :: CorpusId -> UserId -> GargServer PostPhylo
117 postPhylo n userId _lId = do
118 -- TODO get Reader settings
121 -- _vrs = Just ("1" :: Text)
122 -- _sft = Just (Software "Gargantext" "4")
123 -- _prm = initPhyloParam vrs sft (Just q)
125 pId <- insertNodes [nodePhyloW (Just "Phylo") (Just $ HyperdataPhylo Nothing (Just phy)) n userId]
126 pure $ NodeId (fromIntegral pId)
128 ------------------------------------------------------------------------
129 -- | DELETE Phylo == delete a node
130 ------------------------------------------------------------------------
131 ------------------------------------------------------------------------
133 type PutPhylo = (Put '[JSON] Phylo )
134 --putPhylo :: PhyloId -> Maybe ListId -> PhyloQueryBuild -> Phylo
135 putPhylo :: PhyloId -> GargServer PutPhylo
141 instance Arbitrary PhyloView
143 arbitrary = elements [phyloView]
145 -- | TODO add phyloGroup ex
146 instance Arbitrary PhyloGroup
148 arbitrary = elements []
150 instance Arbitrary Phylo
152 arbitrary = elements [phylo]
154 instance ToSchema Order
156 instance ToParamSchema Order
157 instance FromHttpApiData Order
159 parseUrlPiece = readTextData
162 instance ToParamSchema Metric
163 instance FromHttpApiData [Metric]
165 parseUrlPiece = readTextData
166 instance FromHttpApiData Metric
168 parseUrlPiece = readTextData
171 instance ToParamSchema DisplayMode
172 instance FromHttpApiData DisplayMode
174 parseUrlPiece = readTextData
177 instance ToParamSchema ExportMode
178 instance FromHttpApiData ExportMode
180 parseUrlPiece = readTextData
183 instance FromHttpApiData Sort
185 parseUrlPiece = readTextData
186 instance ToParamSchema Sort
188 instance FromHttpApiData [Tagger]
190 parseUrlPiece = readTextData
191 instance FromHttpApiData Tagger
193 parseUrlPiece = readTextData
194 instance ToParamSchema Tagger
196 instance FromHttpApiData Filiation
198 parseUrlPiece = readTextData
199 instance ToParamSchema Filiation