2 Module : Gargantext.API.Metrics
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
16 {-# LANGUAGE TemplateHaskell #-}
17 {-# LANGUAGE TypeOperators #-}
19 module Gargantext.API.Metrics
23 import Data.Time (UTCTime)
25 import qualified Data.Map as Map
27 import Gargantext.API.Ngrams
28 import Gargantext.API.Ngrams.NTree
29 import Gargantext.Core.Types (CorpusId, Limit, ListId, ListType(..))
30 import qualified Gargantext.Database.Action.Metrics as Metrics
31 import Gargantext.Database.Action.Flow
32 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataList(..))
33 import Gargantext.Database.Admin.Types.Metrics (ChartMetrics(..), Metric(..), Metrics(..))
34 import Gargantext.Database.Query.Table.Node (defaultList, getNodeWith)
35 import Gargantext.Database.Query.Table.Node.Error (HasNodeError)
36 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
37 import Gargantext.Database.Prelude
38 import Gargantext.Database.Schema.Node (node_hyperdata)
39 import Gargantext.Prelude
40 import Gargantext.Text.Metrics (Scored(..))
41 import Gargantext.Viz.Chart
42 import Gargantext.Viz.Types
44 -------------------------------------------------------------
45 -- | Scatter metrics API
46 type ScatterAPI = Summary "SepGen IncExc metrics"
47 :> QueryParam "list" ListId
48 :> QueryParamR "ngramsType" TabType
49 :> QueryParam "limit" Int
50 :> Get '[JSON] Metrics
51 :<|> Summary "Scatter update"
52 :> QueryParam "list" ListId
53 :> QueryParamR "ngramsType" TabType
54 :> QueryParam "limit" Int
57 getScatter :: FlowCmdM env err m =>
63 getScatter cId maybeListId tabType _maybeLimit = do
64 listId <- case maybeListId of
66 Nothing -> defaultList cId
67 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
68 let (HyperdataList { hd_scatter = mChart }) = node ^. node_hyperdata
71 Just chart -> pure chart
73 s <- updateScatter' cId maybeListId tabType Nothing
76 updateScatter :: FlowCmdM env err m =>
82 updateScatter cId maybeListId tabType maybeLimit = do
83 _ <- updateScatter' cId maybeListId tabType maybeLimit
86 updateScatter' :: FlowCmdM env err m =>
92 updateScatter' cId maybeListId tabType maybeLimit = do
93 (ngs', scores) <- Metrics.getMetrics cId maybeListId tabType maybeLimit
96 metrics = map (\(Scored t s1 s2) -> Metric t (log' 5 s1) (log' 2 s2) (listType t ngs')) scores
97 log' n x = 1 + (if x <= 0 then 0 else (log $ (10^(n::Int)) * x))
98 listType t m = maybe (panic errorMsg) fst $ Map.lookup t m
99 errorMsg = "API.Node.metrics: key absent"
101 listId <- case maybeListId of
103 Nothing -> defaultList cId
104 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
105 let (HyperdataList { hd_chart = hdc
108 , hd_tree = hdt }) = node ^. node_hyperdata
109 _ <- updateHyperdata listId $ HyperdataList hdc hdl hdp (Just $ Metrics metrics) hdt
111 pure $ Metrics metrics
114 -------------------------------------------------------------
115 -- | Chart metrics API
116 type ChartApi = Summary " Chart API"
117 :> QueryParam "from" UTCTime
118 :> QueryParam "to" UTCTime
119 :> QueryParam "list" ListId
120 :> QueryParamR "ngramsType" TabType
121 :> Get '[JSON] (ChartMetrics Histo)
122 :<|> Summary "Chart update"
123 :> QueryParam "list" ListId
124 :> QueryParamR "ngramsType" TabType
125 :> QueryParam "limit" Int
128 -- TODO add start / end
129 getChart :: HasNodeError err
135 -> Cmd err (ChartMetrics Histo)
136 getChart cId _start _end maybeListId tabType = do
137 listId <- case maybeListId of
139 Nothing -> defaultList cId
140 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
141 let (HyperdataList { hd_chart = mChart }) = node ^. node_hyperdata
144 Just chart -> pure chart
146 h <- updateChart' cId maybeListId tabType Nothing
149 updateChart :: HasNodeError err =>
155 updateChart cId maybeListId tabType maybeLimit = do
156 _ <- updateChart' cId maybeListId tabType maybeLimit
159 updateChart' :: HasNodeError err =>
164 -> Cmd err (ChartMetrics Histo)
165 updateChart' cId maybeListId _tabType _maybeLimit = do
166 listId <- case maybeListId of
168 Nothing -> defaultList cId
169 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
170 let (HyperdataList { hd_list = hdl
173 , hd_tree = hdt }) = node ^. node_hyperdata
175 _ <- updateHyperdata listId $ HyperdataList (Just $ ChartMetrics h) hdl hdp hds hdt
177 pure $ ChartMetrics h
178 -------------------------------------------------------------
180 type PieApi = Summary "Pie Chart"
181 :> QueryParam "from" UTCTime
182 :> QueryParam "to" UTCTime
183 :> QueryParam "list" ListId
184 :> QueryParamR "ngramsType" TabType
185 :> Get '[JSON] (ChartMetrics Histo)
186 :<|> Summary "Pie Chart update"
187 :> QueryParam "list" ListId
188 :> QueryParamR "ngramsType" TabType
189 :> QueryParam "limit" Int
192 getPie :: FlowCmdM env err m
198 -> m (ChartMetrics Histo)
199 getPie cId _start _end maybeListId tabType = do
200 listId <- case maybeListId of
202 Nothing -> defaultList cId
203 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
204 let (HyperdataList { hd_pie = mChart }) = node ^. node_hyperdata
207 Just chart -> pure chart
209 p <- updatePie' cId maybeListId tabType Nothing
212 updatePie :: FlowCmdM env err m =>
218 updatePie cId maybeListId tabType maybeLimit = do
219 _ <- updatePie' cId maybeListId tabType maybeLimit
222 updatePie' :: FlowCmdM env err m =>
227 -> m (ChartMetrics Histo)
228 updatePie' cId maybeListId tabType _maybeLimit = do
229 listId <- case maybeListId of
231 Nothing -> defaultList cId
232 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
233 let (HyperdataList { hd_chart = hdc
236 , hd_tree = hdt }) = node ^. node_hyperdata
238 p <- pieData cId (ngramsTypeFromTabType tabType) MapTerm
239 _ <- updateHyperdata listId $ HyperdataList hdc hdl (Just $ ChartMetrics p) hds hdt
241 pure $ ChartMetrics p
243 -------------------------------------------------------------
244 -- | Tree metrics API
246 type TreeApi = Summary " Tree API"
247 :> QueryParam "from" UTCTime
248 :> QueryParam "to" UTCTime
249 :> QueryParam "list" ListId
250 :> QueryParamR "ngramsType" TabType
251 :> QueryParamR "listType" ListType
252 :> Get '[JSON] (ChartMetrics [MyTree])
253 :<|> Summary "Tree Chart update"
254 :> QueryParam "list" ListId
255 :> QueryParamR "ngramsType" TabType
256 :> QueryParamR "listType" ListType
259 -- Depending on the Type of the Node, we could post
260 -- New documents for a corpus
261 -- New map list terms
262 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
264 getTree :: FlowCmdM env err m
271 -> m (ChartMetrics [MyTree])
272 getTree cId _start _end maybeListId tabType listType = do
273 listId <- case maybeListId of
275 Nothing -> defaultList cId
277 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
278 let (HyperdataList { hd_tree = mChart }) = node ^. node_hyperdata
280 Just chart -> pure chart
282 t <- updateTree' cId maybeListId tabType listType
285 updateTree :: FlowCmdM env err m =>
291 updateTree cId maybeListId tabType listType = do
292 _ <- updateTree' cId maybeListId tabType listType
295 updateTree' :: FlowCmdM env err m =>
300 -> m (ChartMetrics [MyTree])
301 updateTree' cId maybeListId tabType listType = do
302 listId <- case maybeListId of
304 Nothing -> defaultList cId
306 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
307 let (HyperdataList { hd_chart = hdc
310 , hd_pie = hdp }) = node ^. node_hyperdata
311 t <- treeData cId (ngramsTypeFromTabType tabType) listType
312 _ <- updateHyperdata listId $ HyperdataList hdc hdl hdp hds (Just $ ChartMetrics t)
314 pure $ ChartMetrics t