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.Aeson.TH (deriveJSON)
30 import Data.Text (Text)
31 import Data.Time (UTCTime)
32 import GHC.Generics (Generic)
34 import Test.QuickCheck (elements)
35 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
36 import qualified Data.Map as Map
38 import qualified Gargantext.Database.Action.Metrics as Metrics
39 import Gargantext.API.Ngrams
40 import Gargantext.API.Ngrams.NTree
41 import Gargantext.Core.Types (CorpusId, ListId, Limit)
42 import Gargantext.Database.Admin.Types.Metrics (ChartMetrics(..))
43 import Gargantext.Core.Types (ListType(..))
44 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
45 import Gargantext.Database.Action.Flow
46 import Gargantext.Database.Prelude
47 import Gargantext.Prelude
48 import Gargantext.Text.Metrics (Scored(..))
49 import Gargantext.Viz.Chart
51 -------------------------------------------------------------
52 instance ToSchema Histo where
53 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "histo_")
54 instance Arbitrary Histo
56 arbitrary = elements [ Histo ["2012"] [1]
59 deriveJSON (unPrefix "histo_") ''Histo
63 -------------------------------------------------------------
64 -- | Scatter metrics API
65 type ScatterAPI = Summary "SepGen IncExc metrics"
66 :> QueryParam "list" ListId
67 :> QueryParamR "ngramsType" TabType
68 :> QueryParam "limit" Int
69 :> Get '[JSON] Metrics
71 getScatter :: FlowCmdM env err m =>
77 getScatter cId maybeListId tabType maybeLimit = do
78 (ngs', scores) <- Metrics.getMetrics cId maybeListId tabType maybeLimit
81 metrics = map (\(Scored t s1 s2) -> Metric t (log' 5 s1) (log' 2 s2) (listType t ngs')) scores
82 log' n x = 1 + (if x <= 0 then 0 else (log $ (10^(n::Int)) * x))
83 listType t m = maybe (panic errorMsg) fst $ Map.lookup t m
84 errorMsg = "API.Node.metrics: key absent"
86 pure $ Metrics metrics
90 -- TODO add start / end
91 getChart :: CorpusId -> Maybe UTCTime -> Maybe UTCTime -> Cmd err (ChartMetrics Histo)
92 getChart cId _start _end = do
96 getPie :: FlowCmdM env err m => CorpusId -> Maybe UTCTime -> Maybe UTCTime -> TabType -> m (ChartMetrics Histo)
97 getPie cId _start _end tt = do
98 p <- pieData cId (ngramsTypeFromTabType tt) GraphTerm
101 getTree :: FlowCmdM env err m => CorpusId -> Maybe UTCTime -> Maybe UTCTime -> TabType -> ListType -> m (ChartMetrics [MyTree])
102 getTree cId _start _end tt lt = do
103 p <- treeData cId (ngramsTypeFromTabType tt) lt
104 pure (ChartMetrics p)