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.Text (Text)
23 import Data.Time (UTCTime)
24 import Data.Vector (Vector)
25 import Gargantext.API.HashedResponse
26 import Gargantext.API.Ngrams.NgramsTree
27 import Gargantext.API.Ngrams.Types
28 import Gargantext.API.Prelude (GargServer)
29 import Gargantext.Core.Text.Metrics (Scored(..), {-normalizeGlobal,-} normalizeLocal)
30 import Gargantext.Core.Types (CorpusId, ListId, ListType(..))
31 import Gargantext.Core.Types.Query (Limit)
32 import Gargantext.Core.Viz.Chart
33 import Gargantext.Core.Viz.Types
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.Action.Flow.Types (FlowCmdM)
38 import Gargantext.Database.Prelude
39 import Gargantext.Database.Query.Table.Node (defaultList, getNodeWith)
40 import Gargantext.Database.Query.Table.Node.Error (HasNodeError)
41 import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
42 import Gargantext.Database.Schema.Node (node_hyperdata)
43 import Gargantext.Prelude
45 import qualified Data.HashMap.Strict as HashMap
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" Limit
54 :> Get '[JSON] (HashedResponse Metrics)
55 :<|> Summary "Scatter update"
56 :> QueryParam "list" ListId
57 :> QueryParamR "ngramsType" TabType
58 :> QueryParam "limit" Limit
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 = HashMap.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 { m_label = unNgramsTerm t
118 , m_cat = listType t ngs' })
119 $ fmap normalizeLocal scores
120 listType t m = maybe (panic errorMsg) fst $ HashMap.lookup t m
121 errorMsg = "API.Node.metrics: key absent"
123 listId <- case maybeListId of
125 Nothing -> defaultList cId
126 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
127 let hl = node ^. node_hyperdata
128 scatterMap = hl ^. hl_scatter
129 _ <- updateHyperdata listId $ hl { _hl_scatter = HashMap.insert tabType (Metrics metrics) scatterMap }
131 pure $ Metrics metrics
133 getScatterHash :: FlowCmdM env err m =>
138 getScatterHash cId maybeListId tabType = do
139 hash <$> getScatter cId maybeListId tabType Nothing
142 -------------------------------------------------------------
143 -- | Chart metrics API
144 type ChartApi = Summary " Chart API"
145 :> QueryParam "from" UTCTime
146 :> QueryParam "to" UTCTime
147 :> QueryParam "list" ListId
148 :> QueryParamR "ngramsType" TabType
149 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
150 :<|> Summary "Chart update"
151 :> QueryParam "list" ListId
152 :> QueryParamR "ngramsType" TabType
153 :> QueryParam "limit" Limit
155 :<|> "hash" :> Summary "Chart Hash"
156 :> QueryParam "list" ListId
157 :> QueryParamR "ngramsType" TabType
160 chartApi :: NodeId -> GargServer ChartApi
161 chartApi id' = getChart id'
163 :<|> getChartHash id'
165 -- TODO add start / end
166 getChart :: FlowCmdM env err m =>
172 -> m (HashedResponse (ChartMetrics Histo))
173 getChart cId _start _end maybeListId tabType = do
174 listId <- case maybeListId of
176 Nothing -> defaultList cId
177 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
178 let chartMap = node ^. node_hyperdata ^. hl_chart
179 mChart = HashMap.lookup tabType chartMap
181 chart <- case mChart of
182 Just chart -> pure chart
184 updateChart' cId maybeListId tabType Nothing
186 pure $ constructHashedResponse chart
188 updateChart :: HasNodeError err =>
194 updateChart cId maybeListId tabType maybeLimit = do
195 printDebug "[updateChart] cId" cId
196 printDebug "[updateChart] maybeListId" maybeListId
197 printDebug "[updateChart] tabType" tabType
198 printDebug "[updateChart] maybeLimit" maybeLimit
199 _ <- updateChart' cId maybeListId tabType maybeLimit
202 updateChart' :: HasNodeError err =>
207 -> Cmd err (ChartMetrics Histo)
208 updateChart' cId maybeListId tabType _maybeLimit = do
209 listId <- case maybeListId of
211 Nothing -> defaultList cId
212 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
213 let hl = node ^. node_hyperdata
214 chartMap = hl ^. hl_chart
216 _ <- updateHyperdata listId $ hl { _hl_chart = HashMap.insert tabType (ChartMetrics h) chartMap }
218 pure $ ChartMetrics h
221 getChartHash :: FlowCmdM env err m =>
226 getChartHash cId maybeListId tabType = do
227 hash <$> getChart cId Nothing Nothing maybeListId tabType
229 -------------------------------------------------------------
231 type PieApi = Summary "Pie Chart"
232 :> QueryParam "from" UTCTime
233 :> QueryParam "to" UTCTime
234 :> QueryParam "list" ListId
235 :> QueryParamR "ngramsType" TabType
236 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
237 :<|> Summary "Pie Chart update"
238 :> QueryParam "list" ListId
239 :> QueryParamR "ngramsType" TabType
240 :> QueryParam "limit" Limit
242 :<|> "hash" :> Summary "Pie Hash"
243 :> QueryParam "list" ListId
244 :> QueryParamR "ngramsType" TabType
247 pieApi :: NodeId -> GargServer PieApi
248 pieApi id' = getPie id'
252 getPie :: FlowCmdM env err m
258 -> m (HashedResponse (ChartMetrics Histo))
259 getPie cId _start _end maybeListId tabType = do
260 listId <- case maybeListId of
262 Nothing -> defaultList cId
263 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
264 let pieMap = node ^. node_hyperdata ^. hl_pie
265 mChart = HashMap.lookup tabType pieMap
267 chart <- case mChart of
268 Just chart -> pure chart
270 updatePie' cId maybeListId tabType Nothing
272 pure $ constructHashedResponse chart
274 updatePie :: FlowCmdM env err m =>
280 updatePie cId maybeListId tabType maybeLimit = do
281 printDebug "[updatePie] cId" cId
282 printDebug "[updatePie] maybeListId" maybeListId
283 printDebug "[updatePie] tabType" tabType
284 printDebug "[updatePie] maybeLimit" maybeLimit
285 _ <- updatePie' cId maybeListId tabType maybeLimit
288 updatePie' :: FlowCmdM env err m =>
293 -> m (ChartMetrics Histo)
294 updatePie' cId maybeListId tabType _maybeLimit = do
295 listId <- case maybeListId of
297 Nothing -> defaultList cId
298 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
299 let hl = node ^. node_hyperdata
300 pieMap = hl ^. hl_pie
302 p <- chartData cId (ngramsTypeFromTabType tabType) MapTerm
303 _ <- updateHyperdata listId $ hl { _hl_pie = HashMap.insert tabType (ChartMetrics p) pieMap }
305 pure $ ChartMetrics p
307 getPieHash :: FlowCmdM env err m =>
312 getPieHash cId maybeListId tabType = do
313 hash <$> getPie cId Nothing Nothing maybeListId tabType
315 -------------------------------------------------------------
316 -- | Tree metrics API
318 type TreeApi = Summary " Tree API"
319 :> QueryParam "from" UTCTime
320 :> QueryParam "to" UTCTime
321 :> QueryParam "list" ListId
322 :> QueryParamR "ngramsType" TabType
323 :> QueryParamR "listType" ListType
324 :> Get '[JSON] (HashedResponse (ChartMetrics (Vector NgramsTree)))
325 :<|> Summary "Tree Chart update"
326 :> QueryParam "list" ListId
327 :> QueryParamR "ngramsType" TabType
328 :> QueryParamR "listType" ListType
332 :> QueryParam "list" ListId
333 :> QueryParamR "ngramsType" TabType
334 :> QueryParamR "listType" ListType
336 treeApi :: NodeId -> GargServer TreeApi
337 treeApi id' = getTree id'
341 getTree :: FlowCmdM env err m
348 -> m (HashedResponse (ChartMetrics (Vector NgramsTree)))
349 getTree cId _start _end maybeListId tabType listType = do
350 listId <- case maybeListId of
352 Nothing -> defaultList cId
354 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
355 let treeMap = node ^. node_hyperdata ^. hl_tree
356 mChart = HashMap.lookup tabType treeMap
358 chart <- case mChart of
359 Just chart -> pure chart
361 updateTree' cId maybeListId tabType listType
363 pure $ constructHashedResponse chart
365 updateTree :: FlowCmdM env err m =>
371 updateTree cId maybeListId tabType listType = do
372 printDebug "[updateTree] cId" cId
373 printDebug "[updateTree] maybeListId" maybeListId
374 printDebug "[updateTree] tabType" tabType
375 printDebug "[updateTree] listType" listType
376 _ <- updateTree' cId maybeListId tabType listType
379 updateTree' :: FlowCmdM env err m =>
384 -> m (ChartMetrics (Vector NgramsTree))
385 updateTree' cId maybeListId tabType listType = do
386 listId <- case maybeListId of
388 Nothing -> defaultList cId
390 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
391 let hl = node ^. node_hyperdata
392 treeMap = hl ^. hl_tree
393 t <- treeData cId (ngramsTypeFromTabType tabType) listType
394 _ <- updateHyperdata listId $ hl { _hl_tree = HashMap.insert tabType (ChartMetrics t) treeMap }
396 pure $ ChartMetrics t
398 getTreeHash :: FlowCmdM env err m =>
404 getTreeHash cId maybeListId tabType listType = do
405 hash <$> getTree cId Nothing Nothing maybeListId tabType listType