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.Time (UTCTime)
31 import Data.Text (Text)
32 import GHC.Generics (Generic)
33 import Gargantext.Core.Types (ListType(..))
34 import Gargantext.Core.Utils.Prefix (unPrefix)
35 import Gargantext.Database.Utils
36 import Gargantext.Core.Types (CorpusId)
37 import Gargantext.Prelude
38 import Gargantext.API.Ngrams
39 import Gargantext.API.Ngrams.NTree
40 import Gargantext.Database.Flow
41 import Gargantext.Viz.Chart
42 import Test.QuickCheck (elements)
43 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
45 data Metrics = Metrics
46 { metrics_data :: [Metric]}
47 deriving (Generic, Show)
49 instance ToSchema Metrics
50 instance Arbitrary Metrics
52 arbitrary = Metrics <$> arbitrary
59 } deriving (Generic, Show)
61 instance ToSchema Metric
62 instance Arbitrary Metric
64 arbitrary = Metric <$> arbitrary
69 deriveJSON (unPrefix "metrics_") ''Metrics
70 deriveJSON (unPrefix "m_") ''Metric
72 -------------------------------------------------------------
74 data ChartMetrics a = ChartMetrics { chartMetrics_data :: a }
75 deriving (Generic, Show)
77 instance (ToSchema a) => ToSchema (ChartMetrics a)
78 instance (Arbitrary a) => Arbitrary (ChartMetrics a)
80 arbitrary = ChartMetrics <$> arbitrary
82 deriveJSON (unPrefix "chartMetrics_") ''ChartMetrics
84 -------------------------------------------------------------
85 instance ToSchema Histo
86 instance Arbitrary Histo
88 arbitrary = elements [ Histo ["2012"] [1]
91 deriveJSON (unPrefix "histo_") ''Histo
94 instance ToSchema MyTree
95 instance Arbitrary MyTree
97 arbitrary = MyTree <$> arbitrary <*> arbitrary <*> arbitrary
103 -- TODO add start / end
104 getChart :: CorpusId -> Maybe UTCTime -> Maybe UTCTime -> Cmd err (ChartMetrics Histo)
105 getChart cId _start _end = do
107 pure (ChartMetrics h)
109 getPie :: FlowCmdM env err m => CorpusId -> Maybe UTCTime -> Maybe UTCTime -> TabType -> m (ChartMetrics Histo)
110 getPie cId _start _end tt = do
111 p <- pieData cId (ngramsTypeFromTabType tt) GraphTerm
112 pure (ChartMetrics p)
114 getTree :: FlowCmdM env err m => CorpusId -> Maybe UTCTime -> Maybe UTCTime -> TabType -> ListType -> m (ChartMetrics [MyTree])
115 getTree cId _start _end tt lt = do
116 p <- treeData cId (ngramsTypeFromTabType tt) lt
117 pure (ChartMetrics p)