2 Module : Gargantext.Core.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 OverloadedLists #-} -- allows to write Map and HashMap as lists
15 {-# LANGUAGE TypeOperators #-}
17 module Gargantext.Core.Viz.Phylo.API
20 import GHC.Generics (Generic)
23 import Data.Maybe (fromMaybe)
25 import Gargantext.API.Prelude
26 import Gargantext.Core.Types (TODO(..))
27 import Gargantext.Core.Viz.LegacyPhylo
28 import Gargantext.Core.Viz.Phylo (defaultConfig)
29 import Gargantext.Core.Viz.Phylo.API.Tools
30 import Gargantext.Core.Viz.Phylo.Example (phyloExample)
31 import Gargantext.Core.Viz.Phylo.Legacy.LegacyMain
32 import Gargantext.Database.Admin.Types.Hyperdata
33 import Gargantext.Database.Admin.Types.Node -- (PhyloId, ListId, CorpusId, UserId, NodeId(..))
34 import Gargantext.Database.Query.Table.Node (getClosestParentIdByType, defaultList)
35 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
36 import Gargantext.Prelude
37 import Network.HTTP.Media ((//), (/:))
39 import Test.QuickCheck (elements)
40 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
41 import Web.HttpApiData (readTextData)
42 import qualified Data.ByteString as DB
43 import qualified Data.ByteString.Lazy as DBL
45 ------------------------------------------------------------------------
46 type PhyloAPI = Summary "Phylo API"
52 phyloAPI :: PhyloId -> UserId -> GargServer PhyloAPI
53 phyloAPI n u = getPhylo n
58 newtype SVG = SVG DB.ByteString
59 --instance Show a => MimeRender PlainText a where mimeRender _ val = cs ("" <> show val)
60 instance Accept SVG where contentType _ = "SVG" // "image/svg+xml" /: ("charset", "utf-8")
61 instance MimeRender SVG SVG where mimeRender _ (SVG s) = DBL.fromStrict s
62 instance MimeUnrender SVG SVG where mimeUnrender _ lbs = Right $ SVG (DBL.toStrict lbs)
63 instance Show SVG where show (SVG a) = show a
64 instance ToSchema SVG where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
66 ------------------------------------------------------------------------
67 instance ToSchema Value where declareNamedSchema _ = declareNamedSchema (Proxy :: Proxy TODO)
69 ------------------------------------------------------------------------
71 data PhyloData = PhyloData { pd_corpusId :: NodeId
77 instance FromJSON PhyloData
78 instance ToJSON PhyloData
79 instance ToSchema PhyloData
81 type GetPhylo = QueryParam "listId" ListId
82 :> QueryParam "level" Level
83 :> QueryParam "minSizeBranch" MinSizeBranch
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
99 :> Get '[JSON] PhyloData
103 -- Add real text processing
104 -- Fix Filter parameters
105 -- TODO fix parameters to default config that should be in Node
106 getPhylo :: PhyloId -> GargServer GetPhylo
107 getPhylo phyloId lId _level _minSizeBranch = do
108 corpusId <- fromMaybe (panic $ "[G.C.V.Phylo.API] no parent for NodeId " <> (cs $ show phyloId))
109 <$> getClosestParentIdByType phyloId NodeCorpus
110 listId <- case lId of
111 Nothing -> defaultList corpusId
113 theData <- getPhyloDataJson phyloId
114 -- printDebug "getPhylo" theData
115 pure $ PhyloData corpusId listId theData
119 getPhyloDataJson :: PhyloId -> GargNoServer Value
120 getPhyloDataJson phyloId = do
121 phyloData <- fromMaybe phyloExample <$> getPhyloData phyloId
122 phyloJson <- liftBase $ phylo2dot2json phyloData
126 -- getPhyloDataSVG phId _lId l msb = do
128 -- level = fromMaybe 2 l
129 -- branc = fromMaybe 2 msb
130 -- maybePhylo = phNode ^. (node_hyperdata . hp_data)
132 -- p <- liftBase $ viewPhylo2Svg
133 -- $ viewPhylo level branc
134 -- $ fromMaybe phyloFromQuery maybePhylo
138 ------------------------------------------------------------------------
139 type PostPhylo = QueryParam "listId" ListId
140 -- :> ReqBody '[JSON] PhyloQueryBuild
141 :> (Post '[JSON] NodeId)
143 postPhylo :: PhyloId -> UserId -> GargServer PostPhylo
144 postPhylo phyloId _userId _lId = do
145 -- TODO get Reader settings
148 -- _vrs = Just ("1" :: Text)
149 -- _sft = Just (Software "Gargantext" "4")
150 -- _prm = initPhyloParam vrs sft (Just q)
151 corpusId <- getClosestParentIdByType phyloId NodeCorpus
152 phy <- flowPhyloAPI defaultConfig (fromMaybe (panic "[G.C.V.P.API] no corpus ID found") corpusId) -- params
153 -- phyloId <- insertNodes [node NodePhylo "Phylo" (HyperdataPhylo Nothing (Just phy)) (Just corpusId) userId]
154 _ <- updateHyperdata phyloId (HyperdataPhylo Nothing (Just phy))
157 ------------------------------------------------------------------------
158 -- | DELETE Phylo == delete a node
159 ------------------------------------------------------------------------
160 ------------------------------------------------------------------------
162 type PutPhylo = (Put '[JSON] Phylo )
163 --putPhylo :: PhyloId -> Maybe ListId -> PhyloQueryBuild -> Phylo
164 putPhylo :: PhyloId -> GargServer PutPhylo
170 -- instance Arbitrary Phylo where arbitrary = elements [phylo]
171 instance Arbitrary PhyloGroup where arbitrary = elements []
172 -- instance Arbitrary PhyloView where arbitrary = elements [phyloView]
173 instance FromHttpApiData DisplayMode where parseUrlPiece = readTextData
174 instance FromHttpApiData ExportMode where parseUrlPiece = readTextData
175 instance FromHttpApiData Filiation where parseUrlPiece = readTextData
176 instance FromHttpApiData Metric where parseUrlPiece = readTextData
177 instance FromHttpApiData Order where parseUrlPiece = readTextData
178 instance FromHttpApiData Sort where parseUrlPiece = readTextData
179 instance FromHttpApiData Tagger where parseUrlPiece = readTextData
180 instance FromHttpApiData [Metric] where parseUrlPiece = readTextData
181 instance FromHttpApiData [Tagger] where parseUrlPiece = readTextData
182 instance ToParamSchema DisplayMode
183 instance ToParamSchema ExportMode
184 instance ToParamSchema Filiation
185 instance ToParamSchema Tagger
186 instance ToParamSchema Metric
187 instance ToParamSchema Order
188 instance ToParamSchema Sort
189 instance ToSchema Order