]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/Metrics.hs
[hyperdata] refactor code to add hyperdata graph metrics
[gargantext.git] / src / Gargantext / API / Metrics.hs
1 {-|
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
8 Portability : POSIX
9
10 Metrics API
11
12 -}
13
14 {-# OPTIONS_GHC -fno-warn-orphans #-}
15
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE DeriveGeneric #-}
18 {-# LANGUAGE FlexibleContexts #-}
19 {-# LANGUAGE NoImplicitPrelude #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE RankNTypes #-}
22 {-# LANGUAGE TemplateHaskell #-}
23 {-# LANGUAGE TypeOperators #-}
24
25 module Gargantext.API.Metrics
26 where
27
28 import Data.Time (UTCTime)
29 import Servant
30 import qualified Data.Map as Map
31
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
43
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
55 :> Post '[JSON] ()
56
57 getScatter :: FlowCmdM env err m =>
58 CorpusId
59 -> Maybe ListId
60 -> TabType
61 -> Maybe Limit
62 -> m Metrics
63 getScatter cId maybeListId tabType maybeLimit = do
64 (ngs', scores) <- Metrics.getMetrics cId maybeListId tabType maybeLimit
65
66 let
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"
71
72 pure $ Metrics metrics
73
74
75 updateScatter :: FlowCmdM env err m =>
76 CorpusId
77 -> Maybe ListId
78 -> TabType
79 -> Maybe Limit
80 -> m ()
81 updateScatter cId maybeListId tabType maybeLimit = do
82 (_ngs', _scores) <- Metrics.getMetrics cId maybeListId tabType maybeLimit
83
84 let
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"
89
90 --pure $ Metrics metrics
91 pure ()
92
93
94
95 -- TODO add start / end
96 getChart :: CorpusId -> Maybe UTCTime -> Maybe UTCTime -> Cmd err (ChartMetrics Histo)
97 getChart cId _start _end = do
98 h <- histoData cId
99 pure (ChartMetrics h)
100
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)
105
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)
110
111
112