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 TemplateHaskell #-}
16 {-# LANGUAGE TypeOperators #-}
17 {-# LANGUAGE DeriveAnyClass #-}
19 module Gargantext.API.Count
23 import GHC.Generics (Generic)
24 import Prelude (Bounded, Enum, minBound, maxBound)
26 import Data.Aeson hiding (Error)
27 import Data.Aeson.TH (deriveJSON)
30 import Data.List (repeat, permutations)
32 import Data.Text (Text, pack)
35 import Test.QuickCheck.Arbitrary
36 import Test.QuickCheck (elements)
37 -- import Control.Applicative ((<*>))
39 import Gargantext.Prelude
40 import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
42 -----------------------------------------------------------------------
43 -- TODO-ACCESS: CanCount
44 -- TODO-EVENTS: No events as this is a read only query.
45 type CountAPI = Post '[JSON] Counts
47 -----------------------------------------------------------------------
48 data Scraper = Pubmed | Hal | IsTex | Isidore
49 deriving (Eq, Show, Generic, Enum, Bounded)
52 scrapers = [minBound..maxBound]
54 instance FromJSON Scraper
55 instance ToJSON Scraper
57 instance Arbitrary Scraper where
58 arbitrary = elements scrapers
60 instance ToSchema Scraper
62 -----------------------------------------------------------------------
63 data QueryBool = QueryBool Text
64 deriving (Eq, Show, Generic)
66 queries :: [QueryBool]
67 queries = [QueryBool (pack "(X OR X') AND (Y OR Y') NOT (Z OR Z')")]
68 --queries = [QueryBool (pack "(X + X') * (Y + Y') - (Z + Z')")]
70 instance Arbitrary QueryBool where
71 arbitrary = elements queries
73 instance FromJSON QueryBool
74 instance ToJSON QueryBool
76 instance ToSchema QueryBool
77 -----------------------------------------------------------------------
79 data Query = Query { query_query :: QueryBool
80 , query_name :: Maybe [Scraper]
82 deriving (Eq, Show, Generic)
83 instance FromJSON Query
86 instance Arbitrary Query where
87 arbitrary = elements [ Query q (Just n)
89 , n <- take 10 $ permutations scrapers
92 instance ToSchema Query where
93 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "query_")
95 -----------------------------------------------------------------------
100 -----------------------------------------------------------------------
101 data Message = Message Code Errors
102 deriving (Eq, Show, Generic)
104 toMessage :: [(Code, Errors)] -> [Message]
105 toMessage = map (\(c,err) -> Message c err)
107 messages :: [Message]
108 messages = toMessage $ [ (400, ["Ill formed query "])
109 , (300, ["API connexion error "])
110 , (300, ["Internal Gargantext Error "])
111 ] <> take 10 ( repeat (200, [""]))
113 instance Arbitrary Message where
114 arbitrary = elements messages
116 instance FromJSON Message
117 instance ToJSON Message
119 instance ToSchema Message
120 -----------------------------------------------------------------------
121 data Counts = Counts { results :: [Either Message Count]
122 } deriving (Eq, Show, Generic)
125 instance FromJSON Counts
126 instance ToJSON Counts
128 instance Arbitrary Counts where
129 arbitrary = elements [Counts [ Right (Count Pubmed (Just 20 ))
130 , Right (Count IsTex (Just 150))
131 , Right (Count Hal (Just 150))
135 instance ToSchema Counts
137 -----------------------------------------------------------------------
138 data Count = Count { count_name :: Scraper
139 , count_count :: Maybe Int
141 deriving (Eq, Show, Generic)
143 $(deriveJSON (unPrefix "count_") ''Count)
145 instance ToSchema Count where
146 declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "count_")
147 --instance Arbitrary Count where
148 -- arbitrary = Count <$> arbitrary <*> arbitrary <*> arbitrary
150 -----------------------------------------------------------------------
151 count :: Monad m => Query -> m Counts