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 {-# LANGUAGE TemplateHaskell #-}
14 {-# LANGUAGE TypeOperators #-}
15 {-# LANGUAGE DeriveAnyClass #-}
17 module Gargantext.API.Count
20 import Data.Aeson hiding (Error)
21 import Data.Aeson.TH (deriveJSON)
23 import Data.List (permutations)
25 import Data.Text (Text, pack)
26 import GHC.Generics (Generic)
27 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
28 import Gargantext.Prelude
30 import Test.QuickCheck (elements)
31 import Test.QuickCheck.Arbitrary
33 -----------------------------------------------------------------------
34 -- TODO-ACCESS: CanCount
35 -- TODO-EVENTS: No events as this is a read only query.
36 type CountAPI = Post '[JSON] Counts
38 -----------------------------------------------------------------------
39 data Scraper = Pubmed | Hal | IsTex | Isidore
40 deriving (Eq, Show, Generic, Enum, Bounded)
43 scrapers = [minBound..maxBound]
45 instance FromJSON Scraper
46 instance ToJSON Scraper
48 instance Arbitrary Scraper where
49 arbitrary = elements scrapers
51 instance ToSchema Scraper
53 -----------------------------------------------------------------------
54 data QueryBool = QueryBool Text
55 deriving (Eq, Show, Generic)
57 queries :: [QueryBool]
58 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
59 --queries = [QueryBool (pack "(X + X') * (Y + Y') - (Z + Z')")]
61 instance Arbitrary QueryBool where
62 arbitrary = elements queries
64 instance FromJSON QueryBool
65 instance ToJSON QueryBool
67 instance ToSchema QueryBool
68 -----------------------------------------------------------------------
70 data Query = Query { query_query :: QueryBool
71 , query_name :: Maybe [Scraper]
73 deriving (Eq, Show, Generic)
74 instance FromJSON Query
77 instance Arbitrary Query where
78 arbitrary = elements [ Query q (Just n)
80 , n <- take 10 $ permutations scrapers
83 instance ToSchema Query where
84 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "query_")
86 -----------------------------------------------------------------------
91 -----------------------------------------------------------------------
92 data Message = Message Code Errors
93 deriving (Eq, Show, Generic)
95 toMessage :: [(Code, Errors)] -> [Message]
96 toMessage = map (\(c,err) -> Message c err)
99 messages = toMessage $ [ (400, ["Ill formed query "])
100 , (300, ["API connexion error "])
101 , (300, ["Internal Gargantext Error "])
102 ] <> take 10 ( repeat (200, [""]))
104 instance Arbitrary Message where
105 arbitrary = elements messages
107 instance FromJSON Message
108 instance ToJSON Message
110 instance ToSchema Message
111 -----------------------------------------------------------------------
112 data Counts = Counts { results :: [Either Message Count]
113 } deriving (Eq, Show, Generic)
116 instance FromJSON Counts
117 instance ToJSON Counts
119 instance Arbitrary Counts where
120 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
121 , Right (Count IsTex (Just 150))
122 , Right (Count Hal (Just 150))
126 instance ToSchema Counts
128 -----------------------------------------------------------------------
129 data Count = Count { count_name :: Scraper
130 , count_count :: Maybe Int
132 deriving (Eq, Show, Generic)
134 $(deriveJSON (unPrefix "count_") ''Count)
136 instance ToSchema Count where
137 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "count_")
138 --instance Arbitrary Count where
139 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
141 -----------------------------------------------------------------------
142 count :: Monad m => Query -> m Counts