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(..))
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 (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
145 :<|> "hash" :> Summary "Chart Hash"
146 :> QueryParam "list" ListId
147 :> QueryParamR "ngramsType" TabType
150 chartApi :: NodeId -> GargServer ChartApi
151 chartApi id' = getChart id'
153 :<|> getChartHash id'
155 -- TODO add start / end
156 getChart :: FlowCmdM env err m =>
162 -> m (HashedResponse (ChartMetrics Histo))
163 getChart cId _start _end maybeListId tabType = do
164 listId <- case maybeListId of
166 Nothing -> defaultList cId
167 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
168 let HyperdataList { _hl_chart = mChart } = node ^. node_hyperdata
170 chart <- case mChart of
171 Just chart -> pure chart
173 updateChart' cId maybeListId tabType Nothing
175 pure $ constructHashedResponse chart
177 updateChart :: HasNodeError err =>
183 updateChart cId maybeListId tabType maybeLimit = do
184 _ <- updateChart' cId maybeListId tabType maybeLimit
187 updateChart' :: HasNodeError err =>
192 -> Cmd err (ChartMetrics Histo)
193 updateChart' cId maybeListId _tabType _maybeLimit = do
194 listId <- case maybeListId of
196 Nothing -> defaultList cId
197 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
198 let hl = node ^. node_hyperdata
200 _ <- updateHyperdata listId $ hl { _hl_chart = Just $ ChartMetrics h }
202 pure $ ChartMetrics h
205 getChartHash :: FlowCmdM env err m =>
210 getChartHash cId maybeListId tabType = do
211 hash <$> getChart cId Nothing Nothing maybeListId tabType
213 -------------------------------------------------------------
215 type PieApi = Summary "Pie Chart"
216 :> QueryParam "from" UTCTime
217 :> QueryParam "to" UTCTime
218 :> QueryParam "list" ListId
219 :> QueryParamR "ngramsType" TabType
220 :> Get '[JSON] (HashedResponse (ChartMetrics Histo))
221 :<|> Summary "Pie Chart update"
222 :> QueryParam "list" ListId
223 :> QueryParamR "ngramsType" TabType
224 :> QueryParam "limit" Int
226 :<|> "hash" :> Summary "Pie Hash"
227 :> QueryParam "list" ListId
228 :> QueryParamR "ngramsType" TabType
231 pieApi :: NodeId -> GargServer PieApi
232 pieApi id' = getPie id'
236 getPie :: FlowCmdM env err m
242 -> m (HashedResponse (ChartMetrics Histo))
243 getPie cId _start _end maybeListId tabType = do
244 listId <- case maybeListId of
246 Nothing -> defaultList cId
247 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
248 let HyperdataList { _hl_pie = mChart } = node ^. node_hyperdata
250 chart <- case mChart of
251 Just chart -> pure chart
253 updatePie' cId maybeListId tabType Nothing
255 pure $ constructHashedResponse chart
257 updatePie :: FlowCmdM env err m =>
263 updatePie cId maybeListId tabType maybeLimit = do
264 _ <- updatePie' cId maybeListId tabType maybeLimit
267 updatePie' :: FlowCmdM env err m =>
272 -> m (ChartMetrics Histo)
273 updatePie' cId maybeListId tabType _maybeLimit = do
274 listId <- case maybeListId of
276 Nothing -> defaultList cId
277 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
278 let hl = node ^. node_hyperdata
280 p <- chartData cId (ngramsTypeFromTabType tabType) MapTerm
281 _ <- updateHyperdata listId $ hl { _hl_pie = Just $ ChartMetrics p }
283 pure $ ChartMetrics p
285 getPieHash :: FlowCmdM env err m =>
290 getPieHash cId maybeListId tabType = do
291 hash <$> getPie cId Nothing Nothing maybeListId tabType
293 -------------------------------------------------------------
294 -- | Tree metrics API
296 type TreeApi = Summary " Tree API"
297 :> QueryParam "from" UTCTime
298 :> QueryParam "to" UTCTime
299 :> QueryParam "list" ListId
300 :> QueryParamR "ngramsType" TabType
301 :> QueryParamR "listType" ListType
302 :> Get '[JSON] (HashedResponse (ChartMetrics [MyTree]))
303 :<|> Summary "Tree Chart update"
304 :> QueryParam "list" ListId
305 :> QueryParamR "ngramsType" TabType
306 :> QueryParamR "listType" ListType
310 :> QueryParam "list" ListId
311 :> QueryParamR "ngramsType" TabType
312 :> QueryParamR "listType" ListType
315 -- Depending on the Type of the Node, we could post
316 -- New documents for a corpus
317 -- New map list terms
318 -- :<|> "process" :> MultipartForm MultipartData :> Post '[JSON] Text
320 treeApi :: NodeId -> GargServer TreeApi
321 treeApi id' = getTree id'
325 getTree :: FlowCmdM env err m
332 -> m (HashedResponse (ChartMetrics [MyTree]))
333 getTree cId _start _end maybeListId tabType listType = do
334 listId <- case maybeListId of
336 Nothing -> defaultList cId
338 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
339 let HyperdataList { _hl_tree = mChart } = node ^. node_hyperdata
341 chart <- case mChart of
342 Just chart -> pure chart
344 updateTree' cId maybeListId tabType listType
346 pure $ constructHashedResponse chart
348 updateTree :: FlowCmdM env err m =>
354 updateTree cId maybeListId tabType listType = do
355 _ <- updateTree' cId maybeListId tabType listType
358 updateTree' :: FlowCmdM env err m =>
363 -> m (ChartMetrics [MyTree])
364 updateTree' cId maybeListId tabType listType = do
365 listId <- case maybeListId of
367 Nothing -> defaultList cId
369 node <- getNodeWith listId (Proxy :: Proxy HyperdataList)
370 let hl = node ^. node_hyperdata
371 t <- treeData cId (ngramsTypeFromTabType tabType) listType
372 _ <- updateHyperdata listId $ hl { _hl_tree = Just $ ChartMetrics t }
374 pure $ ChartMetrics t
376 getTreeHash :: FlowCmdM env err m =>
382 getTreeHash cId maybeListId tabType listType = do
383 hash <$> getTree cId Nothing Nothing maybeListId tabType listType