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.HashMap.Strict (HashMap)
23 import qualified Data.HashMap.Strict as HM
24 import Data.Text (Text)
25 import Data.Time (UTCTime)
28 import Gargantext.API.HashedResponse
29 import Gargantext.API.Ngrams.NgramsTree
30 import Gargantext.API.Ngrams.Types
31 import Gargantext.API.Prelude (GargServer)
32 import Gargantext.Core.Types (CorpusId, Limit, ListId, ListType(..))
33 import Gargantext.Database.Action.Flow
34 import Gargantext.Database.Admin.Types.Hyperdata (HyperdataList(..), hl_chart, hl_pie, hl_scatter, hl_tree)
35 import Gargantext.Database.Admin.Types.Metrics (ChartMetrics(..), Metric(..), Metrics(..))
36 import Gargantext.Database.Admin.Types.Node (NodeId)
37 import Gargantext.Database.Prelude
38 import Gargantext.Database.Query.Table.Node (defaultList, getNodeWith)
39 import Gargantext.Database.Query.Table.Node.Error (HasNodeError)
40 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
41 import Gargantext.Database.Schema.Node (node_hyperdata)
42 import Gargantext.Prelude
43 import Gargantext.Core.Text.Metrics (Scored(..), {-normalizeGlobal,-} normalizeLocal)
44 import Gargantext.Core.Viz.Chart
45 import Gargantext.Core.Viz.Types
46 import qualified Gargantext.Database.Action.Metrics as Metrics
48 -------------------------------------------------------------
49 -- | Scatter metrics API
50 type ScatterAPI = Summary "SepGen IncExc metrics"
51 :> QueryParam "list" ListId
52 :> QueryParamR "ngramsType" TabType
53 :> QueryParam "limit" Int
54 :> Get '[JSON] (HashedResponse Metrics)
55 :<|> Summary "Scatter update"
56 :> QueryParam "list" ListId
57 :> QueryParamR "ngramsType" TabType
58 :> QueryParam "limit" Int
60 :<|> "hash" :> Summary "Scatter Hash"
61 :> QueryParam "list" ListId
62 :> QueryParamR "ngramsType" TabType
65 scatterApi :: NodeId -> GargServer ScatterAPI
66 scatterApi id' = getScatter id'
67 :<|> updateScatter id'
68 :<|> getScatterHash id'
70 getScatter :: FlowCmdM env err m =>
75 -> m (HashedResponse Metrics)
76 getScatter cId maybeListId tabType _maybeLimit = do
77 listId <- case maybeListId of
79 Nothing -> defaultList cId
80 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
81 let HyperdataList { _hl_scatter = scatterMap } = node ^. node_hyperdata
82 mChart = HM.lookup tabType scatterMap
84 chart <- case mChart of
85 Just chart -> pure chart
87 updateScatter' cId maybeListId tabType Nothing
89 pure $ constructHashedResponse chart
91 updateScatter :: FlowCmdM env err m =>
97 updateScatter cId maybeListId tabType maybeLimit = do
98 printDebug "[updateScatter] cId" cId
99 printDebug "[updateScatter] maybeListId" maybeListId
100 printDebug "[updateScatter] tabType" tabType
101 printDebug "[updateScatter] maybeLimit" maybeLimit
102 _ <- updateScatter' cId maybeListId tabType maybeLimit
105 updateScatter' :: FlowCmdM env err m =>
111 updateScatter' cId maybeListId tabType maybeLimit = do
112 (ngs', scores) <- Metrics.getMetrics cId maybeListId tabType maybeLimit
115 metrics = fmap (\(Scored t s1 s2) -> Metric t s1 s2 (listType t ngs'))
116 $ fmap normalizeLocal scores
117 listType t m = maybe (panic errorMsg) fst $ HM.lookup t m
118 errorMsg = "API.Node.metrics: key absent"
120 listId <- case maybeListId of
122 Nothing -> defaultList cId
123 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
124 let hl = node ^. node_hyperdata
125 scatterMap = hl ^. hl_scatter
126 _ <- updateHyperdata listId $ hl { _hl_scatter = HM.insert tabType (Metrics metrics) scatterMap }
128 pure $ Metrics metrics
130 getScatterHash :: FlowCmdM env err m =>
135 getScatterHash cId maybeListId tabType = do
136 hash <$> getScatter cId maybeListId tabType Nothing
139 -------------------------------------------------------------
140 -- | Chart metrics API
141 type ChartApi = Summary " Chart API"
142 :> QueryParam "from" UTCTime
143 :> QueryParam "to" UTCTime
144 :> QueryParam "list" ListId
145 :> QueryParamR "ngramsType" TabType
146 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
147 :<|> Summary "Chart update"
148 :> QueryParam "list" ListId
149 :> QueryParamR "ngramsType" TabType
150 :> QueryParam "limit" Int
152 :<|> "hash" :> Summary "Chart Hash"
153 :> QueryParam "list" ListId
154 :> QueryParamR "ngramsType" TabType
157 chartApi :: NodeId -> GargServer ChartApi
158 chartApi id' = getChart id'
160 :<|> getChartHash id'
162 -- TODO add start / end
163 getChart :: FlowCmdM env err m =>
169 -> m (HashedResponse (ChartMetrics Histo))
170 getChart cId _start _end maybeListId tabType = do
171 listId <- case maybeListId of
173 Nothing -> defaultList cId
174 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
175 let chartMap = node ^. node_hyperdata ^. hl_chart
176 mChart = HM.lookup tabType chartMap
178 chart <- case mChart of
179 Just chart -> pure chart
181 updateChart' cId maybeListId tabType Nothing
183 pure $ constructHashedResponse chart
185 updateChart :: HasNodeError err =>
191 updateChart cId maybeListId tabType maybeLimit = do
192 printDebug "[updateChart] cId" cId
193 printDebug "[updateChart] maybeListId" maybeListId
194 printDebug "[updateChart] tabType" tabType
195 printDebug "[updateChart] maybeLimit" maybeLimit
196 _ <- updateChart' cId maybeListId tabType maybeLimit
199 updateChart' :: HasNodeError err =>
204 -> Cmd err (ChartMetrics Histo)
205 updateChart' cId maybeListId tabType _maybeLimit = do
206 listId <- case maybeListId of
208 Nothing -> defaultList cId
209 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
210 let hl = node ^. node_hyperdata
211 chartMap = hl ^. hl_chart
213 _ <- updateHyperdata listId $ hl { _hl_chart = HM.insert tabType (ChartMetrics h) chartMap }
215 pure $ ChartMetrics h
218 getChartHash :: FlowCmdM env err m =>
223 getChartHash cId maybeListId tabType = do
224 hash <$> getChart cId Nothing Nothing maybeListId tabType
226 -------------------------------------------------------------
228 type PieApi = Summary "Pie Chart"
229 :> QueryParam "from" UTCTime
230 :> QueryParam "to" UTCTime
231 :> QueryParam "list" ListId
232 :> QueryParamR "ngramsType" TabType
233 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
234 :<|> Summary "Pie Chart update"
235 :> QueryParam "list" ListId
236 :> QueryParamR "ngramsType" TabType
237 :> QueryParam "limit" Int
239 :<|> "hash" :> Summary "Pie Hash"
240 :> QueryParam "list" ListId
241 :> QueryParamR "ngramsType" TabType
244 pieApi :: NodeId -> GargServer PieApi
245 pieApi id' = getPie id'
249 getPie :: FlowCmdM env err m
255 -> m (HashedResponse (ChartMetrics Histo))
256 getPie cId _start _end maybeListId tabType = do
257 listId <- case maybeListId of
259 Nothing -> defaultList cId
260 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
261 let pieMap = node ^. node_hyperdata ^. hl_pie
262 mChart = HM.lookup tabType pieMap
264 chart <- case mChart of
265 Just chart -> pure chart
267 updatePie' cId maybeListId tabType Nothing
269 pure $ constructHashedResponse chart
271 updatePie :: FlowCmdM env err m =>
277 updatePie cId maybeListId tabType maybeLimit = do
278 printDebug "[updatePie] cId" cId
279 printDebug "[updatePie] maybeListId" maybeListId
280 printDebug "[updatePie] tabType" tabType
281 printDebug "[updatePie] maybeLimit" maybeLimit
282 _ <- updatePie' cId maybeListId tabType maybeLimit
285 updatePie' :: FlowCmdM env err m =>
290 -> m (ChartMetrics Histo)
291 updatePie' cId maybeListId tabType _maybeLimit = do
292 listId <- case maybeListId of
294 Nothing -> defaultList cId
295 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
296 let hl = node ^. node_hyperdata
297 pieMap = hl ^. hl_pie
299 p <- chartData cId (ngramsTypeFromTabType tabType) MapTerm
300 _ <- updateHyperdata listId $ hl { _hl_pie = HM.insert tabType (ChartMetrics p) pieMap }
302 pure $ ChartMetrics p
304 getPieHash :: FlowCmdM env err m =>
309 getPieHash cId maybeListId tabType = do
310 hash <$> getPie cId Nothing Nothing maybeListId tabType
312 -------------------------------------------------------------
313 -- | Tree metrics API
315 type TreeApi = Summary " Tree API"
316 :> QueryParam "from" UTCTime
317 :> QueryParam "to" UTCTime
318 :> QueryParam "list" ListId
319 :> QueryParamR "ngramsType" TabType
320 :> QueryParamR "listType" ListType
321 :> Get '[JSON] (HashedResponse (ChartMetrics [NgramsTree]))
322 :<|> Summary "Tree Chart update"
323 :> QueryParam "list" ListId
324 :> QueryParamR "ngramsType" TabType
325 :> QueryParamR "listType" ListType
329 :> QueryParam "list" ListId
330 :> QueryParamR "ngramsType" TabType
331 :> QueryParamR "listType" ListType
333 treeApi :: NodeId -> GargServer TreeApi
334 treeApi id' = getTree id'
338 getTree :: FlowCmdM env err m
345 -> m (HashedResponse (ChartMetrics [NgramsTree]))
346 getTree cId _start _end maybeListId tabType listType = do
347 listId <- case maybeListId of
349 Nothing -> defaultList cId
351 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
352 let treeMap = node ^. node_hyperdata ^. hl_tree
353 mChart = HM.lookup tabType treeMap
355 chart <- case mChart of
356 Just chart -> pure chart
358 updateTree' cId maybeListId tabType listType
360 pure $ constructHashedResponse chart
362 updateTree :: FlowCmdM env err m =>
368 updateTree cId maybeListId tabType listType = do
369 printDebug "[updateTree] cId" cId
370 printDebug "[updateTree] maybeListId" maybeListId
371 printDebug "[updateTree] tabType" tabType
372 printDebug "[updateTree] listType" listType
373 _ <- updateTree' cId maybeListId tabType listType
376 updateTree' :: FlowCmdM env err m =>
381 -> m (ChartMetrics [NgramsTree])
382 updateTree' cId maybeListId tabType listType = do
383 listId <- case maybeListId of
385 Nothing -> defaultList cId
387 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
388 let hl = node ^. node_hyperdata
389 treeMap = hl ^. hl_tree
390 t <- treeData cId (ngramsTypeFromTabType tabType) listType
391 _ <- updateHyperdata listId $ hl { _hl_tree = HM.insert tabType (ChartMetrics t) treeMap }
393 pure $ ChartMetrics t
395 getTreeHash :: FlowCmdM env err m =>
401 getTreeHash cId maybeListId tabType listType = do
402 hash <$> getTree cId Nothing Nothing maybeListId tabType listType