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 qualified Data.Map as Map
23 import Data.Time (UTCTime)
24 import Data.Text (Text)
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 Gargantext.Database.Admin.Types.Hyperdata (HyperdataList(..))
34 import Gargantext.Database.Admin.Types.Metrics (ChartMetrics(..), Metric(..), Metrics(..))
35 import Gargantext.Database.Admin.Types.Node (NodeId)
36 import Gargantext.Database.Prelude
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.Schema.Node (node_hyperdata)
41 import Gargantext.Prelude
42 import Gargantext.Core.Text.Metrics (Scored(..), {-normalizeGlobal,-} normalizeLocal)
43 import Gargantext.Core.Viz.Chart
44 import Gargantext.Core.Viz.Types
45 import qualified Gargantext.Database.Action.Metrics as Metrics
47 -------------------------------------------------------------
48 -- | Scatter metrics API
49 type ScatterAPI = Summary "SepGen IncExc metrics"
50 :> QueryParam "list" ListId
51 :> QueryParamR "ngramsType" TabType
52 :> QueryParam "limit" Int
53 :> Get '[JSON] (HashedResponse Metrics)
54 :<|> Summary "Scatter update"
55 :> QueryParam "list" ListId
56 :> QueryParamR "ngramsType" TabType
57 :> QueryParam "limit" Int
59 :<|> "hash" :> 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 s1 s2 (listType t ngs'))
111 $ map normalizeLocal scores
112 listType t m = maybe (panic errorMsg) fst $ Map.lookup t m
113 errorMsg = "API.Node.metrics: key absent"
115 listId <- case maybeListId of
117 Nothing -> defaultList cId
118 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
119 let hl = node ^. node_hyperdata
120 _ <- updateHyperdata listId $ hl { _hl_scatter = Just $ Metrics metrics }
122 pure $ Metrics metrics
124 getScatterHash :: FlowCmdM env err m =>
129 getScatterHash cId maybeListId tabType = do
130 hash <$> getScatter cId maybeListId tabType Nothing
133 -------------------------------------------------------------
134 -- | Chart metrics API
135 type ChartApi = Summary " Chart API"
136 :> QueryParam "from" UTCTime
137 :> QueryParam "to" UTCTime
138 :> QueryParam "list" ListId
139 :> QueryParamR "ngramsType" TabType
140 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
141 :<|> Summary "Chart update"
142 :> QueryParam "list" ListId
143 :> QueryParamR "ngramsType" TabType
144 :> QueryParam "limit" Int
146 :<|> "hash" :> Summary "Chart Hash"
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
227 :<|> "hash" :> Summary "Pie Hash"
228 :> QueryParam "list" ListId
229 :> QueryParamR "ngramsType" TabType
232 pieApi :: NodeId -> GargServer PieApi
233 pieApi id' = getPie id'
237 getPie :: FlowCmdM env err m
243 -> m (HashedResponse (ChartMetrics Histo))
244 getPie cId _start _end maybeListId tabType = do
245 listId <- case maybeListId of
247 Nothing -> defaultList cId
248 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
249 let HyperdataList { _hl_pie = mChart } = node ^. node_hyperdata
251 chart <- case mChart of
252 Just chart -> pure chart
254 updatePie' cId maybeListId tabType Nothing
256 pure $ constructHashedResponse chart
258 updatePie :: FlowCmdM env err m =>
264 updatePie cId maybeListId tabType maybeLimit = do
265 _ <- updatePie' cId maybeListId tabType maybeLimit
268 updatePie' :: FlowCmdM env err m =>
273 -> m (ChartMetrics Histo)
274 updatePie' cId maybeListId tabType _maybeLimit = do
275 listId <- case maybeListId of
277 Nothing -> defaultList cId
278 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
279 let hl = node ^. node_hyperdata
281 p <- chartData cId (ngramsTypeFromTabType tabType) MapTerm
282 _ <- updateHyperdata listId $ hl { _hl_pie = Just $ ChartMetrics p }
284 pure $ ChartMetrics p
286 getPieHash :: FlowCmdM env err m =>
291 getPieHash cId maybeListId tabType = do
292 hash <$> getPie cId Nothing Nothing maybeListId tabType
294 -------------------------------------------------------------
295 -- | Tree metrics API
297 type TreeApi = Summary " Tree API"
298 :> QueryParam "from" UTCTime
299 :> QueryParam "to" UTCTime
300 :> QueryParam "list" ListId
301 :> QueryParamR "ngramsType" TabType
302 :> QueryParamR "listType" ListType
303 :> Get '[JSON] (HashedResponse (ChartMetrics [MyTree]))
304 :<|> Summary "Tree Chart update"
305 :> QueryParam "list" ListId
306 :> QueryParamR "ngramsType" TabType
307 :> QueryParamR "listType" ListType
311 :> QueryParam "list" ListId
312 :> QueryParamR "ngramsType" TabType
313 :> QueryParamR "listType" ListType
316 -- Depending on the Type of the Node, we could post
317 -- New documents for a corpus
318 -- New map list terms
319 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
321 treeApi :: NodeId -> GargServer TreeApi
322 treeApi id' = getTree id'
326 getTree :: FlowCmdM env err m
333 -> m (HashedResponse (ChartMetrics [MyTree]))
334 getTree cId _start _end maybeListId tabType listType = do
335 listId <- case maybeListId of
337 Nothing -> defaultList cId
339 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
340 let HyperdataList { _hl_tree = mChart } = node ^. node_hyperdata
342 chart <- case mChart of
343 Just chart -> pure chart
345 updateTree' cId maybeListId tabType listType
347 pure $ constructHashedResponse chart
349 updateTree :: FlowCmdM env err m =>
355 updateTree cId maybeListId tabType listType = do
356 _ <- updateTree' cId maybeListId tabType listType
359 updateTree' :: FlowCmdM env err m =>
364 -> m (ChartMetrics [MyTree])
365 updateTree' cId maybeListId tabType listType = do
366 listId <- case maybeListId of
368 Nothing -> defaultList cId
370 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
371 let hl = node ^. node_hyperdata
372 t <- treeData cId (ngramsTypeFromTabType tabType) listType
373 _ <- updateHyperdata listId $ hl { _hl_tree = Just $ ChartMetrics t }
375 pure $ ChartMetrics t
377 getTreeHash :: FlowCmdM env err m =>
383 getTreeHash cId maybeListId tabType listType = do
384 hash <$> getTree cId Nothing Nothing maybeListId tabType listType