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 DataKinds #-}
17 {-# LANGUAGE DeriveGeneric #-}
18 {-# LANGUAGE FlexibleContexts #-}
19 {-# LANGUAGE NoImplicitPrelude #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE RankNTypes #-}
22 {-# LANGUAGE TemplateHaskell #-}
23 {-# LANGUAGE TypeOperators #-}
25 module Gargantext.API.Metrics
28 import Data.Time (UTCTime)
30 import qualified Data.Map as Map
32 import Gargantext.API.Ngrams
33 import Gargantext.API.Ngrams.NTree
34 import Gargantext.Core.Types (CorpusId, Limit, ListId, ListType(..))
35 import qualified Gargantext.Database.Action.Metrics as Metrics
36 import Gargantext.Database.Action.Flow
37 import Gargantext.Database.Admin.Types.Metrics (ChartMetrics(..), Metric(..), Metrics(..))
38 import Gargantext.Database.Prelude
39 import Gargantext.Prelude
40 import Gargantext.Text.Metrics (Scored(..))
41 import Gargantext.Viz.Chart
42 import Gargantext.Viz.Types
44 -------------------------------------------------------------
45 -- | Scatter metrics API
46 type ScatterAPI = Summary "SepGen IncExc metrics"
47 :> QueryParam "list" ListId
48 :> QueryParamR "ngramsType" TabType
49 :> QueryParam "limit" Int
50 :> Get '[JSON] Metrics
51 :<|> Summary "SepGen IncExc metrics update"
52 :> QueryParam "list" ListId
53 :> QueryParamR "ngramsType" TabType
54 :> QueryParam "limit" Int
57 getScatter :: FlowCmdM env err m =>
63 getScatter cId maybeListId tabType maybeLimit = do
64 (ngs', scores) <- Metrics.getMetrics cId maybeListId tabType maybeLimit
67 metrics = map (\(Scored t s1 s2) -> Metric t (log' 5 s1) (log' 2 s2) (listType t ngs')) scores
68 log' n x = 1 + (if x <= 0 then 0 else (log $ (10^(n::Int)) * x))
69 listType t m = maybe (panic errorMsg) fst $ Map.lookup t m
70 errorMsg = "API.Node.metrics: key absent"
72 pure $ Metrics metrics
75 updateScatter :: FlowCmdM env err m =>
81 updateScatter cId maybeListId tabType maybeLimit = do
82 (_ngs', _scores) <- Metrics.getMetrics cId maybeListId tabType maybeLimit
85 -- metrics = map (\(Scored t s1 s2) -> Metric t (log' 5 s1) (log' 2 s2) (listType t ngs')) scores
86 -- log' n x = 1 + (if x <= 0 then 0 else (log $ (10^(n::Int)) * x))
87 -- listType t m = maybe (panic errorMsg) fst $ Map.lookup t m
88 -- errorMsg = "API.Node.metrics: key absent"
90 --pure $ Metrics metrics
95 -- TODO add start / end
96 getChart :: CorpusId -> Maybe UTCTime -> Maybe UTCTime -> Cmd err (ChartMetrics Histo)
97 getChart cId _start _end = do
101 getPie :: FlowCmdM env err m => CorpusId -> Maybe UTCTime -> Maybe UTCTime -> TabType -> m (ChartMetrics Histo)
102 getPie cId _start _end tt = do
103 p <- pieData cId (ngramsTypeFromTabType tt) GraphTerm
104 pure (ChartMetrics p)
106 getTree :: FlowCmdM env err m => CorpusId -> Maybe UTCTime -> Maybe UTCTime -> TabType -> ListType -> m (ChartMetrics [MyTree])
107 getTree cId _start _end tt lt = do
108 p <- treeData cId (ngramsTypeFromTabType tt) lt
109 pure (ChartMetrics p)