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.
15 {-# LANGUAGE TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE DeriveAnyClass #-}
19 module Gargantext.API.Count
23 import GHC.Generics (Generic)
25 import Data.Aeson hiding (Error)
26 import Data.Aeson.TH (deriveJSON)
28 import Data.List (permutations)
30 import Data.Text (Text, pack)
33 import Test.QuickCheck.Arbitrary
34 import Test.QuickCheck (elements)
35 -- import Control.Applicative ((<*>))
37 import Gargantext.Prelude
38 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
40 -----------------------------------------------------------------------
41 -- TODO-ACCESS: CanCount
42 -- TODO-EVENTS: No events as this is a read only query.
43 type CountAPI = Post '[JSON] Counts
45 -----------------------------------------------------------------------
46 data Scraper = Pubmed | Hal | IsTex | Isidore
47 deriving (Eq, Show, Generic, Enum, Bounded)
50 scrapers = [minBound..maxBound]
52 instance FromJSON Scraper
53 instance ToJSON Scraper
55 instance Arbitrary Scraper where
56 arbitrary = elements scrapers
58 instance ToSchema Scraper
60 -----------------------------------------------------------------------
61 data QueryBool = QueryBool Text
62 deriving (Eq, Show, Generic)
64 queries :: [QueryBool]
65 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
66 --queries = [QueryBool (pack "(X + X') * (Y + Y') - (Z + Z')")]
68 instance Arbitrary QueryBool where
69 arbitrary = elements queries
71 instance FromJSON QueryBool
72 instance ToJSON QueryBool
74 instance ToSchema QueryBool
75 -----------------------------------------------------------------------
77 data Query = Query { query_query :: QueryBool
78 , query_name :: Maybe [Scraper]
80 deriving (Eq, Show, Generic)
81 instance FromJSON Query
84 instance Arbitrary Query where
85 arbitrary = elements [ Query q (Just n)
87 , n <- take 10 $ permutations scrapers
90 instance ToSchema Query where
91 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "query_")
93 -----------------------------------------------------------------------
98 -----------------------------------------------------------------------
99 data Message = Message Code Errors
100 deriving (Eq, Show, Generic)
102 toMessage :: [(Code, Errors)] -> [Message]
103 toMessage = map (\(c,err) -> Message c err)
105 messages :: [Message]
106 messages = toMessage $ [ (400, ["Ill formed query "])
107 , (300, ["API connexion error "])
108 , (300, ["Internal Gargantext Error "])
109 ] <> take 10 ( repeat (200, [""]))
111 instance Arbitrary Message where
112 arbitrary = elements messages
114 instance FromJSON Message
115 instance ToJSON Message
117 instance ToSchema Message
118 -----------------------------------------------------------------------
119 data Counts = Counts { results :: [Either Message Count]
120 } deriving (Eq, Show, Generic)
123 instance FromJSON Counts
124 instance ToJSON Counts
126 instance Arbitrary Counts where
127 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
128 , Right (Count IsTex (Just 150))
129 , Right (Count Hal (Just 150))
133 instance ToSchema Counts
135 -----------------------------------------------------------------------
136 data Count = Count { count_name :: Scraper
137 , count_count :: Maybe Int
139 deriving (Eq, Show, Generic)
141 $(deriveJSON (unPrefix "count_") ''Count)
143 instance ToSchema Count where
144 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "count_")
145 --instance Arbitrary Count where
146 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
148 -----------------------------------------------------------------------
149 count :: Monad m => Query -> m Counts