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 TypeOperators #-}
18 module Gargantext.API.Metrics
22 import Data.Time (UTCTime)
25 import qualified Data.Map as Map
27 import Gargantext.API.HashedResponse
28 import Gargantext.API.Ngrams
29 import Gargantext.API.Ngrams.NTree
30 import Gargantext.API.Prelude (GargServer)
31 import Gargantext.Core.Types (CorpusId, Limit, ListId, ListType(..))
32 import Gargantext.Database.Action.Flow
33 import qualified Gargantext.Database.Action.Metrics as Metrics
34 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataList(..))
35 import Gargantext.Database.Admin.Types.Metrics (ChartMetrics(..), Metric(..), Metrics(..))
36 import Gargantext.Database.Admin.Types.Node (NodeId)
37 import Gargantext.Database.Query.Table.Node (defaultList, getNodeWith)
38 import Gargantext.Database.Query.Table.Node.Error (HasNodeError)
39 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
40 import Gargantext.Database.Prelude
41 import Gargantext.Database.Schema.Node (node_hyperdata)
42 import Gargantext.Text.Metrics (Scored(..))
43 import Gargantext.Viz.Chart
44 import Gargantext.Viz.Types
46 -------------------------------------------------------------
47 -- | Scatter metrics API
48 type ScatterAPI = Summary "SepGen IncExc metrics"
49 :> QueryParam "list" ListId
50 :> QueryParamR "ngramsType" TabType
51 :> QueryParam "limit" Int
52 :> Get '[JSON] (HashedResponse Metrics)
53 :<|> Summary "Scatter update"
54 :> QueryParam "list" ListId
55 :> QueryParamR "ngramsType" TabType
56 :> QueryParam "limit" Int
60 :> QueryParam "list" ListId
61 :> QueryParamR "ngramsType" TabType
64 scatterApi :: NodeId -> GargServer ScatterAPI
65 scatterApi id' = getScatter id'
66 :<|> updateScatter id'
67 :<|> getScatterMD5 id'
69 getScatter :: FlowCmdM env err m =>
74 -> m (HashedResponse Metrics)
75 getScatter cId maybeListId tabType _maybeLimit = do
76 listId <- case maybeListId of
78 Nothing -> defaultList cId
79 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
80 let HyperdataList { _hl_scatter = mChart } = node ^. node_hyperdata
82 chart <- case mChart of
83 Just chart -> pure chart
85 updateScatter' cId maybeListId tabType Nothing
87 pure $ constructHashedResponse chart
89 updateScatter :: FlowCmdM env err m =>
95 updateScatter cId maybeListId tabType maybeLimit = do
96 _ <- updateScatter' cId maybeListId tabType maybeLimit
99 updateScatter' :: FlowCmdM env err m =>
105 updateScatter' cId maybeListId tabType maybeLimit = do
106 (ngs', scores) <- Metrics.getMetrics cId maybeListId tabType maybeLimit
109 metrics = map (\(Scored t s1 s2) -> Metric t (log' 5 s1) (log' 2 s2) (listType t ngs')) scores
110 log' n x = 1 + (if x <= 0 then 0 else log $ (10^(n::Int)) * x)
111 listType t m = maybe (panic errorMsg) fst $ Map.lookup t m
112 errorMsg = "API.Node.metrics: key absent"
114 listId <- case maybeListId of
116 Nothing -> defaultList cId
117 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
118 let HyperdataList { _hl_chart = hdc
121 , _hl_tree = hdt } = node ^. node_hyperdata
122 _ <- updateHyperdata listId $ HyperdataList hdc hdl hdp (Just $ Metrics metrics) hdt
124 pure $ Metrics metrics
126 getScatterMD5 :: FlowCmdM env err m =>
131 getScatterMD5 cId maybeListId tabType = do
132 HashedResponse { md5 = md5' } <- getScatter cId maybeListId tabType Nothing
136 -------------------------------------------------------------
137 -- | Chart metrics API
138 type ChartApi = Summary " Chart API"
139 :> QueryParam "from" UTCTime
140 :> QueryParam "to" UTCTime
141 :> QueryParam "list" ListId
142 :> QueryParamR "ngramsType" TabType
143 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
144 :<|> Summary "Chart update"
145 :> QueryParam "list" ListId
146 :> QueryParamR "ngramsType" TabType
147 :> QueryParam "limit" Int
151 :> QueryParam "list" ListId
152 :> QueryParamR "ngramsType" TabType
155 chartApi :: NodeId -> GargServer ChartApi
156 chartApi id' = getChart id'
160 -- TODO add start / end
161 getChart :: FlowCmdM env err m =>
167 -> m (HashedResponse (ChartMetrics Histo))
168 getChart cId _start _end maybeListId tabType = do
169 listId <- case maybeListId of
171 Nothing -> defaultList cId
172 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
173 let HyperdataList { _hl_chart = mChart } = node ^. node_hyperdata
175 chart <- case mChart of
176 Just chart -> pure chart
178 updateChart' cId maybeListId tabType Nothing
180 pure $ constructHashedResponse chart
182 updateChart :: HasNodeError err =>
188 updateChart cId maybeListId tabType maybeLimit = do
189 _ <- updateChart' cId maybeListId tabType maybeLimit
192 updateChart' :: HasNodeError err =>
197 -> Cmd err (ChartMetrics Histo)
198 updateChart' cId maybeListId _tabType _maybeLimit = do
199 listId <- case maybeListId of
201 Nothing -> defaultList cId
202 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
203 let HyperdataList { _hl_list = hdl
206 , _hl_tree = hdt } = node ^. node_hyperdata
208 _ <- updateHyperdata listId $ HyperdataList (Just $ ChartMetrics h) hdl hdp hds hdt
210 pure $ ChartMetrics h
213 getChartMD5 :: FlowCmdM env err m =>
218 getChartMD5 cId maybeListId tabType = do
219 HashedResponse { md5 = md5' } <- getChart cId Nothing Nothing maybeListId tabType
221 -------------------------------------------------------------
223 type PieApi = Summary "Pie Chart"
224 :> QueryParam "from" UTCTime
225 :> QueryParam "to" UTCTime
226 :> QueryParam "list" ListId
227 :> QueryParamR "ngramsType" TabType
228 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
229 :<|> Summary "Pie Chart update"
230 :> QueryParam "list" ListId
231 :> QueryParamR "ngramsType" TabType
232 :> QueryParam "limit" Int
236 :> QueryParam "list" ListId
237 :> QueryParamR "ngramsType" TabType
240 pieApi :: NodeId -> GargServer PieApi
241 pieApi id' = getPie id'
245 getPie :: FlowCmdM env err m
251 -> m (HashedResponse (ChartMetrics Histo))
252 getPie cId _start _end maybeListId tabType = do
253 listId <- case maybeListId of
255 Nothing -> defaultList cId
256 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
257 let HyperdataList { _hl_pie = mChart } = node ^. node_hyperdata
259 chart <- case mChart of
260 Just chart -> pure chart
262 updatePie' cId maybeListId tabType Nothing
264 pure $ constructHashedResponse chart
266 updatePie :: FlowCmdM env err m =>
272 updatePie cId maybeListId tabType maybeLimit = do
273 _ <- updatePie' cId maybeListId tabType maybeLimit
276 updatePie' :: FlowCmdM env err m =>
281 -> m (ChartMetrics Histo)
282 updatePie' cId maybeListId tabType _maybeLimit = do
283 listId <- case maybeListId of
285 Nothing -> defaultList cId
286 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
287 let HyperdataList { _hl_chart = hdc
290 , _hl_tree = hdt } = node ^. node_hyperdata
292 p <- pieData cId (ngramsTypeFromTabType tabType) MapTerm
293 _ <- updateHyperdata listId $ HyperdataList hdc hdl (Just $ ChartMetrics p) hds hdt
295 pure $ ChartMetrics p
297 getPieMD5 :: FlowCmdM env err m =>
302 getPieMD5 cId maybeListId tabType = do
303 HashedResponse { md5 = md5' } <- getPie cId Nothing Nothing maybeListId tabType
305 -------------------------------------------------------------
306 -- | Tree metrics API
308 type TreeApi = Summary " Tree API"
309 :> QueryParam "from" UTCTime
310 :> QueryParam "to" UTCTime
311 :> QueryParam "list" ListId
312 :> QueryParamR "ngramsType" TabType
313 :> QueryParamR "listType" ListType
314 :> Get '[JSON] (HashedResponse (ChartMetrics [MyTree]))
315 :<|> Summary "Tree Chart update"
316 :> QueryParam "list" ListId
317 :> QueryParamR "ngramsType" TabType
318 :> QueryParamR "listType" ListType
322 :> QueryParam "list" ListId
323 :> QueryParamR "ngramsType" TabType
324 :> QueryParamR "listType" ListType
327 -- Depending on the Type of the Node, we could post
328 -- New documents for a corpus
329 -- New map list terms
330 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
332 treeApi :: NodeId -> GargServer TreeApi
333 treeApi id' = getTree id'
337 getTree :: FlowCmdM env err m
344 -> m (HashedResponse (ChartMetrics [MyTree]))
345 getTree cId _start _end maybeListId tabType listType = do
346 listId <- case maybeListId of
348 Nothing -> defaultList cId
350 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
351 let HyperdataList { _hl_tree = mChart } = node ^. node_hyperdata
353 chart <- case mChart of
354 Just chart -> pure chart
356 updateTree' cId maybeListId tabType listType
358 pure $ constructHashedResponse chart
360 updateTree :: FlowCmdM env err m =>
366 updateTree cId maybeListId tabType listType = do
367 _ <- updateTree' cId maybeListId tabType listType
370 updateTree' :: FlowCmdM env err m =>
375 -> m (ChartMetrics [MyTree])
376 updateTree' cId maybeListId tabType listType = do
377 listId <- case maybeListId of
379 Nothing -> defaultList cId
381 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
382 let HyperdataList { _hl_chart = hdc
385 , _hl_pie = hdp } = node ^. node_hyperdata
386 t <- treeData cId (ngramsTypeFromTabType tabType) listType
387 _ <- updateHyperdata listId $ HyperdataList hdc hdl hdp hds (Just $ ChartMetrics t)
389 pure $ ChartMetrics t
391 getTreeMD5 :: FlowCmdM env err m =>
397 getTreeMD5 cId maybeListId tabType listType = do
398 HashedResponse { md5 = md5' } <- getTree cId Nothing Nothing maybeListId tabType listType