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)
23 import Data.Text (Text)
24 import Gargantext.API.HashedResponse
25 import Gargantext.API.Ngrams
26 import Gargantext.API.Ngrams.NTree
27 import Gargantext.API.Prelude (GargServer)
28 import Gargantext.Core.Types (CorpusId, Limit, ListId, ListType(..))
29 import Gargantext.Database.Action.Flow
30 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataList(..))
31 import Gargantext.Database.Admin.Types.Metrics (ChartMetrics(..), Metric(..), Metrics(..))
32 import Gargantext.Database.Admin.Types.Node (NodeId)
33 import Gargantext.Database.Prelude
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.Schema.Node (node_hyperdata)
38 import Gargantext.Prelude
39 import Gargantext.Text.Metrics (Scored(..))
40 import Gargantext.Viz.Chart
41 import Gargantext.Viz.Types
43 import qualified Data.Map as Map
44 import qualified Gargantext.Database.Action.Metrics as Metrics
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
59 Summary "Scatter Hash"
60 :> QueryParam "list" ListId
61 :> QueryParamR "ngramsType" TabType
64 scatterApi :: NodeId -> GargServer ScatterAPI
65 scatterApi id' = getScatter id'
66 :<|> updateScatter id'
67 :<|> getScatterHash 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 hl = node ^. node_hyperdata
119 _ <- updateHyperdata listId $ hl { _hl_scatter = Just $ Metrics metrics }
121 pure $ Metrics metrics
123 getScatterHash :: FlowCmdM env err m =>
128 getScatterHash cId maybeListId tabType = do
129 hash <$> getScatter cId maybeListId tabType Nothing
132 -------------------------------------------------------------
133 -- | Chart metrics API
134 type ChartApi = Summary " Chart API"
135 :> QueryParam "from" UTCTime
136 :> QueryParam "to" UTCTime
137 :> QueryParam "list" ListId
138 :> QueryParamR "ngramsType" TabType
139 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
140 :<|> Summary "Chart update"
141 :> QueryParam "list" ListId
142 :> QueryParamR "ngramsType" TabType
143 :> QueryParam "limit" Int
147 :> QueryParam "list" ListId
148 :> QueryParamR "ngramsType" TabType
151 chartApi :: NodeId -> GargServer ChartApi
152 chartApi id' = getChart id'
154 :<|> getChartHash id'
156 -- TODO add start / end
157 getChart :: FlowCmdM env err m =>
163 -> m (HashedResponse (ChartMetrics Histo))
164 getChart cId _start _end maybeListId tabType = do
165 listId <- case maybeListId of
167 Nothing -> defaultList cId
168 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
169 let HyperdataList { _hl_chart = mChart } = node ^. node_hyperdata
171 chart <- case mChart of
172 Just chart -> pure chart
174 updateChart' cId maybeListId tabType Nothing
176 pure $ constructHashedResponse chart
178 updateChart :: HasNodeError err =>
184 updateChart cId maybeListId tabType maybeLimit = do
185 _ <- updateChart' cId maybeListId tabType maybeLimit
188 updateChart' :: HasNodeError err =>
193 -> Cmd err (ChartMetrics Histo)
194 updateChart' cId maybeListId _tabType _maybeLimit = do
195 listId <- case maybeListId of
197 Nothing -> defaultList cId
198 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
199 let hl = node ^. node_hyperdata
201 _ <- updateHyperdata listId $ hl { _hl_chart = Just $ ChartMetrics h }
203 pure $ ChartMetrics h
206 getChartHash :: FlowCmdM env err m =>
211 getChartHash cId maybeListId tabType = do
212 hash <$> getChart cId Nothing Nothing maybeListId tabType
214 -------------------------------------------------------------
216 type PieApi = Summary "Pie Chart"
217 :> QueryParam "from" UTCTime
218 :> QueryParam "to" UTCTime
219 :> QueryParam "list" ListId
220 :> QueryParamR "ngramsType" TabType
221 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
222 :<|> Summary "Pie Chart update"
223 :> QueryParam "list" ListId
224 :> QueryParamR "ngramsType" TabType
225 :> QueryParam "limit" Int
229 :> QueryParam "list" ListId
230 :> QueryParamR "ngramsType" TabType
233 pieApi :: NodeId -> GargServer PieApi
234 pieApi id' = getPie id'
238 getPie :: FlowCmdM env err m
244 -> m (HashedResponse (ChartMetrics Histo))
245 getPie cId _start _end maybeListId tabType = do
246 listId <- case maybeListId of
248 Nothing -> defaultList cId
249 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
250 let HyperdataList { _hl_pie = mChart } = node ^. node_hyperdata
252 chart <- case mChart of
253 Just chart -> pure chart
255 updatePie' cId maybeListId tabType Nothing
257 pure $ constructHashedResponse chart
259 updatePie :: FlowCmdM env err m =>
265 updatePie cId maybeListId tabType maybeLimit = do
266 _ <- updatePie' cId maybeListId tabType maybeLimit
269 updatePie' :: FlowCmdM env err m =>
274 -> m (ChartMetrics Histo)
275 updatePie' cId maybeListId tabType _maybeLimit = do
276 listId <- case maybeListId of
278 Nothing -> defaultList cId
279 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
280 let hl = node ^. node_hyperdata
282 p <- pieData cId (ngramsTypeFromTabType tabType) MapTerm
283 _ <- updateHyperdata listId $ hl { _hl_pie = Just $ ChartMetrics p }
285 pure $ ChartMetrics p
287 getPieHash :: FlowCmdM env err m =>
292 getPieHash cId maybeListId tabType = do
293 hash <$> getPie cId Nothing Nothing maybeListId tabType
295 -------------------------------------------------------------
296 -- | Tree metrics API
298 type TreeApi = Summary " Tree API"
299 :> QueryParam "from" UTCTime
300 :> QueryParam "to" UTCTime
301 :> QueryParam "list" ListId
302 :> QueryParamR "ngramsType" TabType
303 :> QueryParamR "listType" ListType
304 :> Get '[JSON] (HashedResponse (ChartMetrics [MyTree]))
305 :<|> Summary "Tree Chart update"
306 :> QueryParam "list" ListId
307 :> QueryParamR "ngramsType" TabType
308 :> QueryParamR "listType" ListType
312 :> QueryParam "list" ListId
313 :> QueryParamR "ngramsType" TabType
314 :> QueryParamR "listType" ListType
317 -- Depending on the Type of the Node, we could post
318 -- New documents for a corpus
319 -- New map list terms
320 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
322 treeApi :: NodeId -> GargServer TreeApi
323 treeApi id' = getTree id'
327 getTree :: FlowCmdM env err m
334 -> m (HashedResponse (ChartMetrics [MyTree]))
335 getTree cId _start _end maybeListId tabType listType = do
336 listId <- case maybeListId of
338 Nothing -> defaultList cId
340 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
341 let HyperdataList { _hl_tree = mChart } = node ^. node_hyperdata
343 chart <- case mChart of
344 Just chart -> pure chart
346 updateTree' cId maybeListId tabType listType
348 pure $ constructHashedResponse chart
350 updateTree :: FlowCmdM env err m =>
356 updateTree cId maybeListId tabType listType = do
357 _ <- updateTree' cId maybeListId tabType listType
360 updateTree' :: FlowCmdM env err m =>
365 -> m (ChartMetrics [MyTree])
366 updateTree' cId maybeListId tabType listType = do
367 listId <- case maybeListId of
369 Nothing -> defaultList cId
371 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
372 let hl = node ^. node_hyperdata
373 t <- treeData cId (ngramsTypeFromTabType tabType) listType
374 _ <- updateHyperdata listId $ hl { _hl_tree = Just $ ChartMetrics t }
376 pure $ ChartMetrics t
378 getTreeHash :: FlowCmdM env err m =>
384 getTreeHash cId maybeListId tabType listType = do
385 hash <$> getTree cId Nothing Nothing maybeListId tabType listType