2 Module : Gargantext.API.Count
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
10 Count API part of Gargantext.
13 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
15 {-# LANGUAGE NoImplicitPrelude #-}
16 {-# LANGUAGE DataKinds #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeOperators #-}
19 {-# LANGUAGE DeriveGeneric #-}
20 {-# LANGUAGE DeriveAnyClass #-}
21 {-# LANGUAGE OverloadedStrings #-}
23 module Gargantext.API.Count
27 import GHC.Generics (Generic)
28 import Prelude (Bounded, Enum, minBound, maxBound)
30 import Data.Aeson hiding (Error)
31 import Data.Aeson.TH (deriveJSON)
34 import Data.List (repeat, permutations)
36 import Data.Text (Text, pack)
39 import Test.QuickCheck.Arbitrary
40 import Test.QuickCheck (elements)
41 -- import Control.Applicative ((<*>))
43 import Gargantext.Prelude
44 import Gargantext.Core.Utils.Prefix (unPrefix)
46 -----------------------------------------------------------------------
47 -- TODO-ACCESS: CanCount
48 -- TODO-EVENTS: No events as this is a read only query.
49 type CountAPI = Post '[JSON] Counts
51 -----------------------------------------------------------------------
52 data Scraper = Pubmed | Hal | IsTex | Isidore
53 deriving (Eq, Show, Generic, Enum, Bounded)
56 scrapers = [minBound..maxBound]
58 instance FromJSON Scraper
59 instance ToJSON Scraper
61 instance Arbitrary Scraper where
62 arbitrary = elements scrapers
64 instance ToSchema Scraper
66 -----------------------------------------------------------------------
67 data QueryBool = QueryBool Text
68 deriving (Eq, Show, Generic)
70 queries :: [QueryBool]
71 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
73 instance Arbitrary QueryBool where
74 arbitrary = elements queries
76 instance FromJSON QueryBool
77 instance ToJSON QueryBool
79 instance ToSchema QueryBool
80 -----------------------------------------------------------------------
82 data Query = Query { query_query :: QueryBool
83 , query_name :: Maybe [Scraper]
85 deriving (Eq, Show, Generic)
86 instance FromJSON Query
89 instance Arbitrary Query where
90 arbitrary = elements [ Query q (Just n)
92 , n <- take 10 $ permutations scrapers
95 instance ToSchema Query
96 -----------------------------------------------------------------------
101 -----------------------------------------------------------------------
102 data Message = Message Code Errors
103 deriving (Eq, Show, Generic)
105 toMessage :: [(Code, Errors)] -> [Message]
106 toMessage = map (\(c,err) -> Message c err)
108 messages :: [Message]
109 messages = toMessage $ [ (400, ["Ill formed query "])
110 , (300, ["API connexion error "])
111 , (300, ["Internal Gargantext Error "])
112 ] <> take 10 ( repeat (200, [""]))
114 instance Arbitrary Message where
115 arbitrary = elements messages
117 instance FromJSON Message
118 instance ToJSON Message
120 instance ToSchema Message
121 -----------------------------------------------------------------------
122 data Counts = Counts { results :: [Either Message Count]
123 } deriving (Eq, Show, Generic)
126 instance FromJSON Counts
127 instance ToJSON Counts
129 instance Arbitrary Counts where
130 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
131 , Right (Count IsTex (Just 150))
132 , Right (Count Hal (Just 150))
136 instance ToSchema Counts
138 -----------------------------------------------------------------------
139 data Count = Count { count_name :: Scraper
140 , count_count :: Maybe Int
142 deriving (Eq, Show, Generic)
144 $(deriveJSON (unPrefix "count_") ''Count)
146 instance ToSchema Count
147 --instance Arbitrary Count where
148 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
150 -----------------------------------------------------------------------
151 count :: Monad m => Query -> m Counts